KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import javax.swing.SwingUtilities JavaDoc;
25 import javax.swing.event.ChangeEvent JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
27 import junit.framework.TestCase;
28
29 /** Tests the OutWriter class
30  *
31  * @author Tim Boudreau
32  */

33 public class OutWriterTest extends TestCase {
34     private static final byte[] lineSepBytes = OutWriter.lineSepBytes;
35
36     public OutWriterTest(String JavaDoc testName) {
37         super(testName);
38     }
39
40     public void testPositionOfLine() throws UnsupportedEncodingException JavaDoc {
41         System.out.println("testPositionOfLine");
42
43         OutWriter ow = new OutWriter ();
44         
45         String JavaDoc first = "This is the first string";
46         String JavaDoc second = "This is the second string, ain't it?";
47         String JavaDoc third = "This is the third string";
48         
49         ow.println(first);
50         
51         ow.println (second);
52         
53         ow.println (third);
54         
55         int pos = ow.getLines().getLineStart(0);
56         
57         assertTrue ("First line position should be 0 but is " + pos, pos == 0);
58         String JavaDoc lineSeparator = new String JavaDoc(OutWriter.lineSepBytes, "UTF-16");
59         
60         int expectedPosition = first.length() + lineSeparator.length();
61         pos = ow.getLines().getLineStart(1);
62         
63         assertTrue ("Second line position should be length of first (" + first.length() + ") + line " +
64             "separator length (" + lineSepBytes.length + "), which should be " +
65             expectedPosition + " but is " + pos,
66             pos == expectedPosition);
67          
68         
69         pos = ow.getLines().getLineStart (2);
70         int targetPos = first.length() + second.length() + (lineSeparator.length() * 2);
71         
72         assertTrue ("Third line position should be " + targetPos + " but is " +
73             pos, pos == targetPos);
74     }
75     
76     
77     public void testPosition() throws UnsupportedEncodingException JavaDoc {
78         System.out.println("testPosition");
79         
80         OutWriter ow = new OutWriter();
81
82         
83         String JavaDoc first = "This is the first string";
84         String JavaDoc second ="This is the second string";
85         String JavaDoc third = "This is the third string";
86         
87         assertTrue (ow.getLines().getLineCount() == 0);
88         
89         ow.println(first);
90         
91         assertTrue (ow.getLines().getLineCount() == 1);
92         
93         ow.println (second);
94         
95         assertTrue (ow.getLines().getLineCount() == 2);
96
97         String JavaDoc lineSeparator = new String JavaDoc(OutWriter.lineSepBytes, "UTF-16");
98         
99         int targetLength = first.length() + second.length() + (2 * lineSeparator.length());
100
101         assertTrue (
102             "After printing strings with length " + first.length() + " and " +
103             second.length() + " outfile position should be " + targetLength +
104             " not " + ow.getLines().getCharCount(),
105             ow.getLines().getCharCount() == targetLength);
106         
107         ow.println (third);
108         
109         targetLength = first.length() + second.length() + third.length() +
110             (3 * lineSeparator.length());
111         
112         assertTrue ("Length should be " + targetLength + " but position is "
113             + ow.getLines().getCharCount(), targetLength == ow.getLines().getCharCount());
114     }
115     
116     public void testLine() throws UnsupportedEncodingException JavaDoc {
117         System.out.println("testLine");
118         
119         
120         OutWriter ow = new OutWriter ();
121         
122         String JavaDoc first = "This is the first string";
123         String JavaDoc second = "This is the second string, ain't it?";
124         String JavaDoc third = "This is the third string";
125         
126         ow.println(first);
127         
128         ow.println (second);
129         
130         ow.println (third);
131         
132         assertTrue ("After writing 3 lines, linecount should be 3, not " +
133             ow.getLines().getLineCount(), ow.getLines().getLineCount() == 3);
134         
135         String JavaDoc firstBack = null;
136         String JavaDoc secondBack = null;
137         String JavaDoc thirdBack = null;
138         try {
139             firstBack = ow.getLines().getLine(0);
140             secondBack = ow.getLines().getLine(1);
141             thirdBack = ow.getLines().getLine(2);
142         } catch (IOException JavaDoc ioe) {
143             ioe.printStackTrace();
144             fail (ioe.getMessage());
145         }
146         String JavaDoc lineSeparator = new String JavaDoc(OutWriter.lineSepBytes, "UTF-16");
147         String JavaDoc firstExpected = first + lineSeparator;
148         String JavaDoc secondExpected = second + lineSeparator;
149         String JavaDoc thirdExpected = third + lineSeparator;
150         
151         assertEquals("First string should be \"" + firstExpected + "\" but was \"" + firstBack + "\"",
152             firstBack, firstExpected);
153         
154         assertEquals("Second string should be \"" + secondExpected + "\" but was \"" + secondBack + "\"",
155             secondBack, secondExpected);
156
157         assertEquals("Third string should be \"" + thirdExpected + "\" but was \"" + thirdBack + "\"",
158             thirdBack, thirdExpected);
159         
160     }
161      
162     public void testLineForPosition() {
163         System.out.println("testLineForPosition");
164         
165         
166         OutWriter ow = new OutWriter ();
167         
168         String JavaDoc first = "This is the first string";
169         String JavaDoc second = "This is the second string, ain't it?";
170         String JavaDoc third = "This is the third string";
171         
172         ow.println(first);
173         
174         ow.println (second);
175         
176         ow.println (third);
177         
178         int line = ow.getLines().getLineAt (first.length() / 2);
179         
180         assertTrue ("Position halfway through first line should map to line 0," +
181             " not " + line,
182             line == 0);
183         
184         line = ow.getLines().getLineAt (first.length() + lineSepBytes.length +
185             (second.length() / 2));
186         
187         assertTrue ("Position halfway through line 1 should map to line 1, not " +
188             line,
189             line == 1);
190         
191         //XXX do some more tests here for very large buffers, to ensure no
192
//off-by-ones
193

194     }
195     
196     public void testLineCount() {
197         System.out.println("testLineCount");
198         
199         OutWriter ow = new OutWriter ();
200         
201         String JavaDoc first = "This is the first string";
202         String JavaDoc second = "This is the second string, ain't it?";
203         String JavaDoc third = "This is the third string";
204         
205         ow.println(first);
206         
207         ow.println (second);
208         
209         ow.println (third);
210         
211         ow.flush();
212         try {
213         SwingUtilities.invokeAndWait (new Runnable JavaDoc() {
214             public void run() {
215                 System.currentTimeMillis();
216              }
217         });
218         } catch (Exception JavaDoc e) {}
219         Thread.currentThread().yield();
220         
221         assertTrue ("Linecount should be 3 after printing 3 lines, not " +
222             ow.getLines().getLineCount(), ow.getLines().getLineCount()==3);
223     }
224
225     // mkleint TODO temporary disable.
226
// public void testAddChangeListener() {
227
// System.out.println("testAddChangeListener");
228
// OutWriter ow = new OutWriter ();
229
//
230
// CL cl = new CL();
231
// try {
232
// ow.getLines().addChangeListener(cl);
233
// } catch (Exception e) {
234
// e.printStackTrace();
235
// fail ("Caught exception " + e);
236
// }
237
//
238
//
239
// String first = "This is the first string";
240
// String second = "This is the second string, ain't it?";
241
// String third = "This is the third string";
242
//
243
// ow.println(first);
244
//
245
// ow.println (second);
246
//
247
// ow.println (third);
248
//
249
// ow.flush();
250
//
251
// cl.assertChanged();
252
//
253
// }
254

255     public void testMultilineText() {
256         System.out.println("testMultilineText");
257         OutWriter ow = new OutWriter ();
258         String JavaDoc threeLines = "This is\nthree lines of\nText";
259         ow.println(threeLines);
260         assertTrue ("Line count should be 3, not " + ow.getLines().getLineCount(), ow.getLines().getLineCount() == 3);
261         ow.println("This is another line");
262         assertTrue ("Line count should be 4, not " + ow.getLines().getLineCount(), ow.getLines().getLineCount() == 4);
263         ow.println(threeLines);
264         assertTrue ("Line count should be 7, not " + ow.getLines().getLineCount(), ow.getLines().getLineCount() == 7);
265     }
266     
267     public void testRemoveChangeListener() {
268         System.out.println("testRemoveChangeListener");
269         
270         
271         
272         OutWriter ow = new OutWriter ();
273         
274         CL cl = new CL();
275         try {
276             ow.getLines().addChangeListener(cl);
277         } catch (Exception JavaDoc e) {
278             e.printStackTrace();
279             fail ("Caught exception " + e);
280         }
281
282
283         ow.getLines().removeChangeListener(cl);
284
285         String JavaDoc first = "This is the first string";
286         String JavaDoc second = "This is the second string, ain't it?";
287         String JavaDoc third = "This is the third string";
288         
289         ow.println(first);
290         
291         ow.println (second);
292         
293         ow.println (third);
294         
295         ow.flush();
296         
297         cl.assertNoChange();
298     }
299     
300     public void testCheckDirty() {
301         System.out.println("testCheckDirty");
302         
303         
304         OutWriter ow = new OutWriter ();
305
306         boolean dirty = ow.getLines().checkDirty(true);
307         
308         String JavaDoc first = "This is the a test";
309         
310         ow.println(first);
311         
312         
313         //plan to delete checkDirty
314
}
315     
316     public void testSubstring() {
317         System.out.println("testSubstring");
318         
319         OutWriter ow = new OutWriter ();
320         
321         String JavaDoc first = "This is the first string";
322         String JavaDoc second = "This is the second string, ain't it?";
323         String JavaDoc third = "This is the third string";
324         
325         ow.println(first);
326         ow.println (second);
327         ow.println (third);
328         ow.flush();
329         
330         //First test intra-line substrings
331

332         String JavaDoc expected = first.substring(5, 15);
333         String JavaDoc gotten = ow.getLines().getText (5, 15);
334         System.err.println("\nGot " + gotten + "\n");
335         
336         assertEquals ("Should have gotten string \"" + expected + "\" but got \"" + gotten + "\"", expected, gotten);
337         
338         
339     }
340     
341     public void testPrintln() {
342         System.out.println("testPrintln");
343
344         try {
345             OutWriter ow = new OutWriter ();
346
347             String JavaDoc first = "This is a test string";
348
349             ow.println(first);
350             ow.flush();
351             
352             String JavaDoc firstExpected = first + new String JavaDoc(OutWriter.lineSepBytes, "UTF-16");
353             String JavaDoc firstReceived = ow.getLines().getLine(0);
354             
355             assertEquals ("First line should be \"" + firstExpected + "\" but was \"" + firstReceived + "\"", firstExpected, firstReceived);
356         
357         } catch (Exception JavaDoc e) {
358             e.printStackTrace();
359             fail (e.getMessage());
360         }
361         
362     }
363     
364     public void testReset() {
365         System.out.println("testReset");
366         
367     }
368     
369     public void testFlush() {
370         System.out.println("testFlush");
371         
372     }
373     
374     public void testClose() {
375         System.out.println("testClose");
376         
377     }
378
379     public void testCheckError() {
380         System.out.println("testCheckError");
381         
382     }
383
384     public void testSetError() {
385         System.out.println("testSetError");
386         
387     }
388
389     public void testWrite() {
390         System.out.println("testWrite");
391         try {
392             OutWriter ow = new OutWriter ();
393             String JavaDoc lineSeparator = new String JavaDoc(OutWriter.lineSepBytes, "UTF-16");
394   
395             ow.write('x');
396             ow.write('y');
397             ow.write('z');
398             ow.println();
399             ow.flush();
400             assertEquals(1, ow.getLines().getLineCount());
401             String JavaDoc firstReceived = ow.getLines().getLine(0);
402             assertEquals ("xyz" + lineSeparator, firstReceived);
403         
404             ow = new OutWriter();
405             ow.println("firstline");
406             ow.write('x');
407             ow.println("yz");
408             ow.flush();
409             assertEquals(2, ow.getLines().getLineCount());
410             firstReceived = ow.getLines().getLine(1);
411             assertEquals ("xyz" + lineSeparator, firstReceived);
412             
413             ow = new OutWriter();
414             ow.println("firstline");
415             ow.write(new char[] {'x', 'y', 'z'});
416             ow.write(new char[] {'x', 'y', 'z'});
417             ow.println("-end");
418             ow.flush();
419             assertEquals(2, ow.getLines().getLineCount());
420             firstReceived = ow.getLines().getLine(1);
421             assertEquals ("xyzxyz-end" + lineSeparator, firstReceived);
422             
423             ow = new OutWriter();
424             ow.write(new char[] {'x', 'y', '\n', 'z', 'z', 'z', '\n', 'A'});
425             ow.println();
426             ow.flush();
427             assertEquals(3, ow.getLines().getLineCount());
428             assertEquals("xy" + lineSeparator, ow.getLines().getLine(0));
429             assertEquals("zzz" + lineSeparator, ow.getLines().getLine(1));
430             assertEquals("A" + lineSeparator, ow.getLines().getLine(2));
431             
432             ow = new OutWriter();
433             ow.write(new char[] {'x', 'y', '\n', 'z', 'z', 'z', '\n'});
434             ow.flush();
435             assertEquals(2, ow.getLines().getLineCount());
436             assertEquals("xy" + lineSeparator, ow.getLines().getLine(0));
437             assertEquals("zzz" + lineSeparator, ow.getLines().getLine(1));
438             
439             ow = new OutWriter();
440             ow.write(new char[] {'\n', '\n', '\n', 'z', 'z', 'z', '\n'});
441             ow.flush();
442             assertEquals(4, ow.getLines().getLineCount());
443             assertEquals(lineSeparator, ow.getLines().getLine(0));
444             assertEquals(lineSeparator, ow.getLines().getLine(1));
445             assertEquals(lineSeparator, ow.getLines().getLine(2));
446             assertEquals("zzz" + lineSeparator, ow.getLines().getLine(3));
447             
448             
449         } catch (Exception JavaDoc e) {
450             e.printStackTrace();
451             fail (e.getMessage());
452         }
453     }
454     
455     public void testWritePartial() {
456         System.out.println("testWritePartial");
457         try {
458             OutWriter ow = new OutWriter ();
459             String JavaDoc lineSeparator = new String JavaDoc(OutWriter.lineSepBytes, "UTF-16");
460
461             ow.write('x');
462             assertEquals(1, ow.getLines().getLineCount());
463             assertEquals ("x", ow.getLines().getLine(0));
464             ow.write('y');
465             assertEquals ("xy", ow.getLines().getLine(0));
466             ow.write('z');
467             assertEquals ("xyz", ow.getLines().getLine(0));
468             ow.println();
469             assertEquals ("xyz" + lineSeparator, ow.getLines().getLine(0));
470             ow.write('a');
471             assertEquals(2, ow.getLines().getLineCount());
472             assertEquals ("a", ow.getLines().getLine(1));
473             
474             
475             ow = new OutWriter();
476             ow.write(new char[] { 'x', 'y', 'z', '\n', 'A'});
477             assertEquals(2, ow.getLines().getLineCount());
478             assertEquals ("xyz" + lineSeparator, ow.getLines().getLine(0));
479             assertEquals ("A", ow.getLines().getLine(1));
480             ow.write('B');
481             assertEquals(2, ow.getLines().getLineCount());
482             assertEquals ("AB", ow.getLines().getLine(1));
483             ow.println("CD");
484             assertEquals(2, ow.getLines().getLineCount());
485             assertEquals ("ABCD" + lineSeparator, ow.getLines().getLine(1));
486             
487         } catch (Exception JavaDoc e) {
488             e.printStackTrace();
489             fail (e.getMessage());
490         }
491         
492     }
493     
494
495
496    
497     
498     // TODO add test methods here, they have to start with 'test' name.
499
// for example:
500
// public void testHello() {}
501

502     
503     private class CL implements ChangeListener JavaDoc {
504         
505         public void assertChanged () {
506             ChangeEvent JavaDoc oldCE = ce;
507             ce = null;
508             assertTrue ("No change happened", oldCE != null);
509         }
510         
511         public void assertNoChange() {
512             ChangeEvent JavaDoc oldCE = ce;
513             ce = null;
514             assertFalse ("Change happened", oldCE != null);
515         }
516         
517         private ChangeEvent JavaDoc ce = null;
518         public void stateChanged(ChangeEvent JavaDoc changeEvent) {
519             ce = changeEvent;
520         }
521         
522     }
523      
524     
525 }
526
Popular Tags