Skip to content

Commit

Permalink
[COLLECTIONS-806] Continue JUnit v5
Browse files Browse the repository at this point in the history
Break Inherifance need for BulkTest constants

TransformedSplitMapTest break inheritance of BulkTest

IteratorIterableTest break inheritance of BulkTest

Break Inheritance need for AbstractObjectTest
  • Loading branch information
nhojpatrick committed Mar 18, 2022
1 parent b4edfcc commit 5201c92
Show file tree
Hide file tree
Showing 19 changed files with 297 additions and 173 deletions.
133 changes: 11 additions & 122 deletions src/test/java/org/apache/commons/collections4/AbstractObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@
*/
package org.apache.commons.collections4;

import org.junit.Test;
import static org.apache.commons.collections4.AbstractObjectTestUtils.getCanonicalEmptyCollectionName;
import static org.apache.commons.collections4.AbstractObjectTestUtils.getCanonicalFullCollectionName;
import static org.apache.commons.collections4.AbstractObjectTestUtils.readExternalFormFromBytes;
import static org.apache.commons.collections4.AbstractObjectTestUtils.serializeDeserialize;
import static org.apache.commons.collections4.AbstractObjectTestUtils.writeExternalFormToBytes;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import org.junit.Test;

/**
* Abstract test class for {@link java.lang.Object} methods and contracts.
* <p>
Expand All @@ -40,7 +38,7 @@
* you may still use this base set of cases. Simply override the
* test case (method) your {@link Object} fails.
*/
public abstract class AbstractObjectTest extends BulkTest {
public abstract class AbstractObjectTest extends BulkTest implements AbstractObjectTestInterface {

/** Current major release for Collections */
public static final int COLLECTIONS_MAJOR_VERSION = 4;
Expand Down Expand Up @@ -134,19 +132,6 @@ public void testObjectHashCodeEqualsContract() {
}
}

protected Object serializeDeserialize(final Object obj) throws Exception {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(obj);
out.close();

final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
final Object dest = in.readObject();
in.close();

return dest;
}

@Test
public void testSerializeDeserializeThenCompare() throws Exception {
final Object obj = makeObject();
Expand Down Expand Up @@ -184,7 +169,7 @@ public void testCanonicalEmptyCollectionExists() {
if (supportsEmptyCollections() && isTestSerialization() && !skipSerializedCanonicalTests()) {
final Object object = makeObject();
if (object instanceof Serializable) {
final String name = getCanonicalEmptyCollectionName(object);
final String name = getCanonicalEmptyCollectionName(object, this);
assertTrue(
"Canonical empty collection (" + name + ") is not in SCM",
new File(name).exists());
Expand All @@ -201,7 +186,7 @@ public void testCanonicalFullCollectionExists() {
if (supportsFullCollections() && isTestSerialization() && !skipSerializedCanonicalTests()) {
final Object object = makeObject();
if (object instanceof Serializable) {
final String name = getCanonicalFullCollectionName(object);
final String name = getCanonicalFullCollectionName(object, this);
assertTrue(
"Canonical full collection (" + name + ") is not in SCM",
new File(name).exists());
Expand All @@ -225,109 +210,13 @@ public void testCanonicalFullCollectionExists() {
* @return The version, or {@code null} if this object shouldn't be
* tested for compatibility with previous versions.
*/
@Override
public String getCompatibilityVersion() {
return "4";
}

protected String getCanonicalEmptyCollectionName(final Object object) {
final StringBuilder retval = new StringBuilder();
retval.append(TEST_DATA_PATH);
String colName = object.getClass().getName();
colName = colName.substring(colName.lastIndexOf(".") + 1);
retval.append(colName);
retval.append(".emptyCollection.version");
retval.append(getCompatibilityVersion());
retval.append(".obj");
return retval.toString();
}

protected String getCanonicalFullCollectionName(final Object object) {
final StringBuilder retval = new StringBuilder();
retval.append(TEST_DATA_PATH);
String colName = object.getClass().getName();
colName = colName.substring(colName.lastIndexOf(".") + 1);
retval.append(colName);
retval.append(".fullCollection.version");
retval.append(getCompatibilityVersion());
retval.append(".obj");
return retval.toString();
}

/**
* Writes a Serializable or Externalizable object as
* a file at the given path. NOT USEFUL as part
* of a unit test; this is just a utility method
* for creating disk-based objects in SCM that can become
* the basis for compatibility tests using
* readExternalFormFromDisk(String path)
*
* @param o Object to serialize
* @param path path to write the serialized Object
* @throws IOException
*/
protected void writeExternalFormToDisk(final Serializable o, final String path) throws IOException {
try (FileOutputStream fileStream = new FileOutputStream(path)) {
writeExternalFormToStream(o, fileStream);
}
}

/**
* Converts a Serializable or Externalizable object to
* bytes. Useful for in-memory tests of serialization
*
* @param o Object to convert to bytes
* @return serialized form of the Object
* @throws IOException
*/
protected byte[] writeExternalFormToBytes(final Serializable o) throws IOException {
final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
writeExternalFormToStream(o, byteStream);
return byteStream.toByteArray();
}

/**
* Reads a Serialized or Externalized Object from disk.
* Useful for creating compatibility tests between
* different SCM versions of the same class
*
* @param path path to the serialized Object
* @return the Object at the given path
* @throws IOException
* @throws ClassNotFoundException
*/
protected Object readExternalFormFromDisk(final String path) throws IOException, ClassNotFoundException {
try (FileInputStream stream = new FileInputStream(path)) {
return readExternalFormFromStream(stream);
}
}

/**
* Read a Serialized or Externalized Object from bytes.
* Useful for verifying serialization in memory.
*
* @param b byte array containing a serialized Object
* @return Object contained in the bytes
* @throws IOException
* @throws ClassNotFoundException
*/
protected Object readExternalFormFromBytes(final byte[] b) throws IOException, ClassNotFoundException {
final ByteArrayInputStream stream = new ByteArrayInputStream(b);
return readExternalFormFromStream(stream);
}

protected boolean skipSerializedCanonicalTests() {
return Boolean.getBoolean("org.apache.commons.collections:with-clover");
}

// private implementation
private Object readExternalFormFromStream(final InputStream stream) throws IOException, ClassNotFoundException {
final ObjectInputStream oStream = new ObjectInputStream(stream);
return oStream.readObject();
}

private void writeExternalFormToStream(final Serializable o, final OutputStream stream) throws IOException {
final ObjectOutputStream oStream = new ObjectOutputStream(stream);
oStream.writeObject(o);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.collections4;

/**
* Abstract object test interface, extracted from AbstractObjectTest for easier reuse.
*/
public interface AbstractObjectTestInterface {

String getCompatibilityVersion();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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.collections4;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import static org.apache.commons.collections4.BulkTestConstants.TEST_DATA_PATH;

/**
* Abstract object test utils, extracted from AbstractObjectTest for easier reuse.
*/
public final class AbstractObjectTestUtils {

public static String getCanonicalEmptyCollectionName(final Object object, final AbstractObjectTestInterface test) {
final StringBuilder retval = new StringBuilder();
retval.append(TEST_DATA_PATH);
String colName = object.getClass().getName();
colName = colName.substring(colName.lastIndexOf(".") + 1);
retval.append(colName);
retval.append(".emptyCollection.version");
retval.append(test.getCompatibilityVersion());
retval.append(".obj");
return retval.toString();
}

public static String getCanonicalFullCollectionName(final Object object, final AbstractObjectTestInterface test) {
final StringBuilder retval = new StringBuilder();
retval.append(TEST_DATA_PATH);
String colName = object.getClass().getName();
colName = colName.substring(colName.lastIndexOf(".") + 1);
retval.append(colName);
retval.append(".fullCollection.version");
retval.append(test.getCompatibilityVersion());
retval.append(".obj");
return retval.toString();
}

public static Object serializeDeserialize(final Object obj) throws Exception {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(obj);
out.close();

final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
final Object dest = in.readObject();
in.close();

return dest;
}

/**
* Writes a Serializable or Externalizable object as
* a file at the given path. NOT USEFUL as part
* of a unit test; this is just a utility method
* for creating disk-based objects in SCM that can become
* the basis for compatibility tests using
* readExternalFormFromDisk(String path)
*
* @param o Object to serialize
* @param path path to write the serialized Object
* @throws IOException
*/
public static void writeExternalFormToDisk(final Serializable o, final String path) throws IOException {
try (FileOutputStream fileStream = new FileOutputStream(path)) {
writeExternalFormToStream(o, fileStream);
}
}

/**
* Converts a Serializable or Externalizable object to
* bytes. Useful for in-memory tests of serialization
*
* @param o Object to convert to bytes
* @return serialized form of the Object
* @throws IOException
*/
public static byte[] writeExternalFormToBytes(final Serializable o) throws IOException {
final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
writeExternalFormToStream(o, byteStream);
return byteStream.toByteArray();
}

/**
* Reads a Serialized or Externalized Object from disk.
* Useful for creating compatibility tests between
* different SCM versions of the same class
*
* @param path path to the serialized Object
* @return the Object at the given path
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object readExternalFormFromDisk(final String path) throws IOException, ClassNotFoundException {
try (FileInputStream stream = new FileInputStream(path)) {
return readExternalFormFromStream(stream);
}
}

/**
* Read a Serialized or Externalized Object from bytes.
* Useful for verifying serialization in memory.
*
* @param b byte array containing a serialized Object
* @return Object contained in the bytes
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object readExternalFormFromBytes(final byte[] b) throws IOException, ClassNotFoundException {
final ByteArrayInputStream stream = new ByteArrayInputStream(b);
return readExternalFormFromStream(stream);
}

// private implementation
private static Object readExternalFormFromStream(final InputStream stream) throws IOException, ClassNotFoundException {
final ObjectInputStream oStream = new ObjectInputStream(stream);
return oStream.readObject();
}

private static void writeExternalFormToStream(final Serializable o, final OutputStream stream) throws IOException {
final ObjectOutputStream oStream = new ObjectOutputStream(stream);
oStream.writeObject(o);
}

}
6 changes: 0 additions & 6 deletions src/test/java/org/apache/commons/collections4/BulkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,6 @@ public class BulkTest extends TestCase implements Cloneable {
// Given one BulkTest instance, we can just clone it and reset the
// method name for every simple test it defines.

/** Path to test data resources */
protected static final String TEST_DATA_PATH = "src/test/resources/org/apache/commons/collections4/data/test/";

/** Path to test properties resources */
public static final String TEST_PROPERTIES_PATH = "src/test/resources/org/apache/commons/collections4/properties/";

/**
* The full name of this bulk test instance. This is the full name
* that is compared to {@link #ignoredTests} to see if this
Expand Down

0 comments on commit 5201c92

Please sign in to comment.