View Javadoc

1   package org.apache.torque.generator.outlet;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Component tests for OutletResult.
34   *
35   * @version $Id: $
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      /** System under test. */
46      private OutletResult stringOutletResult;
47  
48      /** System under test. */
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       * Checks that the getStringResult() method returns the correct content.
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       * Checks that the getStringResult() method returns the correct content.
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       * Checks that the isStringResult() method returns the correct content.
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       * Checks that the isByteArrayResult() method returns the correct content.
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      * Checks that the concatenate() throws an exception if
112      * OutletResults of different types are concatenated.
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      * Checks that the concatenate() method works for normal String results.
149      *
150      * @throws GeneratorException
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      * Checks that the concatenate() method works for null String results.
169      *
170      * @throws GeneratorException
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      * Checks that the concatenate() method works if we start
183      * with a null string result.
184      *
185      * @throws GeneratorException
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      * Checks that the concatenate() method works for normal byte array results.
199      *
200      * @throws GeneratorException
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      * Checks that the concatenate() method works for null byte array results.
219      *
220      * @throws GeneratorException
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      * Checks that the concatenate() method works if we start
235      * with a null byte array result.
236      *
237      * @throws GeneratorException
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      * Checks that the concatenate() method throws an exception for a null
253      * input.
254      *
255      * @throws GeneratorException
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      * Checks that the concatenate() method throws an exception for a null
273      * input.
274      *
275      * @throws GeneratorException
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      * Checks that the concatenate() method throws an exception for an empty
293      * input.
294      *
295      * @throws GeneratorException
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      * Checks that the equals() method returns the correct content.
313      */
314     @Test
315     public void testEquals()
316     {
317         // null
318         assertFalse(
319                 stringOutletResult.equals(null));
320 
321         // different class
322         assertFalse(
323                 stringOutletResult.equals("abc"));
324 
325         // different content class
326         assertFalse(
327                 stringOutletResult.equals(byteArrayOutletResult));
328 
329         // same object
330         assertTrue(stringOutletResult.equals(stringOutletResult));
331 
332         // same string content
333         assertTrue(
334                 stringOutletResult.equals(
335                         new OutletResult(STRING_INITIAL_CONTENT)));
336 
337         // different string content
338         assertFalse(
339                 stringOutletResult.equals(
340                         new OutletResult("abc")));
341         assertFalse(
342                 stringOutletResult.equals(
343                         new OutletResult((String) null)));
344 
345         // same byte array content
346         assertTrue(
347                 byteArrayOutletResult.equals(
348                         new OutletResult(BYTE_ARRAY_INITIAL_CONTENT)));
349 
350         // different byte array content
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 }