1 15 package org.apache.tapestry.record; 16 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.List ; 20 import java.util.Random ; 21 22 import org.apache.hivemind.ApplicationRuntimeException; 23 import org.apache.hivemind.test.HiveMindTestCase; 24 25 31 public class TestPersistentPropertyDataEncoder extends HiveMindTestCase 32 { 33 37 38 public void testRoundTripShort() throws Exception 39 { 40 PropertyChange pc = new PropertyChangeImpl(null, "property", "foo"); 41 List input = Collections.singletonList(pc); 42 43 PersistentPropertyDataEncoder encoder = new PersistentPropertyDataEncoderImpl(); 44 45 String encoded = encoder.encodePageChanges(input); 46 47 System.out.println(encoded); 48 49 List output = encoder.decodePageChanges(encoded); 50 51 assertEquals(input, output); 52 53 } 54 55 58 59 public void testRoundTripLong() throws Exception 60 { 61 Random r = new Random (); 62 63 List input = new ArrayList (); 64 65 for (int i = 0; i < 20; i++) 66 { 67 PropertyChange pc = new PropertyChangeImpl(i % 2 == 0 ? null : "componentId", 68 "property" + i, new Long (r.nextLong())); 69 70 input.add(pc); 71 } 72 73 PersistentPropertyDataEncoder encoder = new PersistentPropertyDataEncoderImpl(); 74 75 String encoded = encoder.encodePageChanges(input); 76 77 assertEquals("Z", encoded.substring(0, 1)); 78 79 List output = encoder.decodePageChanges(encoded); 80 81 assertEquals(input, output); 82 } 83 84 public void testEmptyEncoding() 85 { 86 PersistentPropertyDataEncoder encoder = new PersistentPropertyDataEncoderImpl(); 87 88 assertEquals("", encoder.encodePageChanges(Collections.EMPTY_LIST)); 89 90 assertEquals(0, encoder.decodePageChanges("").size()); 91 } 92 93 public void testEncodeNonSerializable() 94 { 95 PropertyChange pc = new PropertyChangeImpl(null, "property", new Object ()); 96 List l = Collections.singletonList(pc); 97 98 PersistentPropertyDataEncoder encoder = new PersistentPropertyDataEncoderImpl(); 99 100 try 101 { 102 encoder.encodePageChanges(l); 103 unreachable(); 104 } 105 catch (ApplicationRuntimeException ex) 106 { 107 assertEquals( 108 "An exception occured encoding the data stream into MIME format: java.lang.Object", 109 ex.getMessage()); 110 } 111 } 112 113 public void testDecodeInvalid() 114 { 115 PersistentPropertyDataEncoder encoder = new PersistentPropertyDataEncoderImpl(); 116 117 try 118 { 119 encoder.decodePageChanges("ZZZZZZZZZZZZZZZZZZZ"); 120 unreachable(); 121 } 122 catch (ApplicationRuntimeException ex) 123 { 124 assertEquals( 125 "An exception occured decoding the MIME data stream: Not in GZIP format", 126 ex.getMessage()); 127 } 128 } 129 130 public void testDecodeUnknownPrefix() 131 { 132 PersistentPropertyDataEncoder encoder = new PersistentPropertyDataEncoderImpl(); 133 134 try 135 { 136 encoder.decodePageChanges("QQQQQQQQQQQ"); 137 unreachable(); 138 } 139 catch (ApplicationRuntimeException ex) 140 { 141 assertEquals( 142 "The prefix of the MIME encoded data stream was 'Q', it should be 'B' or 'Z'.", 143 ex.getMessage()); 144 } 145 146 } 147 } | Popular Tags |