KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > junit > utils > TestDataSqueezer


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.junit.utils;
16
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import java.math.BigDecimal JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import junit.framework.AssertionFailedError;
27 import junit.framework.TestCase;
28
29 import org.apache.hivemind.impl.DefaultClassResolver;
30 import org.apache.hivemind.util.PropertyUtils;
31 import org.apache.tapestry.services.DataSqueezer;
32 import org.apache.tapestry.spec.BeanLifecycle;
33 import org.apache.tapestry.util.ComponentAddress;
34 import org.apache.tapestry.util.io.DataSqueezerImpl;
35 import org.apache.tapestry.util.io.ISqueezeAdaptor;
36
37 /**
38  * A series of tests for {@link DataSqueezerImpl} and friends.
39  *
40  * @author Howard Lewis Ship
41  */

42
43 public class TestDataSqueezer extends TestCase
44 {
45     private DataSqueezerImpl ds = new DataSqueezerImpl(new DefaultClassResolver());
46
47     public TestDataSqueezer(String JavaDoc name)
48     {
49         super(name);
50     }
51
52     private void attempt(Object JavaDoc input, String JavaDoc expectedEncoding) throws IOException JavaDoc
53     {
54         attempt(input, expectedEncoding, ds);
55     }
56
57     private void attempt(Object JavaDoc input, String JavaDoc expectedEncoding, DataSqueezer ds) throws IOException JavaDoc
58     {
59         String JavaDoc encoding = ds.squeeze(input);
60
61         assertEquals("String encoding.", expectedEncoding, encoding);
62
63         Object JavaDoc output = ds.unsqueeze(encoding);
64
65         assertEquals("Decoded object.", input, output);
66     }
67
68     public void testBoolean() throws IOException JavaDoc
69     {
70         attempt(Boolean.TRUE, "T");
71         attempt(Boolean.FALSE, "F");
72     }
73
74     public void testNull() throws IOException JavaDoc
75     {
76         attempt(null, "X");
77     }
78
79     public void testByte() throws IOException JavaDoc
80     {
81         attempt(new Byte JavaDoc((byte) 0), "b0");
82         attempt(new Byte JavaDoc((byte) -5), "b-5");
83         attempt(new Byte JavaDoc((byte) 72), "b72");
84     }
85
86     public void testFloat() throws IOException JavaDoc
87     {
88         attempt(new Float JavaDoc(0), "f0.0");
89         attempt(new Float JavaDoc(3.1459), "f3.1459");
90         attempt(new Float JavaDoc(-37.23), "f-37.23");
91     }
92
93     public void testDouble() throws IOException JavaDoc
94     {
95         attempt(new Double JavaDoc(0), "d0.0");
96         attempt(new Double JavaDoc(3.1459), "d3.1459");
97         attempt(new Double JavaDoc(-37.23), "d-37.23");
98     }
99
100     public void testInteger() throws IOException JavaDoc
101     {
102         attempt(new Integer JavaDoc(0), "0");
103         attempt(new Integer JavaDoc(205), "205");
104         attempt(new Integer JavaDoc(-173), "-173");
105     }
106
107     public void testLong() throws IOException JavaDoc
108     {
109         attempt(new Long JavaDoc(0), "l0");
110         attempt(new Long JavaDoc(800400300l), "l800400300");
111         attempt(new Long JavaDoc(-987654321l), "l-987654321");
112     }
113
114     public void testShort() throws IOException JavaDoc
115     {
116         attempt(new Short JavaDoc((short) 0), "s0");
117         attempt(new Short JavaDoc((short) -10), "s-10");
118         attempt(new Short JavaDoc((short) 57), "s57");
119     }
120
121     /** @since 2.2 * */
122
123     public void testCharacter() throws IOException JavaDoc
124     {
125         attempt(new Character JavaDoc('a'), "ca");
126         attempt(new Character JavaDoc('Z'), "cZ");
127     }
128
129     public void testString() throws IOException JavaDoc
130     {
131         attempt("Now is the time for all good men ...", "SNow is the time for all good men ...");
132         attempt("X marks the spot!", "SX marks the spot!");
133         attempt("So long, sucker!", "SSo long, sucker!");
134     }
135
136     public void testComponentAddress() throws IOException JavaDoc
137     {
138         ComponentAddress objAddress = new ComponentAddress("framework:DirectLink",
139                 "component.subcomponent");
140         attempt(objAddress, "Aframework:DirectLink,component.subcomponent");
141
142         objAddress = new ComponentAddress("framework:DirectLink", null);
143         attempt(objAddress, "Aframework:DirectLink,");
144     }
145
146     public void testArray() throws IOException JavaDoc
147     {
148         Object JavaDoc[] input =
149         { new Short JavaDoc((short) -82), "Time to encode an array.", new Long JavaDoc(38383833273789l), null,
150                 Boolean.TRUE, new Double JavaDoc(22. / 7.) };
151
152         String JavaDoc[] encoded = ds.squeeze(input);
153
154         assertEquals("Encoded array length.", input.length, encoded.length);
155
156         Object JavaDoc[] output = ds.unsqueeze(encoded);
157
158         assertEquals("Output array length.", input.length, output.length);
159
160         for (int i = 0; i < input.length; i++)
161         {
162             assertEquals(input[i], output[i]);
163         }
164     }
165
166     public void testNullArray() throws IOException JavaDoc
167     {
168         Object JavaDoc[] input = null;
169
170         String JavaDoc[] encoded = ds.squeeze(input);
171
172         assertNull(encoded);
173
174         Object JavaDoc[] output = ds.unsqueeze(encoded);
175
176         assertNull(output);
177     }
178
179     private void attempt(Serializable JavaDoc s, DataSqueezer ds) throws IOException JavaDoc
180     {
181         String JavaDoc encoded = ds.squeeze(s);
182
183         Object JavaDoc output = ds.unsqueeze(encoded);
184
185         assertEquals(s, output);
186     }
187
188     public void testSerializable() throws IOException JavaDoc
189     {
190
191         Map JavaDoc map = new HashMap JavaDoc();
192
193         map.put("alpha", Boolean.TRUE);
194         map.put("beta", new StringHolder("FredFlintstone"));
195         map.put("gamma", new BigDecimal JavaDoc(
196                 "2590742358742358972.234592348957230948578975248972390857490725"));
197
198         attempt((Serializable JavaDoc) map, ds);
199     }
200
201     public static class BooleanHolder
202     {
203         private boolean value;
204
205         public BooleanHolder()
206         {
207         }
208
209         public BooleanHolder(boolean value)
210         {
211             this.value = value;
212         }
213
214         public boolean getValue()
215         {
216             return value;
217         }
218
219         public void setValue(boolean value)
220         {
221             this.value = value;
222         }
223
224         public boolean equals(Object JavaDoc other)
225         {
226             if (other == null)
227                 return false;
228
229             if (this == other)
230                 return true;
231
232             if (!(other instanceof BooleanHolder))
233                 return false;
234
235             BooleanHolder otherHolder = (BooleanHolder) other;
236
237             return value == otherHolder.value;
238         }
239     }
240
241     public static class BHSqueezer implements ISqueezeAdaptor
242     {
243         private static final String JavaDoc PREFIX = "B";
244
245         private static final String JavaDoc TRUE = "BT";
246
247         private static final String JavaDoc FALSE = "BF";
248
249         public void register(DataSqueezer squeezer)
250         {
251             squeezer.register(PREFIX, BooleanHolder.class, this);
252         }
253
254         public String JavaDoc squeeze(DataSqueezer squeezer, Object JavaDoc data) throws IOException JavaDoc
255         {
256             BooleanHolder h = (BooleanHolder) data;
257
258             return h.getValue() ? TRUE : FALSE;
259
260         }
261
262         public Object JavaDoc unsqueeze(DataSqueezer squeezer, String JavaDoc string) throws IOException JavaDoc
263         {
264             if (string.equals(TRUE))
265                 return new BooleanHolder(true);
266
267             if (string.equals(FALSE))
268                 return new BooleanHolder(false);
269
270             throw new IOException JavaDoc("Unexpected value.");
271         }
272
273     }
274
275     public void testCustom() throws IOException JavaDoc
276     {
277         DataSqueezer ds = new DataSqueezerImpl(new DefaultClassResolver(), new ISqueezeAdaptor[]
278         { new BHSqueezer() });
279
280         attempt(new BooleanHolder(true), "BT", ds);
281         attempt(new BooleanHolder(false), "BF", ds);
282
283         attempt("BooleanHolder", "SBooleanHolder", ds);
284     }
285
286     public void testRegisterShortPrefix()
287     {
288         try
289         {
290             ds.register("", BooleanHolder.class, new BHSqueezer());
291
292             throw new AssertionFailedError("Null prefix should be invalid.");
293         }
294         catch (IllegalArgumentException JavaDoc ex)
295         {
296         }
297     }
298
299     public void testRegisterInvalidPrefix()
300     {
301         try
302         {
303             ds.register("\n", BooleanHolder.class, new BHSqueezer());
304
305             throw new AssertionFailedError("Prefix should be invalid.");
306         }
307         catch (IllegalArgumentException JavaDoc ex)
308         {
309         }
310     }
311
312     public void testRegisterDupePrefix()
313     {
314         try
315         {
316             ds.register("b", BooleanHolder.class, new BHSqueezer());
317
318             throw new AssertionFailedError("Duplicate prefix should be invalid.");
319         }
320         catch (IllegalArgumentException JavaDoc ex)
321         {
322         }
323     }
324
325     public void testRegisterNullClass()
326     {
327         try
328         {
329             ds.register("B", null, new BHSqueezer());
330
331             throw new AssertionFailedError("Null data class should be invalid.");
332         }
333         catch (IllegalArgumentException JavaDoc ex)
334         {
335         }
336     }
337
338     public void testRegisterNullSqueezer()
339     {
340         try
341         {
342             ds.register("B", BooleanHolder.class, null);
343
344             throw new AssertionFailedError("Null squeezer should be invalid.");
345         }
346         catch (IllegalArgumentException JavaDoc ex)
347         {
348         }
349     }
350
351     private void unable(String JavaDoc message)
352     {
353         System.err.println("Unable to run test " + getClass().getName() + " " + getName() + ":");
354         System.err.println(message);
355         System.err.println("This may be ignored when running tests inside Eclipse.");
356     }
357
358     public void testClassLoader() throws Exception JavaDoc
359     {
360         // TODO: Change the build to download some JAR file,
361
// and change this code to reference a class within that
362
// JAR file.
363

364         File JavaDoc dir = new File JavaDoc(System.getProperty("PROJECT_ROOT", ".")
365                 + "/examples/Workbench/target/classes");
366
367         if (!dir.exists())
368         {
369             unable("Unable to find classes directory " + dir + ".");
370             return;
371         }
372
373         URL JavaDoc tutorialClassesURL = dir.toURL();
374
375         URLClassLoader JavaDoc classLoader = new URLClassLoader JavaDoc(new URL JavaDoc[]
376         { tutorialClassesURL });
377
378         Class JavaDoc visitClass = classLoader.loadClass("org.apache.tapestry.workbench.Visit");
379
380         Object JavaDoc visit = visitClass.newInstance();
381
382         ClassLoader JavaDoc visitClassLoader = visit.getClass().getClassLoader();
383
384         if (getClass().getClassLoader() == visitClassLoader)
385         {
386             unable("Unable to setup necessary ClassLoaders for test.");
387             return;
388         }
389
390         // System.out.println("This classloader = " + getClass().getClassLoader());
391
// System.out.println("Visit classloader = " + visit.getClass().getClassLoader());
392

393         String JavaDoc stringValue = Long.toHexString(System.currentTimeMillis());
394
395         PropertyUtils.write(visit, "stringValue", stringValue);
396
397         DataSqueezer squeezer = new DataSqueezerImpl(new DefaultClassResolver(visitClassLoader));
398
399         String JavaDoc squeezed = squeezer.squeeze(visit);
400
401         Object JavaDoc outVisit = squeezer.unsqueeze(squeezed);
402
403         // System.out.println("outVisit classloader = " + outVisit.getClass().getClassLoader());
404

405         assertNotNull(outVisit);
406         assertTrue("Input and output objects not same.", visit != outVisit);
407
408         String JavaDoc outStringValue = (String JavaDoc) PropertyUtils.read(outVisit, "stringValue");
409
410         assertEquals("Stored string.", stringValue, outStringValue);
411     }
412
413 }
Popular Tags