KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > record > TestPersistentPropertyDataEncoder


1 // Copyright 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.record;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Random JavaDoc;
21
22 import org.apache.hivemind.ApplicationRuntimeException;
23 import org.apache.hivemind.test.HiveMindTestCase;
24
25 /**
26  * Tests for {@link org.apache.tapestry.record.PersistentPropertyDataEncoderImpl}.
27  *
28  * @author Howard M. Lewis Ship
29  * @since 4.0
30  */

31 public class TestPersistentPropertyDataEncoder extends HiveMindTestCase
32 {
33     /**
34      * Test pushing minimal amounts of data, which should favor the non-GZipped version of the
35      * output stream.
36      */

37
38     public void testRoundTripShort() throws Exception JavaDoc
39     {
40         PropertyChange pc = new PropertyChangeImpl(null, "property", "foo");
41         List JavaDoc input = Collections.singletonList(pc);
42
43         PersistentPropertyDataEncoder encoder = new PersistentPropertyDataEncoderImpl();
44
45         String JavaDoc encoded = encoder.encodePageChanges(input);
46
47         System.out.println(encoded);
48
49         List JavaDoc output = encoder.decodePageChanges(encoded);
50
51         assertEquals(input, output);
52
53     }
54
55     /**
56      * Test pushing a lot of data, which should trigger the GZip encoding option.
57      */

58
59     public void testRoundTripLong() throws Exception JavaDoc
60     {
61         Random JavaDoc r = new Random JavaDoc();
62
63         List JavaDoc input = new ArrayList JavaDoc();
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 JavaDoc(r.nextLong()));
69
70             input.add(pc);
71         }
72
73         PersistentPropertyDataEncoder encoder = new PersistentPropertyDataEncoderImpl();
74
75         String JavaDoc encoded = encoder.encodePageChanges(input);
76
77         assertEquals("Z", encoded.substring(0, 1));
78
79         List JavaDoc 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 JavaDoc());
96         List JavaDoc 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