29import java.io.BufferedInputStream;
30import java.io.BufferedOutputStream;
32import java.io.FileOutputStream;
33import java.io.IOException;
34import java.io.OutputStream;
35import java.net.URISyntaxException;
36import java.net.URLConnection;
37import java.nio.ByteBuffer;
38import java.util.Arrays;
40import org.jau.io.IOUtil;
41import org.jau.lang.ExceptionUtils;
42import org.jau.sys.MachineDataInfo;
43import org.jau.sys.PlatformProps;
44import org.junit.AfterClass;
45import org.junit.Assert;
46import org.junit.BeforeClass;
47import org.junit.FixMethodOrder;
49import org.junit.runners.MethodSorters;
51import jau.test.junit.util.JunitTracer;
53@FixMethodOrder(MethodSorters.NAME_ASCENDING)
56 static final MachineDataInfo machine = PlatformProps.MACH_DESC_STAT;
57 static final int tsz = machine.pageSizeInBytes() + machine.pageSizeInBytes() / 2 ;
58 static final byte[] orig =
new byte[tsz];
59 static final String tfilename =
"./test.bin" ;
62 public static void setup() throws IOException {
63 final File tfile =
new File(tfilename);
65 final OutputStream tout =
new BufferedOutputStream(
new FileOutputStream(tfile));
66 for(
int i=0; i<tsz; i++) {
67 final byte b = (byte) (i%256);
76 final File tfile =
new File(tfilename);
83 final String input =
"./dummy/nop/../a.txt";
84 final String expected =
"dummy/a.txt";
85 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
88 final String input =
"../dummy/nop/../a.txt";
89 final String expected =
"../dummy/a.txt";
90 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
93 final String input =
".././dummy/nop/../a.txt";
94 final String expected =
"../dummy/a.txt";
95 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
98 final String input =
"./../dummy/nop/../a.txt";
99 final String expected =
"../dummy/a.txt";
100 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
103 final String input =
"../dummy/./nop/../a.txt";
104 final String expected =
"../dummy/a.txt";
105 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
108 final String input =
"/dummy/nop/./../a.txt";
109 final String expected =
"/dummy/a.txt";
110 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
113 final String input =
"dummy/../nop/./.././aaa/bbb/../../a.txt";
114 final String expected =
"a.txt";
115 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
118 final String input =
"/dummy/../nop/./.././aaa/bbb/././ccc/../../../a.txt";
119 final String expected =
"/a.txt";
120 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
123 URISyntaxException use =
null;
126 final String input =
"../../error.txt";
127 final String expected =
"error.txt";
128 final String result = IOUtil.cleanPathString(input);
129 System.err.println(
"input : "+input);
130 System.err.println(
"expected: "+expected);
131 System.err.println(
"result : "+result);
132 Assert.assertEquals(expected, result);
133 }
catch (
final URISyntaxException _use) {
135 ExceptionUtils.dumpThrowable(
"", _use, 0, 3);
137 Assert.assertNotNull(
"URISyntaxException expected", use);
140 URISyntaxException use =
null;
143 final String input =
".././a/../../error.txt";
144 final String expected =
"error.txt";
145 final String result = IOUtil.cleanPathString(input);
146 System.err.println(
"input : "+input);
147 System.err.println(
"expected: "+expected);
148 System.err.println(
"result : "+result);
149 Assert.assertEquals(expected, result);
150 }
catch (
final URISyntaxException _use) {
152 ExceptionUtils.dumpThrowable(
"", _use, 0, 3);
154 Assert.assertNotNull(
"URISyntaxException expected", use);
160 final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
161 Assert.assertNotNull(urlConn);
162 final BufferedInputStream bis =
new BufferedInputStream( urlConn.getInputStream() );
165 bb = IOUtil.copyStream2ByteArray( bis );
167 IOUtil.close(bis,
false);
169 Assert.assertEquals(
"Byte number not equal orig vs array", orig.length, bb.length);
170 Assert.assertTrue(
"Bytes not equal orig vs array", Arrays.equals(orig, bb));
176 final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
177 Assert.assertNotNull(urlConn);
178 final BufferedInputStream bis =
new BufferedInputStream( urlConn.getInputStream() );
181 bb = IOUtil.copyStream2ByteBuffer( bis );
183 IOUtil.close(bis,
false);
185 Assert.assertEquals(
"Byte number not equal orig vs buffer", orig.length, bb.limit());
187 for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
188 Assert.assertTrue(
"Bytes not equal orig vs array", 0>i);
193 final String tfilename2 =
"./test2.bin" ;
194 final URLConnection urlConn1 = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
195 Assert.assertNotNull(urlConn1);
197 final File file2 =
new File(tfilename2);
198 file2.deleteOnExit();
200 IOUtil.copyURLConn2File(urlConn1, file2);
201 final URLConnection urlConn2 = IOUtil.getResource(tfilename2, this.getClass().getClassLoader(), this.getClass());
202 Assert.assertNotNull(urlConn2);
204 final BufferedInputStream bis =
new BufferedInputStream( urlConn2.getInputStream() );
207 bb = IOUtil.copyStream2ByteBuffer( bis );
209 IOUtil.close(bis,
false);
211 Assert.assertEquals(
"Byte number not equal orig vs buffer", orig.length, bb.limit());
213 for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
214 Assert.assertTrue(
"Bytes not equal orig vs array", 0>i);
220 public static void main(
final String args[])
throws IOException {
222 org.junit.runner.JUnitCore.
main(tstname);
void test11CopyStream01Array()
static void main(final String args[])
void test13CopyStream03Buffer()
void test01CleanPathString()
void test12CopyStream02Buffer()