1 package org.apache.torque.generator.outlet;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.assertArrayEquals;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import org.apache.torque.generator.GeneratorException;
29 import org.junit.Before;
30 import org.junit.Test;
31
32
33
34
35
36
37
38 public class OutletResultTest
39 {
40 private static final String STRING_INITIAL_CONTENT = "String!\r\ncontent";
41
42 private static final byte[] BYTE_ARRAY_INITIAL_CONTENT
43 = new byte[] {34, -56, 126, -127 };
44
45
46 private OutletResult stringOutletResult;
47
48
49 private OutletResult byteArrayOutletResult;
50
51 @Before
52 public void setUp()
53 {
54 byteArrayOutletResult = new OutletResult(BYTE_ARRAY_INITIAL_CONTENT);
55 stringOutletResult = new OutletResult(STRING_INITIAL_CONTENT);
56 }
57
58
59
60
61 @Test
62 public void testGetStringResult()
63 {
64 assertEquals(
65 STRING_INITIAL_CONTENT,
66 stringOutletResult.getStringResult());
67 assertEquals(
68 null,
69 byteArrayOutletResult.getStringResult());
70 }
71
72
73
74
75 @Test
76 public void testGetByteArrayResult()
77 {
78 assertEquals(
79 null,
80 stringOutletResult.getByteArrayResult());
81 assertEquals(
82 BYTE_ARRAY_INITIAL_CONTENT,
83 byteArrayOutletResult.getByteArrayResult());
84 }
85
86
87
88
89 @Test
90 public void testIsStringResult()
91 {
92 assertTrue(stringOutletResult.isStringResult());
93 assertFalse(byteArrayOutletResult.isStringResult());
94 assertTrue(new OutletResult((byte[]) null).isStringResult());
95 assertTrue(new OutletResult((String) null).isStringResult());
96 }
97
98
99
100
101 @Test
102 public void testIsByteArrayResult()
103 {
104 assertFalse(stringOutletResult.isByteArrayResult());
105 assertTrue(byteArrayOutletResult.isByteArrayResult());
106 assertTrue(new OutletResult((byte[]) null).isByteArrayResult());
107 assertTrue(new OutletResult((String) null).isByteArrayResult());
108 }
109
110
111
112
113
114 @Test
115 public void testConcatenateWrongTypes()
116 {
117 try
118 {
119 OutletResult.concatenate(stringOutletResult, byteArrayOutletResult);
120 fail("Exception expected");
121 }
122 catch (GeneratorException e)
123 {
124 assertEquals("first OutletResult to concatenate is a "
125 + "String result but a following result is a "
126 + "byte array."
127 + " All concatenated results must be "
128 + "of the same type",
129 e.getMessage());
130 }
131 try
132 {
133 OutletResult.concatenate(byteArrayOutletResult, stringOutletResult);
134 fail("Exception expected");
135 }
136 catch (GeneratorException e)
137 {
138 assertEquals("first OutletResult to concatenate is a "
139 + "byte array result but a following result is a "
140 + "String result."
141 + " All concatenated results must be "
142 + "of the same type",
143 e.getMessage());
144 }
145 }
146
147
148
149
150
151
152 @Test
153 public void testConcatenateString() throws GeneratorException
154 {
155 OutletResult result = OutletResult.concatenate(
156 stringOutletResult,
157 new OutletResult("bdf"),
158 new OutletResult("123"));
159 assertEquals(
160 STRING_INITIAL_CONTENT + "bdf" + "123",
161 result.getStringResult());
162 assertEquals(null, result.getByteArrayResult());
163 assertTrue(result.isStringResult());
164 assertFalse(result.isByteArrayResult());
165 }
166
167
168
169
170
171
172 @Test
173 public void testConcatenateNullString() throws GeneratorException
174 {
175 OutletResult result = OutletResult.concatenate(
176 stringOutletResult,
177 new OutletResult((String) null));
178 assertEquals(STRING_INITIAL_CONTENT, result.getStringResult());
179 }
180
181
182
183
184
185
186
187 @Test
188 public void testConcatenateStartingWithNullString()
189 throws GeneratorException
190 {
191 OutletResult result = OutletResult.concatenate(
192 new OutletResult((String) null),
193 stringOutletResult);
194 assertEquals(STRING_INITIAL_CONTENT, result.getStringResult());
195 }
196
197
198
199
200
201
202 @Test
203 public void testConcatenateByteArray() throws GeneratorException
204 {
205 OutletResult result = OutletResult.concatenate(
206 byteArrayOutletResult,
207 new OutletResult(new byte[] {11, -22}),
208 new OutletResult(new byte[] {1, 0}));
209 assertArrayEquals(
210 new byte[] {34, -56, 126, -127, 11, -22, 1, 0},
211 result.getByteArrayResult());
212 assertEquals(null, result.getStringResult());
213 assertFalse(result.isStringResult());
214 assertTrue(result.isByteArrayResult());
215 }
216
217
218
219
220
221
222 @Test
223 public void testConcatenateNullByteArray() throws GeneratorException
224 {
225 OutletResult result = OutletResult.concatenate(
226 byteArrayOutletResult,
227 new OutletResult((byte[]) null));
228 assertArrayEquals(
229 BYTE_ARRAY_INITIAL_CONTENT,
230 result.getByteArrayResult());
231 }
232
233
234
235
236
237
238
239 @Test
240 public void testConcatenateStartingWithNullByteArray()
241 throws GeneratorException
242 {
243 OutletResult result = OutletResult.concatenate(
244 new OutletResult((byte[]) null),
245 byteArrayOutletResult);
246 assertArrayEquals(
247 BYTE_ARRAY_INITIAL_CONTENT,
248 result.getByteArrayResult());
249 }
250
251
252
253
254
255
256
257 @Test
258 public void testConcatenateNullArray() throws GeneratorException
259 {
260 try
261 {
262 OutletResult.concatenate((OutletResult[]) null);
263 fail("Exception expected");
264 }
265 catch (NullPointerException e)
266 {
267 assertEquals("input must not be null", e.getMessage());
268 }
269 }
270
271
272
273
274
275
276
277 @Test
278 public void testConcatenateNullList() throws GeneratorException
279 {
280 try
281 {
282 OutletResult.concatenate((Iterable<OutletResult>) null);
283 fail("Exception expected");
284 }
285 catch (NullPointerException e)
286 {
287 assertEquals("input must not be null", e.getMessage());
288 }
289 }
290
291
292
293
294
295
296
297 @Test
298 public void testConcatenateEmpty() throws GeneratorException
299 {
300 try
301 {
302 OutletResult.concatenate();
303 fail("Exception expected");
304 }
305 catch (IllegalArgumentException e)
306 {
307 assertEquals("input must not be empty", e.getMessage());
308 }
309 }
310
311
312
313
314 @Test
315 public void testEquals()
316 {
317
318 assertFalse(
319 stringOutletResult.equals(null));
320
321
322 assertFalse(
323 stringOutletResult.equals("abc"));
324
325
326 assertFalse(
327 stringOutletResult.equals(byteArrayOutletResult));
328
329
330 assertTrue(stringOutletResult.equals(stringOutletResult));
331
332
333 assertTrue(
334 stringOutletResult.equals(
335 new OutletResult(STRING_INITIAL_CONTENT)));
336
337
338 assertFalse(
339 stringOutletResult.equals(
340 new OutletResult("abc")));
341 assertFalse(
342 stringOutletResult.equals(
343 new OutletResult((String) null)));
344
345
346 assertTrue(
347 byteArrayOutletResult.equals(
348 new OutletResult(BYTE_ARRAY_INITIAL_CONTENT)));
349
350
351 assertFalse(
352 byteArrayOutletResult.equals(
353 new OutletResult(new byte[] {37, 51})));
354 assertFalse(
355 byteArrayOutletResult.equals(
356 new OutletResult((byte[]) null)));
357 }
358
359
360 }