All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.inftec.ju.util.JuCollectionUtilsTest Maven / Gradle / Ivy

package ch.inftec.ju.util;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.junit.Test;

import ch.inftec.ju.util.function.Function;

public class JuCollectionUtilsTest {
	private List list12 = Arrays.asList(1, 2);
	private List list12_copy = Arrays.asList(1, 2);
	private List list23 = Arrays.asList(2, 3);
	private List list123 = Arrays.asList(1, 2, 3);
	private List list21 = Arrays.asList(2, 1);
	private List list34 = Arrays.asList(3, 4);

	@Test
	public void iterator_asList() {
		final List l = JuCollectionUtils.arrayList("A", "B");
		
		Iterable iterable = new Iterable() {
			@Override
			public Iterator iterator() {
				return l.iterator();
			}
		};
		
		List lRes = JuCollectionUtils.asList(iterable);
		TestUtils.assertCollectionEquals(lRes, "A", "B");
	}

	@Test
	public void nullAndCollection_collectionEquals_returnsFalse() {
		assertFalse(JuCollectionUtils.collectionEquals(list12, null));
	}

	@Test
	public void nullAndNull_collectionEquals_returnsTrue() {
		assertTrue(JuCollectionUtils.collectionEquals(null, null));
	}

	@Test
	public void nullAndEmpty_collectionEquals_returnsFalse() {
		assertFalse(JuCollectionUtils.collectionEquals(null, Collections.emptyList()));
	}

	@Test
	public void sameCollection_collectionEquals_returnsTrue() {
		assertTrue(JuCollectionUtils.collectionEquals(list12, list12));
	}

	@Test
	public void equalCollection_collectionEquals_returnsTrue() {
		assertNotSame(list12, list12_copy);

		assertTrue(JuCollectionUtils.collectionEquals(list12, list12_copy));
	}

	@Test
	public void nonEqualCollectionDifferentLength_collectionEquals_returnsFalse() {
		assertFalse(JuCollectionUtils.collectionEquals(list12, list123));
	}

	@Test
	public void nonEqualCollectionSameLength_collectionEquals_returnsFalse() {
		assertFalse(JuCollectionUtils.collectionEquals(list12, list23));
	}

	@Test
	public void collectionWithSameItemsDifferentOrder_collectionEquals_returnsFalse() {
		assertFalse(JuCollectionUtils.collectionEquals(list12, list21));
	}

	@Test
	public void collectionWithSameItemsDifferentOrder_collectionEqualsIgnoreOrder_returnsTrue() {
		assertTrue(JuCollectionUtils.collectionEqualsIgnoreOrder(list12, list21));
	}

	@Test
	public void nullParameter_intersection_returnsEmptyList() {
		assertTrue(JuCollectionUtils.intersection(null, list12).isEmpty());
	}

	@Test
	public void sameCollection_intersection_returnsNewList() {
		List intersection = JuCollectionUtils.intersection(list12, list12);

		assertNotSame(list12, intersection);
		assertTrue(JuCollectionUtils.collectionEquals(list12, intersection));
	}

	@Test
	public void nonOverlappingCollections_intersection_returnsEmptyList() {
		assertTrue(JuCollectionUtils.intersection(list12, list34).isEmpty());
	}

	@Test
	public void overlappingCollections_intersection_returnsOverlappingItems() {
		List intersection = JuCollectionUtils.intersection(list12, list123);

		assertNotSame(list12, intersection);
		assertTrue(JuCollectionUtils.collectionEquals(list12, intersection));
	}

	@Test
	public void iteratorTransformed_transformsElements() {
		List list = JuCollectionUtils.asArrayList(1, 2);

		Iterator transformedIterator = JuCollectionUtils.iteratorTransformed(list.iterator(), getMultiplyByTwoFunction());

		assertEquals(Integer.valueOf(2), transformedIterator.next());
		assertEquals(Integer.valueOf(4), transformedIterator.next());
		assertFalse(transformedIterator.hasNext());
	}

	private Function getMultiplyByTwoFunction() {
		return new Function() {
			@Override
			public Integer apply(Integer integer) {
				return integer * 2;
			}
		};
	}

	@Test
	public void iterableTransformed_transformsElements() {
		List list = JuCollectionUtils.asArrayList(1);

		Iterable transformedIterable = JuCollectionUtils.iterableTransformed(list, getMultiplyByTwoFunction());

		Iterator transformedIterator = transformedIterable.iterator();

		assertEquals(Integer.valueOf(2), transformedIterator.next());
		assertFalse(transformedIterator.hasNext());
	}
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy