KickJava   Java API By Example, From Geeks To Geeks.

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


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.nio.ByteBuffer JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import junit.framework.TestCase;
25
26 /**
27  *
28  * @author tim
29  */

30 public class StorageTest extends TestCase {
31
32     public StorageTest(String JavaDoc testName) {
33         super(testName);
34     }
35
36     Storage filemap = null;
37     Storage heap = null;
38     protected void setUp() throws Exception JavaDoc {
39         filemap = new FileMapStorage();
40         heap = new HeapStorage();
41     }
42
43     protected void tearDown() throws Exception JavaDoc {
44         filemap.dispose();
45         heap.dispose();
46     }
47     
48     public void testIsClosed() throws Exception JavaDoc {
49         doTestIsClosed(heap);
50         doTestIsClosed(filemap);
51     }
52     
53     private void doTestIsClosed (Storage storage) throws Exception JavaDoc {
54         System.out.println("testIsClosed - " + storage.getClass());
55         assertTrue (storage.isClosed());
56         
57         String JavaDoc 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 JavaDoc s) throws Exception JavaDoc {
74         ByteBuffer JavaDoc 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 JavaDoc {
84         String JavaDoc[] s = new String JavaDoc[10];
85         String JavaDoc a = "abcd";
86         String JavaDoc 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 JavaDoc hbuf = heap.getReadBuffer(hwrite, heap.size() - hwrite);
96             ByteBuffer JavaDoc 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 JavaDoc s = new String JavaDoc(c);
109         try {
110             while (filemap.size() < Integer.MAX_VALUE) {
111                  write (filemap, s);
112             }
113         } catch (Exception JavaDoc e) {
114             e.printStackTrace();
115             fail ("Could not create a large file - " + e.getMessage());
116         }
117     }
118     
119     public void testOutputWriterUsesHeapStorageWithLowMemoryFlagSet() throws Exception JavaDoc {
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