/*
 * Copyright 2006 Takahiro Nakamura.
 *
 * 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 woolpack.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import junit.framework.TestCase;

public class DelegationCollectionTest extends TestCase {
	private Collection<String> base = new ArrayList<String>();

	private Collection<String> collection = new DelegationCollection<String>(
			base);

	public void testConstructor() {
		try {
			new DelegationCollection<String>(null);
			fail();
		} catch (final NullPointerException expected) {
		}
	}

	public void testNormal() {
		collection.add("value0");
		{
			final Collection<String> tmpMap = new ArrayList<String>();
			tmpMap.add("value1");
			tmpMap.add("value2");
			collection.addAll(tmpMap);
		}

		{
			assertEquals(Arrays.asList(new String[] { "value0", "value1",
					"value2", }), Arrays.asList(collection
					.toArray(new String[0])));
			assertEquals(base.toString(), collection.toString());
			assertEquals(base.hashCode(), collection.hashCode());
			assertTrue(collection.equals(base));
		}

		{
			final Collection<String> expected = new ArrayList<String>();
			expected.add("value0");
			expected.add("value1");
			expected.add("value2");
			assertTrue(expected.containsAll(collection));
			assertTrue(collection.containsAll(expected));

			final Collection<String> tmpCollection = new ArrayList<String>();
			for (final String s : collection) {
				tmpCollection.add(s);
			}
			assertEquals(expected, tmpCollection);

			assertTrue(Arrays.equals(expected.toArray(), collection.toArray()));
		}
		assertFalse(collection.isEmpty());
		assertEquals(3, collection.size());
		assertTrue(collection.contains("value0"));
		assertTrue(collection.contains("value1"));
		assertTrue(collection.contains("value2"));

		collection.remove("value2");

		{
			final Collection<String> expected = new ArrayList<String>();
			expected.add("value0");
			expected.add("value1");
			assertTrue(expected.containsAll(collection));
			assertTrue(collection.containsAll(expected));

			final Collection<String> tmpCollection = new ArrayList<String>();
			for (final String s : collection) {
				tmpCollection.add(s);
			}
			assertEquals(expected, tmpCollection);

			assertTrue(Arrays.equals(expected.toArray(), collection.toArray()));
		}
		assertFalse(collection.isEmpty());
		assertEquals(2, collection.size());
		assertTrue(collection.contains("value0"));
		assertTrue(collection.contains("value1"));
		assertFalse(collection.contains("value2"));

		collection.clear();

		{
			final Collection<String> expected = new ArrayList<String>();
			assertTrue(expected.containsAll(collection));
			assertTrue(collection.containsAll(expected));

			final Collection<String> tmpCollection = new ArrayList<String>();
			for (final String s : collection) {
				tmpCollection.add(s);
			}
			assertEquals(expected, tmpCollection);

			assertTrue(Arrays.equals(expected.toArray(), collection.toArray()));
		}
		assertTrue(collection.isEmpty());
		assertEquals(0, collection.size());
		assertFalse(collection.contains("value0"));
		assertFalse(collection.contains("value1"));
		assertFalse(collection.contains("value2"));
	}

	public void testRemoveAllRetainAll() {
		collection.add("value0");
		{
			final Collection<String> tmpMap = new ArrayList<String>();
			tmpMap.add("value1");
			tmpMap.add("value2");
			collection.addAll(tmpMap);
		}

		{
			final Collection<String> expected = new ArrayList<String>();
			expected.add("value0");
			expected.add("value1");
			expected.add("value2");
			assertTrue(expected.containsAll(collection));
			assertTrue(collection.containsAll(expected));

			final Collection<String> tmpCollection = new ArrayList<String>();
			for (final String s : collection) {
				tmpCollection.add(s);
			}
			assertEquals(expected, tmpCollection);

			assertTrue(Arrays.equals(expected.toArray(), collection.toArray()));
		}
		assertFalse(collection.isEmpty());
		assertEquals(3, collection.size());
		assertTrue(collection.contains("value0"));
		assertTrue(collection.contains("value1"));
		assertTrue(collection.contains("value2"));

		{
			final Collection<String> tmpCollection = new ArrayList<String>();
			tmpCollection.add("value2");
			collection.removeAll(tmpCollection);
		}

		{
			final Collection<String> expected = new ArrayList<String>();
			expected.add("value0");
			expected.add("value1");
			assertTrue(expected.containsAll(collection));
			assertTrue(collection.containsAll(expected));

			final Collection<String> tmpCollection = new ArrayList<String>();
			for (final String s : collection) {
				tmpCollection.add(s);
			}
			assertEquals(expected, tmpCollection);

			assertTrue(Arrays.equals(expected.toArray(), collection.toArray()));
		}
		assertFalse(collection.isEmpty());
		assertEquals(2, collection.size());
		assertTrue(collection.contains("value0"));
		assertTrue(collection.contains("value1"));
		assertFalse(collection.contains("value2"));

		{
			final Collection<String> tmpCollection = new ArrayList<String>();
			tmpCollection.add("value1");
			collection.retainAll(tmpCollection);
		}

		{
			final Collection<String> expected = new ArrayList<String>();
			expected.add("value1");
			assertTrue(expected.containsAll(collection));
			assertTrue(collection.containsAll(expected));

			final Collection<String> tmpCollection = new ArrayList<String>();
			for (final String s : collection) {
				tmpCollection.add(s);
			}
			assertEquals(expected, tmpCollection);

			assertTrue(Arrays.equals(expected.toArray(), collection.toArray()));
		}
		assertFalse(collection.isEmpty());
		assertEquals(1, collection.size());
		assertFalse(collection.contains("value0"));
		assertTrue(collection.contains("value1"));
		assertFalse(collection.contains("value2"));
	}
}
