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