1 19 20 package org.netbeans.core.output2; 21 22 import java.nio.ByteBuffer ; 23 import java.util.Arrays ; 24 import junit.framework.TestCase; 25 26 30 public class StorageTest extends TestCase { 31 32 public StorageTest(String testName) { 33 super(testName); 34 } 35 36 Storage filemap = null; 37 Storage heap = null; 38 protected void setUp() throws Exception { 39 filemap = new FileMapStorage(); 40 heap = new HeapStorage(); 41 } 42 43 protected void tearDown() throws Exception { 44 filemap.dispose(); 45 heap.dispose(); 46 } 47 48 public void testIsClosed() throws Exception { 49 doTestIsClosed(heap); 50 doTestIsClosed(filemap); 51 } 52 53 private void doTestIsClosed (Storage storage) throws Exception { 54 System.out.println("testIsClosed - " + storage.getClass()); 55 assertTrue (storage.isClosed()); 56 57 String test = "Hello world"; 58 storage.write(ByteBuffer.wrap(test.getBytes()), true); 59 60 assertFalse (storage.isClosed()); 61 62 storage.close(); 63 assertTrue (storage.isClosed()); 64 65 write (storage, test); 66 assertFalse (storage.isClosed()); 67 68 storage.close(); 69 assertTrue (storage.isClosed()); 70 71 } 72 73 private int write (Storage storage, String s) throws Exception { 74 ByteBuffer buf = storage.getWriteBuffer(AbstractLines.toByteIndex(s.length())); 75 buf.asCharBuffer().put(s); 76 buf.position (buf.position() + AbstractLines.toByteIndex(s.length())); 77 int result = storage.write(buf, true); 78 storage.flush(); 79 return result; 80 } 81 82 83 public void testIdenticalBehaviors() throws Exception { 84 String [] s = new String [10]; 85 String a = "abcd"; 86 String b = a; 87 for (int i=0; i < s.length; i++) { 88 s[i] = b; 89 b += a; 90 int hwrite = write (heap, s[i]); 91 int fwrite = write (filemap, s[i]); 92 assertEquals (hwrite, fwrite); 93 assertEquals(heap.isClosed(), filemap.isClosed()); 94 assertEquals(heap.size(), filemap.size()); 95 ByteBuffer hbuf = heap.getReadBuffer(hwrite, heap.size() - hwrite); 96 ByteBuffer fbuf = filemap.getReadBuffer(hwrite, filemap.size() - fwrite); 97 } 98 } 99 100 public void testFileMapStorageCanBeAsLargeAsIntegerMaxValue() { 101 System.out.println("testFileMapStorageCanBeAsLargeAsIntegerMaxValue - THIS TEST WILL CREATE A 2 GIGABYTE TEMP FILE!!!!"); 102 if (true) { 103 System.out.println("Wisely skipping this test"); 104 return; 105 } 106 char[] c = new char[16384]; 107 Arrays.fill (c, 'a'); 108 String s = new String (c); 109 try { 110 while (filemap.size() < Integer.MAX_VALUE) { 111 write (filemap, s); 112 } 113 } catch (Exception e) { 114 e.printStackTrace(); 115 fail ("Could not create a large file - " + e.getMessage()); 116 } 117 } 118 119 public void testOutputWriterUsesHeapStorageWithLowMemoryFlagSet() throws Exception { 120 System.out.println("testOutputWriterUsesHeapStorageWithLowMemoryFlagSet"); 121 boolean old = OutWriter.lowDiskSpace; 122 OutWriter.lowDiskSpace = true; 123 OutWriter ow = new OutWriter (); 124 try { 125 ow.println("Foo"); 126 assertTrue (ow.getStorage() instanceof HeapStorage); 127 } finally { 128 ow.dispose(); 129 OutWriter.lowDiskSpace = old; 130 } 131 } 132 133 } 134 | Popular Tags |