Search in sources :

Example 1 with ReferenceMap

use of org.apache.commons.collections4.map.ReferenceMap in project commons-collections by apache.

the class ReferenceMapTest method testDataSizeAfterSerialization.

/**
 * Test whether after serialization the "data" HashEntry array is the same size as the original.<p>
 *
 * See <a href="https://issues.apache.org/jira/browse/COLLECTIONS-599">COLLECTIONS-599: HashEntry array object naming data initialized with double the size during deserialization</a>
 */
@Test
public void testDataSizeAfterSerialization() throws IOException, ClassNotFoundException {
    final ReferenceMap<String, String> serializeMap = new ReferenceMap<>(ReferenceStrength.WEAK, ReferenceStrength.WEAK, true);
    serializeMap.put("KEY", "VALUE");
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (ObjectOutputStream out = new ObjectOutputStream(baos)) {
        out.writeObject(serializeMap);
    }
    final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    try (ObjectInputStream in = new ObjectInputStream(bais)) {
        @SuppressWarnings("unchecked") final ReferenceMap<String, String> deserializedMap = (ReferenceMap<String, String>) in.readObject();
        assertEquals(1, deserializedMap.size());
        assertEquals(serializeMap.data.length, deserializedMap.data.length);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) BulkTest(org.apache.commons.collections4.BulkTest)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 BulkTest (org.apache.commons.collections4.BulkTest)1 Test (org.junit.Test)1