Repository navigation
Expand file tree
/
Copy pathSerializerPojoTest.java
More file actions
424 lines (314 loc) · 12.2 KB
/
Copy pathSerializerPojoTest.java
File metadata and controls
424 lines (314 loc) · 12.2 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package org.mapdb;
import junit.framework.TestCase;
import javax.security.sasl.RealmCallback;
import javax.swing.*;
import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class SerializerPojoTest extends TestCase {
SerializerPojo p = new SerializerPojo(new CopyOnWriteArrayList<SerializerPojo.ClassInfo>());
enum Order
{
ASCENDING,
DESCENDING
}
private byte[] serialize(Object i) throws IOException {
DataOutput2 in = new DataOutput2();
p.serialize(in, i);
return in.copyBytes();
}
private Object deserialize(byte[] buf) throws IOException {
return p.deserialize(new DataInput2(ByteBuffer.wrap(buf),0),-1);
}
public void testEnum() throws Exception{
Order o = Order.ASCENDING;
o = (Order) UtilsTest.clone(o, p);
assertEquals(o,Order.ASCENDING );
assertEquals(o.ordinal(),Order.ASCENDING .ordinal());
assertEquals(o.name(),Order.ASCENDING .name());
o = Order.DESCENDING;
o = (Order) UtilsTest.clone(o, p);
assertEquals(o,Order.DESCENDING );
assertEquals(o.ordinal(),Order.DESCENDING .ordinal());
assertEquals(o.name(),Order.DESCENDING .name());
}
static class Extr implements Externalizable{
public Extr(){}
int aaa = 11;
String l = "agfa";
@Override public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(l);
out.writeInt(aaa);
}
@Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
l = (String) in.readObject();
aaa = in.readInt()+1;
}
}
public void testExternalizable() throws Exception{
Extr e = new Extr();
e.aaa = 15;
e.l = "pakla";
e = (Extr) deserialize(serialize(e));
assertEquals(e.aaa, 16); //was incremented during serialization
assertEquals(e.l,"pakla");
}
static class Bean1 implements Serializable {
private static final long serialVersionUID = -2549023895082866523L;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Bean1 bean1 = (Bean1) o;
if (Double.compare(bean1.doubleField, doubleField) != 0) return false;
if (Float.compare(bean1.floatField, floatField) != 0) return false;
if (intField != bean1.intField) return false;
if (longField != bean1.longField) return false;
if (field1 != null ? !field1.equals(bean1.field1) : bean1.field1 != null) return false;
if (field2 != null ? !field2.equals(bean1.field2) : bean1.field2 != null) return false;
return true;
}
protected String field1 = null;
protected String field2 = null;
protected int intField = Integer.MAX_VALUE;
protected long longField = Long.MAX_VALUE;
protected double doubleField = Double.MAX_VALUE;
protected float floatField = Float.MAX_VALUE;
transient int getCalled = 0;
transient int setCalled = 0;
public String getField2() {
getCalled++;
return field2;
}
public void setField2(String field2) {
setCalled++;
this.field2 = field2;
}
Bean1(String field1, String field2) {
this.field1 = field1;
this.field2 = field2;
}
Bean1() {
}
}
static class Bean2 extends Bean1 {
private static final long serialVersionUID = 8376654194053933530L;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Bean2 bean2 = (Bean2) o;
if (field3 != null ? !field3.equals(bean2.field3) : bean2.field3 != null) return false;
return true;
}
@Override
public int hashCode() {
return field3 != null ? field3.hashCode() : 0;
}
private String field3 = null;
Bean2(String field1, String field2, String field3) {
super(field1, field2);
this.field3 = field3;
}
Bean2() {
}
}
Bean1 b = new Bean1("aa", "bb");
Bean2 b2 = new Bean2("aa", "bb", "cc");
public void testGetFieldValue1() throws Exception {
assertEquals("aa", p.getFieldValue(new SerializerPojo.FieldInfo("field1",false,String.class.getName(),b.getClass()), b));
}
public void testGetFieldValue2() throws Exception {
assertEquals("bb", p.getFieldValue(new SerializerPojo.FieldInfo("field2",false,String.class.getName(),b.getClass()), b));
assertEquals(0, b.getCalled);
}
public void testGetFieldValue3() throws Exception {
assertEquals("aa", p.getFieldValue(new SerializerPojo.FieldInfo("field1",false,String.class.getName(),b2.getClass()), b2));
}
public void testGetFieldValue4() throws Exception {
assertEquals("bb", p.getFieldValue(new SerializerPojo.FieldInfo("field2",false,String.class.getName(),b2.getClass()), b2));
assertEquals(0, b2.getCalled);
}
public void testGetFieldValue5() throws Exception {
assertEquals("cc", p.getFieldValue(new SerializerPojo.FieldInfo("field3",false,String.class.getName(),b2.getClass()), b2));
}
public void testSerializable() throws Exception {
assertEquals(b, UtilsTest.clone(b, p));
}
public void testRecursion() throws Exception {
AbstractMap.SimpleEntry b = new AbstractMap.SimpleEntry("abcd", null);
b.setValue(b.getKey());
AbstractMap.SimpleEntry bx = (AbstractMap.SimpleEntry) UtilsTest.clone(b, p);
assertEquals(bx, b);
assert (bx.getKey() == bx.getValue());
}
public void testRecursion2() throws Exception {
AbstractMap.SimpleEntry b = new AbstractMap.SimpleEntry("abcd", null);
b.setValue(b);
AbstractMap.SimpleEntry bx = (AbstractMap.SimpleEntry) UtilsTest.clone(b, p);
assertTrue(bx == bx.getValue());
assertEquals(bx.getKey(), "abcd");
}
public void testRecursion3() throws Exception {
ArrayList l = new ArrayList();
l.add("123");
l.add(l);
ArrayList l2 = (ArrayList) UtilsTest.clone(l, p);
assertTrue(l2.size() == 2);
assertEquals(l2.get(0), "123");
assertTrue(l2.get(1) == l2);
}
public void testPersistedSimple() throws Exception {
File f = UtilsTest.tempDbFile();
DB r1 = DBMaker.newFileDB(f).make();
long recid = r1.engine.put("AA",r1.getDefaultSerializer());
r1.commit();
r1.close();
r1 = DBMaker.newFileDB(f).make();
String a2 = (String) r1.engine.get(recid, r1.getDefaultSerializer());
r1.close();
assertEquals("AA", a2);
}
public void testPersisted() throws Exception {
Bean1 b1 = new Bean1("abc", "dcd");
File f = UtilsTest.tempDbFile();
DB r1 = DBMaker.newFileDB(f).make();
long recid = r1.engine.put(b1, r1.getDefaultSerializer());
r1.commit();
r1.close();
r1 = DBMaker.newFileDB(f).make();
Bean1 b2 = (Bean1) r1.engine.get(recid,r1.getDefaultSerializer());
r1.close();
assertEquals(b1, b2);
}
public void test_write_object_advanced_serializationm(){
Object[] o = new Object[]{
new GregorianCalendar(1,1,1),
new JLabel("aa")
};
for(Object oo:o){
DB db = DBMaker.newMemoryDB().make();
long recid = db.engine.put(oo, db.getDefaultSerializer());
assertEquals(oo, db.engine.get(recid, db.getDefaultSerializer()));
}
}
public static class test_pojo_reload_TestClass implements Serializable
{
private String name;
public test_pojo_reload_TestClass(String name) {
this.name = name;
}
}
/** @author Jan Sileny */
public void test_pojo_reload() throws IOException {
File f = UtilsTest.tempDbFile();
DB db = DBMaker.newFileDB(f).make();
Set set = db.getHashSet("testSerializerPojo");
set.add(new test_pojo_reload_TestClass("test"));
db.commit();
// System.out.println(((SerializerPojo)db.defaultSerializer).registered);
int prevsize = ((SerializerPojo)db.getDefaultSerializer()).registered.size();
db.close();
db = DBMaker.newFileDB(f).deleteFilesAfterClose().make();
set = db.getHashSet("testSerializerPojo");
set.add(new test_pojo_reload_TestClass("test2"));
db.commit();
int newsize = ((SerializerPojo)db.getDefaultSerializer()).registered.size();
// System.out.println(((SerializerPojo)db.defaultSerializer).registered);
db.close();
assertEquals(prevsize, newsize);
}
public static class test_transient implements Serializable{
transient int aa = 11;
transient String ss = "aa";
int bb = 11;
}
public void test_transient(){
test_transient t = new test_transient();
t.aa = 12;
t.ss = "bb";
t.bb = 13;
t = (test_transient) UtilsTest.clone(t, p);
assertEquals(0,t.aa);
assertEquals(null,t.ss);
assertEquals(13,t.bb);
}
public void test_transient2(){
test_transient t = new test_transient();
t.aa = 12;
t.ss = "bb";
t.bb = 13;
t = outputStreamClone(t);
assertEquals(0,t.aa);
assertEquals(null,t.ss);
assertEquals(13,t.bb);
}
/** clone value using serialization */
public static <E> E outputStreamClone(E value){
try{
ByteArrayOutputStream out = new ByteArrayOutputStream();
new ObjectOutputStream(out).writeObject(value);
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));
return (E) in.readObject();
}catch(Exception ee){
throw new IOError(ee);
}
}
public void testIssue177() throws UnknownHostException {
DB db = DBMaker.newMemoryDB().cacheDisable().make();
InetAddress value = InetAddress.getByName("127.0.0.1");
long recid = db.engine.put(value, db.getDefaultSerializer());
Object value2 = db.engine.get(recid,db.getDefaultSerializer());
assertEquals(value,value2);
}
//this can not be serialized, it alwaes throws exception on serialization
static final class RealClass implements Serializable, Externalizable{
@Override
public void writeExternal(ObjectOutput out) throws IOException {
throw new Error();
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
throw new Error();
}
}
//this is placeholder which gets serialized instead
static final class PlaceHolder implements Serializable{
}
public void test_interlizeceptors(){
final AtomicInteger counter = new AtomicInteger();
Fun.Function1 ser = new Fun.Function1() {
@Override
public Object run(Object o) {
if(o instanceof RealClass) {
counter.incrementAndGet();
return new PlaceHolder();
}
return o;
}
};
Fun.Function1 deser = new Fun.Function1() {
@Override
public Object run(Object o) {
if(o instanceof PlaceHolder) {
counter.incrementAndGet();
return new RealClass();
}
return o;
}
};
p.serializerTransformAdd(ser,deser);
Object o = UtilsTest.clone(new RealClass(), p);
assertTrue(o instanceof RealClass);
assertEquals(2,counter.get());
}
}