1 19 20 package org.netbeans.core.output2; 21 22 import java.io.IOException ; 23 import java.io.UnsupportedEncodingException ; 24 import javax.swing.SwingUtilities ; 25 import javax.swing.event.ChangeEvent ; 26 import javax.swing.event.ChangeListener ; 27 import junit.framework.TestCase; 28 29 33 public class OutWriterTest extends TestCase { 34 private static final byte[] lineSepBytes = OutWriter.lineSepBytes; 35 36 public OutWriterTest(String testName) { 37 super(testName); 38 } 39 40 public void testPositionOfLine() throws UnsupportedEncodingException { 41 System.out.println("testPositionOfLine"); 42 43 OutWriter ow = new OutWriter (); 44 45 String first = "This is the first string"; 46 String second = "This is the second string, ain't it?"; 47 String 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 lineSeparator = new String (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 { 78 System.out.println("testPosition"); 79 80 OutWriter ow = new OutWriter(); 81 82 83 String first = "This is the first string"; 84 String second ="This is the second string"; 85 String 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 lineSeparator = new String (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 { 117 System.out.println("testLine"); 118 119 120 OutWriter ow = new OutWriter (); 121 122 String first = "This is the first string"; 123 String second = "This is the second string, ain't it?"; 124 String 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 firstBack = null; 136 String secondBack = null; 137 String 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 ioe) { 143 ioe.printStackTrace(); 144 fail (ioe.getMessage()); 145 } 146 String lineSeparator = new String (OutWriter.lineSepBytes, "UTF-16"); 147 String firstExpected = first + lineSeparator; 148 String secondExpected = second + lineSeparator; 149 String 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 first = "This is the first string"; 169 String second = "This is the second string, ain't it?"; 170 String 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 194 } 195 196 public void testLineCount() { 197 System.out.println("testLineCount"); 198 199 OutWriter ow = new OutWriter (); 200 201 String first = "This is the first string"; 202 String second = "This is the second string, ain't it?"; 203 String 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 () { 214 public void run() { 215 System.currentTimeMillis(); 216 } 217 }); 218 } catch (Exception 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 255 public void testMultilineText() { 256 System.out.println("testMultilineText"); 257 OutWriter ow = new OutWriter (); 258 String 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 e) { 278 e.printStackTrace(); 279 fail ("Caught exception " + e); 280 } 281 282 283 ow.getLines().removeChangeListener(cl); 284 285 String first = "This is the first string"; 286 String second = "This is the second string, ain't it?"; 287 String 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 first = "This is the a test"; 309 310 ow.println(first); 311 312 313 } 315 316 public void testSubstring() { 317 System.out.println("testSubstring"); 318 319 OutWriter ow = new OutWriter (); 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 ow.println (second); 327 ow.println (third); 328 ow.flush(); 329 330 332 String expected = first.substring(5, 15); 333 String 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 first = "This is a test string"; 348 349 ow.println(first); 350 ow.flush(); 351 352 String firstExpected = first + new String (OutWriter.lineSepBytes, "UTF-16"); 353 String firstReceived = ow.getLines().getLine(0); 354 355 assertEquals ("First line should be \"" + firstExpected + "\" but was \"" + firstReceived + "\"", firstExpected, firstReceived); 356 357 } catch (Exception 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 lineSeparator = new String (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 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 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 lineSeparator = new String (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 e) { 488 e.printStackTrace(); 489 fail (e.getMessage()); 490 } 491 492 } 493 494 495 496 497 498 502 503 private class CL implements ChangeListener { 504 505 public void assertChanged () { 506 ChangeEvent oldCE = ce; 507 ce = null; 508 assertTrue ("No change happened", oldCE != null); 509 } 510 511 public void assertNoChange() { 512 ChangeEvent oldCE = ce; 513 ce = null; 514 assertFalse ("Change happened", oldCE != null); 515 } 516 517 private ChangeEvent ce = null; 518 public void stateChanged(ChangeEvent changeEvent) { 519 ce = changeEvent; 520 } 521 522 } 523 524 525 } 526 | Popular Tags |