1   package org.apache.fulcrum.yaafi.interceptor.util;
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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.Hashtable;
25  import java.util.Properties;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * Test suite for the SmartToStringBuilderImpl.
31   *
32   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
33   */
34  
35  public class ArgumentToStringBuilderTest extends TestCase
36  {
37      private String result;
38      private ArgumentToStringBuilderImpl toStringBuilder;
39      private int maxArgLength = 100;
40      int mode = 3;
41  
42  
43      /**
44       * Constructor
45       * @param name the name of the test case
46       */
47      public ArgumentToStringBuilderTest( String name )
48      {
49          super(name);
50      }
51  
52      /**
53       * @see junit.framework.TestCase#setUp()
54       */
55      protected void setUp() throws Exception
56      {
57          super.setUp();
58          this.toStringBuilder = new ArgumentToStringBuilderImpl();
59          this.toStringBuilder.setMaxArgLength(this.maxArgLength);
60          this.toStringBuilder.setMode(this.mode);
61          System.out.println( "=== " + this.getName() + " ====================================");
62      }
63  
64      /**
65       * @see junit.framework.TestCase#setUp()
66       */
67      protected void tearDown() throws Exception
68      {
69          System.out.println(this.result);
70          super.tearDown();
71      }
72  
73      /**
74       * Test with a simple String
75       */
76      public void testString()
77      {
78          String target = "In vino veritas";
79          this.toStringBuilder.setTarget(target);
80          result = toStringBuilder.toString();
81      }
82  
83      /**
84       * Test with a simple Integer
85       */
86      public void testInteger()
87      {
88          Integer target = new Integer(69);
89          this.toStringBuilder.setTarget(target);
90          result = toStringBuilder.toString();
91      }
92  
93      /**
94       * Test with an empty array
95       */
96      public void testEmptyArray()
97      {
98          String[] target = {};
99          this.toStringBuilder.setTarget(target);
100         result = toStringBuilder.toString();
101     }
102 
103     /**
104      * Test with a simple String[]
105      */
106     public void testStringArray()
107     {
108         String[] target = {"foo","bar"};
109         this.toStringBuilder.setTarget(target);
110         result = toStringBuilder.toString();
111     }
112 
113     /**
114      * Test with a simple Integer[]
115      */
116     public void testIntegerArray()
117     {
118         Integer[] target = {new Integer(4711), new Integer(815)};
119         this.toStringBuilder.setTarget(target);
120         result = toStringBuilder.toString();
121     }
122 
123     /**
124      * Test with an Exception
125      */
126     public void testException()
127     {
128         Exception target = new RuntimeException(this.getName());
129         this.toStringBuilder.setTarget(target);
130         result = toStringBuilder.toString();
131     }
132 
133     /**
134      * Test with an NULL object
135      */
136     public void testNull()
137     {
138         Object target = null;
139         this.toStringBuilder.setTarget(target);
140         result = toStringBuilder.toString();
141     }
142 
143     /**
144      * Test with an array containing NULL values
145      */
146     public void testWithANullArray()
147     {
148         Object target = new String[] { "foo", null, "bar" };
149         this.toStringBuilder.setTarget(target);
150         result = toStringBuilder.toString();
151     }
152 
153     /**
154      * Create a plain vanilla Java object
155      */
156     public void testPlainVanillaObject() throws Exception
157     {
158         File target = new File("./LICENSE.txt");
159         this.toStringBuilder.setTarget(target);
160         result = toStringBuilder.toString();
161     }
162 
163     /**
164      * Test with a char[]
165      */
166     public void testCharArray()
167     {
168         char[] target = this.getName().toCharArray();
169         this.toStringBuilder.setTarget(target);
170         result = toStringBuilder.toString();
171     }
172 
173     /**
174      * Test with a long char[] which will be truncated
175      */
176     public void testLongCharArray()
177     {
178         char[] target = System.getProperties().toString().toCharArray();
179         this.toStringBuilder.setTarget(target);
180         result = toStringBuilder.toString();
181     }
182 
183     /**
184      * Test with a byte[]
185      */
186     public void testByteArray()
187     {
188         Exception target = new RuntimeException(this.getName());
189         this.toStringBuilder.setTarget(target.toString().getBytes());
190         result = toStringBuilder.toString();
191     }
192 
193     /**
194      * Test with a multidimensional array
195      */
196     public void  testMultiDimensionalArray()
197     {
198         String[] row1 = {"r1.1", "1.2", "r1.3" };
199         int[] row2 = { 1, 2, 3 };
200         String[] row3 = {"r3.1" };
201         Object[] target = { row1, row2, row3, this.getName().toCharArray() };
202 
203         this.toStringBuilder.setTarget(target);
204         result = toStringBuilder.toString();
205     }
206 
207     /**
208      * Test with a hashtable
209      */
210     public void testHashtable()
211     {
212         Hashtable target = new Hashtable();
213         target.put("foo","foo");
214         target.put("bar","bar");
215 
216         this.toStringBuilder.setTarget(target);
217         result = toStringBuilder.toString();
218     }
219 
220     /**
221      * Test with a java.util.Properties
222      */
223     public void testProperties()
224     {
225         Properties target = System.getProperties();
226 
227         this.toStringBuilder.setTarget(target);
228         result = toStringBuilder.toString();
229     }
230 
231     /**
232      * Test with an ArrayList
233      */
234     public void testArrayList()
235     {
236         ArrayList target = new ArrayList();
237         target.add("foo");
238         target.add("bar");
239 
240         this.toStringBuilder.setTarget(target);
241         result = toStringBuilder.toString();
242     }
243 
244     /**
245      * Test with a non-empty int[]
246      */
247     public void testIntArray()
248     {
249         int[] target = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
250 
251         this.toStringBuilder.setTarget(target);
252         result = toStringBuilder.toString();
253     }
254 
255     /**
256      * Test with an empty int[]
257      */
258     public void testEmptyIntArray()
259     {
260         int[] target = {};
261 
262         this.toStringBuilder.setTarget(target);
263         result = toStringBuilder.toString();
264     }
265 
266     /**
267      * Test with an boolean[]
268      */
269     public void testBooleanArray()
270     {
271         boolean[] target = { true, false };
272 
273         this.toStringBuilder.setTarget(target);
274         result = toStringBuilder.toString();
275     }
276 
277     /**
278      * Test with an File[]
279      */
280     public void testFileArray()
281     {
282         File[] target = { new File("foo"), new File("bar") };
283 
284         this.toStringBuilder.setTarget(target);
285         result = toStringBuilder.toString();
286     }
287 
288 
289 }