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.FileInputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11
12 import org.apache.commons.io.IOUtils;
13 import org.jastacry.JastacryException;
14 import org.jastacry.test.utils.Tooling;
15
16 import org.junit.jupiter.api.AfterEach;
17 import org.junit.jupiter.api.Assertions;
18 import org.junit.jupiter.api.BeforeEach;
19 import org.junit.jupiter.api.Test;
20
21
22
23
24
25
26
27
28 public class TestLayerFilemerge
29 {
30
31
32
33 private final String testdata = "The quick brown fox jumps over the lazy dog.";
34
35
36
37
38 private FilemergeLayer layer = null;
39
40
41
42
43 private static final String INITVALUE = "src/test/resources/foto.jpg";
44
45
46
47
48 private static final String LONGTEXTFILE = "src/test/resources/longtext.txt";
49
50
51
52
53
54
55
56 @BeforeEach
57 public void setUp() throws Exception
58 {
59 layer = new FilemergeLayer();
60 }
61
62
63
64
65
66
67
68 @AfterEach
69 public void tearDown() throws Exception
70 {
71 layer = null;
72 }
73
74
75
76
77
78
79
80 @Test
81 public void testEncDecStream() throws JastacryException
82 {
83 byte[] buf = testdata.getBytes();
84 final InputStream isEncode = new ByteArrayInputStream(buf);
85 final ByteArrayOutputStream osEncode = new ByteArrayOutputStream();
86 layer.init(INITVALUE);
87 layer.encStream(isEncode, osEncode);
88 buf = osEncode.toByteArray();
89
90 layer = null;
91 layer = new FilemergeLayer();
92 final InputStream isDecode = new ByteArrayInputStream(buf);
93 final OutputStream osDecode = new ByteArrayOutputStream();
94 layer.init(INITVALUE);
95 layer.decStream(isDecode, osDecode);
96 assertEquals("decoding differs", testdata, osDecode.toString());
97 }
98
99
100
101
102
103
104
105
106 @Test
107
108 public void testEncDecStreamLong() throws JastacryException, IOException
109 {
110 InputStream is = new FileInputStream(LONGTEXTFILE);
111 byte[] buf = IOUtils.toByteArray(is);
112
113 final InputStream isEncode = new ByteArrayInputStream(buf);
114 final ByteArrayOutputStream osEncode = new ByteArrayOutputStream();
115 layer.init(INITVALUE);
116 layer.encStream(isEncode, osEncode);
117 byte[] buf2 = osEncode.toByteArray();
118
119 layer = null;
120 layer = new FilemergeLayer();
121 final InputStream isDecode = new ByteArrayInputStream(buf2);
122 final OutputStream osDecode = new ByteArrayOutputStream();
123 layer.init(INITVALUE);
124 layer.decStream(isDecode, osDecode);
125
126 String sTextcontent = new String(buf, "ISO-8859-1");
127 assertEquals("decoding differs", sTextcontent, osDecode.toString());
128 }
129
130
131
132
133
134
135
136
137 @Test
138 public void testEncStreamException() throws JastacryException, IOException
139 {
140 Toolingg.html#Tooling">Tooling tool = new Tooling();
141 Assertions.assertThrows(JastacryException.class, () -> {
142 tool.mockupInputOutputEncStreams(layer);
143 });
144 }
145
146
147
148
149
150
151
152
153 @Test
154 public void testDecStreamException() throws JastacryException, IOException
155 {
156 Toolingg.html#Tooling">Tooling tool = new Tooling();
157 Assertions.assertThrows(JastacryException.class, () -> {
158 tool.mockupInputOutputDecStreams(layer);
159 });
160 }
161
162
163
164
165 @Test
166 public void testToString()
167 {
168 assertEquals("Layer name mismatch", FilemergeLayer.LAYERNAME, layer.toString());
169 }
170
171
172
173
174 @Test
175 public void testEquals()
176 {
177 FilemergeLayer l1 = new FilemergeLayer();
178 FilemergeLayer l2 = new FilemergeLayer();
179 l1.init(INITVALUE);
180 l2.init(INITVALUE);
181 assertEquals("Layer object equal", l1, l2);
182 }
183
184
185
186
187 @Test
188 public void testEqualsSame()
189 {
190 FilemergeLayer l1 = new FilemergeLayer();
191 l1.init(INITVALUE);
192 assertEquals("Layer object same", l1, l1);
193 }
194
195
196
197
198 @Test
199 public void testNotEqualsNull()
200 {
201 FilemergeLayer l1 = new FilemergeLayer();
202 l1.init(INITVALUE);
203 Object o = null;
204 assertEquals("Layer object null unequal", l1.equals(o), false);
205 }
206
207
208
209
210 @Test
211 public void testNotEqualsWrongclass()
212 {
213 FilemergeLayer l1 = new FilemergeLayer();
214 l1.init(INITVALUE);
215 Object o = new Object();
216 assertEquals("Layer object wrong class unequal", l1.equals(o), false);
217 }
218
219
220
221
222 @Test
223 public void testHashcode()
224 {
225 FilemergeLayer l1 = new FilemergeLayer();
226 FilemergeLayer l2 = new FilemergeLayer();
227 l1.init(INITVALUE);
228 l2.init(INITVALUE);
229 assertEquals("Layer hash equal", l1.hashCode(), l2.hashCode());
230 }
231 }