001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core.output2;
043:
044: import java.io.IOException;
045: import java.io.UnsupportedEncodingException;
046: import javax.swing.SwingUtilities;
047: import javax.swing.event.ChangeEvent;
048: import javax.swing.event.ChangeListener;
049: import junit.framework.TestCase;
050:
051: /** Tests the OutWriter class
052: *
053: * @author Tim Boudreau
054: */
055: public class OutWriterTest extends TestCase {
056: private static final byte[] lineSepBytes = OutWriter.lineSepBytes;
057:
058: public OutWriterTest(String testName) {
059: super (testName);
060: }
061:
062: public void testPositionOfLine()
063: throws UnsupportedEncodingException {
064: System.out.println("testPositionOfLine");
065:
066: OutWriter ow = new OutWriter();
067:
068: String first = "This is the first string";
069: String second = "This is the second string, ain't it?";
070: String third = "This is the third string";
071:
072: ow.println(first);
073:
074: ow.println(second);
075:
076: ow.println(third);
077:
078: int pos = ow.getLines().getLineStart(0);
079:
080: assertTrue("First line position should be 0 but is " + pos,
081: pos == 0);
082: String lineSeparator = new String(OutWriter.lineSepBytes,
083: "UTF-16");
084:
085: int expectedPosition = first.length() + lineSeparator.length();
086: pos = ow.getLines().getLineStart(1);
087:
088: assertTrue("Second line position should be length of first ("
089: + first.length() + ") + line " + "separator length ("
090: + lineSepBytes.length + "), which should be "
091: + expectedPosition + " but is " + pos,
092: pos == expectedPosition);
093:
094: pos = ow.getLines().getLineStart(2);
095: int targetPos = first.length() + second.length()
096: + (lineSeparator.length() * 2);
097:
098: assertTrue("Third line position should be " + targetPos
099: + " but is " + pos, pos == targetPos);
100: }
101:
102: public void testPosition() throws UnsupportedEncodingException {
103: System.out.println("testPosition");
104:
105: OutWriter ow = new OutWriter();
106:
107: String first = "This is the first string";
108: String second = "This is the second string";
109: String third = "This is the third string";
110:
111: assertTrue(ow.getLines().getLineCount() == 0);
112:
113: ow.println(first);
114:
115: assertTrue(ow.getLines().getLineCount() == 1);
116:
117: ow.println(second);
118:
119: assertTrue(ow.getLines().getLineCount() == 2);
120:
121: String lineSeparator = new String(OutWriter.lineSepBytes,
122: "UTF-16");
123:
124: int targetLength = first.length() + second.length()
125: + (2 * lineSeparator.length());
126:
127: assertTrue("After printing strings with length "
128: + first.length() + " and " + second.length()
129: + " outfile position should be " + targetLength
130: + " not " + ow.getLines().getCharCount(), ow.getLines()
131: .getCharCount() == targetLength);
132:
133: ow.println(third);
134:
135: targetLength = first.length() + second.length()
136: + third.length() + (3 * lineSeparator.length());
137:
138: assertTrue("Length should be " + targetLength
139: + " but position is " + ow.getLines().getCharCount(),
140: targetLength == ow.getLines().getCharCount());
141: }
142:
143: public void testLine() throws UnsupportedEncodingException {
144: System.out.println("testLine");
145:
146: OutWriter ow = new OutWriter();
147:
148: String first = "This is the first string";
149: String second = "This is the second string, ain't it?";
150: String third = "This is the third string";
151:
152: ow.println(first);
153:
154: ow.println(second);
155:
156: ow.println(third);
157:
158: assertTrue("After writing 3 lines, linecount should be 3, not "
159: + ow.getLines().getLineCount(), ow.getLines()
160: .getLineCount() == 3);
161:
162: String firstBack = null;
163: String secondBack = null;
164: String thirdBack = null;
165: try {
166: firstBack = ow.getLines().getLine(0);
167: secondBack = ow.getLines().getLine(1);
168: thirdBack = ow.getLines().getLine(2);
169: } catch (IOException ioe) {
170: ioe.printStackTrace();
171: fail(ioe.getMessage());
172: }
173: String lineSeparator = new String(OutWriter.lineSepBytes,
174: "UTF-16");
175: String firstExpected = first + lineSeparator;
176: String secondExpected = second + lineSeparator;
177: String thirdExpected = third + lineSeparator;
178:
179: assertEquals("First string should be \"" + firstExpected
180: + "\" but was \"" + firstBack + "\"", firstBack,
181: firstExpected);
182:
183: assertEquals("Second string should be \"" + secondExpected
184: + "\" but was \"" + secondBack + "\"", secondBack,
185: secondExpected);
186:
187: assertEquals("Third string should be \"" + thirdExpected
188: + "\" but was \"" + thirdBack + "\"", thirdBack,
189: thirdExpected);
190:
191: }
192:
193: public void testLineForPosition() {
194: System.out.println("testLineForPosition");
195:
196: OutWriter ow = new OutWriter();
197:
198: String first = "This is the first string";
199: String second = "This is the second string, ain't it?";
200: String third = "This is the third string";
201:
202: ow.println(first);
203:
204: ow.println(second);
205:
206: ow.println(third);
207:
208: int line = ow.getLines().getLineAt(first.length() / 2);
209:
210: assertTrue(
211: "Position halfway through first line should map to line 0,"
212: + " not " + line, line == 0);
213:
214: line = ow.getLines().getLineAt(
215: first.length() + lineSepBytes.length
216: + (second.length() / 2));
217:
218: assertTrue(
219: "Position halfway through line 1 should map to line 1, not "
220: + line, line == 1);
221:
222: //XXX do some more tests here for very large buffers, to ensure no
223: //off-by-ones
224:
225: }
226:
227: public void testLineCount() {
228: System.out.println("testLineCount");
229:
230: OutWriter ow = new OutWriter();
231:
232: String first = "This is the first string";
233: String second = "This is the second string, ain't it?";
234: String third = "This is the third string";
235:
236: ow.println(first);
237:
238: ow.println(second);
239:
240: ow.println(third);
241:
242: ow.flush();
243: try {
244: SwingUtilities.invokeAndWait(new Runnable() {
245: public void run() {
246: System.currentTimeMillis();
247: }
248: });
249: } catch (Exception e) {
250: }
251: Thread.currentThread().yield();
252:
253: assertTrue("Linecount should be 3 after printing 3 lines, not "
254: + ow.getLines().getLineCount(), ow.getLines()
255: .getLineCount() == 3);
256: }
257:
258: // mkleint TODO temporary disable.
259: // public void testAddChangeListener() {
260: // System.out.println("testAddChangeListener");
261: // OutWriter ow = new OutWriter ();
262: //
263: // CL cl = new CL();
264: // try {
265: // ow.getLines().addChangeListener(cl);
266: // } catch (Exception e) {
267: // e.printStackTrace();
268: // fail ("Caught exception " + e);
269: // }
270: //
271: //
272: // String first = "This is the first string";
273: // String second = "This is the second string, ain't it?";
274: // String third = "This is the third string";
275: //
276: // ow.println(first);
277: //
278: // ow.println (second);
279: //
280: // ow.println (third);
281: //
282: // ow.flush();
283: //
284: // cl.assertChanged();
285: //
286: // }
287:
288: public void testMultilineText() {
289: System.out.println("testMultilineText");
290: OutWriter ow = new OutWriter();
291: String threeLines = "This is\nthree lines of\nText";
292: ow.println(threeLines);
293: assertTrue("Line count should be 3, not "
294: + ow.getLines().getLineCount(), ow.getLines()
295: .getLineCount() == 3);
296: ow.println("This is another line");
297: assertTrue("Line count should be 4, not "
298: + ow.getLines().getLineCount(), ow.getLines()
299: .getLineCount() == 4);
300: ow.println(threeLines);
301: assertTrue("Line count should be 7, not "
302: + ow.getLines().getLineCount(), ow.getLines()
303: .getLineCount() == 7);
304: }
305:
306: public void testRemoveChangeListener() {
307: System.out.println("testRemoveChangeListener");
308:
309: OutWriter ow = new OutWriter();
310:
311: CL cl = new CL();
312: try {
313: ow.getLines().addChangeListener(cl);
314: } catch (Exception e) {
315: e.printStackTrace();
316: fail("Caught exception " + e);
317: }
318:
319: ow.getLines().removeChangeListener(cl);
320:
321: String first = "This is the first string";
322: String second = "This is the second string, ain't it?";
323: String third = "This is the third string";
324:
325: ow.println(first);
326:
327: ow.println(second);
328:
329: ow.println(third);
330:
331: ow.flush();
332:
333: cl.assertNoChange();
334: }
335:
336: public void testCheckDirty() {
337: System.out.println("testCheckDirty");
338:
339: OutWriter ow = new OutWriter();
340:
341: boolean dirty = ow.getLines().checkDirty(true);
342:
343: String first = "This is the a test";
344:
345: ow.println(first);
346:
347: //plan to delete checkDirty
348: }
349:
350: public void testSubstring() {
351: System.out.println("testSubstring");
352:
353: OutWriter ow = new OutWriter();
354:
355: String first = "This is the first string";
356: String second = "This is the second string, ain't it?";
357: String third = "This is the third string";
358:
359: ow.println(first);
360: ow.println(second);
361: ow.println(third);
362: ow.flush();
363:
364: //First test intra-line substrings
365:
366: String expected = first.substring(5, 15);
367: String gotten = ow.getLines().getText(5, 15);
368: System.err.println("\nGot " + gotten + "\n");
369:
370: assertEquals("Should have gotten string \"" + expected
371: + "\" but got \"" + gotten + "\"", expected, gotten);
372:
373: }
374:
375: public void testPrintln() {
376: System.out.println("testPrintln");
377:
378: try {
379: OutWriter ow = new OutWriter();
380:
381: String first = "This is a test string";
382:
383: ow.println(first);
384: ow.flush();
385:
386: String firstExpected = first
387: + new String(OutWriter.lineSepBytes, "UTF-16");
388: String firstReceived = ow.getLines().getLine(0);
389:
390: assertEquals("First line should be \"" + firstExpected
391: + "\" but was \"" + firstReceived + "\"",
392: firstExpected, firstReceived);
393:
394: } catch (Exception e) {
395: e.printStackTrace();
396: fail(e.getMessage());
397: }
398:
399: }
400:
401: public void testReset() {
402: System.out.println("testReset");
403:
404: }
405:
406: public void testFlush() {
407: System.out.println("testFlush");
408:
409: }
410:
411: public void testClose() {
412: System.out.println("testClose");
413:
414: }
415:
416: public void testCheckError() {
417: System.out.println("testCheckError");
418:
419: }
420:
421: public void testSetError() {
422: System.out.println("testSetError");
423:
424: }
425:
426: public void testWrite() {
427: System.out.println("testWrite");
428: try {
429: OutWriter ow = new OutWriter();
430: String lineSeparator = new String(OutWriter.lineSepBytes,
431: "UTF-16");
432:
433: ow.write('x');
434: ow.write('y');
435: ow.write('z');
436: ow.println();
437: ow.flush();
438: assertEquals(1, ow.getLines().getLineCount());
439: String firstReceived = ow.getLines().getLine(0);
440: assertEquals("xyz" + lineSeparator, firstReceived);
441:
442: ow = new OutWriter();
443: ow.println("firstline");
444: ow.write('x');
445: ow.println("yz");
446: ow.flush();
447: assertEquals(2, ow.getLines().getLineCount());
448: firstReceived = ow.getLines().getLine(1);
449: assertEquals("xyz" + lineSeparator, firstReceived);
450:
451: ow = new OutWriter();
452: ow.println("firstline");
453: ow.write(new char[] { 'x', 'y', 'z' });
454: ow.write(new char[] { 'x', 'y', 'z' });
455: ow.println("-end");
456: ow.flush();
457: assertEquals(2, ow.getLines().getLineCount());
458: firstReceived = ow.getLines().getLine(1);
459: assertEquals("xyzxyz-end" + lineSeparator, firstReceived);
460:
461: ow = new OutWriter();
462: ow.write(new char[] { 'x', 'y', '\n', 'z', 'z', 'z', '\n',
463: 'A' });
464: ow.println();
465: ow.flush();
466: assertEquals(3, ow.getLines().getLineCount());
467: assertEquals("xy" + lineSeparator, ow.getLines().getLine(0));
468: assertEquals("zzz" + lineSeparator, ow.getLines()
469: .getLine(1));
470: assertEquals("A" + lineSeparator, ow.getLines().getLine(2));
471:
472: ow = new OutWriter();
473: ow
474: .write(new char[] { 'x', 'y', '\n', 'z', 'z', 'z',
475: '\n' });
476: ow.flush();
477: assertEquals(2, ow.getLines().getLineCount());
478: assertEquals("xy" + lineSeparator, ow.getLines().getLine(0));
479: assertEquals("zzz" + lineSeparator, ow.getLines()
480: .getLine(1));
481:
482: ow = new OutWriter();
483: ow
484: .write(new char[] { '\n', '\n', '\n', 'z', 'z',
485: 'z', '\n' });
486: ow.flush();
487: assertEquals(4, ow.getLines().getLineCount());
488: assertEquals(lineSeparator, ow.getLines().getLine(0));
489: assertEquals(lineSeparator, ow.getLines().getLine(1));
490: assertEquals(lineSeparator, ow.getLines().getLine(2));
491: assertEquals("zzz" + lineSeparator, ow.getLines()
492: .getLine(3));
493:
494: } catch (Exception e) {
495: e.printStackTrace();
496: fail(e.getMessage());
497: }
498: }
499:
500: public void testWritePartial() {
501: System.out.println("testWritePartial");
502: try {
503: OutWriter ow = new OutWriter();
504: String lineSeparator = new String(OutWriter.lineSepBytes,
505: "UTF-16");
506:
507: ow.write('x');
508: assertEquals(1, ow.getLines().getLineCount());
509: assertEquals("x", ow.getLines().getLine(0));
510: ow.write('y');
511: assertEquals("xy", ow.getLines().getLine(0));
512: ow.write('z');
513: assertEquals("xyz", ow.getLines().getLine(0));
514: ow.println();
515: assertEquals("xyz" + lineSeparator, ow.getLines()
516: .getLine(0));
517: ow.write('a');
518: assertEquals(2, ow.getLines().getLineCount());
519: assertEquals("a", ow.getLines().getLine(1));
520:
521: ow = new OutWriter();
522: ow.write(new char[] { 'x', 'y', 'z', '\n', 'A' });
523: assertEquals(2, ow.getLines().getLineCount());
524: assertEquals("xyz" + lineSeparator, ow.getLines()
525: .getLine(0));
526: assertEquals("A", ow.getLines().getLine(1));
527: ow.write('B');
528: assertEquals(2, ow.getLines().getLineCount());
529: assertEquals("AB", ow.getLines().getLine(1));
530: ow.println("CD");
531: assertEquals(2, ow.getLines().getLineCount());
532: assertEquals("ABCD" + lineSeparator, ow.getLines().getLine(
533: 1));
534:
535: } catch (Exception e) {
536: e.printStackTrace();
537: fail(e.getMessage());
538: }
539:
540: }
541:
542: // TODO add test methods here, they have to start with 'test' name.
543: // for example:
544: // public void testHello() {}
545:
546: private class CL implements ChangeListener {
547:
548: public void assertChanged() {
549: ChangeEvent oldCE = ce;
550: ce = null;
551: assertTrue("No change happened", oldCE != null);
552: }
553:
554: public void assertNoChange() {
555: ChangeEvent oldCE = ce;
556: ce = null;
557: assertFalse("Change happened", oldCE != null);
558: }
559:
560: private ChangeEvent ce = null;
561:
562: public void stateChanged(ChangeEvent changeEvent) {
563: ce = changeEvent;
564: }
565:
566: }
567:
568: }
|