Skip to content

Commit

Permalink
Use guava for tests
Browse files Browse the repository at this point in the history
Using Guava just for tests simplifies some code and allows us to remove some helpers.
  • Loading branch information
kilink committed Mar 2, 2024
1 parent f09ee17 commit caff66a
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.netflix.archaius.api;

import com.google.common.reflect.TypeParameter;
import com.google.common.reflect.TypeToken;
import org.junit.jupiter.api.Test;

import java.lang.reflect.ParameterizedType;
Expand All @@ -13,6 +15,15 @@
import static org.junit.jupiter.api.Assertions.assertNotSame;

public class ArchaiusTypeTest {

private static final Type listOfString = new TypeToken<List<String>>() {}.getType();
private static final Type setOfLong = new TypeToken<Set<Long>>() {}.getType();
private static final Type mapOfIntToCharSequence = new TypeToken<Map<Integer, CharSequence>>() {}.getType();

private static <T> TypeToken<List<T>> listOfType(Class<T> klazz) {
return new TypeToken<List<T>>() {}.where(new TypeParameter<T>() {}, klazz);
}

@Test
public void testEquals() {
ParameterizedType archaiusType = ArchaiusType.forListOf(String.class);
Expand All @@ -21,6 +32,9 @@ public void testEquals() {
assertEquals(archaiusType, ArchaiusType.forListOf(String.class));
assertNotEquals(archaiusType, ArchaiusType.forListOf(Integer.class));
assertNotEquals(archaiusType, setOfLong);

// Test against Guava's ParameterizedType implementation
assertEquals(archaiusType, listOfType(String.class).getType());
}

@Test
Expand All @@ -29,6 +43,9 @@ public void testHashCode() {
assertEquals(ArchaiusType.forListOf(String.class).hashCode(), ArchaiusType.forListOf(String.class).hashCode());
assertEquals(setOfLong.hashCode(), ArchaiusType.forSetOf(Long.class).hashCode());
assertEquals(ArchaiusType.forMapOf(Integer.class, CharSequence.class).hashCode(), mapOfIntToCharSequence.hashCode());

// Test against Guava's ParameterizedType implementation
assertEquals(ArchaiusType.forListOf(String.class).hashCode(), listOfType(String.class).getType().hashCode());
}

@Test
Expand All @@ -53,21 +70,4 @@ public void testGetTypeParameters() {
assertEquals(1, typeArguments.length);
assertEquals(Long.class, typeArguments[0]);
}

private static List<String> listOfString() { throw new AssertionError(); }
private static Set<Long> setOfLong() { throw new AssertionError(); }
private static Map<Integer, CharSequence> mapOfIntToCharSequence() { throw new AssertionError(); }
private static final ParameterizedType listOfString;
private static final ParameterizedType setOfLong;
private static final ParameterizedType mapOfIntToCharSequence;

static {
try {
listOfString = (ParameterizedType) ArchaiusTypeTest.class.getDeclaredMethod("listOfString").getGenericReturnType();
setOfLong = (ParameterizedType) ArchaiusTypeTest.class.getDeclaredMethod("setOfLong").getGenericReturnType();
mapOfIntToCharSequence = (ParameterizedType) ArchaiusTypeTest.class.getDeclaredMethod("mapOfIntToCharSequence").getGenericReturnType();
} catch (NoSuchMethodException exc) {
throw new AssertionError("Method not found", exc);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.netflix.archaius;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -43,6 +42,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.google.common.reflect.TypeToken;
import com.netflix.archaius.api.Decoder;
import com.netflix.archaius.api.TypeConverter;
import com.netflix.archaius.converters.ArrayTypeConverterFactory;
Expand All @@ -58,30 +58,10 @@


public class DefaultDecoderTest {
@SuppressWarnings("unused") // accessed via reflection
private static Collection<Long> collectionOfLong;
@SuppressWarnings("unused") // accessed via reflection
private static List<Integer> listOfInteger;
@SuppressWarnings("unused") // accessed via reflection
private static Set<Long> setOfLong;
@SuppressWarnings("unused") // accessed via reflection
private static Map<String, Integer> mapOfStringToInteger;

private static final ParameterizedType collectionOfLongType;
private static final ParameterizedType listOfIntegerType;
private static final ParameterizedType setOfLongType;
private static final ParameterizedType mapofStringToIntegerType;

static {
try {
collectionOfLongType = (ParameterizedType) DefaultDecoderTest.class.getDeclaredField("collectionOfLong").getGenericType();
listOfIntegerType = (ParameterizedType) DefaultDecoderTest.class.getDeclaredField("listOfInteger").getGenericType();
setOfLongType = (ParameterizedType) DefaultDecoderTest.class.getDeclaredField("setOfLong").getGenericType();
mapofStringToIntegerType = (ParameterizedType) DefaultDecoderTest.class.getDeclaredField("mapOfStringToInteger").getGenericType();
} catch (NoSuchFieldException exc) {
throw new AssertionError("listOfString field not found", exc);
}
}
private static final Type collectionOfLongType = new TypeToken<Collection<Long>>() {}.getType();
private static final Type listOfIntegerType = new TypeToken<List<Integer>>() {}.getType();
private static final Type setOfLongType = new TypeToken<Set<Long>>() {}.getType();
private static final Type mapofStringToIntegerType = new TypeToken<Map<String, Integer>>() {}.getType();

@Test
public void testJavaNumbers() {
Expand Down
37 changes: 0 additions & 37 deletions archaius2-core/src/test/java/com/netflix/archaius/TestUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Properties;
import java.util.concurrent.Callable;

import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.netflix.archaius.api.Config;
import com.netflix.archaius.api.config.SettableConfig;
import com.netflix.archaius.config.polling.ManualPollingStrategy;
Expand All @@ -37,8 +39,6 @@
import com.netflix.archaius.visitor.PrintStreamVisitor;
import org.junit.jupiter.api.Test;

import static com.netflix.archaius.TestUtils.set;
import static com.netflix.archaius.TestUtils.size;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -151,18 +151,18 @@ public void getKeysTest() throws ConfigException {
composite.addConfig("b", MapConfig.builder().put("b1", "A").put("b2", "B").build());

iter = composite.getKeys();
assertEquals(set("b1", "b2"), set(iter));
assertEquals(Sets.newHashSet("b1", "b2"), Sets.newHashSet(iter));

composite.addConfig("c", EmptyConfig.INSTANCE);

iter = composite.getKeys();
assertEquals(set("b1", "b2"), set(iter));
assertEquals(Sets.newHashSet("b1", "b2"), Sets.newHashSet(iter));

composite.addConfig("d", MapConfig.builder().put("d1", "A").put("d2", "B").build());
composite.addConfig("e", MapConfig.builder().put("e1", "A").put("e2", "B").build());

iter = composite.getKeys();
assertEquals(set("b1", "b2", "d1", "d2", "e1", "e2"), set(iter));
assertEquals(Sets.newHashSet("b1", "b2", "d1", "d2", "e1", "e2"), Sets.newHashSet(iter));
}

@Test
Expand All @@ -189,8 +189,8 @@ public void testKeysIterable() throws ConfigException {

Iterable<String> keys = composite.keys();

assertEquals(4, size(keys));
assertEquals(set("d1", "d2", "e1", "e2"), set(keys));
assertEquals(4, Iterables.size(keys));
assertEquals(Sets.newHashSet("d1", "d2", "e1", "e2"), Sets.newHashSet(keys));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.netflix.archaius.config;

import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.netflix.archaius.Layers;
import com.netflix.archaius.api.Config;
import com.netflix.archaius.api.ConfigListener;
Expand All @@ -16,13 +18,12 @@
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Callable;

import static com.netflix.archaius.TestUtils.set;
import static com.netflix.archaius.TestUtils.size;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -41,7 +42,8 @@ public void validateApiOnEmptyConfig() {

assertFalse(config.getProperty("propname").isPresent());
assertNull(config.getRawProperty("propname"));


@SuppressWarnings("unchecked")
LayeredConfig.LayeredVisitor<String> visitor = Mockito.mock(LayeredConfig.LayeredVisitor.class);
config.accept(visitor);
Mockito.verify(visitor, Mockito.never()).visitConfig(any(), any());
Expand Down Expand Up @@ -227,8 +229,8 @@ public void testKeysIterable() {
config.addConfig(Layers.LIBRARY, libConfig);

Iterable<String> keys = config.keys();
assertEquals(1, size(keys));
assertEquals(set("propname"), set(keys));
assertEquals(1, Iterables.size(keys));
assertEquals(Collections.singleton("propname"), Sets.newHashSet(keys));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.netflix.archaius.config;

import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.netflix.archaius.api.config.SettableConfig;
import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.Properties;

import static com.netflix.archaius.TestUtils.set;
import static com.netflix.archaius.TestUtils.size;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -25,18 +25,18 @@ public void testGetKeys() {
config.setProperty("prop2", "value2");
config.setProperty("prop3", "value3");

assertEquals(set("prop1", "prop2", "prop3"), set(config.getKeys()));
assertEquals(Sets.newHashSet("prop1", "prop2", "prop3"), Sets.newHashSet(config.getKeys()));

config.clearProperty("prop3");
assertEquals(set("prop1", "prop2"), set(config.getKeys()));
assertEquals(Sets.newHashSet("prop1", "prop2"), Sets.newHashSet(config.getKeys()));

config.setProperties(MapConfig.builder().put("prop4", "value4").build());
assertEquals(set("prop1", "prop2", "prop4"), set(config.getKeys()));
assertEquals(Sets.newHashSet("prop1", "prop2", "prop4"), Sets.newHashSet(config.getKeys()));

Properties props = new Properties();
props.put("prop5", "value5");
config.setProperties(props);
assertEquals(set("prop1", "prop2", "prop4", "prop5"), set(config.getKeys()));
assertEquals(Sets.newHashSet("prop1", "prop2", "prop4", "prop5"), Sets.newHashSet(config.getKeys()));
}

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -68,24 +68,24 @@ public void testGetKeysIteratorRemoveThrows() {
public void testKeysIterable() {
SettableConfig config = new DefaultSettableConfig();

assertEquals(0, size(config.keys()));
assertEquals(0, Iterables.size(config.keys()));

config.setProperty("prop1", "value1");
config.setProperty("prop2", "value2");
config.setProperty("prop3", "value3");

assertEquals(set("prop1", "prop2", "prop3"), set(config.keys()));
assertEquals(Sets.newHashSet("prop1", "prop2", "prop3"), Sets.newHashSet(config.keys()));

config.clearProperty("prop3");
assertEquals(set("prop1", "prop2"), set(config.keys()));
assertEquals(Sets.newHashSet("prop1", "prop2"), Sets.newHashSet(config.keys()));

config.setProperties(MapConfig.builder().put("prop4", "value4").build());
assertEquals(set("prop1", "prop2", "prop4"), set(config.keys()));
assertEquals(Sets.newHashSet("prop1", "prop2", "prop4"), Sets.newHashSet(config.keys()));

Properties props = new Properties();
props.put("prop5", "value5");
config.setProperties(props);
assertEquals(set("prop1", "prop2", "prop4", "prop5"), set(config.keys()));
assertEquals(Sets.newHashSet("prop1", "prop2", "prop4", "prop5"), Sets.newHashSet(config.keys()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import java.util.NoSuchElementException;
import java.util.Set;

import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.netflix.archaius.api.Config;
import com.netflix.archaius.exceptions.ParseException;
import org.junit.jupiter.api.Test;

import static com.netflix.archaius.TestUtils.set;
import static com.netflix.archaius.TestUtils.size;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -226,8 +226,8 @@ public void testKeysIterable() {
.build();
Iterable<String> keys = config.keys();

assertEquals(2, size(keys));
assertEquals(set("key1", "key2"), set(keys));
assertEquals(2, Iterables.size(keys));
assertEquals(Sets.newHashSet("key1", "key2"), Sets.newHashSet(keys));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;

import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.netflix.archaius.api.PropertyDetails;
import com.netflix.archaius.config.polling.PollingResponse;
import com.netflix.archaius.instrumentation.AccessMonitorUtil;
Expand All @@ -39,8 +41,6 @@
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;

import static com.netflix.archaius.TestUtils.set;
import static com.netflix.archaius.TestUtils.size;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -290,8 +290,8 @@ public void testKeysIterable() throws Exception {
strategy.fire();

Iterable<String> keys = config.keys();
assertEquals(2, size(keys));
assertEquals(set("foo", "bar"), set(keys));
assertEquals(2, Iterables.size(keys));
assertEquals(Sets.newHashSet("foo", "bar"), Sets.newHashSet(keys));
}

@Test
Expand Down
Loading

0 comments on commit caff66a

Please sign in to comment.