Skip to content

Commit 3444035

Browse files
committed
IO-487 - MoreComplexObjectTest added, shows the (somewhat painful) settings needed to deserialize more realistic classes
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1715257 13f79535-47bb-0310-9956-ffa450edef68
1 parent 84dcf5f commit 3444035

4 files changed

Lines changed: 169 additions & 23 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.commons.io.serialization;
20+
21+
import java.io.Closeable;
22+
import java.io.IOException;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import org.junit.After;
27+
import org.junit.Before;
28+
29+
/** Test base class that keeps track of Closeable objects
30+
* and cleans them up.
31+
*/
32+
public class ClosingBase {
33+
private final List<Closeable> toClose = new ArrayList<Closeable>();
34+
35+
protected <T extends Closeable> T willClose(T t) {
36+
toClose.add(t);
37+
return t;
38+
}
39+
40+
@Before
41+
public void setup() throws IOException {
42+
toClose.clear();
43+
}
44+
45+
@After
46+
public void cleanup() {
47+
for (Closeable c : toClose) {
48+
try {
49+
c.close();
50+
} catch (IOException ignored) {
51+
}
52+
}
53+
}
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.commons.io.serialization;
20+
21+
import java.io.Serializable;
22+
import java.util.ArrayList;
23+
import java.util.Arrays;
24+
import java.util.List;
25+
import java.util.Random;
26+
import java.util.UUID;
27+
28+
/** A test class that uses various java.* member objects,
29+
* to show which settings are necessary to deserialize
30+
* those.
31+
*/
32+
public class MoreComplexObject implements Serializable {
33+
private static final long serialVersionUID = -5187124661539240729L;
34+
35+
private final Random random = new Random(System.currentTimeMillis());
36+
private final String string = UUID.randomUUID().toString();
37+
private final Integer integer = random.nextInt();
38+
private final int pInt = random.nextInt();
39+
private final long pLong = random.nextLong();
40+
private final Integer [] intArray = { random.nextInt(), random.nextInt() };
41+
private final List<Boolean> boolList = new ArrayList<Boolean>();
42+
43+
MoreComplexObject() {
44+
for(int i=0 ; i < 5; i++) {
45+
boolList.add(random.nextBoolean());
46+
}
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return string + integer + pInt + pLong + Arrays.asList(intArray) + boolList;
52+
}
53+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.commons.io.serialization;
20+
21+
import static org.junit.Assert.assertEquals;
22+
23+
import java.io.ByteArrayInputStream;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.ObjectInputStream;
28+
import java.io.ObjectOutputStream;
29+
import java.util.ArrayList;
30+
import java.util.Random;
31+
32+
import org.junit.Test;
33+
34+
/** Test deserializing our {@link MoreComplexObject} to verify
35+
* which settings it requires, as the object uses a number
36+
* of primitive and java.* member objects.
37+
*/
38+
public class MoreComplexObjectTest extends ClosingBase {
39+
40+
@Test
41+
public void serializeAndCheck() throws IOException, ClassNotFoundException {
42+
final MoreComplexObject original = new MoreComplexObject();
43+
final ByteArrayOutputStream bos = willClose(new ByteArrayOutputStream());
44+
final ObjectOutputStream oos = willClose(new ObjectOutputStream(bos));
45+
oos.writeObject(original);
46+
47+
final InputStream is = willClose(new ByteArrayInputStream(bos.toByteArray()));
48+
49+
// Having to specify all the MoreComplexObject member classes like
50+
// this is a bit painful - we might create a utility that analyzes the
51+
// class members and accepts their classes
52+
final ObjectInputStream ois = willClose(
53+
new ValidatingObjectInputStream(is)
54+
.accept(MoreComplexObject.class, ArrayList.class, Integer[].class, Random.class)
55+
.accept("java.lang.*")
56+
);
57+
final MoreComplexObject copy = (MoreComplexObject) (ois.readObject());
58+
assertEquals("Expecting same data after deserializing", original.toString(), copy.toString());
59+
}
60+
}

src/test/java/org/apache/commons/io/serialization/ValidatingObjectInputStreamTest.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,24 @@
1818
*/
1919
package org.apache.commons.io.serialization;
2020

21+
import static org.junit.Assert.assertEquals;
2122
import static org.junit.Assert.assertTrue;
2223
import static org.junit.Assert.fail;
23-
import static org.junit.Assert.assertEquals;
2424

2525
import java.io.ByteArrayInputStream;
2626
import java.io.ByteArrayOutputStream;
27-
import java.io.Closeable;
2827
import java.io.IOException;
2928
import java.io.InputStream;
3029
import java.io.InvalidClassException;
3130
import java.io.ObjectInputStream;
3231
import java.io.ObjectOutputStream;
33-
import java.util.ArrayList;
34-
import java.util.List;
3532
import java.util.UUID;
3633
import java.util.regex.Pattern;
3734

38-
import org.junit.After;
3935
import org.junit.Before;
4036
import org.junit.Test;
4137

42-
public class ValidatingObjectInputStreamTest {
43-
private List<Closeable> toClose;
38+
public class ValidatingObjectInputStreamTest extends ClosingBase {
4439
private OurTestClass testObject;
4540
private InputStream testStream;
4641

@@ -51,31 +46,15 @@ public boolean matches(String className) {
5146
}
5247
};
5348

54-
private <T extends Closeable> T willClose(T t) {
55-
toClose.add(t);
56-
return t;
57-
}
58-
5949
@Before
6050
public void setup() throws IOException {
61-
toClose = new ArrayList<Closeable>();
6251
testObject = new OurTestClass(UUID.randomUUID().toString());
6352
final ByteArrayOutputStream bos = willClose(new ByteArrayOutputStream());
6453
final ObjectOutputStream oos = willClose(new ObjectOutputStream(bos));
6554
oos.writeObject(testObject);
6655
testStream = willClose(new ByteArrayInputStream(bos.toByteArray()));
6756
}
6857

69-
@After
70-
public void cleanup() {
71-
for (Closeable c : toClose) {
72-
try {
73-
c.close();
74-
} catch (IOException ignored) {
75-
}
76-
}
77-
}
78-
7958
private void assertSerialization(ObjectInputStream ois) throws ClassNotFoundException, IOException {
8059
final OurTestClass result = (OurTestClass) (ois.readObject());
8160
assertEquals(testObject, result);

0 commit comments

Comments
 (0)