/* * Copyright 2013 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.bag.mutable.primitive; import com.gs.collections.api.bag.primitive.MutableBooleanBag; import com.gs.collections.api.iterator.MutableBooleanIterator; import com.gs.collections.impl.list.mutable.primitive.BooleanArrayList; import com.gs.collections.impl.test.Verify; import org.junit.Assert; import org.junit.Test; /** * JUnit test for {@link UnmodifiableBooleanBag}. */ public class UnmodifiableBooleanBagTest extends AbstractMutableBooleanBagTestCase { private final MutableBooleanBag bag = this.classUnderTest(); @Override protected final UnmodifiableBooleanBag classUnderTest() { return new UnmodifiableBooleanBag(BooleanHashBag.newBagWith(true, false, true)); } @Override protected UnmodifiableBooleanBag newWith(boolean... elements) { return new UnmodifiableBooleanBag(BooleanHashBag.newBagWith(elements)); } @Override @Test(expected = UnsupportedOperationException.class) public void addOccurrences() { this.bag.addOccurrences(false, 3); } @Override @Test(expected = UnsupportedOperationException.class) public void addOccurrences_throws() { this.newWith().addOccurrences(true, -1); } @Override @Test(expected = UnsupportedOperationException.class) public void removeOccurrences() { this.bag.removeOccurrences(true, 1); } @Override @Test(expected = UnsupportedOperationException.class) public void removeOccurrences_throws() { this.newWith().removeOccurrences(true, -1); } @Override @Test(expected = UnsupportedOperationException.class) public void clear() { this.classUnderTest().clear(); } @Override @Test(expected = UnsupportedOperationException.class) public void add() { this.newWith().add(true); } @Override @Test(expected = UnsupportedOperationException.class) public void addAllArray() { this.classUnderTest().addAll(true, false, true); } @Override @Test(expected = UnsupportedOperationException.class) public void addAllIterable() { this.classUnderTest().addAll(this.newMutableCollectionWith()); } @Override @Test(expected = UnsupportedOperationException.class) public void remove() { this.classUnderTest().remove(false); } @Override @Test(expected = UnsupportedOperationException.class) public void removeAll() { this.classUnderTest().removeAll(true, false); } @Override @Test(expected = UnsupportedOperationException.class) public void removeAll_iterable() { this.classUnderTest().removeAll(this.newMutableCollectionWith()); } @Override @Test(expected = UnsupportedOperationException.class) public void retainAll() { this.classUnderTest().retainAll(true, false); } @Override @Test(expected = UnsupportedOperationException.class) public void retainAll_iterable() { this.classUnderTest().retainAll(this.newMutableCollectionWith()); } @Override @Test(expected = UnsupportedOperationException.class) public void with() { this.newWith().with(true); } @Override @Test(expected = UnsupportedOperationException.class) public void withAll() { this.newWith().withAll(this.newMutableCollectionWith(true)); } @Override @Test(expected = UnsupportedOperationException.class) public void without() { this.newWith(true, false, true, false, true).without(true); } @Override @Test(expected = UnsupportedOperationException.class) public void withoutAll() { this.newWith(true, false, true, false, true).withoutAll(this.newMutableCollectionWith(false, false)); } @Override @Test public void containsAllArray() { UnmodifiableBooleanBag collection = this.classUnderTest(); Assert.assertTrue(collection.containsAll(true)); Assert.assertTrue(collection.containsAll(true, false, true)); Assert.assertTrue(collection.containsAll(true, false)); Assert.assertTrue(collection.containsAll(true, true)); Assert.assertTrue(collection.containsAll(false, false)); UnmodifiableBooleanBag emptyCollection = this.newWith(); Assert.assertFalse(emptyCollection.containsAll(true)); Assert.assertFalse(emptyCollection.containsAll(false)); Assert.assertFalse(emptyCollection.containsAll(false, true, false)); Assert.assertFalse(this.newWith(true, true).containsAll(false, true, false)); UnmodifiableBooleanBag trueCollection = this.newWith(true, true, true, true); Assert.assertFalse(trueCollection.containsAll(true, false)); UnmodifiableBooleanBag falseCollection = this.newWith(false, false, false, false); Assert.assertFalse(falseCollection.containsAll(true, false)); } @Override @Test public void containsAllIterable() { UnmodifiableBooleanBag emptyCollection = this.newWith(); Assert.assertTrue(emptyCollection.containsAll(new BooleanArrayList())); Assert.assertFalse(emptyCollection.containsAll(BooleanArrayList.newListWith(true))); Assert.assertFalse(emptyCollection.containsAll(BooleanArrayList.newListWith(false))); UnmodifiableBooleanBag collection = this.newWith(true, true, false, false, false); Assert.assertTrue(collection.containsAll(BooleanArrayList.newListWith(true))); Assert.assertTrue(collection.containsAll(BooleanArrayList.newListWith(false))); Assert.assertTrue(collection.containsAll(BooleanArrayList.newListWith(true, false))); Assert.assertTrue(collection.containsAll(BooleanArrayList.newListWith(true, true))); Assert.assertTrue(collection.containsAll(BooleanArrayList.newListWith(false, false))); Assert.assertTrue(collection.containsAll(BooleanArrayList.newListWith(true, false, true))); Assert.assertFalse(this.newWith(true, true).containsAll(BooleanArrayList.newListWith(false, true, false))); UnmodifiableBooleanBag trueCollection = this.newWith(true, true, true, true); Assert.assertFalse(trueCollection.containsAll(BooleanArrayList.newListWith(true, false))); UnmodifiableBooleanBag falseCollection = this.newWith(false, false, false, false); Assert.assertFalse(falseCollection.containsAll(BooleanArrayList.newListWith(true, false))); } @Override @Test public void asUnmodifiable() { super.asUnmodifiable(); Assert.assertSame(this.bag, this.bag.asUnmodifiable()); Assert.assertEquals(this.bag, this.bag.asUnmodifiable()); } @Override @Test public void booleanIterator_with_remove() { MutableBooleanIterator booleanIterator = this.classUnderTest().booleanIterator(); Assert.assertTrue(booleanIterator.hasNext()); booleanIterator.next(); Verify.assertThrows(UnsupportedOperationException.class, booleanIterator::remove); } @Override @Test public void iterator_throws_on_invocation_of_remove_before_next() { MutableBooleanIterator booleanIterator = this.classUnderTest().booleanIterator(); Assert.assertTrue(booleanIterator.hasNext()); Verify.assertThrows(UnsupportedOperationException.class, booleanIterator::remove); } @Override @Test public void iterator_throws_on_consecutive_invocation_of_remove() { // Not applicable for Unmodifiable* } }