You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bd...@apache.org on 2015/11/19 21:04:36 UTC
svn commit: r1715257 - in
/commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization:
ClosingBase.java MoreComplexObject.java MoreComplexObjectTest.java
ValidatingObjectInputStreamTest.java
Author: bdelacretaz
Date: Thu Nov 19 20:04:36 2015
New Revision: 1715257
URL: http://svn.apache.org/viewvc?rev=1715257&view=rev
Log:
IO-487 - MoreComplexObjectTest added, shows the (somewhat painful) settings needed to deserialize more realistic classes
Added:
commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/ClosingBase.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/MoreComplexObject.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/MoreComplexObjectTest.java
Modified:
commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/ValidatingObjectInputStreamTest.java
Added: commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/ClosingBase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/ClosingBase.java?rev=1715257&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/ClosingBase.java (added)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/ClosingBase.java Thu Nov 19 20:04:36 2015
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.io.serialization;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+
+/** Test base class that keeps track of Closeable objects
+ * and cleans them up.
+ */
+public class ClosingBase {
+ private final List<Closeable> toClose = new ArrayList<Closeable>();
+
+ protected <T extends Closeable> T willClose(T t) {
+ toClose.add(t);
+ return t;
+ }
+
+ @Before
+ public void setup() throws IOException {
+ toClose.clear();
+ }
+
+ @After
+ public void cleanup() {
+ for (Closeable c : toClose) {
+ try {
+ c.close();
+ } catch (IOException ignored) {
+ }
+ }
+ }
+}
\ No newline at end of file
Added: commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/MoreComplexObject.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/MoreComplexObject.java?rev=1715257&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/MoreComplexObject.java (added)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/MoreComplexObject.java Thu Nov 19 20:04:36 2015
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.io.serialization;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
+import java.util.UUID;
+
+/** A test class that uses various java.* member objects,
+ * to show which settings are necessary to deserialize
+ * those.
+ */
+public class MoreComplexObject implements Serializable {
+ private static final long serialVersionUID = -5187124661539240729L;
+
+ private final Random random = new Random(System.currentTimeMillis());
+ private final String string = UUID.randomUUID().toString();
+ private final Integer integer = random.nextInt();
+ private final int pInt = random.nextInt();
+ private final long pLong = random.nextLong();
+ private final Integer [] intArray = { random.nextInt(), random.nextInt() };
+ private final List<Boolean> boolList = new ArrayList<Boolean>();
+
+ MoreComplexObject() {
+ for(int i=0 ; i < 5; i++) {
+ boolList.add(random.nextBoolean());
+ }
+ }
+
+ @Override
+ public String toString() {
+ return string + integer + pInt + pLong + Arrays.asList(intArray) + boolList;
+ }
+}
\ No newline at end of file
Added: commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/MoreComplexObjectTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/MoreComplexObjectTest.java?rev=1715257&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/MoreComplexObjectTest.java (added)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/MoreComplexObjectTest.java Thu Nov 19 20:04:36 2015
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.io.serialization;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.ArrayList;
+import java.util.Random;
+
+import org.junit.Test;
+
+/** Test deserializing our {@link MoreComplexObject} to verify
+ * which settings it requires, as the object uses a number
+ * of primitive and java.* member objects.
+ */
+public class MoreComplexObjectTest extends ClosingBase {
+
+ @Test
+ public void serializeAndCheck() throws IOException, ClassNotFoundException {
+ final MoreComplexObject original = new MoreComplexObject();
+ final ByteArrayOutputStream bos = willClose(new ByteArrayOutputStream());
+ final ObjectOutputStream oos = willClose(new ObjectOutputStream(bos));
+ oos.writeObject(original);
+
+ final InputStream is = willClose(new ByteArrayInputStream(bos.toByteArray()));
+
+ // Having to specify all the MoreComplexObject member classes like
+ // this is a bit painful - we might create a utility that analyzes the
+ // class members and accepts their classes
+ final ObjectInputStream ois = willClose(
+ new ValidatingObjectInputStream(is)
+ .accept(MoreComplexObject.class, ArrayList.class, Integer[].class, Random.class)
+ .accept("java.lang.*")
+ );
+ final MoreComplexObject copy = (MoreComplexObject) (ois.readObject());
+ assertEquals("Expecting same data after deserializing", original.toString(), copy.toString());
+ }
+}
\ No newline at end of file
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/ValidatingObjectInputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/ValidatingObjectInputStreamTest.java?rev=1715257&r1=1715256&r2=1715257&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/ValidatingObjectInputStreamTest.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/serialization/ValidatingObjectInputStreamTest.java Thu Nov 19 20:04:36 2015
@@ -18,29 +18,24 @@
*/
package org.apache.commons.io.serialization;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
-import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
-import java.util.ArrayList;
-import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-public class ValidatingObjectInputStreamTest {
- private List<Closeable> toClose;
+public class ValidatingObjectInputStreamTest extends ClosingBase {
private OurTestClass testObject;
private InputStream testStream;
@@ -51,14 +46,8 @@ public class ValidatingObjectInputStream
}
};
- private <T extends Closeable> T willClose(T t) {
- toClose.add(t);
- return t;
- }
-
@Before
public void setup() throws IOException {
- toClose = new ArrayList<Closeable>();
testObject = new OurTestClass(UUID.randomUUID().toString());
final ByteArrayOutputStream bos = willClose(new ByteArrayOutputStream());
final ObjectOutputStream oos = willClose(new ObjectOutputStream(bos));
@@ -66,16 +55,6 @@ public class ValidatingObjectInputStream
testStream = willClose(new ByteArrayInputStream(bos.toByteArray()));
}
- @After
- public void cleanup() {
- for (Closeable c : toClose) {
- try {
- c.close();
- } catch (IOException ignored) {
- }
- }
- }
-
private void assertSerialization(ObjectInputStream ois) throws ClassNotFoundException, IOException {
final OurTestClass result = (OurTestClass) (ois.readObject());
assertEquals(testObject, result);