From 5202fd7a499b7fcdb33eafc909cce62dbcdec3e4 Mon Sep 17 00:00:00 2001 From: DOHA Date: Thu, 30 Oct 2014 00:14:10 +0200 Subject: [PATCH] Add class GuavaCollectionTypesTest --- .../guava/GuavaCollectionTypesTest.java | 234 ++++++++++++++++++ 1 file changed, 234 insertions(+) create mode 100644 guava/src/test/java/org/baeldung/guava/GuavaCollectionTypesTest.java diff --git a/guava/src/test/java/org/baeldung/guava/GuavaCollectionTypesTest.java b/guava/src/test/java/org/baeldung/guava/GuavaCollectionTypesTest.java new file mode 100644 index 0000000000..31ed0d0661 --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/GuavaCollectionTypesTest.java @@ -0,0 +1,234 @@ +package org.baeldung.guava; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Test; + +import com.google.common.base.Function; +import com.google.common.base.Joiner; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.BiMap; +import com.google.common.collect.ClassToInstanceMap; +import com.google.common.collect.ContiguousSet; +import com.google.common.collect.DiscreteDomain; +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.HashBiMap; +import com.google.common.collect.HashMultiset; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSortedMap; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; +import com.google.common.collect.Multiset; +import com.google.common.collect.Multisets; +import com.google.common.collect.MutableClassToInstanceMap; +import com.google.common.collect.Ordering; +import com.google.common.collect.Range; +import com.google.common.collect.RangeSet; +import com.google.common.collect.Sets; +import com.google.common.collect.Table; +import com.google.common.collect.Tables; +import com.google.common.collect.TreeRangeSet; + +public class GuavaCollectionTypesTest { + + @Test + public void whenReverseList_thenReversed() { + final List names = Lists.newArrayList("John", "Adam", "Jane"); + names.add("Tom"); + assertEquals(4, names.size()); + + final List reversed = Lists.reverse(names); + assertThat(reversed, contains("Tom", "Jane", "Adam", "John")); + } + + @Test + public void whenCreateCharacterListFromString_thenCreated() { + final List chars = Lists.charactersOf("John"); + + assertEquals(4, chars.size()); + assertThat(chars, contains('J', 'o', 'h', 'n')); + } + + @Test + public void whenCalculateUnion_thenCorrect() { + final Set first = ImmutableSet.of('a', 'b', 'c'); + final Set second = ImmutableSet.of('b', 'c', 'd'); + + final Set union = Sets.union(first, second); + assertThat(union, containsInAnyOrder('a', 'b', 'c', 'd')); + } + + @Test + public void whenCalculateSetsProduct_thenCorrect() { + final Set first = ImmutableSet.of('a', 'b'); + final Set second = ImmutableSet.of('c', 'd'); + final Set> result = Sets.cartesianProduct(ImmutableList.of(first, second)); + + final Function, String> func = new Function, String>() { + @Override + public final String apply(final List input) { + return Joiner.on(" ").join(input); + } + }; + + final Iterable joined = Iterables.transform(result, func); + assertThat(joined, containsInAnyOrder("a c", "a d", "b c", "b d")); + } + + @Test + public void whenCreateRangeOfIntegersSet_thenCreated() { + final int start = 10; + final int end = 30; + final ContiguousSet set = ContiguousSet.create(Range.closed(start, end), DiscreteDomain.integers()); + + assertEquals(21, set.size()); + assertEquals(10, set.first().intValue()); + assertEquals(30, set.last().intValue()); + } + + @Test + public void whenCreateRangeSet_thenCreated() { + final RangeSet rangeSet = TreeRangeSet.create(); + rangeSet.add(Range.closed(1, 10)); + rangeSet.add(Range.closed(12, 15)); + + assertEquals(2, rangeSet.asRanges().size()); + + rangeSet.add(Range.closed(10, 12)); + assertTrue(rangeSet.encloses(Range.closed(1, 15))); + assertEquals(1, rangeSet.asRanges().size()); + } + + @Test + public void whenInsertDuplicatesInMultiSet_thenInserted() { + final Multiset names = HashMultiset.create(); + names.add("John"); + names.add("Adam", 3); + names.add("John"); + + assertEquals(2, names.count("John")); + names.remove("John"); + assertEquals(1, names.count("John")); + + assertEquals(3, names.count("Adam")); + names.remove("Adam", 2); + assertEquals(1, names.count("Adam")); + } + + @Test + public void whenGetTopUsingMultiSet_thenCorrect() { + final Multiset names = HashMultiset.create(); + names.add("John"); + names.add("Adam", 5); + names.add("Jane"); + names.add("Tom", 2); + + final Set sorted = Multisets.copyHighestCountFirst(names).elementSet(); + final List topTwo = Lists.newArrayList(sorted).subList(0, 2); + assertEquals(2, topTwo.size()); + assertEquals("Adam", topTwo.get(0)); + assertEquals("Tom", topTwo.get(1)); + } + + @Test + public void whenCreateMap_thenCreated() { + final Map salary = ImmutableMap. builder().put("John", 1000).put("Jane", 1500).put("Adam", 2000).put("Tom", 2000).build(); + + assertEquals(1000, salary.get("John").intValue()); + assertEquals(2000, salary.get("Tom").intValue()); + } + + @Test + public void whenUseSortedMap_thenKeysAreSorted() { + final ImmutableSortedMap salary = new ImmutableSortedMap.Builder(Ordering.natural()).put("John", 1000).put("Jane", 1500).put("Adam", 2000).put("Tom", 2000).build(); + + assertEquals("Adam", salary.firstKey()); + assertEquals(2000, salary.lastEntry().getValue().intValue()); + } + + @Test + public void whenCreateBiMap_thenCreated() { + final BiMap words = HashBiMap.create(); + words.put("First", 1); + words.put("Second", 2); + words.put("Third", 3); + + assertEquals(2, words.get("Second").intValue()); + assertEquals("Third", words.inverse().get(3)); + } + + @Test + public void whenCreateMultimap_thenCreated() { + final Multimap multimap = ArrayListMultimap.create(); + multimap.put("fruit", "apple"); + multimap.put("fruit", "banana"); + multimap.put("pet", "cat"); + multimap.put("pet", "dog"); + + assertThat(multimap.get("fruit"), containsInAnyOrder("apple", "banana")); + assertThat(multimap.get("pet"), containsInAnyOrder("cat", "dog")); + } + + @Test + public void whenGroupListUsingMultimap_thenGrouped() { + final List names = Lists.newArrayList("John", "Adam", "Tom"); + final Function function = new Function() { + @Override + public final Integer apply(final String input) { + return input.length(); + } + }; + final Multimap groups = Multimaps.index(names, function); + + assertThat(groups.get(3), containsInAnyOrder("Tom")); + assertThat(groups.get(4), containsInAnyOrder("John", "Adam")); + } + + @Test + public void whenCreateTable_thenCreated() { + final Table distance = HashBasedTable.create(); + distance.put("London", "Paris", 340); + distance.put("New York", "Los Angeles", 3940); + distance.put("London", "New York", 5576); + + assertEquals(3940, distance.get("New York", "Los Angeles").intValue()); + assertThat(distance.columnKeySet(), containsInAnyOrder("Paris", "New York", "Los Angeles")); + assertThat(distance.rowKeySet(), containsInAnyOrder("London", "New York")); + } + + @Test + public void whenTransposeTable_thenCorrect() { + final Table distance = HashBasedTable.create(); + distance.put("London", "Paris", 340); + distance.put("New York", "Los Angeles", 3940); + distance.put("London", "New York", 5576); + + final Table transposed = Tables.transpose(distance); + assertThat(transposed.rowKeySet(), containsInAnyOrder("Paris", "New York", "Los Angeles")); + assertThat(transposed.columnKeySet(), containsInAnyOrder("London", "New York")); + } + + @Test + public void whenCreateClassToInstanceMap_thenCreated() { + final ClassToInstanceMap numbers = MutableClassToInstanceMap.create(); + numbers.putInstance(Integer.class, 1); + numbers.putInstance(Double.class, 1.5); + + assertEquals(1, numbers.get(Integer.class)); + assertEquals(1.5, numbers.get(Double.class)); + } + + +}