CollectionSortedBagTest.java
/*
* 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
*
* https://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.bag;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.SortedBag;
import org.apache.commons.collections4.collection.AbstractCollectionTest;
import org.junit.jupiter.api.Test;
/**
* Test class for {@link CollectionSortedBag}.
* <p>
* Note: This test is mainly for serialization support, the CollectionSortedBag decorator
* is extensively used and tested in AbstractSortedBagTest.
*/
public class CollectionSortedBagTest<T> extends AbstractCollectionTest<T> {
@Override
public String getCompatibilityVersion() {
return "4";
}
/**
* Override to return comparable objects.
*/
@Override
@SuppressWarnings("unchecked")
public T[] getFullNonNullElements() {
final Object[] elements = new Object[30];
for (int i = 0; i < 30; i++) {
elements[i] = Integer.valueOf(i + i + 1);
}
return (T[]) elements;
}
/**
* Override to return comparable objects.
*/
@Override
@SuppressWarnings("unchecked")
public T[] getOtherNonNullElements() {
final Object[] elements = new Object[30];
for (int i = 0; i < 30; i++) {
elements[i] = Integer.valueOf(i + i + 2);
}
return (T[]) elements;
}
/**
* Overridden because SortedBags don't allow null elements (normally).
* @return false
*/
@Override
public boolean isNullSupported() {
return false;
}
/**
* Returns an empty List for use in modification testing.
*
* @return a confirmed empty collection
*/
@Override
public Collection<T> makeConfirmedCollection() {
return new ArrayList<>();
}
/**
* Returns a full Set for use in modification testing.
*
* @return a confirmed full collection
*/
@Override
public Collection<T> makeConfirmedFullCollection() {
final Collection<T> set = makeConfirmedCollection();
set.addAll(Arrays.asList(getFullElements()));
return set;
}
@Override
public Bag<T> makeObject() {
return CollectionSortedBag.collectionSortedBag(new TreeBag<>());
}
// void testCreate() throws Exception {
// resetEmpty();
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/CollectionSortedBag.emptyCollection.version4.obj");
// resetFull();
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/CollectionSortedBag.fullCollection.version4.obj");
// }
/**
* Compare the current serialized form of the Bag
* against the canonical version in SCM.
*/
@Test
void testEmptyBagCompatibility() throws IOException, ClassNotFoundException {
// test to make sure the canonical form has been preserved
final Bag<T> bag = makeObject();
if (bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
final Bag<?> bag2 = (Bag<?>) readExternalFormFromDisk(getCanonicalEmptyCollectionName(bag));
assertEquals(0, bag2.size(), "Bag is empty");
assertEquals(bag, bag2);
}
}
/**
* Compare the current serialized form of the Bag
* against the canonical version in SCM.
*/
@Test
void testFullBagCompatibility() throws IOException, ClassNotFoundException {
// test to make sure the canonical form has been preserved
final SortedBag<T> bag = (SortedBag<T>) makeFullCollection();
if (bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
final SortedBag<?> bag2 = (SortedBag<?>) readExternalFormFromDisk(getCanonicalFullCollectionName(bag));
assertEquals(bag.size(), bag2.size(), "Bag is the right size");
assertEquals(bag, bag2);
}
}
}