1 package org.jastacry.layer;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.io.ByteArrayInputStream;
6 import java.io.ByteArrayOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.net.MalformedURLException;
11 import java.nio.charset.StandardCharsets;
12 import java.util.concurrent.CountDownLatch;
13
14 import org.apache.commons.io.IOUtils;
15 import org.apache.logging.log4j.LogManager;
16 import org.apache.logging.log4j.Logger;
17 import org.jastacry.GlobalData.Action;
18 import org.jastacry.JastacryException;
19
20 import org.junit.jupiter.api.AfterEach;
21 import org.junit.jupiter.api.Assertions;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.BeforeAll;
24 import org.junit.jupiter.api.Test;
25
26
27
28
29
30
31
32 public class TestLayerAsciiTransport
33 {
34
35
36
37 private static Logger oLogger = null;
38
39
40
41
42 private final String testdata = "The quick brown fox jumps over the lazy dog.";
43
44
45
46
47 private final String testdataEncoded = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=";
48
49
50
51
52 private AsciiTransportLayer layer = null;
53
54 private CountDownLatch endController = new CountDownLatch(2);
55
56
57
58
59
60
61
62 @BeforeAll
63 public static void setLogger() throws MalformedURLException
64 {
65 oLogger = LogManager.getLogger();
66 }
67
68
69
70
71
72
73
74 @BeforeEach
75 public void setUp() throws Exception
76 {
77 layer = new AsciiTransportLayer();
78 layer.init("");
79 }
80
81
82
83
84
85
86
87 @AfterEach
88 public void tearDown() throws Exception
89 {
90 layer = null;
91 }
92
93
94
95
96
97
98
99
100 @Test
101
102 public void testEncStream() throws JastacryException, IOException
103 {
104 final byte[] buf = testdata.getBytes();
105 final InputStream is = new ByteArrayInputStream(buf);
106 is.mark(0);
107 final String text = IOUtils.toString(is, StandardCharsets.UTF_8.name());
108 is.reset();
109 oLogger.debug("testEncStream is='{}'", text);
110 oLogger.debug("size of input text is {}", testdata.length());
111 final OutputStream os = new ByteArrayOutputStream();
112 layer.encStream(is, os);
113 os.flush();
114 oLogger.debug("testEncStream os='{}'", os.toString());
115 assertEquals("encoding differs", testdataEncoded, os.toString());
116 }
117
118
119
120
121
122
123
124
125 @Test
126
127 public void testEncStreamExceptionIn() throws JastacryException, IOException
128 {
129 final InputStream is = new InputStream()
130 {
131 @Override
132 public int read() throws IOException
133 {
134 throw new IOException("Expected as a test");
135 }
136 };
137 final OutputStream os = new ByteArrayOutputStream();
138 Assertions.assertThrows(JastacryException.class, () -> {
139 layer.encStream(is, os);
140 });
141 os.flush();
142 oLogger.debug("testEncStream os='{}'", os.toString());
143 }
144
145
146
147
148
149
150
151
152 @Test
153
154 public void testEncStreamExceptionOut() throws JastacryException, IOException
155 {
156 final byte[] buf = testdata.getBytes();
157 final InputStream is = new ByteArrayInputStream(buf);
158 is.mark(0);
159 final String text = IOUtils.toString(is, StandardCharsets.UTF_8.name());
160 is.reset();
161 oLogger.debug("testEncStream is='{}'", text);
162 oLogger.debug("size of input text is {}", testdata.length());
163 final OutputStream os = new OutputStream()
164 {
165 @Override
166 public void write(int i) throws IOException
167 {
168 throw new IOException("Expected as a test");
169 }
170 };
171
172 Assertions.assertThrows(JastacryException.class, () -> {
173 layer.encStream(is, os);
174 os.flush();
175 oLogger.debug("testEncStream os='{}'", os.toString());
176 });
177 }
178
179
180
181
182
183
184
185
186 @Test
187
188 public void testDecStream() throws JastacryException, IOException
189 {
190 final byte[] buf = testdataEncoded.getBytes();
191 final InputStream is = new ByteArrayInputStream(buf);
192 is.mark(0);
193 final String text = IOUtils.toString(is, StandardCharsets.UTF_8.name());
194 is.reset();
195 oLogger.debug("testDecStream is='{}'", text);
196 oLogger.debug("size of encoded text is {}", testdataEncoded.length());
197 final OutputStream os = new ByteArrayOutputStream();
198 layer.decStream(is, os);
199 oLogger.debug("testDecStream os='{}'", os.toString());
200 assertEquals("decoding differs", testdata, os.toString());
201 }
202
203
204
205
206
207
208
209
210 @Test
211
212 public void testDecStreamExceptionIn() throws JastacryException, IOException
213 {
214 final InputStream is = new InputStream()
215 {
216 @Override
217 public int read() throws IOException
218 {
219 throw new IOException("Expected as a test");
220 }
221 };
222 final OutputStream os = new ByteArrayOutputStream();
223 Assertions.assertThrows(JastacryException.class, () -> {
224 layer.decStream(is, os);
225 });
226 }
227
228
229
230
231
232
233
234
235 @Test
236
237 public void testDecStreamExceptionOut() throws JastacryException, IOException
238 {
239 final byte[] buf = testdataEncoded.getBytes();
240 final InputStream is = new ByteArrayInputStream(buf);
241 is.mark(0);
242 final String text = IOUtils.toString(is, StandardCharsets.UTF_8.name());
243 is.reset();
244 oLogger.debug("testDecStream is='{}'", text);
245 oLogger.debug("size of encoded text is {}", testdataEncoded.length());
246 final OutputStream os = new OutputStream()
247 {
248 @Override
249 public void write(int i) throws IOException
250 {
251 throw new IOException("Expected as a test");
252 }
253 };
254 Assertions.assertThrows(JastacryException.class, () -> {
255 layer.decStream(is, os);
256 });
257 }
258
259
260
261
262
263 @Test
264 public void testRun()
265 {
266 final InputStream is = new InputStream()
267 {
268 @Override
269 public int read() throws IOException
270 {
271 throw new IOException("Expected as a test");
272 }
273 };
274 is.mark(0);
275 final OutputStream os = new OutputStream()
276 {
277 @Override
278 public void write(int i) throws IOException
279 {
280 throw new IOException("Expected as a test");
281 }
282 };
283 layer.inputStream = is;
284 layer.outputStream = os;
285 layer.setEndController(endController);
286 layer.setAction(Action.ENCODE);
287 layer.run();
288 }
289
290
291
292
293 @Test
294 public void testToString()
295 {
296 assertEquals("Layer name mismatch", AsciiTransportLayer.LAYERNAME, layer.toString());
297 }
298
299
300
301
302
303 @Test
304 public void testUnsupported() throws JastacryException
305 {
306 byte[] buf = testdata.getBytes();
307 final InputStream isEncode = new ByteArrayInputStream(buf);
308 final ByteArrayOutputStream osEncode = new ByteArrayOutputStream();
309 Assertions.assertThrows(UnsupportedOperationException.class, () -> {
310 layer.encodeAndDecode(isEncode, osEncode);
311 });
312 }
313
314
315
316
317 @Test
318 public void testEquals()
319 {
320 AsciiTransportLayer l1 = new AsciiTransportLayer();
321 AsciiTransportLayer l2 = new AsciiTransportLayer();
322 assertEquals("Layer object equal", l1, l2);
323 }
324
325
326
327
328 @Test
329 public void testEqualsSame()
330 {
331 AsciiTransportLayer l1 = new AsciiTransportLayer();
332 assertEquals("Layer object same", l1, l1);
333 }
334
335
336
337
338 @Test
339 public void testNotEqualsNull()
340 {
341 AsciiTransportLayer l1 = new AsciiTransportLayer();
342 Object o = null;
343 assertEquals("Layer object null unequal", l1.equals(o), false);
344 }
345
346
347
348
349 @Test
350 public void testNotEqualsWrongclass()
351 {
352 AsciiTransportLayer l1 = new AsciiTransportLayer();
353 Object o = new Object();
354 assertEquals("Layer object wrong class unequal", l1.equals(o), false);
355 }
356
357
358
359
360 @Test
361 public void testHashcode()
362 {
363 AsciiTransportLayer l1 = new AsciiTransportLayer();
364 AsciiTransportLayer l2 = new AsciiTransportLayer();
365 assertEquals("Layer hash equal", l1.hashCode(), l2.hashCode());
366 }
367 }