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 Layer Random.
20   *
21   * @author Kai Kretschmann
22   *
23   */
24  public class TestLayerRandom
25  {
26      /**
27       * Test data 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 RandomLayer layer = null;
35  
36      /**
37       * Init value for random layer.
38       */
39      private static final String INITVALUE = "333";
40  
41      /**
42       * Test Before method.
43       *
44       * @throws Exception
45       *             in case of error
46       */
47      @BeforeEach
48      public void setUp() throws Exception
49      {
50          layer = new RandomLayer();
51      }
52  
53      /**
54       * Test After method.
55       *
56       * @throws Exception
57       *             in case of error
58       */
59      @AfterEach
60      public void tearDown() throws Exception
61      {
62          layer = null;
63      }
64  
65      /**
66       * Testcase testEncDecStream.
67       *
68       * @throws JastacryException
69       *             in case of error
70       */
71      @Test
72      public void testEncDecStream() throws JastacryException
73      {
74          byte[] buf = testdata.getBytes();
75          final InputStream isEncode = new ByteArrayInputStream(buf);
76          final ByteArrayOutputStream osEncode = new ByteArrayOutputStream();
77          layer.init(INITVALUE);
78          layer.encStream(isEncode, osEncode);
79          buf = osEncode.toByteArray();
80  
81          layer = null;
82          layer = new RandomLayer();
83          final InputStream isDecode = new ByteArrayInputStream(buf);
84          final OutputStream osDecode = new ByteArrayOutputStream();
85          layer.init(INITVALUE);
86          layer.decStream(isDecode, osDecode);
87          assertEquals("decoding differs", testdata, osDecode.toString());
88  
89      }
90  
91      /**
92       * Testcase testToString.
93       */
94      @Test
95      public void testToString()
96      {
97          assertEquals("Layer name mismatch", RandomLayer.LAYERNAME, layer.toString());
98      }
99  
100     /**
101      * Testcase testEncStream Exceptions.
102      *
103      * @throws JastacryException
104      *             in case of error
105      * @throws IOException will be thrown in test
106      */
107     @Test
108     public void testEncStreamException() throws JastacryException, IOException
109     {
110         Toolingg.html#Tooling">Tooling tool = new Tooling();
111         Assertions.assertThrows(JastacryException.class, () -> {
112             tool.mockupInputOutputEncStreams(layer);
113         });
114     }
115 
116     /**
117      * Testcase testDecStream Exceptions.
118      *
119      * @throws JastacryException
120      *             in case of error
121      * @throws IOException will be thrown in test
122      */
123     @Test
124     public void testDecStreamException() throws JastacryException, IOException
125     {
126         Toolingg.html#Tooling">Tooling tool = new Tooling();
127         Assertions.assertThrows(JastacryException.class, () -> {
128             tool.mockupInputOutputDecStreams(layer);
129         });
130     }
131 
132     /**
133      * Testcase equals.
134      */
135     @Test
136     public void testEquals()
137     {
138         RandomLayer l1 = new RandomLayer();
139         RandomLayer l2 = new RandomLayer();
140         l1.init(INITVALUE);
141         l2.init(INITVALUE);
142         assertEquals("Layer object equal", true, l1.equals(l2));
143     }
144 
145     /**
146      * Testcase equals.
147      */
148     @Test
149     public void testNotEquals()
150     {
151         RandomLayer l1 = new RandomLayer();
152         RandomLayer l2 = new RandomLayer();
153         l1.init(INITVALUE);
154         l2.init("44");
155         assertEquals("Layer object not equal", false, l1.equals(l2));
156     }
157 
158     /**
159      * Testcase equals.
160      */
161     @Test
162     public void testEqualsSame()
163     {
164         RandomLayer l1 = new RandomLayer();
165         l1.init(INITVALUE);
166         assertEquals("Layer object same", true, l1.equals(l1));
167     }
168 
169     /**
170      * Testcase equals.
171      */
172     @Test
173     public void testNotEqualsNull()
174     {
175         RandomLayer l1 = new RandomLayer();
176         Object o = null;
177         l1.init(INITVALUE);
178         assertEquals("Layer object null unequal", false, l1.equals(o));
179     }
180 
181     /**
182      * Testcase equals.
183      */
184     @Test
185     public void testNotEqualsWrongclass()
186     {
187         RandomLayer l1 = new RandomLayer();
188         Object o = new Object();
189         l1.init(INITVALUE);
190         assertEquals("Layer object type unequal", false, l1.equals(o));
191     }
192 
193     /**
194      * Testcase hashcode.
195      */
196     @Test
197     public void testHashcode()
198     {
199         RandomLayer l1 = new RandomLayer();
200         RandomLayer l2 = new RandomLayer();
201         l1.init(INITVALUE);
202         l2.init(INITVALUE);
203         assertEquals("Layer hash equal", l1.hashCode(), l2.hashCode());
204     }
205 
206 }