More unit tests for jdk17
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
package net.locusworks.test;
|
||||
|
||||
import net.locusworks.common.annotations.MapValue;
|
||||
import net.locusworks.common.utils.Utils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@@ -110,9 +109,136 @@ public class UtilsTest {
|
||||
assertEquals("World", mymap.get("Hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCloneList() {
|
||||
String[] values = new String[]{
|
||||
"Hello",
|
||||
"world"
|
||||
};
|
||||
List<String> cloned = Utils.cloneList(new ArrayList<>(Arrays.stream(values).toList()));
|
||||
assertEquals(2, cloned.size());
|
||||
assertThrows(UnsupportedOperationException.class, () -> cloned.add("asdf"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCloneObject()
|
||||
throws InvocationTargetException, InstantiationException, IllegalAccessException,
|
||||
NoSuchMethodException {
|
||||
TestClass clazz = new TestClass();
|
||||
clazz.field1 = "hi";
|
||||
clazz.field2 = "low";
|
||||
clazz.field3 = "world";
|
||||
|
||||
TestClass clazz3 = new TestClass();
|
||||
clazz3.field1 = "hi";
|
||||
clazz3.field2 = "smash";
|
||||
clazz3.field3 = "world";
|
||||
|
||||
clazz.testClass = clazz3;
|
||||
|
||||
TestClass clazz2 = Utils.cloneObject(clazz);
|
||||
assertNotNull(clazz2);
|
||||
assertEquals(clazz.field1, clazz2.field1);
|
||||
assertEquals(clazz.field2, clazz2.field2);
|
||||
assertEquals(clazz.field3, clazz2.field3);
|
||||
assertEquals(clazz.field5, clazz2.field5);
|
||||
assertEquals(clazz.number, clazz2.number);
|
||||
assertEquals(clazz.aBoolean, clazz2.aBoolean);
|
||||
assertNotNull(clazz2.field4);
|
||||
assertNull(clazz2.nullField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCovertToMap() throws Exception {
|
||||
TestClass clazz = new TestClass();
|
||||
clazz.field1 = "hi";
|
||||
clazz.field2 = "low";
|
||||
clazz.field3 = "world";
|
||||
|
||||
TestClass clazz3 = new TestClass();
|
||||
clazz.field1 = "hi";
|
||||
clazz.field2 = "low";
|
||||
clazz.field3 = "world";
|
||||
clazz.testClass = clazz3;
|
||||
|
||||
Map<String, Object> converted = Utils.convertToMap(clazz);
|
||||
|
||||
assertEquals(7, converted.size());
|
||||
assertEquals("hi", converted.get("other_field"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertToStringMap() throws Exception {
|
||||
TestClass clazz = new TestClass();
|
||||
clazz.field1 = "hi";
|
||||
clazz.field2 = "low";
|
||||
clazz.field3 = "world";
|
||||
Map<String, String> converted = Utils.convertToStringMap(clazz);
|
||||
assertEquals(6, converted.size());
|
||||
assertEquals("hi", converted.get("other_field"));
|
||||
|
||||
converted = Utils.convertToStringMap(converted);
|
||||
assertEquals(6, converted.size());
|
||||
assertEquals("hi", converted.get("other_field"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildStringMap() {
|
||||
Map<String, String> myMap = Utils.buildStringHashMap("hello", "world");
|
||||
assertNotNull(myMap);
|
||||
assertEquals(1, myMap.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildMapExceptions() {
|
||||
Throwable ex = assertThrows(IllegalArgumentException.class, () -> Utils.buildMap(String.class, String.class, String.class, "" ));
|
||||
|
||||
assertNotNull(ex);
|
||||
|
||||
ex = assertThrows(IllegalArgumentException.class, () -> Utils.buildMap(TreeMap.class, String.class, String.class, "Hello"));
|
||||
|
||||
assertEquals("Odd number of arguments provided", ex.getMessage());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> Utils.buildMap(TreeMap.class, String.class, String.class, new Object(), "Hello"));
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> Utils.buildMap(TreeMap.class, String.class, String.class, "Hello", new Object()));
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> Utils.buildMap(TreeMap.class, Object.class, String.class, "Hello", new Object()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildSet() {
|
||||
Set<String> myset = Utils.buildSet("Hello", "World");
|
||||
assertNotNull(myset);
|
||||
assertEquals(2, myset.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsJunitRunning() {
|
||||
assertTrue(Utils.isJUnitRunning());
|
||||
}
|
||||
|
||||
public static class TestClass {
|
||||
@MapValue("other_field")
|
||||
private String field1;
|
||||
|
||||
@MapValue(ignore = true)
|
||||
private String field2;
|
||||
|
||||
|
||||
private String field3;
|
||||
|
||||
private final String field4 = "final";
|
||||
@MapValue
|
||||
private int field5 = 5;
|
||||
|
||||
private Boolean aBoolean = Boolean.valueOf("true");
|
||||
|
||||
private Double number = 1.0d;
|
||||
|
||||
private String nullField = null;
|
||||
|
||||
private TestClass testClass;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user