GlueGen: src/junit/com/jogamp/common/util/TestIOUtil01.java Source File

GlueGen v2.6.0-rc-20250706 GlueGen, Native Binding Generator for Java™ (public API).

TestIOUtil01.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.common.util;
30
31import java.util.*;
32import java.io.BufferedInputStream;
33import java.io.BufferedOutputStream;
34import java.io.File;
35import java.io.FileOutputStream;
36import java.io.IOException;
37import java.io.OutputStream;
38import java.net.URISyntaxException;
39import java.net.URLConnection;
40import java.nio.ByteBuffer;
41
42import org.junit.AfterClass;
43import org.junit.Assert;
44import org.junit.BeforeClass;
45import org.junit.Test;
46
47import com.jogamp.common.ExceptionUtils;
48import com.jogamp.common.net.URIDumpUtil;
49import com.jogamp.common.net.Uri;
50import com.jogamp.common.os.MachineDataInfo;
51import com.jogamp.common.os.Platform;
52import com.jogamp.junit.util.SingletonJunitCase;
53
54import org.junit.FixMethodOrder;
55import org.junit.runners.MethodSorters;
56
57@FixMethodOrder(MethodSorters.NAME_ASCENDING)
58public class TestIOUtil01 extends SingletonJunitCase {
59
60 static final MachineDataInfo machine = Platform.getMachineDataInfo();
61 static final int tsz = machine.pageSizeInBytes() + machine.pageSizeInBytes() / 2 ;
62 static final byte[] orig = new byte[tsz];
63 static final String tfilename = "./test.bin" ;
64
65 @BeforeClass
66 public static void setup() throws IOException {
67 final File tfile = new File(tfilename);
68 tfile.deleteOnExit();
69 final OutputStream tout = new BufferedOutputStream(new FileOutputStream(tfile));
70 for(int i=0; i<tsz; i++) {
71 final byte b = (byte) (i%256);
72 orig[i] = b;
73 tout.write(b);
74 }
75 tout.close();
76 }
77
78 @AfterClass
79 public static void cleanup() {
80 final File tfile = new File(tfilename);
81 tfile.delete();
82 }
83
84 @Test
85 public void test01CleanPathString() throws IOException, URISyntaxException {
86 {
87 final String input = "./dummy/nop/../a.txt";
88 final String expected = "dummy/a.txt";
89 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
90 }
91 {
92 final String input = "../dummy/nop/../a.txt";
93 final String expected = "../dummy/a.txt";
94 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
95 }
96 {
97 final String input = ".././dummy/nop/../a.txt";
98 final String expected = "../dummy/a.txt";
99 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
100 }
101 {
102 final String input = "./../dummy/nop/../a.txt";
103 final String expected = "../dummy/a.txt";
104 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
105 }
106 {
107 final String input = "../dummy/./nop/../a.txt";
108 final String expected = "../dummy/a.txt";
109 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
110 }
111 {
112 final String input = "/dummy/nop/./../a.txt";
113 final String expected = "/dummy/a.txt";
114 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
115 }
116 {
117 final String input = "dummy/../nop/./.././aaa/bbb/../../a.txt";
118 final String expected = "a.txt";
119 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
120 }
121 {
122 final String input = "/dummy/../nop/./.././aaa/bbb/././ccc/../../../a.txt";
123 final String expected = "/a.txt";
124 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
125 }
126 {
127 URISyntaxException use = null;
128 try {
129 // Error case!
130 final String input = "../../error.txt";
131 final String expected = "error.txt";
132 final String result = IOUtil.cleanPathString(input); // URISyntaxException
133 System.err.println("input : "+input);
134 System.err.println("expected: "+expected);
135 System.err.println("result : "+result);
136 Assert.assertEquals(expected, result);
137 } catch (final URISyntaxException _use) {
138 use = _use;
139 ExceptionUtils.dumpThrowable("", _use, 0, 3);
140 }
141 Assert.assertNotNull("URISyntaxException expected", use);
142 }
143 {
144 URISyntaxException use = null;
145 try {
146 // Error case!
147 final String input = ".././a/../../error.txt";
148 final String expected = "error.txt";
149 final String result = IOUtil.cleanPathString(input); // URISyntaxException
150 System.err.println("input : "+input);
151 System.err.println("expected: "+expected);
152 System.err.println("result : "+result);
153 Assert.assertEquals(expected, result);
154 } catch (final URISyntaxException _use) {
155 use = _use;
156 ExceptionUtils.dumpThrowable("", _use, 0, 3);
157 }
158 Assert.assertNotNull("URISyntaxException expected", use);
159 }
160 }
161
162 @Test
163 public void test11CopyStream01Array() throws IOException {
164 final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
165 Assert.assertNotNull(urlConn);
166 final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() );
167 final byte[] bb;
168 try {
169 bb = IOUtil.copyStream2ByteArray( bis );
170 } finally {
171 IOUtil.close(bis, false);
172 }
173 Assert.assertEquals("Byte number not equal orig vs array", orig.length, bb.length);
174 Assert.assertTrue("Bytes not equal orig vs array", Arrays.equals(orig, bb));
175
176 }
177
178 @Test
179 public void test12CopyStream02Buffer() throws IOException {
180 final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
181 Assert.assertNotNull(urlConn);
182 final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() );
183 final ByteBuffer bb;
184 try {
185 bb = IOUtil.copyStream2ByteBuffer( bis );
186 } finally {
187 IOUtil.close(bis, false);
188 }
189 Assert.assertEquals("Byte number not equal orig vs buffer", orig.length, bb.limit());
190 int i;
191 for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
192 Assert.assertTrue("Bytes not equal orig vs array", 0>i);
193 }
194
195 @Test
196 public void test13CopyStream03Buffer() throws IOException {
197 final String tfilename2 = "./test2.bin" ;
198 final URLConnection urlConn1 = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
199 Assert.assertNotNull(urlConn1);
200
201 final File file2 = new File(tfilename2);
202 file2.deleteOnExit();
203 try {
204 IOUtil.copyURLConn2File(urlConn1, file2);
205 final URLConnection urlConn2 = IOUtil.getResource(tfilename2, this.getClass().getClassLoader(), this.getClass());
206 Assert.assertNotNull(urlConn2);
207
208 final BufferedInputStream bis = new BufferedInputStream( urlConn2.getInputStream() );
209 final ByteBuffer bb;
210 try {
211 bb = IOUtil.copyStream2ByteBuffer( bis );
212 } finally {
213 IOUtil.close(bis, false);
214 }
215 Assert.assertEquals("Byte number not equal orig vs buffer", orig.length, bb.limit());
216 int i;
217 for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
218 Assert.assertTrue("Bytes not equal orig vs array", 0>i);
219 } finally {
220 file2.delete();
221 }
222 }
223
224 @Test
225 public void test21CopyStreamChunk01Buffer() throws IOException {
226 final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
227 Assert.assertNotNull(urlConn);
228 final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() );
229 final ByteBuffer bb;
230 final int skipBytes = 0;
231 final int byteCount = orig.length;
232 try {
233 bb = IOUtil.copyStreamChunk2ByteBuffer( bis, skipBytes, byteCount );
234 } finally {
235 IOUtil.close(bis, false);
236 }
237 Assert.assertTrue( machine.pageAlignedSize( byteCount ) >= byteCount );
238 Assert.assertEquals( machine.pageAlignedSize( byteCount ), bb.capacity() );
239 Assert.assertEquals(orig.length, bb.limit());
240 int i;
241 for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
242 Assert.assertTrue("Bytes not equal orig vs array", 0>i);
243 System.err.println(getTestMethodName()+" OK: ["+skipBytes+".."+(skipBytes+byteCount)+"): "+bb);
244 }
245
246 @Test
247 public void test22CopyStreamChunk02Buffer() throws IOException {
248 final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
249 Assert.assertNotNull(urlConn);
250 final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() );
251 final ByteBuffer bb;
252 final int skipBytes = 0;
253 final int byteCount = orig.length + machine.pageSizeInBytes(); // expect more data than exists, complete with actual data
254 try {
255 bb = IOUtil.copyStreamChunk2ByteBuffer( bis, skipBytes, byteCount );
256 } finally {
257 IOUtil.close(bis, false);
258 }
259 Assert.assertTrue( machine.pageAlignedSize( byteCount ) >= byteCount );
260 Assert.assertEquals( machine.pageAlignedSize( byteCount ), bb.capacity() );
261 Assert.assertEquals( orig.length, bb.limit() );
262 int i;
263 for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
264 Assert.assertTrue("Bytes not equal orig vs array", 0>i);
265 System.err.println(getTestMethodName()+" OK: ["+skipBytes+".."+(skipBytes+byteCount)+"): "+bb);
266 }
267
268 @Test
269 public void test23CopyStreamChunk03Buffer() throws IOException {
270 final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
271 Assert.assertNotNull(urlConn);
272 final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() );
273 final ByteBuffer bb;
274 final int skipBytes = 0;
275 final int byteCount = orig.length / 2; // take less data than exists
276 Assert.assertTrue( orig.length >= byteCount );
277 try {
278 bb = IOUtil.copyStreamChunk2ByteBuffer( bis, skipBytes, byteCount );
279 } finally {
280 IOUtil.close(bis, false);
281 }
282 Assert.assertTrue( machine.pageAlignedSize( byteCount ) >= byteCount );
283 Assert.assertEquals( machine.pageAlignedSize( byteCount ), bb.capacity() );
284 Assert.assertEquals( byteCount, bb.limit() );
285 int i;
286 for(i=byteCount-1; i>=0 && orig[i]==bb.get(i); i--) ;
287 Assert.assertTrue("Bytes not equal orig vs array", 0>i);
288 System.err.println(getTestMethodName()+" OK: ["+skipBytes+".."+(skipBytes+byteCount)+"): "+bb);
289 }
290
291 @Test
292 public void test24CopyStreamChunk04Buffer() throws IOException {
293 final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
294 Assert.assertNotNull(urlConn);
295 final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() );
296 final ByteBuffer bb;
297 final int skipBytes = orig.length / 2;
298 final int byteCount = orig.length / 4; // take less data than exists
299 Assert.assertTrue( orig.length >= skipBytes + byteCount );
300 Assert.assertTrue( orig.length >= byteCount );
301 try {
302 bb = IOUtil.copyStreamChunk2ByteBuffer( bis, skipBytes, byteCount );
303 } finally {
304 IOUtil.close(bis, false);
305 }
306 Assert.assertTrue( machine.pageAlignedSize( byteCount ) >= byteCount );
307 Assert.assertEquals( machine.pageAlignedSize( byteCount ), bb.capacity() );
308 Assert.assertEquals( byteCount, bb.limit() );
309 int i;
310 for(i=byteCount-1; i>=0 && orig[skipBytes+i]==bb.get(i); i--) ;
311 Assert.assertTrue("Bytes not equal orig vs array", 0>i);
312 System.err.println(getTestMethodName()+" OK: ["+skipBytes+".."+(skipBytes+byteCount)+"): "+bb);
313 }
314
315 @Test
316 public void test25CopyStreamChunk05Buffer() throws IOException {
317 final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
318 Assert.assertNotNull(urlConn);
319 final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() );
320 final ByteBuffer bb;
321 final int skipBytes = orig.length * 2; // skip all and more ..
322 final int byteCount = orig.length; // expect more data than left, won't take anything
323 Assert.assertTrue( orig.length < skipBytes + byteCount );
324 Assert.assertTrue( orig.length >= byteCount );
325 try {
326 bb = IOUtil.copyStreamChunk2ByteBuffer( bis, skipBytes, byteCount );
327 } finally {
328 IOUtil.close(bis, false);
329 }
330 Assert.assertTrue( machine.pageAlignedSize( byteCount ) >= byteCount );
331 Assert.assertEquals( machine.pageAlignedSize( byteCount ), bb.capacity() );
332 Assert.assertEquals( 0, bb.limit() );
333 System.err.println(getTestMethodName()+" OK: ["+skipBytes+".."+(skipBytes+byteCount)+"): "+bb);
334 }
335
336 public static void main(final String args[]) throws IOException {
337 final String tstname = TestIOUtil01.class.getName();
338 org.junit.runner.JUnitCore.main(tstname);
339 }
340
341}
com.jogamp.common.ExceptionUtils
Definition: ExceptionUtils.java:35
com.jogamp.common.ExceptionUtils.dumpThrowable
static void dumpThrowable(final String additionalDescr, final Throwable t)
Dumps a Throwable to System.err in a decorating message including the current thread name,...
Definition: ExceptionUtils.java:135
com.jogamp.common.os.MachineDataInfo
Machine data description for alignment and size onle, see com.jogamp.gluegen.
Definition: MachineDataInfo.java:57
com.jogamp.common.os.MachineDataInfo.pageAlignedSize
int pageAlignedSize(final int size)
Definition: MachineDataInfo.java:305
com.jogamp.common.os.Platform
Utility class for querying platform specific properties.
Definition: Platform.java:58
com.jogamp.common.os.Platform.getMachineDataInfo
static MachineDataInfo getMachineDataInfo()
Returns the MachineDataInfo of the running machine.
Definition: Platform.java:510
com.jogamp.common.util.IOUtil
Definition: IOUtil.java:69
com.jogamp.common.util.IOUtil.cleanPathString
static String cleanPathString(String path)
Definition: IOUtil.java:712
com.jogamp.common.util.IOUtil.getResource
static URLConnection getResource(final String resourcePath, final ClassLoader classLoader, final Class<?> relContext)
Locating a resource using getResource(String, ClassLoader):
Definition: IOUtil.java:578
com.jogamp.common.util.IOUtil.copyStream2ByteBuffer
static ByteBuffer copyStream2ByteBuffer(final InputStream stream)
Copy the complete specified input stream to a NIO ByteBuffer w/ native byte order,...
Definition: IOUtil.java:297
com.jogamp.common.util.IOUtil.close
static void close(final Closeable stream, final boolean throwRuntimeException)
Definition: IOUtil.java:1405
com.jogamp.common.util.IOUtil.copyStreamChunk2ByteBuffer
static ByteBuffer copyStreamChunk2ByteBuffer(InputStream stream, int skipBytes, int byteCount)
Copy the specified input stream chunk to a NIO ByteBuffer w/ native byte order, which is being return...
Definition: IOUtil.java:347
com.jogamp.common.util.IOUtil.copyURLConn2File
static int copyURLConn2File(final URLConnection conn, final File outFile)
Copy the complete specified URL resource to the specified output file.
Definition: IOUtil.java:182
com.jogamp.common.util.IOUtil.copyStream2ByteArray
static byte[] copyStream2ByteArray(InputStream stream)
Copy the complete specified input stream to a byte array, which is being returned.
Definition: IOUtil.java:262
com.jogamp.common.util.TestIOUtil01
Definition: TestIOUtil01.java:58
com.jogamp.common.util.TestIOUtil01.test21CopyStreamChunk01Buffer
void test21CopyStreamChunk01Buffer()
Definition: TestIOUtil01.java:225
com.jogamp.common.util.TestIOUtil01.test22CopyStreamChunk02Buffer
void test22CopyStreamChunk02Buffer()
Definition: TestIOUtil01.java:247
com.jogamp.common.util.TestIOUtil01.test01CleanPathString
void test01CleanPathString()
Definition: TestIOUtil01.java:85
com.jogamp.common.util.TestIOUtil01.cleanup
static void cleanup()
Definition: TestIOUtil01.java:79
com.jogamp.common.util.TestIOUtil01.test11CopyStream01Array
void test11CopyStream01Array()
Definition: TestIOUtil01.java:163
com.jogamp.common.util.TestIOUtil01.test24CopyStreamChunk04Buffer
void test24CopyStreamChunk04Buffer()
Definition: TestIOUtil01.java:292
com.jogamp.common.util.TestIOUtil01.setup
static void setup()
Definition: TestIOUtil01.java:66
com.jogamp.common.util.TestIOUtil01.test25CopyStreamChunk05Buffer
void test25CopyStreamChunk05Buffer()
Definition: TestIOUtil01.java:316
com.jogamp.common.util.TestIOUtil01.test13CopyStream03Buffer
void test13CopyStream03Buffer()
Definition: TestIOUtil01.java:196
com.jogamp.common.util.TestIOUtil01.test12CopyStream02Buffer
void test12CopyStream02Buffer()
Definition: TestIOUtil01.java:179
com.jogamp.common.util.TestIOUtil01.test23CopyStreamChunk03Buffer
void test23CopyStreamChunk03Buffer()
Definition: TestIOUtil01.java:269
com.jogamp.common.util.TestIOUtil01.main
static void main(final String args[])
Definition: TestIOUtil01.java:336
com.jogamp.junit.util.SingletonJunitCase
Definition: SingletonJunitCase.java:39