Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestIOUtil01.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2010 Gothel Software e.K.
5 * Copyright (c) 2010 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package jau.test.io;
28
29import java.io.BufferedInputStream;
30import java.io.BufferedOutputStream;
31import java.io.File;
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;
39
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;
48import org.junit.Test;
49import org.junit.runners.MethodSorters;
50
51import jau.test.junit.util.JunitTracer;
52
53@FixMethodOrder(MethodSorters.NAME_ASCENDING)
54public class TestIOUtil01 extends JunitTracer {
55
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" ;
60
61 @BeforeClass
62 public static void setup() throws IOException {
63 final File tfile = new File(tfilename);
64 tfile.deleteOnExit();
65 final OutputStream tout = new BufferedOutputStream(new FileOutputStream(tfile));
66 for(int i=0; i<tsz; i++) {
67 final byte b = (byte) (i%256);
68 orig[i] = b;
69 tout.write(b);
70 }
71 tout.close();
72 }
73
74 @AfterClass
75 public static void cleanup() {
76 final File tfile = new File(tfilename);
77 tfile.delete();
78 }
79
80 @Test
81 public void test01CleanPathString() throws IOException, URISyntaxException {
82 {
83 final String input = "./dummy/nop/../a.txt";
84 final String expected = "dummy/a.txt";
85 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
86 }
87 {
88 final String input = "../dummy/nop/../a.txt";
89 final String expected = "../dummy/a.txt";
90 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
91 }
92 {
93 final String input = ".././dummy/nop/../a.txt";
94 final String expected = "../dummy/a.txt";
95 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
96 }
97 {
98 final String input = "./../dummy/nop/../a.txt";
99 final String expected = "../dummy/a.txt";
100 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
101 }
102 {
103 final String input = "../dummy/./nop/../a.txt";
104 final String expected = "../dummy/a.txt";
105 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
106 }
107 {
108 final String input = "/dummy/nop/./../a.txt";
109 final String expected = "/dummy/a.txt";
110 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
111 }
112 {
113 final String input = "dummy/../nop/./.././aaa/bbb/../../a.txt";
114 final String expected = "a.txt";
115 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
116 }
117 {
118 final String input = "/dummy/../nop/./.././aaa/bbb/././ccc/../../../a.txt";
119 final String expected = "/a.txt";
120 Assert.assertEquals(expected, IOUtil.cleanPathString(input));
121 }
122 {
123 URISyntaxException use = null;
124 try {
125 // Error case!
126 final String input = "../../error.txt";
127 final String expected = "error.txt";
128 final String result = IOUtil.cleanPathString(input); // URISyntaxException
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) {
134 use = _use;
135 ExceptionUtils.dumpThrowable("", _use, 0, 3);
136 }
137 Assert.assertNotNull("URISyntaxException expected", use);
138 }
139 {
140 URISyntaxException use = null;
141 try {
142 // Error case!
143 final String input = ".././a/../../error.txt";
144 final String expected = "error.txt";
145 final String result = IOUtil.cleanPathString(input); // URISyntaxException
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) {
151 use = _use;
152 ExceptionUtils.dumpThrowable("", _use, 0, 3);
153 }
154 Assert.assertNotNull("URISyntaxException expected", use);
155 }
156 }
157
158 @Test
159 public void test11CopyStream01Array() throws IOException {
160 final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
161 Assert.assertNotNull(urlConn);
162 final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() );
163 final byte[] bb;
164 try {
165 bb = IOUtil.copyStream2ByteArray( bis );
166 } finally {
167 IOUtil.close(bis, false);
168 }
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));
171
172 }
173
174 @Test
175 public void test12CopyStream02Buffer() throws IOException {
176 final URLConnection urlConn = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
177 Assert.assertNotNull(urlConn);
178 final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() );
179 final ByteBuffer bb;
180 try {
181 bb = IOUtil.copyStream2ByteBuffer( bis );
182 } finally {
183 IOUtil.close(bis, false);
184 }
185 Assert.assertEquals("Byte number not equal orig vs buffer", orig.length, bb.limit());
186 int i;
187 for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
188 Assert.assertTrue("Bytes not equal orig vs array", 0>i);
189 }
190
191 @Test
192 public void test13CopyStream03Buffer() throws IOException {
193 final String tfilename2 = "./test2.bin" ;
194 final URLConnection urlConn1 = IOUtil.getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
195 Assert.assertNotNull(urlConn1);
196
197 final File file2 = new File(tfilename2);
198 file2.deleteOnExit();
199 try {
200 IOUtil.copyURLConn2File(urlConn1, file2);
201 final URLConnection urlConn2 = IOUtil.getResource(tfilename2, this.getClass().getClassLoader(), this.getClass());
202 Assert.assertNotNull(urlConn2);
203
204 final BufferedInputStream bis = new BufferedInputStream( urlConn2.getInputStream() );
205 final ByteBuffer bb;
206 try {
207 bb = IOUtil.copyStream2ByteBuffer( bis );
208 } finally {
209 IOUtil.close(bis, false);
210 }
211 Assert.assertEquals("Byte number not equal orig vs buffer", orig.length, bb.limit());
212 int i;
213 for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
214 Assert.assertTrue("Bytes not equal orig vs array", 0>i);
215 } finally {
216 file2.delete();
217 }
218 }
219
220 public static void main(final String args[]) throws IOException {
221 final String tstname = TestIOUtil01.class.getName();
222 org.junit.runner.JUnitCore.main(tstname);
223 }
224
225}
static void main(final String args[])