1 15 package org.apache.tapestry.junit.utils; 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.io.Serializable ; 20 import java.math.BigDecimal ; 21 import java.net.URL ; 22 import java.net.URLClassLoader ; 23 import java.util.HashMap ; 24 import java.util.Map ; 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 42 43 public class TestDataSqueezer extends TestCase 44 { 45 private DataSqueezerImpl ds = new DataSqueezerImpl(new DefaultClassResolver()); 46 47 public TestDataSqueezer(String name) 48 { 49 super(name); 50 } 51 52 private void attempt(Object input, String expectedEncoding) throws IOException 53 { 54 attempt(input, expectedEncoding, ds); 55 } 56 57 private void attempt(Object input, String expectedEncoding, DataSqueezer ds) throws IOException 58 { 59 String encoding = ds.squeeze(input); 60 61 assertEquals("String encoding.", expectedEncoding, encoding); 62 63 Object output = ds.unsqueeze(encoding); 64 65 assertEquals("Decoded object.", input, output); 66 } 67 68 public void testBoolean() throws IOException 69 { 70 attempt(Boolean.TRUE, "T"); 71 attempt(Boolean.FALSE, "F"); 72 } 73 74 public void testNull() throws IOException 75 { 76 attempt(null, "X"); 77 } 78 79 public void testByte() throws IOException 80 { 81 attempt(new Byte ((byte) 0), "b0"); 82 attempt(new Byte ((byte) -5), "b-5"); 83 attempt(new Byte ((byte) 72), "b72"); 84 } 85 86 public void testFloat() throws IOException 87 { 88 attempt(new Float (0), "f0.0"); 89 attempt(new Float (3.1459), "f3.1459"); 90 attempt(new Float (-37.23), "f-37.23"); 91 } 92 93 public void testDouble() throws IOException 94 { 95 attempt(new Double (0), "d0.0"); 96 attempt(new Double (3.1459), "d3.1459"); 97 attempt(new Double (-37.23), "d-37.23"); 98 } 99 100 public void testInteger() throws IOException 101 { 102 attempt(new Integer (0), "0"); 103 attempt(new Integer (205), "205"); 104 attempt(new Integer (-173), "-173"); 105 } 106 107 public void testLong() throws IOException 108 { 109 attempt(new Long (0), "l0"); 110 attempt(new Long (800400300l), "l800400300"); 111 attempt(new Long (-987654321l), "l-987654321"); 112 } 113 114 public void testShort() throws IOException 115 { 116 attempt(new Short ((short) 0), "s0"); 117 attempt(new Short ((short) -10), "s-10"); 118 attempt(new Short ((short) 57), "s57"); 119 } 120 121 122 123 public void testCharacter() throws IOException 124 { 125 attempt(new Character ('a'), "ca"); 126 attempt(new Character ('Z'), "cZ"); 127 } 128 129 public void testString() throws IOException 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 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 147 { 148 Object [] input = 149 { new Short ((short) -82), "Time to encode an array.", new Long (38383833273789l), null, 150 Boolean.TRUE, new Double (22. / 7.) }; 151 152 String [] encoded = ds.squeeze(input); 153 154 assertEquals("Encoded array length.", input.length, encoded.length); 155 156 Object [] 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 167 { 168 Object [] input = null; 169 170 String [] encoded = ds.squeeze(input); 171 172 assertNull(encoded); 173 174 Object [] output = ds.unsqueeze(encoded); 175 176 assertNull(output); 177 } 178 179 private void attempt(Serializable s, DataSqueezer ds) throws IOException 180 { 181 String encoded = ds.squeeze(s); 182 183 Object output = ds.unsqueeze(encoded); 184 185 assertEquals(s, output); 186 } 187 188 public void testSerializable() throws IOException 189 { 190 191 Map map = new HashMap (); 192 193 map.put("alpha", Boolean.TRUE); 194 map.put("beta", new StringHolder("FredFlintstone")); 195 map.put("gamma", new BigDecimal ( 196 "2590742358742358972.234592348957230948578975248972390857490725")); 197 198 attempt((Serializable ) 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 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 PREFIX = "B"; 244 245 private static final String TRUE = "BT"; 246 247 private static final String FALSE = "BF"; 248 249 public void register(DataSqueezer squeezer) 250 { 251 squeezer.register(PREFIX, BooleanHolder.class, this); 252 } 253 254 public String squeeze(DataSqueezer squeezer, Object data) throws IOException 255 { 256 BooleanHolder h = (BooleanHolder) data; 257 258 return h.getValue() ? TRUE : FALSE; 259 260 } 261 262 public Object unsqueeze(DataSqueezer squeezer, String string) throws IOException 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 ("Unexpected value."); 271 } 272 273 } 274 275 public void testCustom() throws IOException 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 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 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 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 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 ex) 347 { 348 } 349 } 350 351 private void unable(String 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 359 { 360 364 File dir = new File (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 tutorialClassesURL = dir.toURL(); 374 375 URLClassLoader classLoader = new URLClassLoader (new URL [] 376 { tutorialClassesURL }); 377 378 Class visitClass = classLoader.loadClass("org.apache.tapestry.workbench.Visit"); 379 380 Object visit = visitClass.newInstance(); 381 382 ClassLoader 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 393 String stringValue = Long.toHexString(System.currentTimeMillis()); 394 395 PropertyUtils.write(visit, "stringValue", stringValue); 396 397 DataSqueezer squeezer = new DataSqueezerImpl(new DefaultClassResolver(visitClassLoader)); 398 399 String squeezed = squeezer.squeeze(visit); 400 401 Object outVisit = squeezer.unsqueeze(squeezed); 402 403 405 assertNotNull(outVisit); 406 assertTrue("Input and output objects not same.", visit != outVisit); 407 408 String outStringValue = (String ) PropertyUtils.read(outVisit, "stringValue"); 409 410 assertEquals("Stored string.", stringValue, outStringValue); 411 } 412 413 } | Popular Tags |