KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > LineSetTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.text;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import junit.framework.*;
27
28 import org.netbeans.junit.*;
29
30 import org.openide.util.Lookup;
31 import org.openide.util.lookup.*;
32
33
34 /** Testing LineSet impl for CloneableEditorSupport.
35  *
36  * @author Jaroslav Tulach
37  */

38 public class LineSetTest extends NbTestCase implements CloneableEditorSupport.Env {
39     /** the support to work with */
40     private CloneableEditorSupport support;
41     /** the content of lookup of support */
42     private InstanceContent ic;
43
44     
45     // Env variables
46
private String JavaDoc content = "";
47     private boolean valid = true;
48     private boolean modified = false;
49     private java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
50     private java.util.List JavaDoc/*<java.beans.PropertyChangeListener>*/ propL = new java.util.ArrayList JavaDoc ();
51     private java.beans.VetoableChangeListener JavaDoc vetoL;
52
53     
54     public LineSetTest(java.lang.String JavaDoc testName) {
55         super(testName);
56     }
57     
58     public static void main(java.lang.String JavaDoc[] args) {
59         if (args.length == 1) {
60             junit.textui.TestRunner.run (new LineSetTest (args[0]));
61         }
62         junit.textui.TestRunner.run(suite());
63     }
64     
65     public static Test suite() {
66         TestSuite suite = new NbTestSuite(LineSetTest.class);
67         
68         return suite;
69     }
70     
71
72     protected void setUp () {
73         ic = new InstanceContent ();
74         support = new CES (this, new AbstractLookup (ic));
75         ic.add (this);
76     }
77
78     public void testLineSetIsEmpty () throws Exception JavaDoc {
79         support.openDocument ();
80         
81         Line.Set set = support.getLineSet();
82         assertEquals ("empty means one line ;-)", 1, set.getLines().size ());
83     }
84     
85     public void testLineSetContainsTwoLinesWhenOpen () throws Exception JavaDoc {
86         content = "Ahoj\n";
87         support.openDocument ();
88         
89         Line.Set set = support.getLineSet();
90         assertEquals ("Two lines", 2, set.getLines().size ());
91     }
92     
93     public void testLineNumberUpdatedWhenInsertedNewLine () throws Exception JavaDoc {
94         content = "Ahoj\n";
95         javax.swing.text.Document JavaDoc doc = support.openDocument ();
96
97         Line.Set set = support.getLineSet ();
98         java.util.List JavaDoc lines = set.getLines ();
99         assertEquals ("Two lines", 2, lines.size ());
100         
101         Line line = set.getCurrent (1);
102         assertEquals ("Line number is one", 1, line.getLineNumber ());
103         assertGetOriginal ("Original number is of course 1", set, line, 1);
104         
105         doc.insertString (0, "New line\n", null);
106         
107         assertEquals ("Line number is now 2", 2, line.getLineNumber ());
108         
109         Line whereIsOriginalLineOne = set.getOriginal (1);
110         assertEquals ("It is the same", line, whereIsOriginalLineOne);
111         assertEquals ("Line number is two", 2, whereIsOriginalLineOne.getLineNumber ());
112         
113         support.saveDocument ();
114         
115         Line currentLineOne = support.getLineSet ().getOriginal (1);
116         assertEquals ("Well its line number is 1", 1, currentLineOne.getLineNumber ());
117         assertFalse ("And it is not the same", currentLineOne.equals (line));
118         
119         Line currentLineTwo = support.getLineSet ().getOriginal (2);
120         assertEquals ("This is our original line", line, currentLineTwo);
121         assertGetOriginal ("Original number of the line was 1", set, line, 1);
122         
123         assertEquals ("Original set still has two lines", 2, set.getLines ().size ());
124         assertEquals ("Index of current line 1 is 0 in old set", 1, set.getLines ().indexOf (line));
125         assertEquals ("even the line number is two", 2, line.getLineNumber ());
126         java.util.List JavaDoc newLines = support.getLineSet ().getLines ();
127         assertEquals ("and the index in new set is two as well", 2, newLines.indexOf (line));
128         
129     }
130     
131     public void testWhenInsertedNewLineAtTheLineItStaysTheSame () throws Exception JavaDoc {
132         content = "Ahoj\n";
133         javax.swing.text.Document JavaDoc doc = support.openDocument ();
134
135         Line.Set set = support.getLineSet ();
136         
137         Line line = set.getOriginal (0);
138         doc.insertString (0, "Hi\n", null);
139         
140         assertEquals ("First line still stays the first", 0, line.getLineNumber ());
141     }
142
143     public void testWhenInsertedNewLineAtTheLineItStaysTheSame2 () throws Exception JavaDoc {
144         content = "Ahoj\nJak se mas?\nChlape?\n";
145         javax.swing.text.Document JavaDoc doc = support.openDocument ();
146
147         Line.Set set = support.getLineSet ();
148         
149         Line line = set.getOriginal (1);
150         assertEquals("Text of second line", "Jak se mas?\n", line.getText ());
151         assertEquals ("Line number is one", 1, line.getLineNumber());
152         assertEquals ("New line at forth position", "\n", doc.getText (4, 1));
153         doc.insertString (6, "Jarda\n", null);
154         
155         assertEquals ("Second line still stays the second", 1, line.getLineNumber ());
156     }
157     
158     // Mila claims that this is better behaviour, I do not think so,
159
// but as it is the current one, I am at least going to test it
160
public void testJoinedLinesWillNeverPart () throws Exception JavaDoc {
161         content = "111\n2\n";
162         javax.swing.text.Document JavaDoc doc = support.openDocument ();
163         
164         Line.Set set = support.getLineSet ();
165         
166         Line one = set.getOriginal (0);
167         Line two = set.getOriginal (1);
168         
169         assertEquals ("New line after first line", "\n", doc.getText (3, 1));
170         
171         doc.remove (3, 1);
172         
173         assertEquals ("Joined", one, two);
174         
175         doc.insertString (2, "\n", null);
176         
177         assertEquals ("They will not part", one, two);
178         
179         assertEquals ("Line number is 0", 0, one.getLineNumber ());
180     }
181     
182     public void testGetLinesIndexOfDoesNotCreateAllLines () throws Exception JavaDoc {
183         content = "0\n1\n2\n3\n4\n";
184         javax.swing.text.Document JavaDoc doc = support.openDocument ();
185         
186         Line.Set set = support.getLineSet ();
187         Line two = set.getOriginal (2);
188         
189         assertEquals ("Line index is two", 2, set.getLines ().indexOf (two));
190         assertNumberOfLines (1, set);
191         
192         assertEquals ("Really two", 2, new java.util.ArrayList JavaDoc (set.getLines ()).indexOf (two));
193     }
194     
195     public void testLinesAreNonMutable () throws Exception JavaDoc {
196         content = "0\n1\n2\n3\n4\n";
197         javax.swing.text.Document JavaDoc doc = support.openDocument ();
198         
199         assertNonmutable (support.getLineSet ().getLines ());
200     }
201
202     public void testGetLinesIndexWorksAfterModifications () throws Exception JavaDoc {
203         content = "0\n1\n2\n3\n4\n";
204         javax.swing.text.Document JavaDoc doc = support.openDocument ();
205         
206         Line.Set set = support.getLineSet ();
207         
208         int offset = 4;
209         assertEquals ("2 is on the second line", "2", doc.getText (4, 1));
210         doc.insertString (offset, "x\n", null);
211         assertEquals ("x\n is on the second line", "x\n", doc.getText (4, 2));
212         
213         
214         Line two = set.getOriginal (2);
215         assertEquals ("2\n is the line text", "2\n", two.getText ());
216         
217         assertEquals ("Line index is two", 2, set.getLines ().indexOf (two));
218         assertNumberOfLines (1, set);
219         
220         assertEquals ("Really two", 2, new java.util.ArrayList JavaDoc (set.getLines ()).indexOf (two));
221     }
222     
223     public void testWhatHappensWhenAskingForLineOutOfBounds () throws Exception JavaDoc {
224         content = "0";
225         javax.swing.text.Document JavaDoc doc = support.openDocument ();
226         
227         Line.Set set = support.getLineSet ();
228
229         try {
230             Line l = set.getCurrent (1);
231             fail ("Should thrown IndexOutOfBoundsException");
232         } catch (IndexOutOfBoundsException JavaDoc ex) {
233             // ok
234
}
235         
236         try {
237             Line n = set.getOriginal (1);
238             fail ("Should thrown IndexOutOfBoundsException");
239         } catch (IndexOutOfBoundsException JavaDoc ex) {
240             // ok
241
}
242         try {
243             Line l = set.getCurrent (-1);
244             fail ("Should thrown IndexOutOfBoundsException");
245         } catch (IndexOutOfBoundsException JavaDoc ex) {
246             // ok
247
}
248         
249         try {
250             Line n = set.getOriginal (-1);
251             fail ("Should thrown IndexOutOfBoundsException");
252         } catch (IndexOutOfBoundsException JavaDoc ex) {
253             // ok
254
}
255         
256         Line l = set.getCurrent (0);
257         Line n = set.getOriginal (0);
258         
259         assertNotNull (l);
260         assertNotNull (n);
261         
262         assertEquals ("Lines are the same", l, n);
263         assertEquals ("Text is", "0", l.getText ());
264     }
265
266     
267     public void testGetLinesIndexWorksForNewlyAddedLines () throws Exception JavaDoc {
268         content = "0\n1\n2\n3\n4\n";
269         javax.swing.text.Document JavaDoc doc = support.openDocument ();
270         
271         Line.Set set = support.getLineSet ();
272         
273         int offset = 4;
274         assertEquals ("2 is on the second line", "2", doc.getText (4, 1));
275         doc.insertString (offset, "x\n", null);
276         assertEquals ("x\n is on the second line", "x\n", doc.getText (4, 2));
277         
278         
279         Line two = set.getCurrent (2);
280         assertEquals ("x\n is the line text", "x\n", two.getText ());
281         
282         assertEquals ("Line index is -1 as it is not present", -1, set.getLines ().indexOf (two));
283         // two lines created as we need to verify that the current two line is not
284
// in the original set
285
// of course that it can be one if somebody implements this query
286
// in better way
287
assertNumberOfLines (2, set);
288         assertEquals ("Query on set works and does not produce new lines", 2, set.getOriginalLineNumber (two));
289         assertNumberOfLines (2, set);
290
291         // now few additinal checks
292
assertGetOriginal ("However if one asks the line set it works", set, two, 2);
293         assertEquals ("Really missing from the list", -1, new java.util.ArrayList JavaDoc (set.getLines ()).indexOf (two));
294     }
295     
296     
297     public void testGetOriginalForLineFromDifferentLineSet () throws Exception JavaDoc {
298         content = "line 1\nline 2\n";
299         javax.swing.text.Document JavaDoc doc = support.openDocument ();
300
301         Line.Set lineSet1 = support.getLineSet ();
302         Line line = lineSet1.getCurrent (1);
303         assertEquals ("line number is 1", 1, line.getLineNumber ());
304         assertGetOriginal ("original line number is 1", lineSet1, line, 1);
305         doc.insertString (6, "\nnew line", null);
306
307         assertEquals ("line number is 2", 2, line.getLineNumber ());
308         assertGetOriginal ("original line number is 1", lineSet1, line, 1);
309
310         support.saveDocument ();
311
312         assertEquals ("line number is 2", 2, line.getLineNumber ());
313         assertGetOriginal ("original line number is 1", lineSet1, line, 1);
314
315         Line.Set lineSet2 = support.getLineSet ();
316         assertEquals ("line number is 2", 2, line.getLineNumber ());
317         assertGetOriginal ("original line number is 1", lineSet1, line, 1);
318         assertGetOriginal ("original line number is 2", lineSet2, line, 2);
319         doc.insertString (6, "\nnew line", null);
320         assertEquals ("line number is 3", 3, line.getLineNumber ());
321         assertGetOriginal ("original line number is 1", lineSet1, line, 1);
322         assertGetOriginal ("original line number is 2", lineSet2, line, 2);
323         support.saveDocument ();
324         assertEquals ("line number is 3", 3, line.getLineNumber ());
325         assertGetOriginal ("original line number is 1", lineSet1, line, 1);
326         assertGetOriginal ("original line number is 2", lineSet2, line, 2);
327     }
328    
329     
330     public void testGetOriginalForLineFromDifferentLineSet2() throws Exception JavaDoc {
331         content = "line 1\nline 2\n";
332         javax.swing.text.Document JavaDoc doc =
333         support.openDocument();
334         
335         Line.Set lineSet1 = support.getLineSet();
336         
337         doc.insertString(6, "\nnew line", null);
338         support.saveDocument();
339         Line.Set lineSet2 = support.getLineSet();
340         
341         Line line = lineSet2.getCurrent(2);
342         
343         assertEquals("line number is 2",
344             2,
345             line.getLineNumber()
346         );
347         assertGetOriginal ("original line number is 1", lineSet1, line, 1);
348         assertGetOriginal("original line number is 2", lineSet2, line, 2);
349         
350         doc.insertString(6, "\nnew line", null);
351         
352         assertEquals("line number is 3", 3, line.getLineNumber());
353         assertGetOriginal("original line number is 1", lineSet1, line, 1);
354         assertGetOriginal("original line number is 2", lineSet2, line, 2);
355     }
356     
357     public void testThatNullIsNeverReturnedAsALine () throws Exception JavaDoc {
358         content = "line 1\nline 2\n";
359         javax.swing.text.Document JavaDoc doc =
360         support.openDocument();
361         
362         Line.Set lineSet1 = support.getLineSet();
363
364         support.close ();
365         
366         Line l = lineSet1.getCurrent (0);
367         assertNotNull ("Some line needs to be returned", l);
368     }
369
370     public void testLineAndLinePartCannotGoOverTheEndOfLine () throws Exception JavaDoc {
371         content = "line 1\nline 2\n";
372         javax.swing.text.Document JavaDoc doc = support.openDocument();
373         
374         Line.Set lineSet1 = support.getLineSet();
375         Line first = lineSet1.getCurrent (0);
376         Line.Part part = first.createPart (0, 10);
377         assertEquals ("Line text is the same as text of the part", first.getText (), part.getText ());
378         
379         doc.remove (2, 2);
380         assertEquals ("Ends with \n", part.getText ().length () - 1, part.getText ().indexOf ("\n"));
381         assertEquals ("Line as well", first.getText ().length () - 1, first.getText ().indexOf ("\n"));
382         assertEquals ("Still the line text is the same as text of the part", first.getText (), part.getText ());
383     }
384
385     public void testLineAndLinePartDoesNotThrowISEIssue53504 () throws Exception JavaDoc {
386         content = "line 1\nline 2\n";
387         javax.swing.text.Document JavaDoc doc = support.openDocument();
388         
389         Line.Set lineSet1 = support.getLineSet();
390         Line first = lineSet1.getCurrent (0);
391         Line.Part part = first.createPart (0, 10);
392         assertEquals ("Line text is the same as text of the part", first.getText (), part.getText ());
393         
394         doc.remove (2, doc.getLength () - 2);
395         if (!"li".equals (part.getText ())) {
396             fail ("part now contains just first two characters: " + part.getText ());
397         }
398         assertEquals ("Still the line text is the same as text of the part", first.getText (), part.getText ());
399     }
400
401     public void testCreatingLineLongerThanFileFailsIssue53504 () throws Exception JavaDoc {
402         content = "line 1";
403         javax.swing.text.Document JavaDoc doc = support.openDocument();
404         
405         Line.Set lineSet1 = support.getLineSet();
406         Line first = lineSet1.getCurrent (0);
407         Line.Part part = first.createPart (8, 10);
408         
409         assertNotNull ("Part is returned", part);
410         assertEquals ("But it is of course empty", 0, part.getText ().length ());
411     }
412     
413     private void assertNumberOfLines (int cnt, Line.Set set) throws Exception JavaDoc {
414         class MF implements org.netbeans.junit.MemoryFilter {
415             private java.util.HashSet JavaDoc counted = new java.util.HashSet JavaDoc ();
416             public int cnt;
417             public boolean reject(Object JavaDoc obj) {
418                 if (obj instanceof Line) {
419                     Line l = (Line)obj;
420                     if (counted.add (obj)) {
421                         if (l.getLookup ().lookup (LineSetTest.class) == LineSetTest.this) {
422                             cnt++;
423                         }
424                     }
425                 }
426                 return false;
427             }
428         }
429         
430         MF mf = new MF ();
431         
432         // just travel thru the memory
433
assertSize (
434             "Just one line",
435             java.util.Collections.singleton (set),
436             Integer.MAX_VALUE,
437             mf
438         );
439
440         if (mf.cnt > cnt) {
441             fail ("Only given number of instance of line created (" + cnt + ") but was: " + mf.cnt);
442         }
443     }
444
445     private static void assertGetOriginal (String JavaDoc s, Line.Set set, Line line, int expected) {
446         assertEquals (s + " - Overriden DocumentLine.Set.getOriginal as well", expected, set.getOriginalLineNumber (line));
447         assertEquals (s + " - The default Line.Set.computeOriginal method works", expected, Line.Set.computeOriginal (set, line));
448     }
449     
450     private static void assertNonmutable (java.util.List JavaDoc<? extends Line> l) throws Exception JavaDoc {
451         /*
452         try {
453             l.add (new Object ());
454             fail ("add should fail");
455         } catch (java.lang.UnsupportedOperationException ex) {
456             // ok
457         }
458         try {
459             l.add (0, new Object ());
460             fail ("add should fail");
461         } catch (java.lang.UnsupportedOperationException ex) {
462             // ok
463         }
464          */

465         
466         try {
467             l.remove (new Object JavaDoc ());
468             fail ("remove should fail");
469         } catch (java.lang.UnsupportedOperationException JavaDoc ex) {
470             // ok
471
}
472
473         /*
474         try {
475             l.addAll (java.util.Collections.EMPTY_LIST);
476             fail ("addAll should fail");
477         } catch (java.lang.UnsupportedOperationException ex) {
478             // ok
479         }
480         try {
481             l.addAll (0, java.util.Collections.EMPTY_LIST);
482             fail ("addAll should fail");
483         } catch (java.lang.UnsupportedOperationException ex) {
484             // ok
485         }
486          */

487         try {
488             l.removeAll (java.util.Collections.EMPTY_LIST);
489             fail ("removeAll should fail");
490         } catch (java.lang.UnsupportedOperationException JavaDoc ex) {
491             // ok
492
}
493         
494         try {
495             l.retainAll (java.util.Collections.EMPTY_LIST);
496             fail ("retainAll should fail");
497         } catch (java.lang.UnsupportedOperationException JavaDoc ex) {
498             // ok
499
}
500         try {
501             l.set (0, null);
502             fail ("set should fail");
503         } catch (java.lang.UnsupportedOperationException JavaDoc ex) {
504             // ok
505
}
506     }
507     
508     //
509
// Implementation of the CloneableEditorSupport.Env
510
//
511

512     public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
513         propL.add (l);
514     }
515     public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
516         propL.remove (l);
517     }
518     
519     public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
520         assertNull ("This is the first veto listener", vetoL);
521         vetoL = l;
522     }
523     public void removeVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
524         assertEquals ("Removing the right veto one", vetoL, l);
525         vetoL = null;
526     }
527     
528     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
529         return support;
530     }
531     
532     public String JavaDoc getMimeType() {
533         return "text/plain";
534     }
535     
536     public java.util.Date JavaDoc getTime() {
537         return date;
538     }
539     
540     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
541         return new java.io.ByteArrayInputStream JavaDoc (content.getBytes ());
542     }
543     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
544         class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
545             public void close () throws java.io.IOException JavaDoc {
546                 super.close ();
547                 content = new String JavaDoc (toByteArray ());
548             }
549         }
550         
551         return new ContentStream ();
552     }
553     
554     public boolean isValid() {
555         return valid;
556     }
557     
558     public boolean isModified() {
559         return modified;
560     }
561
562     public void markModified() throws java.io.IOException JavaDoc {
563         modified = true;
564     }
565     
566     public void unmarkModified() {
567         modified = false;
568     }
569
570     /** Implementation of the CES */
571     private static final class CES extends CloneableEditorSupport {
572         public CES (Env env, Lookup l) {
573             super (env, l);
574         }
575         
576         protected String JavaDoc messageName() {
577             return "Name";
578         }
579         
580         protected String JavaDoc messageOpened() {
581             return "Opened";
582         }
583         
584         protected String JavaDoc messageOpening() {
585             return "Opening";
586         }
587         
588         protected String JavaDoc messageSave() {
589             return "Save";
590         }
591         
592         protected String JavaDoc messageToolTip() {
593             return "ToolTip";
594         }
595         
596     }
597 }
598
Popular Tags