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