|
| 1 | +package com.baeldung.convertcollectiontoarraylist; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.Comparator; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.Iterator; |
| 8 | +import static java.util.stream.Collectors.toCollection; |
| 9 | +import org.junit.BeforeClass; |
| 10 | +import org.junit.Test; |
| 11 | +import static org.junit.Assert.*; |
| 12 | + |
| 13 | +/** |
| 14 | + * |
| 15 | + * @author chris |
| 16 | + */ |
| 17 | +public class FooUnitTest { |
| 18 | + private static Collection<Foo> srcCollection = new HashSet<>(); |
| 19 | + |
| 20 | + public FooUnitTest() { |
| 21 | + } |
| 22 | + |
| 23 | + @BeforeClass |
| 24 | + public static void setUpClass() { |
| 25 | + int i = 0; |
| 26 | + Foo john = new Foo(i++, "John", null); |
| 27 | + Foo mary = new Foo(i++, "Mary", null); |
| 28 | + Foo sam = new Foo(i++, "Sam", john); |
| 29 | + Foo alice = new Foo(i++, "Alice", john); |
| 30 | + Foo buffy = new Foo(i++, "Buffy", sam); |
| 31 | + srcCollection.add(john); |
| 32 | + srcCollection.add(mary); |
| 33 | + srcCollection.add(sam); |
| 34 | + srcCollection.add(alice); |
| 35 | + srcCollection.add(buffy); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Section 3. Using the ArrayList Constructor |
| 40 | + */ |
| 41 | + @Test |
| 42 | + public void testConstructor() { |
| 43 | + ArrayList<Foo> newList = new ArrayList<>(srcCollection); |
| 44 | + verifyShallowCopy(srcCollection, newList); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Section 4. Using the Streams API |
| 49 | + */ |
| 50 | + @Test |
| 51 | + public void testStream() { |
| 52 | + ArrayList<Foo> newList = srcCollection.stream().collect(toCollection(ArrayList::new)); |
| 53 | + verifyShallowCopy(srcCollection, newList); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Section 5. Deep Copy |
| 58 | + */ |
| 59 | + @Test |
| 60 | + public void testStreamDeepCopy() { |
| 61 | + ArrayList<Foo> newList = srcCollection.stream() |
| 62 | + .map(foo -> foo.deepCopy()) |
| 63 | + .collect(toCollection(ArrayList::new)); |
| 64 | + verifyDeepCopy(srcCollection, newList); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Section 6. Controlling the List Order |
| 69 | + */ |
| 70 | + @Test |
| 71 | + public void testSortOrder() { |
| 72 | + assertFalse("Oops: source collection is already sorted!", isSorted(srcCollection)); |
| 73 | + ArrayList<Foo> newList = srcCollection.stream() |
| 74 | + .map(foo -> foo.deepCopy()) |
| 75 | + .sorted(Comparator.comparing(Foo::getName)) |
| 76 | + .collect(toCollection(ArrayList::new)); |
| 77 | + assertTrue("ArrayList is not sorted by name", isSorted(newList)); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Verify that the contents of the two collections are the same |
| 82 | + * @param a |
| 83 | + * @param b |
| 84 | + */ |
| 85 | + private void verifyShallowCopy(Collection a, Collection b) { |
| 86 | + assertEquals("Collections have different lengths", a.size(), b.size()); |
| 87 | + Iterator<Foo> iterA = a.iterator(); |
| 88 | + Iterator<Foo> iterB = b.iterator(); |
| 89 | + while (iterA.hasNext()) { |
| 90 | + // use '==' to test instance identity |
| 91 | + assertTrue("Foo instances differ!", iterA.next() == iterB.next()); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Verify that the contents of the two collections are the same |
| 97 | + * @param a |
| 98 | + * @param b |
| 99 | + */ |
| 100 | + private void verifyDeepCopy(Collection a, Collection b) { |
| 101 | + assertEquals("Collections have different lengths", a.size(), b.size()); |
| 102 | + Iterator<Foo> iterA = a.iterator(); |
| 103 | + Iterator<Foo> iterB = b.iterator(); |
| 104 | + while (iterA.hasNext()) { |
| 105 | + Foo nextA = iterA.next(); |
| 106 | + Foo nextB = iterB.next(); |
| 107 | + // should not be same instance |
| 108 | + assertFalse("Foo instances are the same!", nextA == nextB); |
| 109 | + // but should have same content |
| 110 | + assertFalse("Foo instances have different content!", fooDiff(nextA, nextB)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Return true if the contents of a and b differ. Test parent recursively |
| 116 | + * @param a |
| 117 | + * @param b |
| 118 | + * @return False if the two items are the same |
| 119 | + */ |
| 120 | + private boolean fooDiff(Foo a, Foo b) { |
| 121 | + if (a != null && b != null) { |
| 122 | + return a.getId() != b.getId() |
| 123 | + || !a.getName().equals(b.getName()) |
| 124 | + || fooDiff(a.getParent(), b.getParent()); |
| 125 | + } |
| 126 | + return !(a == null && b == null); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @param c collection of Foo |
| 131 | + * @return true if the collection is sorted by name |
| 132 | + */ |
| 133 | + private boolean isSorted(Collection<Foo> c) { |
| 134 | + String prevName = null; |
| 135 | + for (Foo foo : c) { |
| 136 | + if (prevName == null || foo.getName().compareTo(prevName) > 0) { |
| 137 | + prevName = foo.getName(); |
| 138 | + } else { |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + return true; |
| 143 | + } |
| 144 | +} |
0 commit comments