KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > OutputDocumentTest


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.netbeans.core.output2;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
26 import javax.swing.event.DocumentEvent JavaDoc;
27 import javax.swing.event.DocumentListener JavaDoc;
28 import javax.swing.text.BadLocationException JavaDoc;
29 import javax.swing.text.DefaultStyledDocument JavaDoc;
30 import javax.swing.text.Document JavaDoc;
31 import javax.swing.text.Element JavaDoc;
32 import javax.swing.text.Position JavaDoc;
33 import javax.swing.text.SimpleAttributeSet JavaDoc;
34 import junit.framework.TestCase;
35 import org.netbeans.junit.NbTestCase;
36
37 /**
38  * @author Tim Boudreau
39  */

40 public class OutputDocumentTest extends NbTestCase {
41     
42     public OutputDocumentTest(String JavaDoc testName) {
43         super(testName);
44     }
45     
46     public void testAddDocumentListener() throws Exception JavaDoc {
47         System.out.println("testAddDocumentListener");
48
49         OutWriter ow = new OutWriter ();
50         OutputDocument doc = new OutputDocument (ow);
51         final ODListener od = new ODListener();
52         doc.addDocumentListener(od);
53         String JavaDoc first = "This is the first string";
54         String JavaDoc second = "This is the second string, ain't it?";
55         String JavaDoc third = "This is the third string";
56         
57         ow.println(first);
58         ow.println (second);
59         ow.println (third);
60         ow.flush();
61 //
62
// // Make sure we process EQ events (since AbstractLines.fire uses Mutex.EVENT):
63
// EventQueue.invokeAndWait(new Runnable() {
64
// public void run() {
65
//
66
// }
67
// });
68
od.assertChanged();
69         
70     }
71     
72     public void testDocumentEvents() throws Exception JavaDoc {
73         System.out.println("testDocumentEvents");
74
75         OutWriter ow = new OutWriter ();
76         OutputDocument doc = new OutputDocument (ow);
77         ODListener od = new ODListener();
78         doc.addDocumentListener(od);
79         
80         for (int i=0; i < 100; i++) {
81             ow.println("This is string " + i);
82         }
83         
84 // SwingUtilities.invokeAndWait (new Runnable() {
85
// public void run(){
86
// System.currentTimeMillis();
87
// }
88
// });
89
Thread.currentThread().sleep(1500);
90         
91         DocumentEvent JavaDoc de = od.getEvent();
92         assertTrue ("Event should have been fired", de != null);
93         
94         int elCount = doc.getElementCount();
95         
96         assertTrue ("Element count should be 100 after printing 100 lines, " +
97             "not " + elCount,
98             elCount == 100);
99         
100         DocumentEvent.ElementChange JavaDoc ec = de.getChange(doc);
101         
102         Element JavaDoc[] added = ec.getChildrenAdded();
103         assertNotNull("Children added should not be null", added);
104 // assertTrue("Number of children added should be 100, not " + added.length, added.length == 100);
105
// assertTrue ("Index of change should be 0, not " + ec.getIndex(), ec.getIndex() == 0);
106
}
107     
108     public void testCreatePosition() {
109         System.out.println("testCreatePosition");
110         
111         OutWriter ow = new OutWriter ();
112         OutputDocument doc = new OutputDocument (ow);
113         String JavaDoc first = "This is the first string";
114         String JavaDoc second = "This is the second string, ain't it?";
115         String JavaDoc third = "This is the third string";
116         ow.println(first);
117         ow.println (second);
118         ow.println (third);
119         ow.flush();
120         Position JavaDoc pos = null;
121         try {
122             pos = doc.createPosition (15);
123         } catch (BadLocationException JavaDoc ble) {
124             ble.printStackTrace();
125             fail ("Unexpected BadLocationException: " + ble.getMessage());
126         }
127         assertNotNull (pos);
128         
129         assertTrue (pos.getOffset() == 15);
130         
131         BadLocationException JavaDoc ble = null;
132         try {
133             pos = doc.createPosition (65543);
134         } catch (BadLocationException JavaDoc e) {
135             ble = e;
136         }
137         assertNotNull("Exception should have been thrown when creating a bogus location", ble);
138         
139         try {
140             pos = doc.createPosition (-2);
141         } catch (BadLocationException JavaDoc e) {
142             ble = e;
143         }
144         assertNotNull("Exception should have been thrown when creating a bogus negative location", ble);
145     }
146     
147     public void testStillGrowing() {
148         System.out.println("testStillGrowing");
149         
150         OutWriter ow = new OutWriter ();
151         OutputDocument doc = new OutputDocument (ow);
152         String JavaDoc first = "This is the first string";
153         String JavaDoc second = "This is the second string, ain't it?";
154         String JavaDoc third = "This is the third string";
155         ow.println(first);
156         ow.println (second);
157         ow.println (third);
158         assertTrue ("After a few writes, document should not think its output stream is closed, it isn't.", doc.getLines().isGrowing());
159         ow.flush();
160         assertTrue ("After a flush, document should not think its output stream is closed, it isn't.", doc.getLines().isGrowing());
161         ow.close();
162         assertFalse ("After closing the writer, the document should not expect itself to grow", doc.getLines().isGrowing());
163     }
164     
165     private static String JavaDoc STATE = "(STATE NOT SET!)";
166     public void testWordWrap() {
167         System.out.println("testWordWrap - line data caching ON");
168         STATE = "(CACHED MODE)";
169         AbstractLines.unitTestUseCache(Boolean.TRUE);
170         doTestWordWrap();
171         
172         System.out.println("testWordWrap - line data caching OFF");
173         STATE = "(DYNAMIC MODE)";
174         AbstractLines.unitTestUseCache(Boolean.FALSE);
175 // doTestWordWrap();
176

177         AbstractLines.unitTestUseCache(null);
178         STATE = "(STATE NOT SET!)";
179     }
180         
181     private void doTestWordWrap() {
182         OutWriter ow = new OutWriter ();
183         OutputDocument doc = new OutputDocument (ow);
184         
185         char[] c120 = new char[120];
186         Arrays.fill(c120, 'A');
187         String JavaDoc s120 = new String JavaDoc(c120);
188         char[] c80 = new char[80];
189         Arrays.fill(c80, 'B');
190         String JavaDoc s80 = new String JavaDoc(c80);
191         char[] c20 = new char[20];
192         Arrays.fill (c20, 'C');
193         String JavaDoc s20 = new String JavaDoc(c20);
194         
195         ow.println (c80);
196         ow.println (c80);
197         ow.println (c80);
198         
199         int val = doc.getLines().getLogicalLineCountAbove(2, 90);
200         assertTrue (STATE + "With three 80 character lines of data, wrapped at 90 characters, there should be 2 lines above line 2, not " + val, val == 2);
201         
202         val = doc.getLines().getLogicalLineCountIfWrappedAt(90);
203         assertTrue (STATE + "With three 80 character lines of data, wrapped at 90 characters, the line count should be 3, not " + val, val == 3);
204         assertTrue (val == ow.getLines().getLineCount());
205         
206         val = doc.getLines().getLogicalLineCountIfWrappedAt(50);
207         assertTrue (STATE + "With three 80 character lines of data, wrapped at 50 characters, the line count should be 6, not " + val, val == 6);
208         
209         val = doc.getLines().getLogicalLineCountAbove(2, 50);
210         assertTrue (STATE + "With three 80 character lines of data, wrapped at 50 characters, there should be 4 logical lines above 2, not " + val, val == 4);
211         
212         int[] wrapData = new int[] {5, 0, 0};
213         doc.getLines().toLogicalLineIndex(wrapData, 50);
214         assertTrue("The logical line index of the 5th phys line with three 80 char lines wrapped at 50 chars should be 2 in the document, not " + wrapData[0], wrapData[0] == 2);
215         assertTrue("An 80 char line should wrap twice, not " + wrapData[2], wrapData[2] == 2);
216         assertTrue("On the 5th physical line with three 80 char lines wrapped at 50 chars should be the 1st line of actual line 3, not " + wrapData[1], wrapData[1] == 1);
217         
218         wrapData[0] = 6;
219         doc.getLines().toLogicalLineIndex(wrapData, 50);
220         assertTrue("On the 5th physical line with three 80 char lines wrapped at 50 chars should be the 2nd line of actual line 3, not " + wrapData[1], wrapData[1] == 2);
221         
222         ow.println(c20);
223         ow.println(c80);
224
225         val = doc.getLines().getLogicalLineCountAbove(3, 50);
226         assertTrue ("There should be 6 logical lines above a 20 char line following three 80 char lines when wrapped at 50 chars", val == 6);
227         
228         wrapData[0] = 6;
229         doc.getLines().toLogicalLineIndex(wrapData, 50);
230         assertTrue ("20 char line should not be wrapped, but shows " + wrapData[2] + " wraps", wrapData[2] == 1);
231     }
232     
233     public void testGetDefaultRootElement() {
234         System.out.println("testGetDefaultRootElement");
235         
236         OutWriter ow = new OutWriter ();
237         OutputDocument doc = new OutputDocument (ow);
238         String JavaDoc first = "This is the first string";
239         String JavaDoc second = "This is the second string, ain't it?";
240         String JavaDoc third = "This is the third string";
241         ow.println(first);
242         ow.println (second);
243         ow.println (third);
244         ow.flush();
245         
246         Element JavaDoc el = doc.getDefaultRootElement();
247         assertNotNull ("Root element should not be null", el);
248         
249         assertTrue ("Root offset should be 0", el.getStartOffset() == 0);
250         assertTrue ("Root ending char should be count of written chars", el.getEndOffset() == ow.getLines().getCharCount());
251         assertTrue ("Wrong document object from default root element's getDocument method", el.getDocument() == doc);
252         assertTrue ("Element count of the root element should be the line count", el.getElementCount() == ow.getLines().getLineCount());
253         
254         
255     }
256     
257     public void testGetEndPosition() {
258         System.out.println("testGetEndPosition");
259         
260         OutWriter ow = new OutWriter ();
261         OutputDocument doc = new OutputDocument (ow);
262         String JavaDoc first = "This is the first string";
263         String JavaDoc second = "This is the second string, ain't it?";
264         String JavaDoc third = "This is the third string";
265         ow.println(first);
266         ow.println (second);
267         ow.flush();
268
269         Position JavaDoc pos = doc.getEndPosition();
270         
271         int offset = pos.getOffset();
272         
273         assertTrue ("End offset should match number of characters written", offset == ow.getLines().getCharCount());
274         
275         ow.println (third);
276         ow.flush();
277
278         assertTrue ("Document end offset should change after writing more data", offset != pos.getOffset());
279         assertTrue ("End offset should match number of characters written", pos.getOffset() == ow.getLines().getCharCount());
280         
281     }
282     
283     public void testGetLength() throws UnsupportedEncodingException JavaDoc {
284         System.out.println("testGetLength");
285         
286         OutWriter ow = new OutWriter ();
287         OutputDocument doc = new OutputDocument (ow);
288         String JavaDoc first = "This is the first string";
289         String JavaDoc second = "This is the second string, ain't it?";
290         String JavaDoc third = "This is the third string";
291         ow.println(first);
292         ow.println (second);
293         ow.println (third);
294         ow.flush();
295         
296         String JavaDoc lineSeparator = new String JavaDoc(OutWriter.lineSepBytes, "UTF-16");
297         
298         int expectedLength = first.length() + second.length() + third.length() + (3 * lineSeparator.length()) ;
299         int receivedLength = doc.getLength();
300         
301         assertTrue ("Number of characters counting carriage returns should be "
302             + expectedLength + " but was " + receivedLength, expectedLength == receivedLength);
303         
304     }
305     
306     public void testGetRootElements() {
307         System.out.println("testGetRootElements");
308         
309         OutWriter ow = new OutWriter ();
310         OutputDocument doc = new OutputDocument (ow);
311         String JavaDoc first = "This is the first string";
312         String JavaDoc second = "This is the second string, ain't it?";
313         String JavaDoc third = "This is the third string";
314         ow.println(first);
315         ow.println (second);
316         ow.println (third);
317         ow.flush();
318         
319         Element JavaDoc[] el = doc.getRootElements();
320         assertTrue ("Should be only one root element", el.length == 1);
321         assertTrue ("Root element should be the document", el[0] == doc);
322         
323     }
324     
325     public void testGetStartPosition() {
326         System.out.println("testGetStartPosition");
327         OutWriter ow = new OutWriter ();
328         OutputDocument doc = new OutputDocument (ow);
329         String JavaDoc first = "This is the first string";
330         String JavaDoc second = "This is the second string, ain't it?";
331         String JavaDoc third = "This is the third string";
332         ow.println(first);
333         ow.println (second);
334         ow.println (third);
335         ow.flush();
336         
337         Position JavaDoc p = doc.getStartPosition();
338         
339         assertTrue ("Start position should be start offset", p.getOffset() == doc.getStartOffset());
340         assertTrue ("Start offset should be 0", p.getOffset() == 0);
341         
342     }
343     
344     public void testGetText() throws UnsupportedEncodingException JavaDoc {
345         System.out.println("testGetText");
346         OutWriter ow = new OutWriter ();
347         OutputDocument doc = new OutputDocument (ow);
348         String JavaDoc first = "This is the first string";
349         ow.println(first);
350         ow.flush();
351         String JavaDoc received = null;
352         
353         try {
354             received = doc.getText(3, 7);
355         } catch (BadLocationException JavaDoc ble) {
356             ble.printStackTrace();
357             fail ("Unexpected BadLocationException: " + ble.getMessage());
358         }
359         String JavaDoc expected = first.substring (3, 10);
360         
361         assertEquals ("getText() returned \"" + received + "\" but should have returned \"" + expected + "\"", expected, received);
362        
363         try {
364             received = doc.getText (0, first.length());
365         } catch (BadLocationException JavaDoc e) {
366             e.printStackTrace();
367             fail ("Unexpected BadLocationException thrown: " + e.getMessage());
368         }
369         assertEquals ("getText for the full first string printed should return \"" + first + "\" but got \"" + received + "\"", first, received);
370         
371         String JavaDoc second = "Lets try a different string this time";
372         ow.println(second);
373         ow.flush();
374         try {
375             received = doc.getText (0, doc.getLength());
376         } catch (BadLocationException JavaDoc ble) {
377             ble.printStackTrace();
378             fail ("Unexpected BadLocationException: " + ble.getMessage());
379         }
380         String JavaDoc lineSeparator = new String JavaDoc(OutWriter.lineSepBytes, "UTF-16");
381         expected = first + lineSeparator + second + lineSeparator;
382         
383         assertEquals ("getText for first two strings should be \"" + expected + "\" but was \"" + received + "\"", expected, received);
384     }
385     
386     public void testInsertString() {
387         System.out.println("testInsertString");
388         OutWriter ow = new OutWriter ();
389         OutputDocument doc = new OutputDocument (ow);
390         String JavaDoc first = "This is the first string";
391         ow.println(first);
392         ow.flush();
393         
394         UnsupportedOperationException JavaDoc uoe = null;
395         try {
396             doc.insertString(5, "Foo", null);
397         } catch (UnsupportedOperationException JavaDoc e) {
398             uoe = e;
399         } catch (BadLocationException JavaDoc e) {
400             fail ("Bad location exception thrown - should have been UnsupportedOperationException");
401         }
402         assertNotNull ("insertString should throw an unsupportedOperationException ", uoe);
403         
404     }
405
406      
407     public void testRender() {
408         System.out.println("testRender");
409
410         OutWriter ow = new OutWriter ();
411         OutputDocument doc = new OutputDocument (ow);
412         String JavaDoc first = "This is the first string";
413         ow.println(first);
414         ow.flush();
415
416         RunIt r = new RunIt();
417         doc.render (r);
418         r.assertWasRun();
419
420     }
421     
422     private static class RunIt implements Runnable JavaDoc {
423         private boolean hasBeenRun = false;
424         
425         public void assertWasRun() {
426             assertTrue (hasBeenRun);
427         }
428         
429         public void run() {
430             hasBeenRun = true;
431         }
432     }
433     
434     public void testGetDocument() {
435         System.out.println("testGetDocument");
436         
437         OutWriter ow = new OutWriter ();
438         OutputDocument doc = new OutputDocument (ow);
439         String JavaDoc first = "This is the first string";
440         ow.println(first);
441         ow.flush();
442
443         
444         assertTrue(doc.getDocument() == doc);
445
446     }
447     
448     public void testGetElement() throws UnsupportedEncodingException JavaDoc {
449         System.out.println("testGetElement");
450         
451         OutWriter ow = new OutWriter ();
452         OutputDocument doc = new OutputDocument (ow);
453         String JavaDoc first = "This is the first string";
454         String JavaDoc second = "This is the second string, ain't it?";
455         String JavaDoc third = "This is the third string";
456         ow.println(first);
457         ow.println (second);
458         ow.println (third);
459         ow.flush();
460         String JavaDoc lineSeparator = new String JavaDoc(OutWriter.lineSepBytes, "UTF-16");
461         
462         Element JavaDoc el = doc.getElement(0);
463         assertTrue (el.getStartOffset() == 0);
464         assertTrue ("End offset should be length of string + separator length ("
465             + (first.length() + lineSeparator.length()) + " but was " + el.getEndOffset(),
466             el.getEndOffset() == first.length() + lineSeparator.length());
467         
468         el = doc.getElement(1);
469     }
470     
471     public void testGetElementCount() {
472         System.out.println("testGetElementCount");
473         OutWriter ow = new OutWriter ();
474         OutputDocument doc = new OutputDocument (ow);
475         String JavaDoc first = "This is the first string";
476         String JavaDoc second = "This is the second string, ain't it?";
477         String JavaDoc third = "This is the third string";
478         ow.println(first);
479         ow.println (second);
480         ow.println (third);
481         ow.flush();
482         assertTrue ("Element count for document should match line count", doc.getElementCount() == ow.getLines().getLineCount());
483     }
484     
485     public void testGetElementIndex() {
486         System.out.println("testGetElementIndex");
487         
488         OutWriter ow = new OutWriter ();
489         OutputDocument doc = new OutputDocument (ow);
490         String JavaDoc first = "This is the first string";
491         String JavaDoc second = "This is the second string, ain't it?";
492         String JavaDoc third = "This is the third string";
493         ow.println(first);
494         ow.println (second);
495         ow.println (third);
496         ow.flush();
497         
498         int idx = doc.getElementIndex(5);
499         assertTrue ("Element index five characters into the first string should be element 0, not " + idx, idx == 0);
500         
501         idx = doc.getElementIndex (first.length() + 1 + 5);
502         assertTrue ("Element index five characters into the second string should be 1, not " + idx, idx == 1);
503         
504         
505         idx = doc.getElementIndex(first.length() + 1 + second.length() + 1 + (third.length() / 2));
506         assertTrue ("Element index halfway through the third string should be 2, not " + idx,
507             idx == 2);
508     }
509     
510     public void testGetEndOffset() {
511         System.out.println("testGetEndOffset");
512         
513         OutWriter ow = new OutWriter ();
514         OutputDocument doc = new OutputDocument (ow);
515         String JavaDoc first = "This is the first string";
516         String JavaDoc second = "This is the second string, ain't it?";
517         String JavaDoc third = "This is the third string";
518         ow.println(first);
519         ow.println (second);
520         ow.println (third);
521         ow.flush();
522         
523         assertTrue ("End offset should be chars printed", doc.getEndOffset() == ow.getLines().getCharCount());
524     }
525     
526     public void testGetParentElement() {
527         System.out.println("testGetParentElement");
528         
529         OutWriter ow = new OutWriter ();
530         OutputDocument doc = new OutputDocument (ow);
531         String JavaDoc first = "This is the first string";
532         String JavaDoc second = "This is the second string, ain't it?";
533         String JavaDoc third = "This is the third string";
534         ow.println(first);
535         ow.println (second);
536         ow.println (third);
537         ow.flush();
538         
539         assertNull ("Document parent element should be null ", doc.getParentElement());
540         
541         Element JavaDoc el = doc.getElement(1);
542         assertSame ("Parent element of line element should be the document", el.getParentElement(), doc);
543         
544     }
545     
546     public void testGetStartOffset() {
547         System.out.println("testGetStartOffset");
548         // XXX
549
}
550     
551     public void testIsLeaf() {
552         System.out.println("testIsLeaf");
553         
554         OutWriter ow = new OutWriter ();
555         OutputDocument doc = new OutputDocument (ow);
556         
557         
558         assertTrue ("Document should be leaf if no text has been written", doc.isLeaf());
559         
560         String JavaDoc first = "This is the first string";
561         String JavaDoc second = "This is the second string, ain't it?";
562         String JavaDoc third = "This is the third string";
563         ow.println(first);
564         ow.println (second);
565         ow.println (third);
566         ow.flush();
567         
568         assertFalse("Document should not be leaf if text has been written", doc.isLeaf());
569     }
570
571     
572     public void testDocumentEventSimilarity() throws Exception JavaDoc {
573         System.out.println("testDocumentEventSimilarity");
574         DefaultStyledDocument JavaDoc styled = new DefaultStyledDocument JavaDoc();
575         OutWriter ow = new OutWriter ();
576         OutputDocument doc = new OutputDocument (ow);
577         
578         ODListener docListener = new ODListener(doc);
579         ODListener styListener = new ODListener(styled);
580         
581         String JavaDoc lineSeparator = new String JavaDoc(OutWriter.lineSepBytes, "UTF-16");
582
583         
584         
585         String JavaDoc s = "This is a string I will append";
586         
587         styled.insertString(styled.getLength(), s + lineSeparator, SimpleAttributeSet.EMPTY);
588         ow.println (s);
589         ow.flush();
590         docListener.assertChanged();
591         styListener.assertChanged();
592         
593         styled.insertString(styled.getLength(), s + lineSeparator, SimpleAttributeSet.EMPTY);
594         ow.println (s);
595         ow.flush();
596         
597 // //Wait for async event firing from output document
598
// SwingUtilities.invokeAndWait (new Runnable() {
599
// public void run() {
600
// System.currentTimeMillis();
601
// }
602
// });
603
// Thread.currentThread().sleep (1000);
604

605         int styLen = styled.getLength();
606         int docLen = doc.getLength();
607         
608         assertTrue ("Length should be same, but OutputDocument length is " +
609             docLen + " and similar StyledDocument length is " + styLen,
610             doc.getLength() == styled.getLength());
611         
612         DocumentEvent JavaDoc docEvent = docListener.getEvent();
613         DocumentEvent JavaDoc styEvent = styListener.getEvent();
614
615         assertNotNull("StyledDocument should have fired an event", styEvent);
616         assertNotNull("OutputDocument should have fired an event", docEvent);
617         
618         assertEventsIdentical (styled, doc, styEvent, docEvent);
619         
620         //Stress test it to ensure no off-by-ones that show up only when the file is large
621
for (int i = 0; i < 10; i++) {
622             for (int j=0; j < STRINGS.length; j++) {
623                 styled.insertString(styled.getLength(), s + lineSeparator, SimpleAttributeSet.EMPTY);
624                 ow.println (s);
625                 ow.flush();
626
627 // //Wait for async event firing from output document
628
// SwingUtilities.invokeAndWait (new Runnable() {
629
// public void run() {
630
// System.currentTimeMillis();
631
// }
632
// });
633
// SwingUtilities.invokeAndWait (new Runnable() {
634
// public void run() {
635
// System.currentTimeMillis();
636
// }
637
// });
638
// Thread.currentThread().sleep (500);
639

640                 styLen = styled.getLength();
641                 docLen = doc.getLength();
642
643                 assertTrue ("Length should be same, but OutputDocument length is " +
644                     docLen + " and similar StyledDocument length is " + styLen,
645                     doc.getLength() == styled.getLength());
646
647                 docEvent = docListener.getEvent();
648                 styEvent = styListener.getEvent();
649                 
650                 assertEventsIdentical (styled, doc, styEvent, docEvent);
651
652             }
653         }
654         
655     }
656     
657     private void assertEventsIdentical (Document JavaDoc styled, OutputDocument doc, DocumentEvent JavaDoc styEvent, DocumentEvent JavaDoc docEvent) throws Exception JavaDoc {
658         int docOffset = docEvent.getOffset();
659         int styOffset = styEvent.getOffset();
660         
661         int docLength = docEvent.getLength();
662         int styLength = styEvent.getLength();
663         
664         assertTrue ("OutputDocument event offset is " + docOffset + " but " +
665             "offset of identical change to a StyledDocument is " + styOffset,
666             docOffset == styOffset);
667         
668         assertTrue ("OutputDocument event length is " + docLength + " but " +
669             "length from identical change to a StyledDocument is " +
670             styLength, styLength == docLength);
671         
672         DocumentEvent.ElementChange JavaDoc docEc = docEvent.getChange(doc);
673         DocumentEvent.ElementChange JavaDoc styEc = styEvent.getChange(styled.getDefaultRootElement());
674         
675         int docIndex = docEc.getIndex();
676         int styIndex = styEc.getIndex();
677         
678         Element JavaDoc[] docAdded = docEc.getChildrenAdded();
679         Element JavaDoc[] styAdded = styEc.getChildrenAdded();
680         
681         assertTrue ("Index of change in OutputDocument was " + docIndex + " but" +
682         " an identical change on a StyledDocument returns " + styIndex,
683         styIndex == docIndex);
684         
685         assertTrue ("OutputDocument returned an array of " + docAdded.length +
686             " affected elements, but an identical change on a StyledDocument " +
687             "produces an array of " + styAdded.length, styAdded.length ==
688             docAdded.length);
689
690         for (int i=0; i < docAdded.length; i++) {
691             int docStartOffset = docAdded[i].getStartOffset();
692             int styStartOffset = styAdded[i].getStartOffset();
693             assertTrue ("Start offset of element " + i + " from " +
694                 "OutputDocument.ODDEvent.EC is " + docStartOffset + " but " +
695                 "offset from identical change in a StyledDocument is " +
696                 styStartOffset, styStartOffset == docStartOffset);
697             
698             int docEndOffset = docAdded[i].getEndOffset();
699             int styEndOffset = styAdded[i].getEndOffset();
700             assertTrue ("End offset of element " + i + " from " +
701                 "OutputDocument.ODDEvent.EC is " + docStartOffset + " but " +
702                 "offset from identical change in a StyledDocument is " +
703                 styEndOffset, styEndOffset == docEndOffset);
704             
705             String JavaDoc styTxt = styled.getText(styAdded[i].getStartOffset(), styAdded[i].getEndOffset() - styAdded[i].getStartOffset());
706             String JavaDoc docTxt = styled.getText(styAdded[i].getStartOffset(), styAdded[i].getEndOffset() - styAdded[i].getStartOffset());
707             assertEquals("Element " + i + " text from styled document is " +
708                 styTxt + " but OutputDocument return " + docTxt + " for the " +
709                 "same indices", styTxt, docTxt);
710         }
711     }
712     
713     private String JavaDoc[] STRINGS = new String JavaDoc[] {
714         "Okay, we need some content for this test. I wonder what it should " +
715             "be? We should probably have some seriously long strings in here, " +
716             "just to make sure everythings okay. After all, we wouldn't want to" +
717             "lose a byte or two of output one day, that would be bad. But if " +
718             "everything works out, this thing might just be pretty cool, and " +
719             "efficient, and nifty, and all that stuff. Wouldn't that be nice?" +
720             " Of course it would be silly! Good heavens, I need a vacation.",
721         "This is a short, non-editorializing string",
722         "Should you get a chance to read it, David McCullough's biography of " +
723         "John Adams is quite good.",
724         "Short string",
725         "Okay, let's just go ahead and write another long string - after all, " +
726         "what else was I going to do with my time anyway? Did you ever consider" +
727         "what it would be like to *be* an output window? I mean really, whatever" +
728         "any body prints, you just display it on the front of you. Never " +
729         "complain, never get to be creative, never get to, say, write some " +
730         "POETRY, or turn that exception message into a nice haiku. I mean, " +
731         "once in a while it would be nice to let your hair down, after all - " +
732         "all this build failed stuff is just a collossal bore. C'mon, gimme some" +
733         "GOOD output for once",
734         "I'm sure there's an explanation for all of this. I just don't know" +
735         "what it is"
736     };
737
738     protected boolean runInEQ() {
739         return true;
740     }
741     
742     /*
743     public void testMultithreadedWrites() throws Exception {
744         System.err.println("testMultithreadedWrites");
745         DefaultStyledDocument styled = new DefaultStyledDocument();
746         OutWriter ow = new OutWriter ();
747         OutputDocument doc = new OutputDocument (ow);
748         
749         ODListener docListener = new ODListener(doc);
750         ODListener styListener = new ODListener(styled);
751         
752         String s = "This is a string I will append";
753         
754         threadCount = 5;
755         bangers = new Thread[threadCount];
756         System.err.println("Starting 15 threads");
757         for (int i=0; i < threadCount; i++) {
758             Banger b = new Banger (i, doc, styled, ow);
759             bangers[i] = new Thread(b);
760             bangers[i].start();
761         }
762         System.err.println("notifying start lock");
763         synchronized (START_LOCK) {
764             START_LOCK.notifyAll();
765         }
766         System.err.println("Waiting on stop lock");
767         synchronized (STOP_LOCK) {
768             STOP_LOCK.wait();
769             System.err.println("Stop lock notified");
770         }
771         System.err.println("Finished with the madness");
772         
773         int len1 = doc.getLength();
774         int len2 = styled.getLength();
775         assertTrue ("Document and styled document should have same length " +
776             " but OutputDocument is " + len1 + " and StyledDocument " + len2,
777             len1 == len2);
778         
779         int ct1 = doc.getElementCount();
780         int ct2 = styled.getDefaultRootElement().getElementCount();
781         
782         assertTrue ("Document and styled document should " +
783             "have same number of elements " +
784             " but OutputDocument is " + ct1 + " and StyledDocument " + ct2,
785             ct1 == ct2);
786         
787         
788     }
789     
790     private static Thread[] bangers = null;
791     private static final Object START_LOCK = new Object();
792     private static final Object WRITE_LOCK = new Object();
793     private static final Object STOP_LOCK = new Object();
794     private static int threadCount = 0;
795     private class Banger implements Runnable {
796         private int index;
797         private OutputDocument doc;
798         private StyledDocument sty;
799         private OutWriter ow;
800        
801         public Banger (int index, OutputDocument doc, StyledDocument sty, OutWriter ow) {
802             this.index = index;
803             this.doc = doc;
804             this.sty = sty;
805             this.ow = ow;
806         }
807         public void run() {
808             synchronized (START_LOCK) {
809                 try {
810                     System.err.println("Banger thread " + index + " waiting to start");
811                     START_LOCK.wait();
812                 } catch (Exception e) {
813                     e.printStackTrace();
814                     fail("Interrupted");
815                 }
816             }
817             System.err.println("Banger thread " + index + " started ");
818             for (int i=0; i < 10; i++) {
819                 String s = "Hello " + i + " from banger thread " + index;
820 // synchronized (WRITE_LOCK) {
821                 System.err.println(s);
822                     try {
823                         sty.insertString(sty.getLength(), s + "\n", SimpleAttributeSet.EMPTY);
824                         ow.println (s);
825 // ow.flush();
826                     } catch (Exception e) {
827                         for (int j=0; j < bangers.length; j++) {
828                             if (bangers[j] != Thread.currentThread()) {
829                                 bangers[j].stop();
830                             }
831                             e.printStackTrace();
832                             fail (e.getMessage());
833                         }
834                     }
835                 Thread.currentThread().yield();
836                 }
837 // }
838             synchronized (START_LOCK) {
839                 threadCount--;
840             }
841             if (threadCount == 0) {
842                 synchronized (STOP_LOCK) {
843                     ow.flush();
844                     STOP_LOCK.notifyAll();
845                 }
846             }
847         }
848     }
849      */

850     
851     
852     final class ODListener implements DocumentListener JavaDoc {
853         DocumentEvent JavaDoc evt = null;
854         
855         public ODListener(){}
856         
857         public ODListener (Document JavaDoc d) {
858             d.addDocumentListener(this);
859         }
860
861         public void assertChanged() {
862             DocumentEvent JavaDoc e = evt;
863             evt = null;
864             assertNotNull("No event received", e);
865             //Trigger consumed on OutputDocument.DO
866
e.getLength();
867         }
868         
869         public void assertNoChange() {
870             DocumentEvent JavaDoc e = evt;
871             evt = null;
872             assertNull("Unexpected event was received " + e, e);
873         }
874         
875         public DocumentEvent JavaDoc getEvent() {
876             return evt;
877         }
878         
879         
880         public void changedUpdate(DocumentEvent JavaDoc documentEvent) {
881             evt = documentEvent;
882         }
883         
884         public void insertUpdate(DocumentEvent JavaDoc documentEvent) {
885             evt = documentEvent;
886         }
887         
888         public void removeUpdate(DocumentEvent JavaDoc documentEvent) {
889             evt = documentEvent;
890         }
891         
892     }
893     
894 }
895
Popular Tags