/* * Copyright 2015 Goldman Sachs. * * Licensed 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 com.gs.collections.impl.set.sorted.mutable; import java.util.Collections; import java.util.TreeSet; import com.gs.collections.api.RichIterable; import com.gs.collections.api.bag.sorted.MutableSortedBag; import com.gs.collections.api.collection.MutableCollection; import com.gs.collections.api.set.sorted.MutableSortedSet; import com.gs.collections.impl.bag.sorted.mutable.TreeBag; import com.gs.collections.impl.block.factory.Comparators; import com.gs.collections.impl.block.factory.Predicates; import com.gs.collections.impl.block.factory.Predicates2; import com.gs.collections.impl.collection.mutable.AbstractSynchronizedCollectionTestCase; import com.gs.collections.impl.list.mutable.FastList; import com.gs.collections.impl.set.mutable.UnifiedSet; import com.gs.collections.impl.test.SerializeTestHelper; import com.gs.collections.impl.test.Verify; import org.junit.Assert; import org.junit.Test; /** * JUnit test for {@link SynchronizedSortedSet}. */ public class SynchronizedSortedSetTest extends AbstractSynchronizedCollectionTestCase { @Override protected <T> MutableCollection<T> newWith(T... littleElements) { return new SynchronizedSortedSet<>(SortedSetAdapter.adapt(new TreeSet<>(FastList.newListWith(littleElements)))); } @Override @Test public void newEmpty() { super.newEmpty(); Verify.assertInstanceOf(SynchronizedSortedSet.class, this.newWith().newEmpty()); } @Override @Test public void removeIf() { MutableCollection<Integer> objects = this.newWith(1, 2, 3, 4); Assert.assertTrue(objects.removeIf(Predicates.equal(2))); Verify.assertSize(3, objects); Verify.assertContainsAll(objects, 1, 3, 4); } @Test public void removeWithIf() { MutableCollection<Integer> objects = this.newWith(1, 2, 3, 4); Assert.assertTrue(objects.removeIfWith(Predicates2.equal(), 2)); Verify.assertSize(3, objects); Verify.assertContainsAll(objects, 1, 3, 4); } @Override @Test public void asUnmodifiable() { Verify.assertInstanceOf(UnmodifiableSortedSet.class, this.newWith().asUnmodifiable()); } @Override @Test public void selectInstancesOf() { MutableSortedSet<Number> mutableSortedSet = SortedSetAdapter.adapt(new TreeSet<>((o1, o2) -> Double.compare(o1.doubleValue(), o2.doubleValue()))); MutableSortedSet<Number> synchronizedSortedSet = new SynchronizedSortedSet<>(mutableSortedSet).withAll(FastList.newListWith(1, 2.0, 3, 4.0, 5)); MutableSortedSet<Integer> integers = synchronizedSortedSet.selectInstancesOf(Integer.class); Assert.assertEquals(UnifiedSet.newSetWith(1, 3, 5), integers); Assert.assertEquals(FastList.newListWith(1, 3, 5), integers.toList()); } @Override @Test public void equalsAndHashCode() { super.equalsAndHashCode(); MutableSortedSet<Integer> integers = TreeSortedSet.newSetWith(Comparators.reverseNaturalOrder(), 1, 2, 3).asSynchronized(); Verify.assertPostSerializedEqualsAndHashCode(this.newWith(1, 2, 3)); Verify.assertPostSerializedEqualsAndHashCode(integers); Verify.assertInstanceOf(SynchronizedSortedSet.class, SerializeTestHelper.serializeDeserialize(integers)); Verify.assertInstanceOf(SynchronizedSortedSet.class, SerializeTestHelper.serializeDeserialize(this.newWith(1, 2, 3))); } @Override @Test public void toSortedBag_natural_ordering() { RichIterable<Integer> integers = this.newWith(1, 2, 5, 3, 4); MutableSortedBag<Integer> bag = integers.toSortedBag(); Verify.assertSortedBagsEqual(TreeBag.newBagWith(1, 2, 3, 4, 5), bag); } @Override @Test public void toSortedBag_with_comparator() { RichIterable<Integer> integers = this.newWith(2, 4, 1, 3); MutableSortedBag<Integer> bag = integers.toSortedBag(Collections.<Integer>reverseOrder()); Verify.assertSortedBagsEqual(TreeBag.newBagWith(Collections.<Integer>reverseOrder(), 4, 3, 2, 1), bag); } @Override @Test(expected = NullPointerException.class) public void toSortedBag_with_null() { this.newWith(3, 4, null, 1, 2).toSortedBag(); } @Override @Test public void toSortedBagBy() { RichIterable<Integer> integers = this.newWith(2, 4, 1, 3); MutableSortedBag<Integer> bag = integers.toSortedBagBy(String::valueOf); Verify.assertSortedBagsEqual(TreeBag.newBagWith(1, 2, 3, 4), bag); } @Override @Test(expected = NullPointerException.class) public void min_null_safe() { super.min_null_safe(); } @Override @Test(expected = NullPointerException.class) public void max_null_safe() { super.max_null_safe(); } }