You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ch...@apache.org on 2017/07/11 17:56:09 UTC
[39/50] commons-collections git commit: Move PrototypeFactory tests
to new TestPrototypeFactory test.
Move PrototypeFactory tests to new TestPrototypeFactory test.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/branches/COLLECTIONS_3_2_X@1713854 13f79535-47bb-0310-9956-ffa450edef68
Project: http://git-wip-us.apache.org/repos/asf/commons-collections/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-collections/commit/c2413184
Tree: http://git-wip-us.apache.org/repos/asf/commons-collections/tree/c2413184
Diff: http://git-wip-us.apache.org/repos/asf/commons-collections/diff/c2413184
Branch: refs/heads/COLLECTIONS_3_2_X
Commit: c2413184717c83d0a39a93dc9b270d9172af9231
Parents: 7533540
Author: Thomas Neidhart <tn...@apache.org>
Authored: Wed Nov 11 14:40:21 2015 +0000
Committer: Thomas Neidhart <tn...@apache.org>
Committed: Wed Nov 11 14:40:21 2015 +0000
----------------------------------------------------------------------
.../commons/collections/TestFactoryUtils.java | 62 ----------
.../functors/TestPrototypeFactory.java | 122 +++++++++++++++++++
2 files changed, 122 insertions(+), 62 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/c2413184/src/test/org/apache/commons/collections/TestFactoryUtils.java
----------------------------------------------------------------------
diff --git a/src/test/org/apache/commons/collections/TestFactoryUtils.java b/src/test/org/apache/commons/collections/TestFactoryUtils.java
index e1b8f89..51dce4a 100644
--- a/src/test/org/apache/commons/collections/TestFactoryUtils.java
+++ b/src/test/org/apache/commons/collections/TestFactoryUtils.java
@@ -129,68 +129,6 @@ public class TestFactoryUtils extends junit.framework.TestCase {
assertSame(ConstantFactory.NULL_INSTANCE, FactoryUtils.prototypeFactory(null));
}
- public void testPrototypeFactoryPublicCloneMethod() throws Exception {
- Date proto = new Date();
- Factory factory = FactoryUtils.prototypeFactory(proto);
- assertNotNull(factory);
- Object created = factory.create();
- assertTrue(proto != created);
- assertEquals(proto, created);
-
- // check serialisation works
- ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- ObjectOutputStream out = new ObjectOutputStream(buffer);
- out.writeObject(factory);
- out.close();
- ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
- Object dest = in.readObject();
- in.close();
- }
-
- public void testPrototypeFactoryPublicCopyConstructor() throws Exception {
- Mock1 proto = new Mock1(6);
- Factory factory = FactoryUtils.prototypeFactory(proto);
- assertNotNull(factory);
- Object created = factory.create();
- assertTrue(proto != created);
- assertEquals(proto, created);
-
- // check serialisation works
- ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- ObjectOutputStream out = new ObjectOutputStream(buffer);
- try {
- out.writeObject(factory);
- } catch (NotSerializableException ex) {
- out.close();
- }
- factory = FactoryUtils.prototypeFactory(new Mock2("S"));
- buffer = new ByteArrayOutputStream();
- out = new ObjectOutputStream(buffer);
- out.writeObject(factory);
- out.close();
- ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
- Object dest = in.readObject();
- in.close();
- }
-
- public void testPrototypeFactoryPublicSerialization() throws Exception {
- Integer proto = new Integer(9);
- Factory factory = FactoryUtils.prototypeFactory(proto);
- assertNotNull(factory);
- Object created = factory.create();
- assertTrue(proto != created);
- assertEquals(proto, created);
-
- // check serialisation works
- ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- ObjectOutputStream out = new ObjectOutputStream(buffer);
- out.writeObject(factory);
- out.close();
- ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
- Object dest = in.readObject();
- in.close();
- }
-
public void testPrototypeFactoryPublicSerializationError() {
Mock2 proto = new Mock2(new Object());
Factory factory = FactoryUtils.prototypeFactory(proto);
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/c2413184/src/test/org/apache/commons/collections/functors/TestPrototypeFactory.java
----------------------------------------------------------------------
diff --git a/src/test/org/apache/commons/collections/functors/TestPrototypeFactory.java b/src/test/org/apache/commons/collections/functors/TestPrototypeFactory.java
index 1ac51e4..3701b04 100644
--- a/src/test/org/apache/commons/collections/functors/TestPrototypeFactory.java
+++ b/src/test/org/apache/commons/collections/functors/TestPrototypeFactory.java
@@ -16,9 +16,17 @@
*/
package org.apache.commons.collections.functors;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.NotSerializableException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
import java.util.ArrayList;
+import java.util.Date;
import org.apache.commons.collections.Factory;
+import org.apache.commons.collections.FactoryUtils;
import junit.framework.Test;
import junit.framework.TestSuite;
@@ -46,4 +54,118 @@ public class TestPrototypeFactory extends AbstractTestSerialization {
return Factory.class;
}
+ // ------------------------------------------------------------------------
+
+ public void testPrototypeFactoryPublicCloneMethod() throws Exception {
+ Date proto = new Date();
+ Factory factory = PrototypeFactory.getInstance(proto);
+ assertNotNull(factory);
+ Object created = factory.create();
+ assertTrue(proto != created);
+ assertEquals(proto, created);
+
+ // check serialisation works - if enabled
+ System.setProperty(FunctorUtils.UNSAFE_SERIALIZABLE_PROPERTY, "true");
+ try {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ ObjectOutputStream out = new ObjectOutputStream(buffer);
+ out.writeObject(factory);
+ out.close();
+ ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
+ Object dest = in.readObject();
+ in.close();
+ } finally {
+ System.clearProperty(FunctorUtils.UNSAFE_SERIALIZABLE_PROPERTY);
+ }
+ }
+
+ public void testPrototypeFactoryPublicCopyConstructor() throws Exception {
+ Mock1 proto = new Mock1(6);
+ Factory factory = PrototypeFactory.getInstance(proto);
+ assertNotNull(factory);
+ Object created = factory.create();
+ assertTrue(proto != created);
+ assertEquals(proto, created);
+
+ // check serialisation works - if enabled
+ System.setProperty(FunctorUtils.UNSAFE_SERIALIZABLE_PROPERTY, "true");
+ try {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ ObjectOutputStream out = new ObjectOutputStream(buffer);
+ try {
+ out.writeObject(factory);
+ } catch (NotSerializableException ex) {
+ out.close();
+ }
+ factory = FactoryUtils.prototypeFactory(new Mock2("S"));
+ buffer = new ByteArrayOutputStream();
+ out = new ObjectOutputStream(buffer);
+ out.writeObject(factory);
+ out.close();
+ ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
+ Object dest = in.readObject();
+ in.close();
+ } finally {
+ System.clearProperty(FunctorUtils.UNSAFE_SERIALIZABLE_PROPERTY);
+ }
+ }
+
+ public void testPrototypeFactoryPublicSerialization() throws Exception {
+ Integer proto = new Integer(9);
+ Factory factory = FactoryUtils.prototypeFactory(proto);
+ assertNotNull(factory);
+ Object created = factory.create();
+ assertTrue(proto != created);
+ assertEquals(proto, created);
+
+ // check serialisation works - if enabled
+ System.setProperty(FunctorUtils.UNSAFE_SERIALIZABLE_PROPERTY, "true");
+ try {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ ObjectOutputStream out = new ObjectOutputStream(buffer);
+ out.writeObject(factory);
+ out.close();
+ ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
+ Object dest = in.readObject();
+ in.close();
+ } finally {
+ System.clearProperty(FunctorUtils.UNSAFE_SERIALIZABLE_PROPERTY);
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ private static class Mock1 {
+ private final int iVal;
+ public Mock1(int val) {
+ iVal = val;
+ }
+ public Mock1(Mock1 mock) {
+ iVal = mock.iVal;
+ }
+ public boolean equals(Object obj) {
+ if (obj instanceof Mock1) {
+ if (iVal == ((Mock1) obj).iVal) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
+ private static class Mock2 implements Serializable {
+ private final Object iVal;
+ public Mock2(Object val) {
+ iVal = val;
+ }
+ public boolean equals(Object obj) {
+ if (obj instanceof Mock2) {
+ if (iVal == ((Mock2) obj).iVal) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
}