View Javadoc
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   * Test class for ascii transport format.
28   * 
29   * @author Kai Kretschmann
30   *
31   */
32  public class TestLayerAsciiTransport
33  {
34      /**
35       * log4j2 object.
36       */
37      private static Logger oLogger = null;
38  
39      /**
40       * default data plain.
41       */
42      private final String testdata = "The quick brown fox jumps over the lazy dog.";
43  
44      /**
45       * default data encoded.
46       */
47      private final String testdataEncoded = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=";
48  
49      /**
50       * The layer object to test.
51       */
52      private AsciiTransportLayer layer = null;
53  
54      private CountDownLatch endController = new CountDownLatch(2);
55      
56      /**
57       * The BeforeClass method.
58       *
59       * @throws MalformedURLException
60       *             in case of error.
61       */
62      @BeforeAll
63      public static void setLogger() throws MalformedURLException
64      {
65          oLogger = LogManager.getLogger();
66      }
67  
68      /**
69       * The Before method.
70       *
71       * @throws Exception
72       *             in case of error.
73       */
74      @BeforeEach
75      public void setUp() throws Exception
76      {
77          layer = new AsciiTransportLayer();
78          layer.init("");
79      }
80  
81      /**
82       * The After method.
83       *
84       * @throws Exception
85       *             in case of error.
86       */
87      @AfterEach
88      public void tearDown() throws Exception
89      {
90          layer = null;
91      }
92  
93      /**
94       * Test method for {@link org.jastacry.layer.AsciiTransportLayer#encStream(java.io.InputStream, java.io.OutputStream)} .
95       *
96       * @throws JastacryException
97       *             in case of error.
98       * @throws IOException i case of error
99       */
100     @Test
101     // TestLink(externalId = "JAS-12")
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      * Test method for {@link org.jastacry.layer.AsciiTransportLayer#encStream(java.io.InputStream, java.io.OutputStream)} .
120      *
121      * @throws JastacryException
122      *             in case of error.
123      * @throws IOException in case of error
124      */
125     @Test
126     // TestLink(externalId = "JAS-12")
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      * Test method for {@link org.jastacry.layer.AsciiTransportLayer#encStream(java.io.InputStream, java.io.OutputStream)} .
147      *
148      * @throws JastacryException
149      *             in case of error.
150      * @throws IOException i case of error
151      */
152     @Test
153     // TestLink(externalId = "JAS-12")
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      * Test method for {@link org.jastacry.layer.AsciiTransportLayer#decStream(java.io.InputStream, java.io.OutputStream)} .
181      *
182      * @throws JastacryException
183      *             in case of error.
184      * @throws IOException in case of error
185      */
186     @Test
187     // TestLink(externalId = "JAS-13")
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      * Test method for {@link org.jastacry.layer.AsciiTransportLayer#decStream(java.io.InputStream, java.io.OutputStream)} .
205      *
206      * @throws JastacryException
207      *             in case of error.
208      * @throws IOException in case of error
209      */
210     @Test
211     // TestLink(externalId = "JAS-13")
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      * Test method for {@link org.jastacry.layer.AsciiTransportLayer#decStream(java.io.InputStream, java.io.OutputStream)} .
230      *
231      * @throws JastacryException
232      *             in case of error.
233      * @throws IOException in case of error
234      */
235     @Test
236     // TestLink(externalId = "JAS-13")
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      * Test method for {@link org.jastacry.layer.AsciiTransportLayer#decStream(java.io.InputStream, java.io.OutputStream)} .
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      * Test method for {@link org.jastacry.layer.AsciiTransportLayer#toString()} .
292      */
293     @Test
294     public void testToString()
295     {
296         assertEquals("Layer name mismatch", AsciiTransportLayer.LAYERNAME, layer.toString());
297     }
298 
299     /**
300      * Testcase unsupported exception.
301      * @throws JastacryException on error
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      * Testcase equals.
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      * Testcase equals.
327      */
328     @Test
329     public void testEqualsSame()
330     {
331         AsciiTransportLayer l1 = new AsciiTransportLayer();
332         assertEquals("Layer object same", l1, l1);
333     }
334 
335     /**
336      * Testcase equals.
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      * Testcase equals.
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      * Testcase hashcode.
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 }