Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.db.dataview.util; |
43 |
|
44 |
import java.io.CharArrayReader; |
45 |
import java.io.IOException; |
46 |
import org.openide.util.Exceptions; |
47 |
import java.io.Reader; |
48 |
import java.io.Writer; |
49 |
import org.junit.After; |
50 |
import org.junit.AfterClass; |
51 |
import org.junit.Before; |
52 |
import org.junit.BeforeClass; |
53 |
import org.junit.Test; |
54 |
import static org.junit.Assert.*; |
55 |
|
56 |
/** |
57 |
* |
58 |
* @author mblaesing |
59 |
*/ |
60 |
public class FileBackedClobTest { |
61 |
private static char[] testCase1; |
62 |
private static char[] testCase2; |
63 |
private static char[] testCase3; |
64 |
|
65 |
private static void assertCharacterStreamEquals(Reader is1, Reader is2) { |
66 |
try { |
67 |
long position = 0; |
68 |
while (true) { |
69 |
int input1 = is1.read(); |
70 |
int input2 = is2.read(); |
71 |
if (input1 != input2) { |
72 |
throw new AssertionError("Reader differ at position: " + Long.toString(position)); |
73 |
} |
74 |
if (input1 == -1 || input2 == -1) { |
75 |
return; |
76 |
} |
77 |
position++; |
78 |
} |
79 |
} catch (IOException ex) { |
80 |
throw new AssertionError(ex); |
81 |
} finally { |
82 |
try { |
83 |
is1.close(); |
84 |
} catch (IOException ex) { |
85 |
Exceptions.printStackTrace(ex); |
86 |
} |
87 |
try { |
88 |
is2.close(); |
89 |
} catch (IOException ex) { |
90 |
Exceptions.printStackTrace(ex); |
91 |
} |
92 |
} |
93 |
} |
94 |
|
95 |
@BeforeClass |
96 |
public static void setUpClass() throws Exception { |
97 |
char[] testPattern = "Testäöüß".toCharArray(); |
98 |
int testLength = testPattern.length; |
99 |
testCase1 = new char[10]; |
100 |
testCase2 = new char[1024]; |
101 |
testCase3 = new char[1024 * 1024]; |
102 |
for (int i = 0; i < testCase1.length; i++) { |
103 |
testCase1[i] = testPattern[ i % testLength]; |
104 |
} |
105 |
for (int i = 0; i < testCase2.length; i++) { |
106 |
testCase2[i] = testPattern[ i % testLength]; |
107 |
} |
108 |
for (int i = 0; i < testCase3.length; i++) { |
109 |
testCase3[i] = testPattern[ i % testLength]; |
110 |
} |
111 |
} |
112 |
|
113 |
|
114 |
public FileBackedClobTest() { |
115 |
} |
116 |
|
117 |
@AfterClass |
118 |
public static void tearDownClass() throws Exception { |
119 |
testCase1 = null; |
120 |
testCase2 = null; |
121 |
testCase3 = null; |
122 |
} |
123 |
|
124 |
@Before |
125 |
public void setUp() { |
126 |
} |
127 |
|
128 |
@After |
129 |
public void tearDown() { |
130 |
} |
131 |
|
132 |
@Test |
133 |
public void testLength() throws Exception { |
134 |
FileBackedClob c; |
135 |
c = new FileBackedClob(new CharArrayReader(testCase1)); |
136 |
assertEquals(c.length(), testCase1.length); |
137 |
assertEquals(c.getBackingFile().length(), testCase1.length * 4); |
138 |
c.free(); |
139 |
c = new FileBackedClob(new String(testCase2)); |
140 |
assertEquals(c.length(), testCase2.length); |
141 |
assertEquals(c.getBackingFile().length(), testCase2.length * 4); |
142 |
c.free(); |
143 |
c = new FileBackedClob(new CharArrayReader(testCase3)); |
144 |
assertEquals(c.length(), testCase3.length); |
145 |
assertEquals(c.getBackingFile().length(), testCase3.length * 4); |
146 |
c.free(); |
147 |
} |
148 |
|
149 |
@Test |
150 |
public void testTruncate() throws Exception { |
151 |
FileBackedClob c; |
152 |
c = new FileBackedClob(new CharArrayReader(testCase1)); |
153 |
c.truncate(5); |
154 |
assertEquals(c.length(), 5); |
155 |
assertEquals(c.getBackingFile().length(), 5 * 4); |
156 |
c.free(); |
157 |
c = new FileBackedClob(new String(testCase2)); |
158 |
c.truncate(42); |
159 |
assertEquals(c.length(), 42); |
160 |
assertEquals(c.getBackingFile().length(), 42 * 4); |
161 |
c.free(); |
162 |
c = new FileBackedClob(new CharArrayReader(testCase3)); |
163 |
c.truncate(1024); |
164 |
assertEquals(c.length(), 1024); |
165 |
assertEquals(c.getBackingFile().length(), 1024 * 4); |
166 |
c.free(); |
167 |
} |
168 |
|
169 |
@Test |
170 |
public void testGetSubString() throws Exception { |
171 |
FileBackedClob c; |
172 |
char[] referenceChars = new char[5]; |
173 |
System.arraycopy(testCase1, 5, referenceChars, 0, 5); |
174 |
String reference = new String(referenceChars); |
175 |
c = new FileBackedClob(new CharArrayReader(testCase1)); |
176 |
assertEquals(reference, c.getSubString(6, 5)); |
177 |
c.free(); |
178 |
c = new FileBackedClob(new String(testCase2)); |
179 |
assertEquals( reference, c.getSubString(6, 5)); |
180 |
c.free(); |
181 |
c = new FileBackedClob(new CharArrayReader(testCase3)); |
182 |
assertEquals(reference, c.getSubString(6, 5)); |
183 |
c.free(); |
184 |
} |
185 |
|
186 |
@Test |
187 |
public void testGetCharacterStream_0args() throws Exception { |
188 |
FileBackedClob c; |
189 |
c = new FileBackedClob(new CharArrayReader(testCase1)); |
190 |
assertCharacterStreamEquals(c.getCharacterStream(), new CharArrayReader(testCase1)); |
191 |
c.free(); |
192 |
c = new FileBackedClob(new String(testCase2)); |
193 |
assertCharacterStreamEquals(c.getCharacterStream(), new CharArrayReader(testCase2)); |
194 |
c.free(); |
195 |
c = new FileBackedClob(new CharArrayReader(testCase3)); |
196 |
assertCharacterStreamEquals(c.getCharacterStream(), new CharArrayReader(testCase3)); |
197 |
c.free(); |
198 |
} |
199 |
|
200 |
@Test |
201 |
public void testSetString_long_String() throws Exception { |
202 |
FileBackedClob b; |
203 |
char[] test1 = "test".toCharArray(); |
204 |
char[] test2 = "test0123456789".toCharArray(); |
205 |
char[] firstPartReference = new char[testCase2.length - test1.length - 4]; |
206 |
char[] secondPartReference = new char[4]; |
207 |
System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4); |
208 |
System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4); |
209 |
b = new FileBackedClob(new CharArrayReader(testCase2)); |
210 |
b.setString(testCase2.length - test1.length - 4 + 1, new String(test1)); |
211 |
assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4)); |
212 |
assertEquals(new String(secondPartReference), b.getSubString(testCase2.length - 4, 4)); |
213 |
assertEquals(new String(test1), b.getSubString(testCase2.length - 4 - test1.length + 1, test1.length)); |
214 |
assertEquals(b.length(), 1024); |
215 |
b.setString(testCase2.length - test1.length - 4 + 1, new String(test2)); |
216 |
assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4)); |
217 |
assertEquals(b.length(), 1024 - test1.length - 4 + test2.length); |
218 |
assertEquals(new String(test2), b.getSubString(b.length() - test2.length + 1, test2.length)); |
219 |
b.free(); |
220 |
} |
221 |
|
222 |
@Test |
223 |
public void testSetString_4args() throws Exception { |
224 |
FileBackedClob b; |
225 |
char[] test1 = "test".toCharArray(); |
226 |
char[] test2 = "01test23456789".toCharArray(); |
227 |
char[] firstPartReference = new char[testCase2.length - test1.length - 4]; |
228 |
char[] secondPartReference = new char[4]; |
229 |
System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4); |
230 |
System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4); |
231 |
b = new FileBackedClob(new CharArrayReader(testCase2)); |
232 |
b.setString(testCase2.length - test1.length - 4 + 1, new String(test2), 2, 4); |
233 |
assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4)); |
234 |
assertEquals(new String(secondPartReference), b.getSubString(testCase2.length - 4, 4)); |
235 |
assertEquals(new String(test1), b.getSubString(testCase2.length - 4 - test1.length + 1, test1.length)); |
236 |
assertEquals(b.length(), 1024); |
237 |
b.free(); |
238 |
} |
239 |
|
240 |
@Test |
241 |
public void testSetCharacterStream() throws Exception { |
242 |
FileBackedClob b; |
243 |
char[] test1 = "test".toCharArray(); |
244 |
char[] test2 = "test0123456789".toCharArray(); |
245 |
char[] firstPartReference = new char[testCase2.length - test1.length - 4]; |
246 |
char[] secondPartReference = new char[4]; |
247 |
System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4); |
248 |
System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4); |
249 |
b = new FileBackedClob(new CharArrayReader(testCase2)); |
250 |
Writer os = b.setCharacterStream(testCase2.length - test1.length - 4 + 1); |
251 |
os.write(test1); |
252 |
os.close(); |
253 |
assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4)); |
254 |
assertEquals(new String(secondPartReference), b.getSubString(testCase2.length - 4, 4)); |
255 |
assertEquals(new String(test1), b.getSubString(testCase2.length - 4 - test1.length + 1, test1.length)); |
256 |
assertEquals(b.length(), 1024); |
257 |
os = b.setCharacterStream(testCase2.length - test1.length - 4 + 1); |
258 |
os.write(test2); |
259 |
os.close(); |
260 |
assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4)); |
261 |
assertEquals(b.length(), 1024 - test1.length - 4 + test2.length); |
262 |
assertEquals(new String(test2), b.getSubString(b.length() - test2.length + 1, test2.length)); |
263 |
b.free(); |
264 |
} |
265 |
|
266 |
@Test |
267 |
public void testFinalize() throws Exception { |
268 |
FileBackedClob b; |
269 |
b = new FileBackedClob(new CharArrayReader(testCase2)); |
270 |
assertTrue(b.getBackingFile().exists()); |
271 |
try { |
272 |
b.finalize(); |
273 |
} catch (Throwable ex) { |
274 |
Exceptions.printStackTrace(ex); |
275 |
} |
276 |
assertFalse(b.getBackingFile().exists()); |
277 |
} |
278 |
|
279 |
@Test |
280 |
public void testFree() throws Exception { |
281 |
FileBackedClob b; |
282 |
b = new FileBackedClob(new CharArrayReader(testCase2)); |
283 |
assertTrue(b.getBackingFile().exists()); |
284 |
b.free(); |
285 |
assertFalse(b.getBackingFile().exists()); |
286 |
} |
287 |
} |