diff --git a/guava/src/test/java/org/baeldung/guava/ClassToInstanceMapTests.java b/guava/src/test/java/org/baeldung/guava/ClassToInstanceMapTests.java new file mode 100644 index 0000000000..c8db9e573e --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/ClassToInstanceMapTests.java @@ -0,0 +1,70 @@ +package org.baeldung.guava; +import com.google.common.collect.ClassToInstanceMap; +import com.google.common.collect.ImmutableClassToInstanceMap; +import com.google.common.collect.MutableClassToInstanceMap; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ClassToInstanceMapTests { + @Test + public void createEmptyImmutableMap() { + ClassToInstanceMap map = ImmutableClassToInstanceMap.of(); + assertTrue(map.isEmpty()); + } + + @Test + public void createEmptyMutableMap() { + ClassToInstanceMap map = MutableClassToInstanceMap.create(); + assertTrue(map.isEmpty()); + } + + @Test + public void createSingleEntryMap() { + ClassToInstanceMap map = ImmutableClassToInstanceMap.of(Save.class, new Save()); + assertEquals(1, map.size()); + } + + @Test + public void createMapWithBuilder() { + ClassToInstanceMap map = ImmutableClassToInstanceMap.builder() + .put(Save.class, new Save()) + .put(Open.class, new Open()) + .build(); + assertEquals(2, map.size()); + } + + @Test + public void shouldReturnElement() { + ClassToInstanceMap map = ImmutableClassToInstanceMap.of(Save.class, new Save()); + Action action = map.get(Save.class); + assertTrue(action instanceof Save); + + // Use getInstance to avoid casting + Save save = map.getInstance(Save.class); + } + + @Test + public void shouldPutElement() { + ClassToInstanceMap map = MutableClassToInstanceMap.create(); + map.put(Save.class, new Save()); + // Put again to get previous value returned + Action action = map.put(Save.class, new Save()); + assertTrue(action instanceof Save); + + // Use putInstance to avoid casting + Save save = map.putInstance(Save.class, new Save()); + } +} + +abstract class Action { +} + +class Save extends Action { +} + +class Open extends Action { +} + +class Delete extends Action { +} diff --git a/guava21/src/test/java/GauavaStreamsTests.java b/guava21/src/test/java/GuavaStreamsTests.java similarity index 99% rename from guava21/src/test/java/GauavaStreamsTests.java rename to guava21/src/test/java/GuavaStreamsTests.java index 1482d685cf..cc5fcff229 100644 --- a/guava21/src/test/java/GauavaStreamsTests.java +++ b/guava21/src/test/java/GuavaStreamsTests.java @@ -9,7 +9,7 @@ import java.util.stream.IntStream; import java.util.stream.LongStream; import java.util.stream.Stream; -public class GauavaStreamsTests { +public class GuavaStreamsTests { List numbers;