From d06f8f87b22e2817dfe697bffc2344c027edd22b Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 31 May 2014 17:56:15 +0300 Subject: [PATCH] conversion simple testing work --- .../JavaCollectionConversionUnitTest.java | 66 ++++++++++++++++++- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/collections/JavaCollectionConversionUnitTest.java b/core-java/src/test/java/org/baeldung/java/collections/JavaCollectionConversionUnitTest.java index 5c8e4adf58..ec3385dfc1 100644 --- a/core-java/src/test/java/org/baeldung/java/collections/JavaCollectionConversionUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/collections/JavaCollectionConversionUnitTest.java @@ -2,17 +2,24 @@ package org.baeldung.java.collections; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.ArrayUtils; import org.junit.Test; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import com.google.common.primitives.Ints; @SuppressWarnings("unused") public class JavaCollectionConversionUnitTest { + // List -> array; array -> List + @Test public final void givenUsingCoreJava_whenArrayConvertedToList_thenCorrect() { final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; @@ -20,7 +27,7 @@ public class JavaCollectionConversionUnitTest { } @Test - public void givenUsingCoreJava_whenListConvertedToArray_thenCorrect() { + public final void givenUsingCoreJava_whenListConvertedToArray_thenCorrect() { final List sourceList = Lists. newArrayList(0, 1, 2, 3, 4, 5); final Integer[] targetArray = sourceList.toArray(new Integer[sourceList.size()]); } @@ -32,16 +39,69 @@ public class JavaCollectionConversionUnitTest { } @Test - public void givenUsingGuava_whenLIistConvertedToArray_thenCorrect() { + public final void givenUsingGuava_whenListConvertedToArray_thenCorrect() { final List sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5); final int[] targetArray = Ints.toArray(sourceList); } @Test - public void givenUsingCommonsCollections_whenArrayConvertedToList_thenCorrect() { + public final void givenUsingCommonsCollections_whenArrayConvertedToList_thenCorrect() { final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; final List targetList = new ArrayList<>(6); CollectionUtils.addAll(targetList, sourceArray); } + // Set -> array; array -> Set + + @Test + public final void givenUsingCoreJavaV1_whenArrayConvertedToSet_thenCorrect() { + final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Set targetSet = new HashSet(Arrays.asList(sourceArray)); + } + + @Test + public final void givenUsingCoreJavaV2_whenArrayConvertedToSet_thenCorrect() { + final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Set targetSet = new HashSet(); + Collections.addAll(targetSet, sourceArray); + } + + @Test + public final void givenUsingCoreJava_whenSetConvertedToArray_thenCorrect() { + final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); + final Integer[] targetArray = sourceSet.toArray(new Integer[sourceSet.size()]); + } + + @Test + public final void givenUsingGuava_whenArrayConvertedToSet_thenCorrect() { + final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Set targetSet = Sets.newHashSet(sourceArray); + } + + @Test + public final void givenUsingGuava_whenSetConvertedToArray_thenCorrect() { + final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); + final int[] targetArray = Ints.toArray(sourceSet); + } + + @Test + public final void givenUsingCommonsCollections_whenArrayConvertedToSet_thenCorrect() { + final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Set targetSet = new HashSet<>(6); + CollectionUtils.addAll(targetSet, sourceArray); + } + + @Test + public final void givenUsingCommonsCollections_whenSetConvertedToArray_thenCorrect() { + final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); + final Integer[] targetArray = sourceSet.toArray(new Integer[sourceSet.size()]); + } + + @Test + public final void givenUsingCommonsCollections_whenSetConvertedToArrayOfPrimitives_thenCorrect() { + final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); + final Integer[] targetArray = sourceSet.toArray(new Integer[sourceSet.size()]); + final int[] primitiveTargetArray = ArrayUtils.toPrimitive(targetArray); + } + }