UnmodifiableSortedSetTest.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.set;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.junit.jupiter.api.Test;
/**
* Extension of {@link AbstractSortedSetTest} for exercising the
* {@link UnmodifiableSortedSet} implementation.
*/
public class UnmodifiableSortedSetTest<E> extends AbstractSortedSetTest<E> {
protected UnmodifiableSortedSet<E> set;
protected ArrayList<E> array;
@Override
public String getCompatibilityVersion() {
return "4";
}
@Override
public boolean isAddSupported() {
return false;
}
@Override
public boolean isRemoveSupported() {
return false;
}
@Override
public UnmodifiableSortedSet<E> makeFullCollection() {
final TreeSet<E> set = new TreeSet<>(Arrays.asList(getFullElements()));
return (UnmodifiableSortedSet<E>) UnmodifiableSortedSet.unmodifiableSortedSet(set);
}
@Override
public SortedSet<E> makeObject() {
return UnmodifiableSortedSet.unmodifiableSortedSet(new TreeSet<>());
}
@SuppressWarnings("unchecked")
protected void setupSet() {
set = makeFullCollection();
array = new ArrayList<>();
array.add((E) Integer.valueOf(1));
}
@Test
void testComparator() {
setupSet();
final Comparator<? super E> c = set.comparator();
assertNull(c, "natural order, so comparator should be null");
}
@Test
void testDecorateFactory() {
final SortedSet<E> set = makeFullCollection();
assertSame(set, UnmodifiableSortedSet.unmodifiableSortedSet(set));
assertThrows(NullPointerException.class, () -> UnmodifiableSortedSet.unmodifiableSortedSet(null));
}
/**
* Verify that base set and subsets are not modifiable
*/
@Test
@SuppressWarnings("unchecked")
void testUnmodifiable() {
setupSet();
verifyUnmodifiable(set);
verifyUnmodifiable(set.headSet((E) Integer.valueOf(1)));
verifyUnmodifiable(set.tailSet((E) Integer.valueOf(1)));
verifyUnmodifiable(set.subSet((E) Integer.valueOf(1), (E) Integer.valueOf(3)));
}
/**
* Verifies that a set is not modifiable
*/
@SuppressWarnings("unchecked")
public void verifyUnmodifiable(final Set<E> set) {
assertThrows(UnsupportedOperationException.class, () -> set.add((E) "value"));
assertThrows(UnsupportedOperationException.class, () -> set.addAll(new TreeSet<>()));
assertThrows(UnsupportedOperationException.class, () -> set.clear());
assertThrows(UnsupportedOperationException.class, () -> set.remove("x"));
assertThrows(UnsupportedOperationException.class, () -> set.removeAll(array));
assertThrows(UnsupportedOperationException.class, () -> set.retainAll(array));
}
// void testCreate() throws Exception {
// resetEmpty();
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableSortedSet.emptyCollection.version4.obj");
// resetFull();
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableSortedSet.fullCollection.version4.obj");
// }
}