More unit tests
This commit is contained in:
@ -41,15 +41,18 @@ public class AES {
|
|||||||
|
|
||||||
private String seed;
|
private String seed;
|
||||||
|
|
||||||
|
private RandomString randomizer;
|
||||||
|
|
||||||
private void initSecureKey(String seed) {
|
private void initSecureKey(String seed) {
|
||||||
try {
|
try {
|
||||||
SecureRandom sr = getSecureRandom(seed);
|
SecureRandom sr = getSecureRandom(seed);
|
||||||
KeyGenerator generator = KeyGenerator.getInstance(ENCRYPTION_TYPE);
|
KeyGenerator generator = KeyGenerator.getInstance(ENCRYPTION_TYPE);
|
||||||
generator.init(128, sr);
|
generator.init(128, sr);
|
||||||
init(generator.generateKey().getEncoded(), sr);
|
|
||||||
|
randomizer = RandomString.newInstance(sr);
|
||||||
|
init(generator.generateKey().getEncoded());
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
System.err.println(ex);
|
throw new IllegalArgumentException("Unable to initialize encryption:", ex);
|
||||||
throw new IllegalArgumentException("Unable to initalize encryption:", ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,13 +72,12 @@ public class AES {
|
|||||||
/**
|
/**
|
||||||
* Initialize the aes engine
|
* Initialize the aes engine
|
||||||
* @param key secret key to use
|
* @param key secret key to use
|
||||||
* @param sr the secure random class
|
|
||||||
*/
|
*/
|
||||||
private void init(final byte[] key, SecureRandom sr) {
|
private void init(final byte[] key) {
|
||||||
try {
|
try {
|
||||||
this.cipher = Cipher.getInstance(ENCRYPTION_ALGORITH, PROVIDER);
|
this.cipher = Cipher.getInstance(ENCRYPTION_ALGORITH, PROVIDER);
|
||||||
this.secretKeySpec = new SecretKeySpec(key, ENCRYPTION_TYPE);
|
this.secretKeySpec = new SecretKeySpec(key, ENCRYPTION_TYPE);
|
||||||
this.ivParamSpec = new IvParameterSpec(RandomString.getBytes(16, sr));
|
this.ivParamSpec = new IvParameterSpec(randomizer.getBytes(16));
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new IllegalArgumentException("Unable to initialize encryption:", ex);
|
throw new IllegalArgumentException("Unable to initialize encryption:", ex);
|
||||||
}
|
}
|
||||||
@ -130,7 +132,7 @@ public class AES {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static AES createInstance() {
|
public static AES createInstance() {
|
||||||
return createInstance(RandomString.getString(16));
|
return createInstance(RandomString.getInstance().getString(16));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AES createInstance(byte[] byteSeed) {
|
public static AES createInstance(byte[] byteSeed) {
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package net.locusworks.common.utils;
|
package net.locusworks.common.utils;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class Checks {
|
public class Checks {
|
||||||
|
|
||||||
public static void checkArguments(boolean expression, String error) {
|
public static void checkArguments(boolean expression, String error) {
|
||||||
@ -19,7 +21,7 @@ public class Checks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void checkNotNull(Object item, String error) {
|
public static void checkNotNull(Object item, String error) {
|
||||||
if (item == null) throw new IllegalAccessError("Provided item is null");
|
Objects.requireNonNull(item, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.locusworks.common.utils;
|
package net.locusworks.common.utils;
|
||||||
|
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@ -17,34 +18,30 @@ public class RandomString {
|
|||||||
private Random random;
|
private Random random;
|
||||||
|
|
||||||
private char[] symbols;
|
private char[] symbols;
|
||||||
private int length;
|
|
||||||
|
|
||||||
private static RandomString instance;
|
private static RandomString instance;
|
||||||
|
|
||||||
private RandomString(Integer length) {
|
private RandomString() {
|
||||||
this(length, new SecureRandom());
|
Random random;
|
||||||
}
|
try {
|
||||||
|
random = SecureRandom.getInstance("SHA1PRNG");
|
||||||
private RandomString(Integer length, Random random) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
this(length, random, ALPHA_NUMERIC);
|
random = new SecureRandom();
|
||||||
}
|
}
|
||||||
|
init(random);
|
||||||
private RandomString(Integer length, Random random, String symbols) {
|
|
||||||
if (length < 1) throw new IllegalArgumentException("Length has to be greater than 1");
|
|
||||||
if (symbols.length() < 2) throw new IllegalArgumentException("Symbols need to be greater than 2");
|
|
||||||
this.random = Objects.requireNonNull(random);
|
|
||||||
this.symbols = symbols.toCharArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
private synchronized final void setRandom(Random random) {
|
|
||||||
this.random = random;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized final void setLength(int length) {
|
private RandomString(Random random) {
|
||||||
this.length = length;
|
init(random);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String nextString() {
|
private void init(Random random) {
|
||||||
|
this.random = Objects.requireNonNull(random, "Random generator cannot be null");
|
||||||
|
this.symbols = ALPHA_NUMERIC.toCharArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String nextString(int length) {
|
||||||
|
if (length < 1) throw new IllegalArgumentException("String Length has to be greater than 0");
|
||||||
char[] buffer = new char[length];
|
char[] buffer = new char[length];
|
||||||
for (int index = 0; index < buffer.length; index++) {
|
for (int index = 0; index < buffer.length; index++) {
|
||||||
buffer[index] = symbols[random.nextInt(symbols.length)];
|
buffer[index] = symbols[random.nextInt(symbols.length)];
|
||||||
@ -52,28 +49,28 @@ public class RandomString {
|
|||||||
return new String(buffer);
|
return new String(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getString(Integer length) {
|
public String getString(Integer length) {
|
||||||
if (instance == null) {
|
return this.nextString(length);
|
||||||
instance = new RandomString(length);
|
|
||||||
}
|
|
||||||
instance.setLength(length);
|
|
||||||
return instance.nextString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getString(Integer length, Random random) {
|
public byte[] getBytes(Integer length) {
|
||||||
if (instance == null) {
|
|
||||||
instance = new RandomString(length);
|
|
||||||
}
|
|
||||||
instance.setLength(length);
|
|
||||||
instance.setRandom(random);
|
|
||||||
return instance.nextString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] getBytes(Integer length) {
|
|
||||||
return getString(length).getBytes(UTF_8);
|
return getString(length).getBytes(UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] getBytes(Integer length, Random random) {
|
public static RandomString getInstance() {
|
||||||
return getString(length, random).getBytes(UTF_8);
|
if (instance == null) {
|
||||||
|
instance = new RandomString();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RandomString newInstance() {
|
||||||
|
instance = new RandomString();
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RandomString newInstance(Random random) {
|
||||||
|
instance = new RandomString(random);
|
||||||
|
return instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,56 +8,82 @@ import java.util.Map;
|
|||||||
import static net.locusworks.common.utils.Checks.checkArguments;
|
import static net.locusworks.common.utils.Checks.checkArguments;
|
||||||
import static net.locusworks.common.utils.Checks.checkNotNull;
|
import static net.locusworks.common.utils.Checks.checkNotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class to help split a string in various ways.
|
||||||
|
* By partition (fixed length split) or by sequence (look for a specific string sequence).
|
||||||
|
* it can also split on new line or not.
|
||||||
|
*/
|
||||||
public class Splitter {
|
public class Splitter {
|
||||||
|
private enum SplitterType {
|
||||||
|
PARTITION,
|
||||||
|
SEQUENCE
|
||||||
|
}
|
||||||
private String splitSeq;
|
private String splitSeq;
|
||||||
private boolean omitEmptyStrings = false;
|
private boolean omitEmptyStrings = false;
|
||||||
private int partition;
|
private int partition;
|
||||||
private int limit;
|
private int limit;
|
||||||
|
private final SplitterType splitterType;
|
||||||
private static Splitter splitter;
|
private static Splitter splitter;
|
||||||
|
|
||||||
private Splitter(String seq) {
|
private Splitter(String seq) {
|
||||||
this.splitSeq = seq;
|
this.splitSeq = seq;
|
||||||
|
this.splitterType = SplitterType.SEQUENCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Splitter(int partition) {
|
private Splitter(int partition) {
|
||||||
this.partition = partition;
|
this.partition = partition;
|
||||||
|
this.splitterType = SplitterType.PARTITION;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove empty string from the resulting lists
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
public Splitter omitEmptyStrings() {
|
public Splitter omitEmptyStrings() {
|
||||||
this.omitEmptyStrings = true;
|
this.omitEmptyStrings = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a subset of the resulting list
|
||||||
|
* @param limit how many items to retrieve
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
public Splitter withLimit(int limit) {
|
public Splitter withLimit(int limit) {
|
||||||
this.limit = limit;
|
this.limit = limit;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MapSplitter withKeyValueSeparator(String separator) {
|
/**
|
||||||
checkArguments(!Utils.isEmptyString(separator), "Key value separator cannot be empty or null");
|
* Return an array instead of a list
|
||||||
return new MapSplitter(this, separator);
|
* @param sentence the string sentence to split
|
||||||
}
|
* @return this
|
||||||
|
*/
|
||||||
public String[] splitToArray(String sentence) {
|
public String[] splitToArray(String sentence) {
|
||||||
List<String> list = split(sentence);
|
List<String> list = split(sentence);
|
||||||
return list.toArray(new String[0]);
|
return list.toArray(new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split the string
|
||||||
|
* @param sentence the string to split
|
||||||
|
* @return the resulting list.
|
||||||
|
*/
|
||||||
public List<String> split(String sentence) {
|
public List<String> split(String sentence) {
|
||||||
checkArguments(!Utils.isEmptyString(sentence), "provided value is null or empty");
|
checkArguments(!Utils.isEmptyString(sentence), "provided value is null or empty");
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
||||||
|
|
||||||
if (!Utils.isEmptyString(splitSeq))
|
if (splitterType == SplitterType.PARTITION) {
|
||||||
populateForTrimmer(sentence, list);
|
|
||||||
else
|
|
||||||
populateForFixedWidth(sentence, list);
|
populateForFixedWidth(sentence, list);
|
||||||
|
} else {
|
||||||
|
populateForTrimmer(sentence, list);
|
||||||
|
}
|
||||||
|
|
||||||
return limit > 0 ? list.subList(0, limit) : list;
|
return limit > 0 ? list.subList(0, limit) : list;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void populateForFixedWidth(String sentence, List<String> list) {
|
private void populateForFixedWidth(String sentence, List<String> list) {
|
||||||
|
checkArguments(partition > 0, "Partition should be greater than 0");
|
||||||
int strLength = sentence.length();
|
int strLength = sentence.length();
|
||||||
for (int i = 0; i < strLength; i += partition) {
|
for (int i = 0; i < strLength; i += partition) {
|
||||||
list.add(sentence.substring(i, Math.min(strLength, i + partition)));
|
list.add(sentence.substring(i, Math.min(strLength, i + partition)));
|
||||||
@ -65,37 +91,63 @@ public class Splitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void populateForTrimmer(String sentence, List<String> list) {
|
private void populateForTrimmer(String sentence, List<String> list) {
|
||||||
|
checkNotNull(splitSeq, "Split value provided was null");
|
||||||
for (String s : sentence.split(splitSeq)) {
|
for (String s : sentence.split(splitSeq)) {
|
||||||
if (s == null || (omitEmptyStrings && s.trim().isEmpty()))
|
if (omitEmptyStrings && s.trim().isEmpty())
|
||||||
continue;
|
continue;
|
||||||
list.add(s.trim());
|
list.add(s.trim());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split the string on fixed length partitions
|
||||||
|
* @param partition the length to split the string on
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
public static Splitter fixedLengthSplit(int partition) {
|
public static Splitter fixedLengthSplit(int partition) {
|
||||||
checkArguments(partition > 0, "Partition has to be greater than 0");
|
|
||||||
splitter = new Splitter(partition);
|
splitter = new Splitter(partition);
|
||||||
return splitter;
|
return splitter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split the length on a specified string sequence
|
||||||
|
* @param split the sequence to split
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
public static Splitter on(String split) {
|
public static Splitter on(String split) {
|
||||||
checkNotNull(split, "Split value provided was null");
|
|
||||||
splitter = new Splitter(split);
|
splitter = new Splitter(split);
|
||||||
return splitter;
|
return splitter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split on new line sequence
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
public static Splitter onNewLine() {
|
public static Splitter onNewLine() {
|
||||||
return on("\\r?\\n");
|
return on("\\r?\\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split on spaces
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
public static Splitter onSpace() {
|
public static Splitter onSpace() {
|
||||||
return on(" ");
|
return on(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Separator on what the key value is. return map
|
||||||
|
* @param separator the separator value
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public MapSplitter withKeyValueSeparator(String separator) {
|
||||||
|
return new MapSplitter(this, separator);
|
||||||
|
}
|
||||||
|
|
||||||
public static class MapSplitter {
|
public static class MapSplitter {
|
||||||
|
|
||||||
private final Splitter splitter;
|
private final Splitter splitter;
|
||||||
private String separator;
|
private final String separator;
|
||||||
private boolean skipInvalid = false;
|
private boolean skipInvalid = false;
|
||||||
|
|
||||||
private MapSplitter(Splitter splitter, String separator) {
|
private MapSplitter(Splitter splitter, String separator) {
|
||||||
|
@ -185,25 +185,18 @@ public class Utils {
|
|||||||
throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
|
throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
|
||||||
Object clone = obj.getClass().getDeclaredConstructor().newInstance();
|
Object clone = obj.getClass().getDeclaredConstructor().newInstance();
|
||||||
for (Field field : obj.getClass().getDeclaredFields()) {
|
for (Field field : obj.getClass().getDeclaredFields()) {
|
||||||
try {
|
field.setAccessible(true);
|
||||||
field.setAccessible(true);
|
if (field.get(obj) == null || Modifier.isFinal(field.getModifiers())){
|
||||||
if (field.get(obj) == null || Modifier.isFinal(field.getModifiers())){
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Class<?> type = field.getType();
|
Class<?> type = field.getType();
|
||||||
|
|
||||||
if (type.isPrimitive() || getWrapperTypes().contains(type)) {
|
if (isPrimitiveOrWrapper(type)) {
|
||||||
field.set(clone, field.get(obj));
|
field.set(clone, field.get(obj));
|
||||||
} else {
|
} else {
|
||||||
Object childObj = field.get(obj);
|
field.set(clone, cloneObject(field.get(obj)));
|
||||||
if (childObj == obj) {
|
}
|
||||||
field.set(clone, clone);
|
|
||||||
} else {
|
|
||||||
field.set(clone, cloneObject(field.get(obj)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (NullPointerException ignored) { }
|
|
||||||
}
|
}
|
||||||
return (O) clone;
|
return (O) clone;
|
||||||
}
|
}
|
||||||
@ -279,8 +272,8 @@ public class Utils {
|
|||||||
* @param <K> The expected class of the Iterable list
|
* @param <K> The expected class of the Iterable list
|
||||||
* @return The converted list
|
* @return The converted list
|
||||||
*/
|
*/
|
||||||
public static <E, K> List<E> extractFieldToList(String fieldName, Iterable<K> list) {
|
public static <E, K> Set<E> extractFieldToSet(String fieldName, Iterable<K> list) {
|
||||||
return setToList(extractFieldToSet(fieldName, list));
|
return listToSet(extractFieldToList(fieldName, list));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -292,10 +285,10 @@ public class Utils {
|
|||||||
* @return set with the desired items
|
* @return set with the desired items
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <E, K> Set<E> extractFieldToSet(String fieldName, Iterable<K> list) {
|
public static <E, K> List<E> extractFieldToList(String fieldName, Iterable<K> list) {
|
||||||
Set<E> newSet = new LinkedHashSet<>();
|
List<E> newSet = new ArrayList<>();
|
||||||
Field field;
|
Field field;
|
||||||
for (K item : Utils.safeList(list)) {
|
for (K item : Utils.safeIterable(list)) {
|
||||||
try {
|
try {
|
||||||
field = getField(item.getClass(), fieldName);
|
field = getField(item.getClass(), fieldName);
|
||||||
|
|
||||||
@ -327,7 +320,7 @@ public class Utils {
|
|||||||
|
|
||||||
Field field;
|
Field field;
|
||||||
|
|
||||||
for (E item : safeList(list)) {
|
for (E item : safeIterable(list)) {
|
||||||
try {
|
try {
|
||||||
field = getField(item.getClass(), fieldName);
|
field = getField(item.getClass(), fieldName);
|
||||||
|
|
||||||
@ -361,27 +354,9 @@ public class Utils {
|
|||||||
* @return - Returns the value of the specified field or null if there is no value.
|
* @return - Returns the value of the specified field or null if there is no value.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({ "unchecked", "unlikely-arg-type" })
|
@SuppressWarnings({"unlikely-arg-type" })
|
||||||
public static <E, K> E findValue(E itemToFind, String fieldName, List<K> list) {
|
public static <E, K> E findValue(E itemToFind, String fieldName, List<K> list) {
|
||||||
for (K item : Utils.safeList(list)) {
|
return findValues(itemToFind, fieldName, list).stream().findFirst().orElse(null);
|
||||||
try {
|
|
||||||
if (isPrimitiveOrWrapper(item.getClass())) {
|
|
||||||
if (itemToFind.equals(item)) {
|
|
||||||
return (E) item;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Field field = getField(item.getClass(), fieldName);
|
|
||||||
E value = (E) field.get(item);
|
|
||||||
if (itemToFind.equals(value)) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch(IllegalArgumentException | IllegalAccessException e) {
|
|
||||||
throw new IllegalArgumentException(String.format("Unable to find field %s. -> %s", fieldName, e.getMessage()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -398,29 +373,12 @@ public class Utils {
|
|||||||
* @param <K> - The expected class of the Iterable list
|
* @param <K> - The expected class of the Iterable list
|
||||||
* @return - Returns the list.
|
* @return - Returns the list.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({ "unchecked", "unlikely-arg-type" })
|
@SuppressWarnings({"unlikely-arg-type" })
|
||||||
public static <E, K> List<E> findValues(E valueToFind, String fieldName, List<K> list) {
|
public static <E, K> List<E> findValues(E valueToFind, String fieldName, List<K> list) {
|
||||||
List<E> tmpList = new ArrayList<>();
|
List<E> extracted = extractFieldToList(fieldName, list);
|
||||||
|
return extracted.stream()
|
||||||
for (K item : Utils.safeList(list)) {
|
.filter(Objects::nonNull)
|
||||||
try {
|
.filter(v -> v.equals(valueToFind)).collect(Collectors.toList());
|
||||||
if (isPrimitiveOrWrapper(item.getClass())) {
|
|
||||||
if (valueToFind.equals(item)) {
|
|
||||||
tmpList.add((E)item);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Field field = getField(item.getClass(), fieldName);
|
|
||||||
E value = (E) field.get(item);
|
|
||||||
if (valueToFind.equals(value)) {
|
|
||||||
tmpList.add(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IllegalArgumentException | IllegalAccessException e) {
|
|
||||||
throw new IllegalArgumentException(String.format("Unable to find field %s. -> %s", fieldName, e.getMessage()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return tmpList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -571,7 +529,7 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a list of items into a map
|
* Converts a list of items into a map.
|
||||||
*
|
*
|
||||||
* @param <K> the type parameter
|
* @param <K> the type parameter
|
||||||
* @param <V> the type parameter
|
* @param <V> the type parameter
|
||||||
@ -589,7 +547,7 @@ public class Utils {
|
|||||||
Field field = getField(value.getClass(), keyFieldName);
|
Field field = getField(value.getClass(), keyFieldName);
|
||||||
K key = (K) field.get(value);
|
K key = (K) field.get(value);
|
||||||
map.put(key, value);
|
map.put(key, value);
|
||||||
} catch(IllegalArgumentException | IllegalAccessException e) {
|
} catch(Exception e) {
|
||||||
throw new IllegalArgumentException(String.format("Unable to find field %s. -> %s", keyFieldName, e.getMessage()));
|
throw new IllegalArgumentException(String.format("Unable to find field %s. -> %s", keyFieldName, e.getMessage()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -712,7 +670,7 @@ public class Utils {
|
|||||||
*/
|
*/
|
||||||
public static <E> Set<E> makeSet(Iterable<E> collection) {
|
public static <E> Set<E> makeSet(Iterable<E> collection) {
|
||||||
Set<E> set = new HashSet<>();
|
Set<E> set = new HashSet<>();
|
||||||
for (E item : safeList(collection)) {
|
for (E item : safeIterable(collection)) {
|
||||||
set.add(item);
|
set.add(item);
|
||||||
}
|
}
|
||||||
return set;
|
return set;
|
||||||
@ -756,15 +714,26 @@ public class Utils {
|
|||||||
* @return map with reverse key value pairs
|
* @return map with reverse key value pairs
|
||||||
*/
|
*/
|
||||||
public static <K,V> Map<K, V> reverseMap(Map<V, K> map) {
|
public static <K,V> Map<K, V> reverseMap(Map<V, K> map) {
|
||||||
Map<K, V> reverseMap = new HashMap<>();
|
return map.entrySet().stream().collect(Collectors.toMap(Entry::getValue, Entry::getKey));
|
||||||
|
|
||||||
for(Map.Entry<V, K> entry : map.entrySet()) {
|
|
||||||
reverseMap.put(entry.getValue(), entry.getKey());
|
|
||||||
}
|
|
||||||
|
|
||||||
return reverseMap;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create safe array e [ ].
|
||||||
|
*
|
||||||
|
* @param <E> the type parameter
|
||||||
|
* @param list the list
|
||||||
|
*
|
||||||
|
* @return the e [ ]
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <E> E[] safeArray(E[] list) {
|
||||||
|
if (!validateValue(list)) {
|
||||||
|
return (E[]) new Object[0];
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a safe list
|
* Creates a safe list
|
||||||
*
|
*
|
||||||
@ -780,21 +749,6 @@ public class Utils {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Safe list e [ ].
|
|
||||||
*
|
|
||||||
* @param <E> the type parameter
|
|
||||||
* @param list the list
|
|
||||||
*
|
|
||||||
* @return the e [ ]
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public static <E> E[] safeList(E[] list) {
|
|
||||||
if (!validateValue(list)) {
|
|
||||||
return (E[]) new Object[0];
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a safe list
|
* Creates a safe list
|
||||||
@ -804,22 +758,7 @@ public class Utils {
|
|||||||
*
|
*
|
||||||
* @return an empty list if list is invalid or the list if its valid
|
* @return an empty list if list is invalid or the list if its valid
|
||||||
*/
|
*/
|
||||||
public static <E> Iterable<E> safeList(Iterable<E> list) {
|
public static <E> Iterable<E> safeIterable(Iterable<E> list) {
|
||||||
if (!validateValue(list)) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a safe list
|
|
||||||
*
|
|
||||||
* @param <E> the type parameter
|
|
||||||
* @param list The list to check
|
|
||||||
*
|
|
||||||
* @return an empty list if list is invalid or the list if its valid
|
|
||||||
*/
|
|
||||||
public static <E> List<E> safeList(List<E> list) {
|
|
||||||
if (!validateValue(list)) {
|
if (!validateValue(list)) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
@ -907,7 +846,6 @@ public class Utils {
|
|||||||
*/
|
*/
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public static <E> Set<E> toSet(E... values) {
|
public static <E> Set<E> toSet(E... values) {
|
||||||
|
|
||||||
return new LinkedHashSet<>(Arrays.asList(values));
|
return new LinkedHashSet<>(Arrays.asList(values));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1002,7 +940,12 @@ public class Utils {
|
|||||||
*/
|
*/
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public static <V> boolean validateValues(V... objectsToValidate) {
|
public static <V> boolean validateValues(V... objectsToValidate) {
|
||||||
return validateValues(false, objectsToValidate);
|
return validateValues(false, toList(objectsToValidate));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public static <V> boolean validateValuesOr(V... objectsToValidate) {
|
||||||
|
return validateValues(true, toList(objectsToValidate));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1012,19 +955,13 @@ public class Utils {
|
|||||||
* @param <V> the expected class type of the objects passed in. All objects need to be of same type
|
* @param <V> the expected class type of the objects passed in. All objects need to be of same type
|
||||||
* @return true if the values are valid, false otherwise
|
* @return true if the values are valid, false otherwise
|
||||||
*/
|
*/
|
||||||
@SafeVarargs
|
public static <V> boolean validateValues(boolean or, List<V> objectsToValidate) {
|
||||||
public static <V> boolean validateValues(boolean or, V... objectsToValidate) {
|
|
||||||
boolean valid = !or;
|
boolean valid = !or;
|
||||||
try {
|
for (Object obj : objectsToValidate) {
|
||||||
for (Object obj : objectsToValidate) {
|
if (or && validateValue(obj)) {
|
||||||
if (or && validateValue(obj)) {
|
return true;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
valid &= validateValue(obj);
|
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
valid &= validateValue(obj);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return valid;
|
return valid;
|
||||||
|
@ -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.interfaces.AutoCloseableIterator;
|
||||||
import net.locusworks.common.utils.FileReader;
|
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.AfterAll;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.mockStatic;
|
||||||
|
|
||||||
public class FileReaderTest {
|
public class FileReaderTest {
|
||||||
|
|
||||||
@ -31,7 +37,7 @@ public class FileReaderTest {
|
|||||||
int count = ThreadLocalRandom.current().nextInt(100);
|
int count = ThreadLocalRandom.current().nextInt(100);
|
||||||
|
|
||||||
for (int i = 1; i <= count; i++) {
|
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());
|
numLines.put(i, randomString.length());
|
||||||
fos.write(randomString.getBytes());
|
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 net.locusworks.common.utils.HashUtils;
|
||||||
import org.apache.commons.codec.digest.DigestUtils;
|
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