Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.editor; |
43 |
|
44 |
import java.io.ByteArrayInputStream; |
45 |
import java.io.FileNotFoundException; |
46 |
import java.io.IOException; |
47 |
import java.util.logging.Level; |
48 |
import java.util.prefs.Preferences; |
49 |
import javax.swing.SwingUtilities; |
50 |
import javax.swing.text.BadLocationException; |
51 |
import javax.swing.text.StyledDocument; |
52 |
import org.netbeans.api.editor.mimelookup.MimeLookup; |
53 |
import org.netbeans.api.editor.mimelookup.MimePath; |
54 |
import org.netbeans.api.editor.mimelookup.test.MockMimeLookup; |
55 |
import org.netbeans.junit.MockServices; |
56 |
import org.netbeans.junit.NbTestCase; |
57 |
import org.openide.cookies.EditCookie; |
58 |
import org.openide.cookies.OpenCookie; |
59 |
import org.openide.filesystems.FileLock; |
60 |
import org.openide.filesystems.FileObject; |
61 |
import org.openide.filesystems.FileStateInvalidException; |
62 |
import org.openide.filesystems.FileSystem; |
63 |
import org.openide.loaders.DataNode; |
64 |
import org.openide.loaders.DataObject; |
65 |
import org.openide.loaders.DataObjectExistsException; |
66 |
import org.openide.loaders.MultiDataObject; |
67 |
import org.openide.nodes.Children; |
68 |
import org.openide.nodes.CookieSet; |
69 |
import org.openide.nodes.Node; |
70 |
import org.openide.text.DataEditorSupport; |
71 |
import org.openide.util.Exceptions; |
72 |
import org.openide.util.NbPreferences; |
73 |
|
74 |
/** |
75 |
* |
76 |
* @author Alexander Simon |
77 |
*/ |
78 |
public class LineSeparatorDataEditorSupportTest extends NbTestCase { |
79 |
// for file object support |
80 |
|
81 |
String content = ""; |
82 |
long expectedSize = -1; |
83 |
java.util.Date date = new java.util.Date(); |
84 |
LineSeparatorDataEditorSupportTest.MyFileObject fileObject; |
85 |
org.openide.filesystems.FileSystem fs; |
86 |
static LineSeparatorDataEditorSupportTest RUNNING; |
87 |
private MimePath textMimePath; |
88 |
|
89 |
static { |
90 |
//System.setProperty ("org.openide.util.Lookup", "org.openide.text.LineSeparatorDataEditorSupportTest$Lkp"); |
91 |
System.setProperty("org.openide.windows.DummyWindowManager.VISIBLE", "false"); |
92 |
} |
93 |
|
94 |
public LineSeparatorDataEditorSupportTest(String s) { |
95 |
super(s); |
96 |
} |
97 |
|
98 |
@Override |
99 |
protected Level logLevel() { |
100 |
return Level.FINE; |
101 |
} |
102 |
|
103 |
@Override |
104 |
protected void setUp() throws Exception { |
105 |
RUNNING = this; |
106 |
MockServices.setServices(new Class[]{MockMimeLookup.class, Pool.class}); |
107 |
textMimePath = MimePath.parse("text/plain"); |
108 |
MockMimeLookup.setInstances(textMimePath, new NbEditorKit(), NbPreferences.forModule(getClass())); |
109 |
|
110 |
fs = org.openide.filesystems.FileUtil.createMemoryFileSystem(); |
111 |
org.openide.filesystems.Repository.getDefault().addFileSystem(fs); |
112 |
org.openide.filesystems.FileObject root = fs.getRoot(); |
113 |
fileObject = new LineSeparatorDataEditorSupportTest.MyFileObject(org.openide.filesystems.FileUtil.createData(root, "my" + getName() + ".txt")); |
114 |
} |
115 |
|
116 |
@Override |
117 |
protected void tearDown() throws Exception { |
118 |
waitEQ(); |
119 |
|
120 |
RUNNING = null; |
121 |
org.openide.filesystems.Repository.getDefault().removeFileSystem(fs); |
122 |
} |
123 |
|
124 |
@Override |
125 |
protected boolean runInEQ() { |
126 |
return false; |
127 |
} |
128 |
|
129 |
private void waitEQ() throws Exception { |
130 |
javax.swing.SwingUtilities.invokeAndWait(new Runnable() { |
131 |
|
132 |
@Override |
133 |
public void run() { |
134 |
} |
135 |
}); |
136 |
} |
137 |
|
138 |
LineSeparatorDataEditorSupportTest.DES support() throws Exception { |
139 |
assertNotNull(MimeLookup.getLookup(textMimePath).lookup(Preferences.class)); |
140 |
DataObject tmpObj = DataObject.find(fileObject); |
141 |
|
142 |
assertEquals("My object was created", LineSeparatorDataEditorSupportTest.MyDataObject.class, tmpObj.getClass()); |
143 |
Object cookie = tmpObj.getCookie(org.openide.cookies.OpenCookie.class); |
144 |
assertNotNull("Our object has this cookie", cookie); |
145 |
assertEquals("It is my cookie", LineSeparatorDataEditorSupportTest.DES.class, cookie.getClass()); |
146 |
|
147 |
return (LineSeparatorDataEditorSupportTest.DES) cookie; |
148 |
} |
149 |
|
150 |
public void testLineSeparator() throws Exception { |
151 |
LineSeparatorDataEditorSupportTest.DES des = support(); |
152 |
final StyledDocument doc = des.openDocument(); |
153 |
SwingUtilities.invokeAndWait(new Runnable() { |
154 |
|
155 |
@Override |
156 |
public void run() { |
157 |
try { |
158 |
doc.insertString(doc.getLength(), " Added text.\n", null); |
159 |
} catch (BadLocationException ex) { |
160 |
Exceptions.printStackTrace(ex); |
161 |
} |
162 |
} |
163 |
}); |
164 |
|
165 |
des.saveDocument(); |
166 |
assertEquals(" Added text.\r", content); |
167 |
} |
168 |
|
169 |
/** |
170 |
* File object that let us know what is happening and delegates to certain |
171 |
* instance variables of the test. |
172 |
*/ |
173 |
private static final class MyFileObject extends org.openide.filesystems.FileObject { |
174 |
|
175 |
private org.openide.filesystems.FileObject delegate; |
176 |
private int openStreams; |
177 |
private Throwable previousStream; |
178 |
|
179 |
public MyFileObject(org.openide.filesystems.FileObject del) { |
180 |
delegate = del; |
181 |
} |
182 |
|
183 |
@Override |
184 |
public java.io.OutputStream getOutputStream(FileLock lock) throws IOException { |
185 |
if (openStreams != 0) { |
186 |
IOException e = new IOException("There is stream already, cannot write down!"); |
187 |
if (previousStream != null) { |
188 |
e.initCause(previousStream); |
189 |
} |
190 |
throw e; |
191 |
} |
192 |
class ContentStream extends java.io.ByteArrayOutputStream { |
193 |
|
194 |
public ContentStream() { |
195 |
openStreams = -1; |
196 |
} |
197 |
|
198 |
@Override |
199 |
public void close() throws java.io.IOException { |
200 |
if (openStreams != -1) { |
201 |
IOException ex = new IOException("One output stream"); |
202 |
ex.initCause(previousStream); |
203 |
throw ex; |
204 |
} |
205 |
//assertEquals("One output stream", -1, openStreams); |
206 |
openStreams = 0; |
207 |
previousStream = new Exception("Closed"); |
208 |
super.close(); |
209 |
RUNNING.content = new String(toByteArray()); |
210 |
} |
211 |
} |
212 |
previousStream = new Exception("Output"); |
213 |
return new ContentStream(); |
214 |
} |
215 |
|
216 |
@Override |
217 |
public void delete(FileLock lock) throws IOException { |
218 |
delegate.delete(lock); |
219 |
} |
220 |
|
221 |
@Override |
222 |
public void setImportant(boolean b) { |
223 |
delegate.setImportant(b); |
224 |
} |
225 |
|
226 |
@Override |
227 |
public void addFileChangeListener(org.openide.filesystems.FileChangeListener fcl) { |
228 |
delegate.addFileChangeListener(fcl); |
229 |
} |
230 |
|
231 |
@Override |
232 |
public void removeFileChangeListener(org.openide.filesystems.FileChangeListener fcl) { |
233 |
delegate.removeFileChangeListener(fcl); |
234 |
} |
235 |
|
236 |
@Override |
237 |
public Object getAttribute(String attrName) { |
238 |
if (attrName.equals("default-line-separator")) { |
239 |
return "\r"; // NOI18N |
240 |
} |
241 |
return delegate.getAttribute(attrName); |
242 |
} |
243 |
|
244 |
@Override |
245 |
public FileObject createFolder(String name) throws IOException { |
246 |
throw new IOException("Not supported"); |
247 |
} |
248 |
|
249 |
@Override |
250 |
public void rename(FileLock lock, String name, String ext) throws IOException { |
251 |
throw new IOException("Not supported"); |
252 |
} |
253 |
|
254 |
@Override |
255 |
public void setAttribute(String attrName, Object value) throws IOException { |
256 |
delegate.setAttribute(attrName, value); |
257 |
} |
258 |
|
259 |
@Override |
260 |
public String getName() { |
261 |
return delegate.getName(); |
262 |
} |
263 |
|
264 |
@Override |
265 |
public java.io.InputStream getInputStream() throws java.io.FileNotFoundException { |
266 |
if (openStreams < 0) { |
267 |
FileNotFoundException e = new FileNotFoundException("Already exists output stream"); |
268 |
if (previousStream != null) { |
269 |
e.initCause(previousStream); |
270 |
} |
271 |
throw e; |
272 |
} |
273 |
|
274 |
class IS extends ByteArrayInputStream { |
275 |
|
276 |
public IS(byte[] arr) { |
277 |
super(arr); |
278 |
openStreams++; |
279 |
} |
280 |
|
281 |
@Override |
282 |
public void close() throws IOException { |
283 |
openStreams--; |
284 |
super.close(); |
285 |
} |
286 |
} |
287 |
previousStream = new Exception("Input"); |
288 |
|
289 |
return new IS(RUNNING.content.getBytes()); |
290 |
} |
291 |
|
292 |
@Override |
293 |
public FileSystem getFileSystem() throws FileStateInvalidException { |
294 |
return delegate.getFileSystem(); |
295 |
} |
296 |
|
297 |
@Override |
298 |
public FileObject getFileObject(String name, String ext) { |
299 |
return null; |
300 |
} |
301 |
|
302 |
@Override |
303 |
public String getExt() { |
304 |
return delegate.getExt(); |
305 |
} |
306 |
|
307 |
@Override |
308 |
public FileObject[] getChildren() { |
309 |
return null; |
310 |
} |
311 |
|
312 |
@Override |
313 |
public java.util.Enumeration getAttributes() { |
314 |
return delegate.getAttributes(); |
315 |
} |
316 |
|
317 |
@Override |
318 |
public FileObject createData(String name, String ext) throws IOException { |
319 |
throw new IOException("Not supported"); |
320 |
} |
321 |
|
322 |
@Override |
323 |
public FileObject getParent() { |
324 |
return delegate.getParent(); |
325 |
} |
326 |
|
327 |
@Override |
328 |
public long getSize() { |
329 |
return RUNNING.expectedSize; |
330 |
} |
331 |
|
332 |
@Override |
333 |
public boolean isData() { |
334 |
return true; |
335 |
} |
336 |
|
337 |
@Override |
338 |
public boolean isFolder() { |
339 |
return false; |
340 |
} |
341 |
|
342 |
@Override |
343 |
public boolean isReadOnly() { |
344 |
return false; |
345 |
} |
346 |
|
347 |
@Override |
348 |
public boolean isRoot() { |
349 |
return false; |
350 |
} |
351 |
|
352 |
@Override |
353 |
public boolean isValid() { |
354 |
return delegate.isValid(); |
355 |
} |
356 |
|
357 |
@Override |
358 |
public java.util.Date lastModified() { |
359 |
return RUNNING.date; |
360 |
} |
361 |
|
362 |
@Override |
363 |
public FileLock lock() throws IOException { |
364 |
return delegate.lock(); |
365 |
} |
366 |
|
367 |
public Object writeReplace() { |
368 |
return new LineSeparatorDataEditorSupportTest.Replace(); |
369 |
} |
370 |
} |
371 |
|
372 |
private static final class Replace extends Object implements java.io.Serializable { |
373 |
|
374 |
static final long serialVersionUID = 2L; |
375 |
|
376 |
public Object readResolve() { |
377 |
return RUNNING.fileObject; |
378 |
} |
379 |
} |
380 |
|
381 |
/** |
382 |
* Implementation of the DES |
383 |
*/ |
384 |
private static final class DES extends DataEditorSupport |
385 |
implements OpenCookie, EditCookie { |
386 |
|
387 |
public DES(DataObject obj, DataEditorSupport.Env env) { |
388 |
super(obj, env); |
389 |
} |
390 |
|
391 |
public org.openide.windows.CloneableTopComponent.Ref getRef() { |
392 |
return allEditors; |
393 |
} |
394 |
} |
395 |
|
396 |
/** |
397 |
* MyEnv that uses DataEditorSupport.Env |
398 |
*/ |
399 |
private static final class MyEnv extends DataEditorSupport.Env { |
400 |
|
401 |
static final long serialVersionUID = 1L; |
402 |
|
403 |
public MyEnv(DataObject obj) { |
404 |
super(obj); |
405 |
} |
406 |
|
407 |
@Override |
408 |
protected FileObject getFile() { |
409 |
return super.getDataObject().getPrimaryFile(); |
410 |
} |
411 |
|
412 |
@Override |
413 |
public String getMimeType() { |
414 |
return "text/plain"; |
415 |
} |
416 |
|
417 |
@Override |
418 |
protected FileLock takeLock() throws IOException { |
419 |
return super.getDataObject().getPrimaryFile().lock(); |
420 |
} |
421 |
} |
422 |
|
423 |
public static final class Pool extends org.openide.loaders.DataLoaderPool { |
424 |
|
425 |
@Override |
426 |
protected java.util.Enumeration loaders() { |
427 |
return org.openide.util.Enumerations.singleton(LineSeparatorDataEditorSupportTest.MyLoader.get()); |
428 |
} |
429 |
} |
430 |
|
431 |
public static final class MyLoader extends org.openide.loaders.UniFileLoader { |
432 |
|
433 |
public int primary; |
434 |
|
435 |
public static LineSeparatorDataEditorSupportTest.MyLoader get() { |
436 |
return LineSeparatorDataEditorSupportTest.MyLoader.findObject(LineSeparatorDataEditorSupportTest.MyLoader.class, true); |
437 |
} |
438 |
|
439 |
public MyLoader() { |
440 |
super(LineSeparatorDataEditorSupportTest.MyDataObject.class.getName()); |
441 |
getExtensions().addExtension("txt"); |
442 |
} |
443 |
|
444 |
protected String displayName() { |
445 |
return "MyPart"; |
446 |
} |
447 |
|
448 |
@Override |
449 |
protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { |
450 |
return new LineSeparatorDataEditorSupportTest.MyDataObject(this, primaryFile); |
451 |
} |
452 |
|
453 |
@Override |
454 |
protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { |
455 |
primary++; |
456 |
return new org.openide.loaders.FileEntry(obj, primaryFile); |
457 |
} |
458 |
} |
459 |
|
460 |
public static final class MyDataObject extends MultiDataObject |
461 |
implements CookieSet.Factory { |
462 |
|
463 |
public MyDataObject(LineSeparatorDataEditorSupportTest.MyLoader l, FileObject folder) throws DataObjectExistsException { |
464 |
super(folder, l); |
465 |
getCookieSet().add(OpenCookie.class, this); |
466 |
} |
467 |
|
468 |
@Override |
469 |
public org.openide.nodes.Node.Cookie createCookie(Class klass) { |
470 |
return new LineSeparatorDataEditorSupportTest.DES(this, new LineSeparatorDataEditorSupportTest.MyEnv(this)); |
471 |
} |
472 |
|
473 |
@Override |
474 |
protected Node createNodeDelegate() { |
475 |
return new LineSeparatorDataEditorSupportTest.MyNode(this, Children.LEAF); |
476 |
} |
477 |
} |
478 |
|
479 |
/* |
480 |
* Node which always returns non-null getHtmlDisplayName |
481 |
*/ |
482 |
public static final class MyNode extends DataNode { |
483 |
|
484 |
public MyNode(DataObject obj, Children ch) { |
485 |
super(obj, ch); |
486 |
} |
487 |
|
488 |
@Override |
489 |
public String getHtmlDisplayName() { |
490 |
return "<b>" + getDisplayName() + "</b>"; |
491 |
} |
492 |
} |
493 |
} |