-
Notifications
You must be signed in to change notification settings - Fork 415
/
SerializingTranscoderTest.java
232 lines (201 loc) · 6.62 KB
/
SerializingTranscoderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) 2006 Dustin Sallings <dustin@spy.net>
package net.spy.memcached;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import net.spy.test.BaseMockCase;
/**
* Test the serializing transcoder.
*/
public class SerializingTranscoderTest extends BaseMockCase {
private SerializingTranscoder tc=null;
@Override
protected void setUp() throws Exception {
super.setUp();
tc=new SerializingTranscoder();
}
public void testNonserializable() throws Exception {
try {
tc.encode(new Object());
fail("Processed a non-serializable object.");
} catch(IllegalArgumentException e) {
// pass
}
}
public void testStrings() throws Exception {
String s1="This is a simple test string.";
CachedData cd=tc.encode(s1);
// Test the stringification while we're here.
String exp="{CachedData flags=0 data=[84, 104, 105, 115, 32, 105, "
+ "115, 32, 97, 32, 115, 105, 109, 112, 108, 101, 32, 116, 101, "
+ "115, 116, 32, 115, 116, 114, 105, 110, 103, 46]}";
assertEquals(exp, String.valueOf(cd));
assertEquals(0, cd.getFlags());
assertTrue(Arrays.equals(s1.getBytes(), cd.getData()));
assertEquals(s1, tc.decode(cd));
}
public void testUTF8String() throws Exception {
String s1="\u2013\u00f3\u2013\u00a5\u2014\u00c4\u2013\u221e\u2013"
+ "\u2264\u2014\u00c5\u2014\u00c7\u2013\u2264\u2014\u00c9\u2013"
+ "\u03c0, \u2013\u00ba\u2013\u220f\u2014\u00c4.";
CachedData cd=tc.encode(s1);
// Test the stringification while we're here.
String exp="{CachedData flags=0 data=[-30, -128, -109, -61, -77, -30, "
+ "-128, -109, -62, -91, -30, -128, -108, -61, -124, -30, "
+ "-128, -109, -30, -120, -98, -30, -128, -109, -30, -119, "
+ "-92, -30, -128, -108, -61, -123, -30, -128, -108, -61, -121, "
+ "-30, -128, -109, -30, -119, -92, -30, -128, -108, -61, -119, "
+ "-30, -128, -109, -49, -128, 44, 32, -30, -128, -109, -62, -70, "
+ "-30, -128, -109, -30, -120, -113, -30, -128, -108, -61, -124, "
+ "46]}";
assertEquals(exp, String.valueOf(cd));
assertEquals(0, cd.getFlags());
assertTrue(Arrays.equals(s1.getBytes("UTF-8"), cd.getData()));
assertEquals(s1, tc.decode(cd));
}
public void testValidCharacterSet() {
tc.setCharset("KOI8");
}
public void testInvalidCharacterSet() {
try {
tc.setCharset("Dustin's Kick Ass Character Set");
} catch(RuntimeException e) {
assertTrue(e.getCause() instanceof UnsupportedEncodingException);
}
}
public void testCompressedStringNotSmaller() throws Exception {
String s1="This is a test simple string that will not be compressed.";
// Reduce the compression threshold so it'll attempt to compress it.
tc.setCompressionThreshold(8);
CachedData cd=tc.encode(s1);
// This should *not* be compressed because it is too small
assertEquals(0, cd.getFlags());
assertTrue(Arrays.equals(s1.getBytes(), cd.getData()));
assertEquals(s1, tc.decode(cd));
}
public void testCompressedString() throws Exception {
// This one will actually compress
String s1="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
tc.setCompressionThreshold(8);
CachedData cd=tc.encode(s1);
assertEquals(SerializingTranscoder.COMPRESSED, cd.getFlags());
assertFalse(Arrays.equals(s1.getBytes(), cd.getData()));
assertEquals(s1, tc.decode(cd));
}
public void testObject() throws Exception {
Calendar c=Calendar.getInstance();
CachedData cd=tc.encode(c);
assertEquals(SerializingTranscoder.SERIALIZED, cd.getFlags());
assertEquals(c, tc.decode(cd));
}
public void testCompressedObject() throws Exception {
tc.setCompressionThreshold(8);
Calendar c=Calendar.getInstance();
CachedData cd=tc.encode(c);
assertEquals(SerializingTranscoder.SERIALIZED
|SerializingTranscoder.COMPRESSED, cd.getFlags());
assertEquals(c, tc.decode(cd));
}
public void testSomethingBigger() throws Exception {
Collection<Date> dates=new ArrayList<Date>();
for(int i=0; i<1024; i++) {
dates.add(new Date());
}
CachedData d=tc.encode(dates);
assertEquals(dates, tc.decode(d));
}
public void testDate() throws Exception {
Date d=new Date();
CachedData cd=tc.encode(d);
assertEquals(d, tc.decode(cd));
}
public void testLong() throws Exception {
assertEquals(923l, tc.decode(tc.encode(923l)));
}
public void testInt() throws Exception {
assertEquals(923, tc.decode(tc.encode(923)));
}
public void testBoolean() throws Exception {
assertSame(Boolean.TRUE, tc.decode(tc.encode(true)));
assertSame(Boolean.FALSE, tc.decode(tc.encode(false)));
}
public void testByte() throws Exception {
assertEquals((byte)-127, tc.decode(tc.encode((byte)-127)));
}
private void assertFloat(float f) {
assertEquals(f, tc.decode(tc.encode(f)));
}
public void testFloat() throws Exception {
assertFloat(0f);
assertFloat(Float.MIN_VALUE);
assertFloat(Float.MAX_VALUE);
assertFloat(3.14f);
assertFloat(-3.14f);
assertFloat(Float.NaN);
assertFloat(Float.POSITIVE_INFINITY);
assertFloat(Float.NEGATIVE_INFINITY);
}
private void assertDouble(double d) {
assertEquals(d, tc.decode(tc.encode(d)));
}
public void testDouble() throws Exception {
assertDouble(0d);
assertDouble(Double.MIN_VALUE);
assertDouble(Double.MAX_VALUE);
assertDouble(3.14d);
assertDouble(-3.14d);
assertDouble(Double.NaN);
assertDouble(Double.POSITIVE_INFINITY);
assertDouble(Double.NEGATIVE_INFINITY);
}
private void assertLong(long l) {
byte[] encoded=tc.encodeLong(l);
long decoded=tc.decodeLong(encoded);
assertEquals(l, decoded);
}
/*
private void displayBytes(long l, byte[] encoded) {
System.out.print(l + " [");
for(byte b : encoded) {
System.out.print((b<0?256+b:b) + " ");
}
System.out.println("]");
}
*/
public void testLongEncoding() throws Exception {
assertLong(Long.MIN_VALUE);
assertLong(1);
assertLong(23852);
assertLong(0l);
assertLong(-1);
assertLong(-23835);
assertLong(Long.MAX_VALUE);
}
private void assertInt(int i) {
byte[] encoded=tc.encodeInt(i);
int decoded=tc.decodeInt(encoded);
assertEquals(i, decoded);
}
public void testIntEncoding() throws Exception {
assertInt(Integer.MIN_VALUE);
assertInt(83526);
assertInt(1);
assertInt(0);
assertInt(-1);
assertInt(-238526);
assertInt(Integer.MAX_VALUE);
}
public void testBooleanEncoding() throws Exception {
assertTrue(tc.decodeBoolean(tc.encodeBoolean(true)));
assertFalse(tc.decodeBoolean(tc.encodeBoolean(false)));
}
public void testByteArray() throws Exception {
byte[] a={'a', 'b', 'c'};
CachedData cd=tc.encode(a);
assertTrue(Arrays.equals(a, cd.getData()));
assertTrue(Arrays.equals(a, (byte[])tc.decode(cd)));
}
}