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  
11  import org.jastacry.JastacryException;
12  import org.jastacry.test.utils.Tooling;
13  import org.junit.jupiter.api.AfterEach;
14  import org.junit.jupiter.api.Assertions;
15  import org.junit.jupiter.api.BeforeEach;
16  import org.junit.jupiter.api.Test;
17  
18  /**
19   * Test of Rotate Layer.
20   *
21   * @author Kai Kretschmann
22   *
23   */
24  public class TestLayerRotate
25  {
26      /**
27       * Testdata to play with.
28       */
29      private final String testdata = "The quick brown fox jumps over the lazy dog.";
30  
31      /**
32       * The layer to test.
33       */
34      private RotateLayer layer = null; // NOPMD by kkretsch on 29.03.18 14:54
35  
36      /**
37       * Shift a small amount.
38       */
39      private static final String SHIFTSMALL = "2";
40  
41      /**
42       * Shift a wide amount.
43       */
44      private static final String SHIFTWIDE = "250";
45  
46      /**
47       * Test Before method.
48       *
49       * @throws Exception
50       *             in case of error
51       */
52      @BeforeEach
53      public void setUp() throws Exception
54      {
55          layer = new RotateLayer();
56      }
57  
58      /**
59       * Test After method.
60       *
61       * @throws Exception
62       *             in case of error
63       */
64      @AfterEach
65      public void tearDown() throws Exception
66      {
67          layer = null;
68      }
69  
70      /**
71       * Testcase JastacryException.
72       *
73       * @throws JastacryException
74       *             in case of error
75       */
76      @Test
77      public void testEncDecStream() throws JastacryException
78      {
79          layer.init(SHIFTSMALL);
80          byte[] buf = testdata.getBytes();
81          final InputStream isEncode = new ByteArrayInputStream(buf);
82          final ByteArrayOutputStream osEncode = new ByteArrayOutputStream();
83          layer.encStream(isEncode, osEncode);
84          buf = osEncode.toByteArray();
85  
86          final InputStream isDecode = new ByteArrayInputStream(buf);
87          final OutputStream osDecode = new ByteArrayOutputStream();
88          layer.decStream(isDecode, osDecode);
89          assertEquals("decoding differs", testdata, osDecode.toString());
90      }
91  
92      /**
93       * Testcase testEncDecStreamWide.
94       *
95       * @throws JastacryException
96       *             in case of error
97       */
98      @Test
99      public void testEncDecStreamWide() throws JastacryException
100     {
101         layer.init(SHIFTWIDE);
102         byte[] buf = testdata.getBytes();
103         final InputStream isEncode = new ByteArrayInputStream(buf);
104         final ByteArrayOutputStream osEncode = new ByteArrayOutputStream();
105         layer.encStream(isEncode, osEncode);
106         buf = osEncode.toByteArray();
107 
108         final InputStream isDecode = new ByteArrayInputStream(buf);
109         final OutputStream osDecode = new ByteArrayOutputStream();
110         layer.decStream(isDecode, osDecode);
111         assertEquals("decoding differs", testdata, osDecode.toString());
112     }
113 
114     /**
115      * Testcase testToString.
116      */
117     @Test
118     public void testToString()
119     {
120         assertEquals("Layer name mismatch", RotateLayer.LAYERNAME, layer.toString());
121     }
122 
123     /**
124      * Testcase unsupported exception.
125      * @throws JastacryException on error
126      */
127     @Test
128     // TestLink(externalId = "JAS-9")
129     public void testUnsupported() throws JastacryException
130     {
131         byte[] buf = testdata.getBytes();
132         final InputStream isEncode = new ByteArrayInputStream(buf);
133         final ByteArrayOutputStream osEncode = new ByteArrayOutputStream();
134         Assertions.assertThrows(UnsupportedOperationException.class, () -> {
135             layer.encodeAndDecode(isEncode, osEncode);
136         });
137     }
138 
139     /**
140      * Testcase testEncStream Exceptions.
141      *
142      * @throws JastacryException
143      *             in case of error
144      * @throws IOException will be thrown in test
145      */
146     @Test
147     public void testEncStreamException() throws JastacryException, IOException
148     {
149         Toolingg.html#Tooling">Tooling tool = new Tooling();
150         Assertions.assertThrows(JastacryException.class, () -> {
151             tool.mockupInputOutputEncStreams(layer);
152         });
153     }
154 
155     /**
156      * Testcase testEncStream Exceptions.
157      *
158      * @throws JastacryException
159      *             in case of error
160      * @throws IOException will be thrown in test
161      */
162     @Test
163     public void testDecStreamException() throws JastacryException, IOException
164     {
165         Toolingg.html#Tooling">Tooling tool = new Tooling();
166         Assertions.assertThrows(JastacryException.class, () -> {
167             tool.mockupInputOutputDecStreams(layer);
168         });
169     }
170 
171     /**
172      * Testcase equals.
173      */
174     @Test
175     public void testEquals()
176     {
177         RotateLayer l1 = new RotateLayer();
178         RotateLayer l2 = new RotateLayer();
179         l1.init(SHIFTSMALL);
180         l2.init(SHIFTSMALL);
181         assertEquals("Layer object equal", true, l1.equals(l2));
182     }
183 
184     /**
185      * Testcase not equals.
186      */
187     @Test
188     public void testNotEquals()
189     {
190         RotateLayer l1 = new RotateLayer();
191         RotateLayer l2 = new RotateLayer();
192         l1.init(SHIFTSMALL);
193         l2.init(SHIFTWIDE);
194         assertEquals("Layer object not equal", false, l1.equals(l2));
195     }
196 
197     /**
198      * Testcase equals.
199      */
200     @Test
201     public void testEqualsSame()
202     {
203         RotateLayer l1 = new RotateLayer();
204         l1.init(SHIFTSMALL);
205         assertEquals("Layer object same", true, l1.equals(l1));
206     }
207 
208     /**
209      * Testcase equals.
210      */
211     @Test
212     public void testNotEqualsNull()
213     {
214         RotateLayer l1 = new RotateLayer();
215         Object o = null;
216         l1.init(SHIFTSMALL);
217         assertEquals("Layer object null unequal", false, l1.equals(o));
218     }
219 
220     /**
221      * Testcase equals.
222      */
223     @Test
224     public void testNotEqualsWrongclass()
225     {
226         RotateLayer l1 = new RotateLayer();
227         Object o = new Object();
228         l1.init(SHIFTSMALL);
229         assertEquals("Layer object type unequal", false, l1.equals(o));
230     }
231 
232     /**
233      * Testcase hashcode.
234      */
235     @Test
236     public void testHashcode()
237     {
238         RotateLayer l1 = new RotateLayer();
239         RotateLayer l2 = new RotateLayer();
240         l1.init(SHIFTSMALL);
241         l2.init(SHIFTSMALL);
242         assertEquals("Layer hash equal", l1.hashCode(), l2.hashCode());
243     }
244 
245 }