TransformedSortedMultiSetTest.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.multiset;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.commons.collections4.SortedMultiSet;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.collection.TransformedCollectionTest;
import org.junit.jupiter.api.Test;

/**
 * Extension of {@link AbstractSortedMultiSetTest} for exercising the
 * {@link TransformedSortedMultiSet} implementation.
 */
public class TransformedSortedMultiSetTest<T> extends AbstractSortedMultiSetTest<T> {

    @Override
    public String getCompatibilityVersion() {
        return "4.6";
    }

    @Override
    @SuppressWarnings("unchecked")
    public SortedMultiSet<T> makeObject() {
        return TransformedSortedMultiSet.transformingSortedMultiSet(new TreeMultiSet<>(),
                (Transformer<T, T>) TransformedCollectionTest.NOOP_TRANSFORMER);
    }

    @Test
    @SuppressWarnings("unchecked")
    void testTransformedMultiSet() {
        final SortedMultiSet<T> multiset = TransformedSortedMultiSet.transformingSortedMultiSet(new TreeMultiSet<>(),
                (Transformer<T, T>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
        assertEquals(0, multiset.size());
        final Object[] els = {"1", "3", "5", "7", "2", "4", "6"};
        for (int i = 0; i < els.length; i++) {
            multiset.add((T) els[i]);
            assertEquals(i + 1, multiset.size());
            assertTrue(multiset.contains(Integer.valueOf((String) els[i])));
        }
        assertEquals(Integer.valueOf(1), multiset.first());
        assertEquals(Integer.valueOf(7), multiset.last());

        assertTrue(multiset.remove(Integer.valueOf((String) els[0])));
    }

    @Test
    @SuppressWarnings("unchecked")
    void testTransformedMultiSet_decorateTransform() {
        final TreeMultiSet<T> originalMultiSet = new TreeMultiSet<>();
        final Object[] els = {"1", "3", "5", "7", "2", "4", "6"};
        for (final Object el : els) {
            originalMultiSet.add((T) el);
        }
        final SortedMultiSet<T> multiset = TransformedSortedMultiSet.transformedSortedMultiSet(originalMultiSet,
                (Transformer<T, T>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
        assertEquals(els.length, multiset.size());
        for (final Object el : els) {
            assertTrue(multiset.contains(Integer.valueOf((String) el)));
        }

        assertTrue(multiset.remove(Integer.valueOf((String) els[0])));
    }

//    void testCreate() throws Exception {
//        SortedMultiSet<T> multiset = makeObject();
//        writeExternalFormToDisk((java.io.Serializable) multiset, "src/test/resources/org/apache/commons/collections4/data/test/TransformedSortedMultiSet.emptyCollection.version4.6.obj");
//        multiset = makeFullCollection();
//        writeExternalFormToDisk((java.io.Serializable) multiset, "src/test/resources/org/apache/commons/collections4/data/test/TransformedSortedMultiSet.fullCollection.version4.6.obj");
//    }

}