More unit tests
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
package net.locusworks.common.utils;
|
||||
|
||||
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
|
||||
import com.fasterxml.jackson.core.*;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.introspect.Annotated;
|
||||
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
|
||||
import com.fasterxml.jackson.databind.ser.impl.WritableObjectId;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DateTimeStampSerializerTest {
|
||||
|
||||
static AtomicLong atomicLong = new AtomicLong(0);
|
||||
|
||||
@Test
|
||||
void testSerializer(){
|
||||
DateTimeStampSerializer serializer = new DateTimeStampSerializer();
|
||||
assertDoesNotThrow(() -> serializer.serialize(new Date(), new MyJsonGenerator(), new MySerializerProvider()));
|
||||
assertTrue(atomicLong.get() > 0);
|
||||
}
|
||||
|
||||
private static class MyJsonGenerator extends JsonGenerator {
|
||||
@Override public JsonGenerator setCodec(ObjectCodec objectCodec) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public ObjectCodec getCodec() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public Version version() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public JsonStreamContext getOutputContext() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public JsonGenerator enable(Feature feature) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public JsonGenerator disable(Feature feature) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public boolean isEnabled(Feature feature) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override public int getFeatureMask() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override public JsonGenerator setFeatureMask(int i) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public JsonGenerator useDefaultPrettyPrinter() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public void writeStartArray() {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeEndArray() {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeStartObject() {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeEndObject() {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeFieldName(String s) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeFieldName(SerializableString serializableString)
|
||||
{
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeString(String s) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeString(char[] chars, int i, int i1) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeString(SerializableString serializableString) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeRawUTF8String(byte[] bytes, int i, int i1) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeUTF8String(byte[] bytes, int i, int i1) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeRaw(String s) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeRaw(String s, int i, int i1) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeRaw(char[] chars, int i, int i1) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeRaw(char c) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeRawValue(String s) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeRawValue(String s, int i, int i1) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeRawValue(char[] chars, int i, int i1) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeBinary(Base64Variant base64Variant, byte[] bytes, int i, int i1)
|
||||
{
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public int writeBinary(Base64Variant base64Variant, InputStream inputStream, int i)
|
||||
{
|
||||
fail();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override public void writeNumber(int i) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeNumber(long l) {
|
||||
atomicLong.set(l);
|
||||
}
|
||||
|
||||
@Override public void writeNumber(BigInteger bigInteger) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeNumber(double v) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeNumber(float v) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeNumber(BigDecimal bigDecimal) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeNumber(String s) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeBoolean(boolean b) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeNull() {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeObject(Object o) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void writeTree(TreeNode treeNode) {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public void flush() {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Override public boolean isClosed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override public void close() {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class MySerializerProvider extends SerializerProvider {
|
||||
@Override
|
||||
public WritableObjectId findObjectId(Object o, ObjectIdGenerator<?> objectIdGenerator) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public JsonSerializer<Object> serializerInstance(Annotated annotated, Object o) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public Object includeFilterInstance(BeanPropertyDefinition beanPropertyDefinition,
|
||||
Class<?> aClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public boolean includeFilterSuppressNulls(Object o) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package net.locusworks.test;
|
||||
package net.locusworks.common.utils;
|
||||
|
||||
import net.locusworks.common.interfaces.AutoCloseableIterator;
|
||||
import net.locusworks.common.utils.FileReader;
|
||||
@@ -7,16 +7,22 @@ import net.locusworks.common.utils.RandomString;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
|
||||
public class FileReaderTest {
|
||||
|
||||
@@ -31,7 +37,7 @@ public class FileReaderTest {
|
||||
int count = ThreadLocalRandom.current().nextInt(100);
|
||||
|
||||
for (int i = 1; i <= count; i++) {
|
||||
String randomString = RandomString.getString(ThreadLocalRandom.current().nextInt(5, 100)) + "\n";
|
||||
String randomString = RandomString.getInstance().getString(ThreadLocalRandom.current().nextInt(5, 100)) + "\n";
|
||||
numLines.put(i, randomString.length());
|
||||
fos.write(randomString.getBytes());
|
||||
}
|
||||
@@ -93,4 +99,12 @@ public class FileReaderTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoAlgorithmException() {
|
||||
try(MockedStatic<SecureRandom> mocked = mockStatic(SecureRandom.class)) {
|
||||
mocked.when(() -> SecureRandom.getInstance(anyString())).thenThrow(NoSuchAlgorithmException.class);
|
||||
assertDoesNotThrow(() -> RandomString.newInstance());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package net.locusworks.test;
|
||||
package net.locusworks.common.utils;
|
||||
|
||||
import net.locusworks.common.utils.HashUtils;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
@@ -0,0 +1,30 @@
|
||||
package net.locusworks.common.utils;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class RandomStringTest {
|
||||
|
||||
@Test
|
||||
public void testStaticBytes() {
|
||||
for (int length = 3; length < 50; length++) {
|
||||
assertEquals(RandomString.getInstance().getBytes(length).length, length);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticString() {
|
||||
for (int length = 3; length < 50; length++) {
|
||||
String random = RandomString.getInstance().getString(length);
|
||||
assertEquals(random.length(), length);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExceptions() {
|
||||
assertThrows(IllegalArgumentException.class, () -> RandomString.newInstance().getString(0));
|
||||
}
|
||||
|
||||
}
|
||||
81
src/test/java/net/locusworks/common/utils/SplitterTest.java
Normal file
81
src/test/java/net/locusworks/common/utils/SplitterTest.java
Normal file
@@ -0,0 +1,81 @@
|
||||
package net.locusworks.common.utils;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SplitterTest {
|
||||
|
||||
@Test
|
||||
void testBasicSplitter() {
|
||||
List<String> split = Splitter.onSpace().split("Hello World");
|
||||
assertNotNull(split);
|
||||
assertEquals(2, split.size());
|
||||
assertEquals("Hello", split.get(0));
|
||||
assertEquals("World", split.get(1));
|
||||
|
||||
split = Splitter.fixedLengthSplit(5).split("HelloWorld");
|
||||
assertNotNull(split);
|
||||
assertEquals(2, split.size());
|
||||
assertEquals("Hello", split.get(0));
|
||||
assertEquals("World", split.get(1));
|
||||
|
||||
split = Splitter.onNewLine().split("Hello\nWorld");
|
||||
assertNotNull(split);
|
||||
assertEquals(2, split.size());
|
||||
assertEquals("Hello", split.get(0));
|
||||
assertEquals("World", split.get(1));
|
||||
|
||||
split = Splitter.onNewLine().split("Hello\r\nWorld");
|
||||
assertNotNull(split);
|
||||
assertEquals(2, split.size());
|
||||
assertEquals("Hello", split.get(0));
|
||||
assertEquals("World", split.get(1));
|
||||
|
||||
String[] array = Splitter.onSpace().splitToArray("Hello World");
|
||||
assertNotNull(array);
|
||||
assertEquals(2, array.length);
|
||||
assertEquals("Hello", array[0]);
|
||||
assertEquals("World", array[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOmitEmptyStringWithLimits() {
|
||||
List<String> split = Splitter.onSpace().split("Hello World");
|
||||
assertEquals(3, split.size());
|
||||
split = Splitter.onSpace().omitEmptyStrings().withLimit(1).split("Hello World");
|
||||
assertNotNull(split);
|
||||
assertEquals(1, split.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExceptions() {
|
||||
assertThrows(IllegalArgumentException.class, () -> Splitter.onSpace().split(""));
|
||||
assertThrows(IllegalArgumentException.class, () -> Splitter.fixedLengthSplit(0).split("Hello World"));
|
||||
assertThrows(NullPointerException.class, () -> Splitter.on(null).split("Hello World"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWithKeyValueSeparator() {
|
||||
Map<String, String> map = Splitter.on(";").withKeyValueSeparator("=").split("Hello=World;bubba=hotep");
|
||||
assertEquals(2, map.size());
|
||||
assertEquals("World", map.get("Hello"));
|
||||
assertEquals("hotep", map.get("bubba"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWithMapSplitterErrors() {
|
||||
assertThrows(IllegalArgumentException.class, () -> Splitter.on(";").withKeyValueSeparator("").split("Hello=;bubba=hotep"));
|
||||
assertThrows(IllegalArgumentException.class, () -> Splitter.on(";").withKeyValueSeparator("=").split("Hello=;bubba=hotep"));
|
||||
assertThrows(IllegalArgumentException.class, () -> Splitter.on(";").withKeyValueSeparator("=").split(null));
|
||||
Map<String, String> map = Splitter.on(";").withKeyValueSeparator("=").skipInvalidKeyValues().split("Hello=;bubba=hotep");
|
||||
assertEquals(1, map.size());
|
||||
assertEquals("hotep", map.get("bubba"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package net.locusworks.common.utils;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class StreamUtilsTest {
|
||||
|
||||
@Test
|
||||
void testAsStream() {
|
||||
List<String> list = List.of("Hello", "World");
|
||||
assertEquals(2, StreamUtils.asStream(list).count());
|
||||
assertEquals(2, StreamUtils.asStream(list.iterator()).count());
|
||||
assertEquals(2, StreamUtils.asStream(list.toArray(), false).count());
|
||||
}
|
||||
|
||||
}
|
||||
512
src/test/java/net/locusworks/common/utils/UtilsTest.java
Normal file
512
src/test/java/net/locusworks/common/utils/UtilsTest.java
Normal file
@@ -0,0 +1,512 @@
|
||||
package net.locusworks.common.utils;
|
||||
|
||||
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.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Test cases for the Utils class
|
||||
* @author Isaac Parenteau
|
||||
* @since 1.0.0-RELEASE
|
||||
*
|
||||
*/
|
||||
public class UtilsTest {
|
||||
|
||||
@Test
|
||||
public void testSafeString() {
|
||||
assertNotNull(Utils.safeString(null));
|
||||
assertTrue(Utils.safeString(null).isEmpty());
|
||||
assertFalse(Utils.safeString("hello world").isEmpty());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testAreEqual() {
|
||||
String val1 = "H";
|
||||
String val2 = "H";
|
||||
assertTrue(Utils.areEqual(val1, val2));
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> Utils.areEqual("1"));
|
||||
}
|
||||
@ParameterizedTest
|
||||
@MethodSource("areEqualParams")
|
||||
public void testAreEqualWithParams(List<Object> objectList, boolean or, boolean equal) {
|
||||
assertEquals(equal, Utils.areEqual(or, objectList.toArray()));
|
||||
}
|
||||
|
||||
static Stream<Arguments> areEqualParams() {
|
||||
return Stream.of(
|
||||
Arguments.of(List.of("Hello", "Hello"), false, true),
|
||||
Arguments.of(List.of("Hello", "World"), false, false),
|
||||
Arguments.of(List.of("Hello", "World", "Hello"), true, true),
|
||||
Arguments.of(List.of("Hello", "World", "Fair"), true, false)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyString() {
|
||||
assertTrue(Utils.isEmptyString(null));
|
||||
assertTrue(Utils.isEmptyString(""));
|
||||
assertTrue(Utils.isEmptyString(" "));
|
||||
assertFalse(Utils.isEmptyString("foo"));
|
||||
assertFalse(Utils.isEmptyString(" bar "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToInteger() {
|
||||
assertEquals(2, (int) Utils.toInteger("Hello word", 2));
|
||||
assertEquals(23, (int) Utils.toInteger("23", 5023));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("validateValueParams")
|
||||
public <V> void testValidateValue(V value, boolean valid) {
|
||||
assertEquals(valid, Utils.validateValue(value));
|
||||
}
|
||||
|
||||
static Stream<Arguments> validateValueParams() {
|
||||
return Stream.of(
|
||||
Arguments.of(null, false),
|
||||
Arguments.of(Collections.emptyList(), false),
|
||||
Arguments.of(Map.of(), false),
|
||||
Arguments.of(false, false),
|
||||
Arguments.of("", false),
|
||||
Arguments.of("Hello", true),
|
||||
Arguments.of(List.of("Hello"), true),
|
||||
Arguments.of(Map.of("Hello", "World"), true),
|
||||
Arguments.of(true, true)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("areValidParams")
|
||||
public void testValidateValues(List<Object> objects, boolean or, boolean valid) {
|
||||
assertEquals(valid, Utils.areValid(objects.toArray()));
|
||||
assertEquals(!valid, Utils.areNotValid(objects.toArray()));
|
||||
}
|
||||
|
||||
static Stream<Arguments> areValidParams() {
|
||||
return Stream.of(
|
||||
Arguments.of(List.of("Hello", List.of("Hello"), Map.of("Hello", "World"), true), false, true),
|
||||
Arguments.of(List.of(Collections.emptyList(), Map.of(), false), false, false)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildMap() {
|
||||
Map<String, String> mymap = Utils.buildMap(TreeMap.class, String.class, String.class, "Hello", "World");
|
||||
assertNotNull(mymap);
|
||||
assertEquals(1, mymap.size());
|
||||
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 testExtractFieldToList() {
|
||||
List<TestClass> list = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
TestClass clazz = new TestClass();
|
||||
clazz.field5 = i;
|
||||
list.add(clazz);
|
||||
}
|
||||
|
||||
List<Integer> extractedList = Utils.extractFieldToList("field5", list);
|
||||
Set<Integer> extractedSet = Utils.extractFieldToSet("field5", list);
|
||||
|
||||
assertEquals(10, extractedList.size());
|
||||
assertEquals(10, extractedSet.size());
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assertEquals(i, extractedList.get(i));
|
||||
assertTrue(extractedSet.contains(i));
|
||||
}
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> Utils.extractFieldToList("fieldName", list));
|
||||
assertThrows(IllegalArgumentException.class, () -> Utils.extractFieldToSet("fieldName", list));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFilterList() {
|
||||
List<TestClass> list = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
TestClass clazz = new TestClass();
|
||||
clazz.field5 = i;
|
||||
list.add(clazz);
|
||||
}
|
||||
|
||||
list.get(0).testClass = list.get(1);
|
||||
|
||||
List<TestClass> filteredList = Utils.filterList("field5", 5, list);
|
||||
assertEquals(1, filteredList.size());
|
||||
assertEquals(5, filteredList.get(0).field5);
|
||||
assertThrows(IllegalArgumentException.class, () -> Utils.filterList("fieldName", 5, list));
|
||||
|
||||
int value = Utils.findValue(5, "field5", list);
|
||||
assertEquals(5, value);
|
||||
|
||||
TestClass tc = Utils.findValue(list.get(1), "testClass", list);
|
||||
assertNotNull(tc);
|
||||
assertEquals(1, tc.field5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindValues() {
|
||||
List<TestClass> list = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
TestClass clazz = new TestClass();
|
||||
clazz.field5 = i % 2;
|
||||
list.add(clazz);
|
||||
}
|
||||
|
||||
List<Integer> values = Utils.findValues(0, "field5", list);
|
||||
assertEquals(5, values.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(HashSet.class, String.class, "Hello", "World");
|
||||
assertNotNull(myset);
|
||||
assertEquals(2, myset.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatString() {
|
||||
String formatted = Utils.formatString("{} {}", "Hi", 1);
|
||||
assertEquals("Hi 1", formatted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetClassName() {
|
||||
TestClass tc = new TestClass();
|
||||
assertEquals(tc.getClass().getSimpleName(), Utils.getClassName(tc));
|
||||
assertEquals(tc.getClass().getName(), Utils.getClassName(tc, true));
|
||||
assertEquals("Unknown", Utils.getClassName(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMapValue() {
|
||||
Integer value = Utils.getMapValue(Map.of("hi", 1), "hi");
|
||||
assertEquals(1, value);
|
||||
|
||||
value = Utils.getMapValue(Map.of("hi", 1), "low", 0);
|
||||
|
||||
assertEquals(0, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsNotValid() {
|
||||
assertTrue(Utils.isNotValid(null, null, null));
|
||||
assertFalse(Utils.isNotValid("null", "null", "null"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsValid() {
|
||||
assertFalse(Utils.isValid(null, null, null));
|
||||
assertTrue(Utils.isValid("null", "null", "null"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListToMap() {
|
||||
List<TestClass> list = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
TestClass clazz = new TestClass();
|
||||
clazz.field5 = i;
|
||||
list.add(clazz);
|
||||
}
|
||||
|
||||
Map<Integer, TestClass> values = Utils.listToMap("field5", list);
|
||||
assertEquals(10, values.size());
|
||||
assertEquals(1, values.get(1).field5);
|
||||
|
||||
assertThrows(Throwable.class, () -> Utils.listToMap("fieldasdf5", list));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMakeArray() {
|
||||
List<TestClass> list = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
TestClass clazz = new TestClass();
|
||||
clazz.field5 = i;
|
||||
list.add(clazz);
|
||||
}
|
||||
|
||||
assertNull(Utils.makeArray(null, String.class));
|
||||
|
||||
TestClass[] array = Utils.makeArray(list, TestClass.class);
|
||||
assertEquals(10, array.length);
|
||||
for(int i = 0; i < 10; i++) {
|
||||
assertEquals(list.get(i), array[i]);
|
||||
}
|
||||
|
||||
assertNull(Utils.makeList((Collection<Object>) null));
|
||||
assertNull(Utils.makeList((Iterable<?>) null));
|
||||
assertNull(Utils.makeList((String[]) null));
|
||||
|
||||
List<TestClass> testList = Utils.makeList(list);
|
||||
for(int i = 0; i < 10; i++) {
|
||||
assertEquals(list.get(i), testList.get(i));
|
||||
}
|
||||
|
||||
TestClassList tcl = new TestClassList();
|
||||
tcl.testClassList = testList;
|
||||
|
||||
List<TestClass> testClassList = Utils.makeList(tcl);
|
||||
for(int i = 0; i < 10; i++) {
|
||||
assertEquals(list.get(i), testClassList.get(i));
|
||||
}
|
||||
|
||||
List<TestClass> testArrayList = Utils.makeList(array);
|
||||
for(int i = 0; i < 10; i++) {
|
||||
assertEquals(list.get(i), testArrayList.get(i));
|
||||
}
|
||||
|
||||
Set<TestClass> testSet = Utils.makeSet(list);
|
||||
assertEquals(10, testSet.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapToList() {
|
||||
List<Integer> values = Utils.mapToList(Map.of("Hello", 1));
|
||||
assertEquals(1, values.size());
|
||||
assertEquals(1, values.get(0));
|
||||
|
||||
Set<Integer> setValues = Utils.mapToSet(Map.of("Hello", 1));
|
||||
assertEquals(1, setValues.size());
|
||||
assertTrue(setValues.contains(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReverseMap() {
|
||||
Map<Integer, String> map = Utils.reverseMap(Map.of("Hello", 1));
|
||||
assertEquals(1, map.size());
|
||||
assertEquals("Hello", map.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSafeList() {
|
||||
assertEquals(0, Utils.safeArray(null).length);
|
||||
assertTrue(Utils.safeList(null).isEmpty());
|
||||
assertFalse(Utils.safeIterable(null).iterator().hasNext());
|
||||
assertTrue(Utils.safeSet(null).isEmpty());
|
||||
|
||||
List<String> list = List.of("Hello", "World");
|
||||
|
||||
String[] stringArray = list.toArray(new String[0]);
|
||||
|
||||
assertEquals(list, Utils.safeList(list));
|
||||
assertEquals(stringArray, Utils.safeArray(stringArray));
|
||||
assertEquals(list, Utils.safeIterable(list));
|
||||
|
||||
Set<String> testSet = Set.of("Hello", "World");
|
||||
|
||||
assertEquals(testSet, Utils.safeSet(testSet));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetToList() {
|
||||
List<String> list = Utils.setToList(Set.of("Hello", "World"));
|
||||
assertEquals(2, list.size());
|
||||
|
||||
list = Utils.toList(Set.of("Hello", "World"));
|
||||
assertEquals(2, list.size());
|
||||
|
||||
Set<String> set = Utils.toSet("Hello", "World");
|
||||
|
||||
assertEquals(2, set.size());
|
||||
|
||||
assertTrue(set.contains("Hello"));
|
||||
assertTrue(set.contains("World"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToByteList() {
|
||||
byte[] bytes = new byte[10];
|
||||
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
bytes[i] = (byte)i;
|
||||
}
|
||||
|
||||
List<Byte> byteList = Utils.toByteList(bytes);
|
||||
assertEquals(bytes.length, byteList.size());
|
||||
|
||||
for(int i = 0; i < bytes.length; i++) {
|
||||
assertEquals(bytes[i], byteList.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterableSize() {
|
||||
List<String> stringList = List.of("Hello", "World");
|
||||
assertEquals(stringList.size(), (int)Utils.size(stringList));
|
||||
assertEquals(stringList.get(1), Utils.get(stringList, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExceptionWrapper() {
|
||||
assertThrows(RuntimeException.class, () -> List.of("Hi", "Low").forEach(Utils.handleExceptionWrapper(o -> {
|
||||
throw new Exception("bleh");
|
||||
})));
|
||||
|
||||
AtomicInteger atomicInteger = new AtomicInteger(0);
|
||||
List.of("Hi", "Low").forEach(Utils.handleExceptionWrapper(o -> {
|
||||
atomicInteger.incrementAndGet();
|
||||
}));
|
||||
|
||||
assertEquals(2, atomicInteger.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateValues() {
|
||||
assertTrue(Utils.validateValues("Hello", "World"));
|
||||
assertTrue(Utils.validateValuesOr("Hello", null));
|
||||
assertTrue(Utils.validateValuesOr(null, "Hello"));
|
||||
assertFalse(Utils.validateValuesOr(null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsJunitRunning() {
|
||||
assertTrue(Utils.isJUnitRunning());
|
||||
}
|
||||
|
||||
static class TestClassList implements Iterable<TestClass> {
|
||||
private List<TestClass> testClassList;
|
||||
@Override public Iterator<TestClass> iterator() {
|
||||
return testClassList.iterator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package net.locusworks.test;
|
||||
|
||||
import net.locusworks.common.utils.RandomString;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class RandomStringTest {
|
||||
|
||||
@Test
|
||||
public void testStaticBytes() {
|
||||
for (Integer length = 3; length < 50; length++) {
|
||||
assertEquals(RandomString.getBytes(length).length, (int) length);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticString() {
|
||||
for (Integer length = 3; length < 50; length++) {
|
||||
String random = RandomString.getString(length);
|
||||
assertEquals(random.length(), (int) length);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,244 +0,0 @@
|
||||
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.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Test cases for the Utils class
|
||||
* @author Isaac Parenteau
|
||||
* @since 1.0.0-RELEASE
|
||||
*
|
||||
*/
|
||||
public class UtilsTest {
|
||||
|
||||
@Test
|
||||
public void testSafeString() {
|
||||
assertNotNull(Utils.safeString(null));
|
||||
assertTrue(Utils.safeString(null).isEmpty());
|
||||
assertFalse(Utils.safeString("hello world").isEmpty());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testAreEqual() {
|
||||
String val1 = "H";
|
||||
String val2 = "H";
|
||||
assertTrue(Utils.areEqual(val1, val2));
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> Utils.areEqual("1"));
|
||||
}
|
||||
@ParameterizedTest
|
||||
@MethodSource("areEqualParams")
|
||||
public void testAreEqualWithParams(List<Object> objectList, boolean or, boolean equal) {
|
||||
assertEquals(equal, Utils.areEqual(or, objectList.toArray()));
|
||||
}
|
||||
|
||||
static Stream<Arguments> areEqualParams() {
|
||||
return Stream.of(
|
||||
Arguments.of(List.of("Hello", "Hello"), false, true),
|
||||
Arguments.of(List.of("Hello", "World"), false, false),
|
||||
Arguments.of(List.of("Hello", "World", "Hello"), true, true),
|
||||
Arguments.of(List.of("Hello", "World", "Fair"), true, false)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyString() {
|
||||
assertTrue(Utils.isEmptyString(null));
|
||||
assertTrue(Utils.isEmptyString(""));
|
||||
assertTrue(Utils.isEmptyString(" "));
|
||||
assertFalse(Utils.isEmptyString("foo"));
|
||||
assertFalse(Utils.isEmptyString(" bar "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToInteger() {
|
||||
assertEquals(2, (int) Utils.toInteger("Hello word", 2));
|
||||
assertEquals(23, (int) Utils.toInteger("23", 5023));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("validateValueParams")
|
||||
public <V> void testValidateValue(V value, boolean valid) {
|
||||
assertEquals(valid, Utils.validateValue(value));
|
||||
}
|
||||
|
||||
static Stream<Arguments> validateValueParams() {
|
||||
return Stream.of(
|
||||
Arguments.of(null, false),
|
||||
Arguments.of(Collections.emptyList(), false),
|
||||
Arguments.of(Map.of(), false),
|
||||
Arguments.of(false, false),
|
||||
Arguments.of("", false),
|
||||
Arguments.of("Hello", true),
|
||||
Arguments.of(List.of("Hello"), true),
|
||||
Arguments.of(Map.of("Hello", "World"), true),
|
||||
Arguments.of(true, true)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("areValidParams")
|
||||
public void testValidateValues(List<Object> objects, boolean or, boolean valid) {
|
||||
assertEquals(valid, Utils.areValid(objects.toArray()));
|
||||
assertEquals(!valid, Utils.areNotValid(objects.toArray()));
|
||||
}
|
||||
|
||||
static Stream<Arguments> areValidParams() {
|
||||
return Stream.of(
|
||||
Arguments.of(List.of("Hello", List.of("Hello"), Map.of("Hello", "World"), true), false, true),
|
||||
Arguments.of(List.of(Collections.emptyList(), Map.of(), false), false, false)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildMap() {
|
||||
Map<String, String> mymap = Utils.buildMap(TreeMap.class, String.class, String.class, "Hello", "World");
|
||||
assertNotNull(mymap);
|
||||
assertEquals(1, mymap.size());
|
||||
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