private <M extends Map<Long, String>> void testMapType(GenericType<M> genericType) { Type containerType = genericType.getType(); Class<?> erasedType = getErasedType(containerType); assertThat(factory.accepts(containerType)).isTrue(); assertThat(factory.accepts(erasedType)).isFalse(); assertThat(factory.elementType(containerType)).contains(resolveMapEntryType(Long.class, String.class)); Collector<Map.Entry<Long, String>, ?, M> collector = (Collector<Map.Entry<Long, String>, ?, M>) factory.build(containerType); assertThat(Stream.of(entry(1L, "foo"), entry(2L, "bar"), entry(3L, "baz")).collect(collector)) .isInstanceOf(erasedType) .containsOnly(entry(1L, "foo"), entry(2L, "bar"), entry(3L, "baz")); assertThatThrownBy(() -> Stream.of(entry(1L, "foo"), entry(1L, "bar")).collect(collector)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("Multiple values"); } }
@SuppressWarnings("unchecked") private <M extends Map<Long, String>> void testMapCollector(Class<? extends Map> erasedType, GenericType<M> genericType) { JdbiCollectors registry = dbRule.getJdbi().getConfig(JdbiCollectors.class); assertThat(registry.findElementTypeFor(genericType.getType())) .contains(new GenericType<Map.Entry<Long, String>>(){}.getType()); Collector<Map.Entry<Long, String>, ?, M> collector = (Collector<Map.Entry<Long, String>, ?, M>) registry .findFor(genericType.getType()) .orElseThrow(() -> new IllegalStateException("Missing collector for " + genericType)); M map = Stream.of(entry(1L, "foo"), entry(2L, "bar")).collect(collector); assertThat(map) .isInstanceOf(erasedType) .containsExactly(entry(1L, "foo"), entry(2L, "bar")); }
@Test public void shouldCreateImmutableMapFromMap() { final Map<String, String> mutableMap = new HashMap<>(); final Map<String, String> immutableMap = MapUtil.immutableMap(mutableMap); assertThat(immutableMap).isInstanceOf(ImmutableMap.class); }
@Test void immutableMapCollectorShouldReturnEmptyImmutableMapWhenEmptyStream() { String[] data = {}; Map<String, Integer> actual = Arrays.stream(data) .collect(Guavate.toImmutableMap(x -> x.toUpperCase(Locale.US), String::length)); assertThat(actual).isInstanceOf(ImmutableMap.class); assertThat(actual).isEmpty(); }
@Test public void immutableMapCollectorShouldReturnEmptyImmutableMapWhenEmptyStream() { String[] data = {}; Map<String, Integer> actual = Arrays.stream(data) .collect(Guavate.toImmutableMap(x -> x.toUpperCase(Locale.US), String::length)); assertThat(actual).isInstanceOf(ImmutableMap.class); assertThat(actual).isEmpty(); }
public void shouldNotRevertToJsonObjectWhenCloning() { JsonObject jsonObject = new MapBasedJsonObject(); for(int i=0;i<1000;i++) { jsonObject.put("key_"+i, i); } assertThat(jsonObject.deepClone()).isInstanceOf(MapBasedJsonObject.class); assertThat(jsonObject.immutableClone()).isInstanceOf(MapBasedJsonObject.class); } }
@Test void immutableMapCollectorShouldReturnAppliedImmutableMapWhenOneElementStream() { String[] data = {"a"}; Map<String, Integer> actual = Arrays.stream(data) .collect(Guavate.toImmutableMap(x -> x.toUpperCase(Locale.US), String::length)); assertThat(actual).isInstanceOf(ImmutableMap.class); assertThat(actual).containsExactly(entry("A", 1)); }
@Test public void immutableMapCollectorShouldReturnAppliedImmutableMapWhenOneElementStream() { String[] data = {"a"}; Map<String, Integer> actual = Arrays.stream(data) .collect(Guavate.toImmutableMap(x -> x.toUpperCase(Locale.US), String::length)); assertThat(actual).isInstanceOf(ImmutableMap.class); assertThat(actual).containsExactly(entry("A", 1)); }
@Test void Should_Return_Any_Instance_When_Value_Is_Null() { // given final SortedMap<String, String> value = null; final Class<SortedMap> type = SortedMap.class; final SortedMapValueChanger valueChanger = new SortedMapValueChanger(); // when final SortedMap<?, ?> result = valueChanger.increaseValue(value, type); // then assertThat(result).isInstanceOf(type); } }
@Test void Should_Return_Any_Instance_When_Value_Is_Null() { // given final Map<String, String> value = null; final Class<Map> type = Map.class; final MapValueChanger valueChanger = new MapValueChanger(); // when final Map<?, ?> result = valueChanger.increaseValue(value, type); // then assertThat(result).isInstanceOf(type); } }
@Test void Should_Return_Any_Instance_When_Value_Is_Null() { // given final Hashtable<String, String> value = null; final Class<Hashtable> type = Hashtable.class; final HashtableValueChanger valueChanger = new HashtableValueChanger(); // when final Hashtable<?, ?> result = valueChanger.increaseValue(value, type); // then assertThat(result).isInstanceOf(type); } }
@Test void Should_Return_Any_Instance_When_Value_Is_Null() { // given final HashMap<String, String> value = null; final Class<HashMap> type = HashMap.class; final HashMapValueChanger valueChanger = new HashMapValueChanger(); // when final HashMap<?, ?> result = valueChanger.increaseValue(value, type); // then assertThat(result).isInstanceOf(type); } }
@Test void immutableMapCollectorShouldReturnAppliedImmutableMapWhen3ElementsStream() { String[] data = {"a", "bb", "ccc"}; Map<String, Integer> actual = Arrays.stream(data) .collect(Guavate.toImmutableMap(x -> x.toUpperCase(Locale.US), String::length)); assertThat(actual).isInstanceOf(ImmutableMap.class); assertThat(actual).containsExactly(entry("A", 1), entry("BB", 2), entry("CCC", 3)); }
@Test void Should_Return_Any_Instance_When_Value_Is_Null() { // given final TreeMap<String, String> value = null; final Class<TreeMap> type = TreeMap.class; final TreeMapValueChanger valueChanger = new TreeMapValueChanger(); // when final TreeMap<?, ?> result = valueChanger.increaseValue(value, type); // then assertThat(result).isInstanceOf(type); } }
@Test void Should_Return_Any_Instance_When_Value_Is_Null() { // given final LinkedHashMap<String, String> value = null; final Class<LinkedHashMap> type = LinkedHashMap.class; final LinkedHashMapValueChanger valueChanger = new LinkedHashMapValueChanger(); // when final LinkedHashMap<?, ?> result = valueChanger.increaseValue(value, type); // then assertThat(result).isInstanceOf(type); } }
@Test public void immutableMapCollectorShouldReturnAppliedImmutableMapWhen3ElementsStream() { String[] data = {"a", "bb", "ccc"}; Map<String, Integer> actual = Arrays.stream(data) .collect(Guavate.toImmutableMap(x -> x.toUpperCase(Locale.US), String::length)); assertThat(actual).isInstanceOf(ImmutableMap.class); assertThat(actual).containsExactly(entry("A", 1), entry("BB", 2), entry("CCC", 3)); }
doReturn(allInstancesForVip).when(discoveryClientMock).getInstancesByVipAddress(vip, false); assertThat(serviceSpy.vipRoundRobinCounterMap).isInstanceOf(ConcurrentMap.class); assertThat(serviceSpy.vipRoundRobinCounterMap.containsKey(vip)).isFalse(); ConcurrentMap<String, AtomicInteger> roundRobinCounterMapSpy = spy(serviceSpy.vipRoundRobinCounterMap);
@SuppressWarnings("unchecked") private <M extends Map<Long, String>> void testMapCollector(Class<? extends Map> erasedType, GenericType<M> genericType) { JdbiCollectors registry = dbRule.getJdbi().getConfig(JdbiCollectors.class); assertThat(registry.findElementTypeFor(genericType.getType())) .contains(new GenericType<Map.Entry<Long, String>>(){}.getType()); Collector<Map.Entry<Long, String>, ?, M> collector = (Collector<Map.Entry<Long, String>, ?, M>) registry .findFor(genericType.getType()) .orElseThrow(() -> new IllegalStateException("Missing collector for " + genericType)); M map = Stream.of(entry(1L, "foo"), entry(2L, "bar")).collect(collector); assertThat(map) .isInstanceOf(erasedType) .containsExactly(entry(1L, "foo"), entry(2L, "bar")); }