More unit tests

This commit is contained in:
2023-09-19 19:53:15 -05:00
parent 3cd7639da1
commit 800cabda34
14 changed files with 1066 additions and 456 deletions

View File

@@ -41,15 +41,18 @@ public class AES {
private String seed;
private RandomString randomizer;
private void initSecureKey(String seed) {
try {
SecureRandom sr = getSecureRandom(seed);
KeyGenerator generator = KeyGenerator.getInstance(ENCRYPTION_TYPE);
generator.init(128, sr);
init(generator.generateKey().getEncoded(), sr);
randomizer = RandomString.newInstance(sr);
init(generator.generateKey().getEncoded());
} catch (Exception ex) {
System.err.println(ex);
throw new IllegalArgumentException("Unable to initalize encryption:", ex);
throw new IllegalArgumentException("Unable to initialize encryption:", ex);
}
}
@@ -69,13 +72,12 @@ public class AES {
/**
* Initialize the aes engine
* @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 {
this.cipher = Cipher.getInstance(ENCRYPTION_ALGORITH, PROVIDER);
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) {
throw new IllegalArgumentException("Unable to initialize encryption:", ex);
}
@@ -130,7 +132,7 @@ public class AES {
}
public static AES createInstance() {
return createInstance(RandomString.getString(16));
return createInstance(RandomString.getInstance().getString(16));
}
public static AES createInstance(byte[] byteSeed) {

View File

@@ -1,5 +1,7 @@
package net.locusworks.common.utils;
import java.util.Objects;
public class Checks {
public static void checkArguments(boolean expression, String error) {
@@ -19,7 +21,7 @@ public class Checks {
}
public static void checkNotNull(Object item, String error) {
if (item == null) throw new IllegalAccessError("Provided item is null");
Objects.requireNonNull(item, error);
}
}

View File

@@ -1,5 +1,6 @@
package net.locusworks.common.utils;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Objects;
import java.util.Random;
@@ -17,34 +18,30 @@ public class RandomString {
private Random random;
private char[] symbols;
private int length;
private static RandomString instance;
private RandomString(Integer length) {
this(length, new SecureRandom());
}
private RandomString(Integer length, Random random) {
this(length, random, ALPHA_NUMERIC);
}
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 RandomString() {
Random random;
try {
random = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
random = new SecureRandom();
}
init(random);
}
private synchronized final void setLength(int length) {
this.length = length;
private RandomString(Random random) {
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];
for (int index = 0; index < buffer.length; index++) {
buffer[index] = symbols[random.nextInt(symbols.length)];
@@ -52,28 +49,28 @@ public class RandomString {
return new String(buffer);
}
public static String getString(Integer length) {
if (instance == null) {
instance = new RandomString(length);
}
instance.setLength(length);
return instance.nextString();
public String getString(Integer length) {
return this.nextString(length);
}
public static String getString(Integer length, Random random) {
if (instance == null) {
instance = new RandomString(length);
}
instance.setLength(length);
instance.setRandom(random);
return instance.nextString();
}
public static byte[] getBytes(Integer length) {
public byte[] getBytes(Integer length) {
return getString(length).getBytes(UTF_8);
}
public static byte[] getBytes(Integer length, Random random) {
return getString(length, random).getBytes(UTF_8);
public static RandomString getInstance() {
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;
}
}

View File

@@ -8,56 +8,82 @@ import java.util.Map;
import static net.locusworks.common.utils.Checks.checkArguments;
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 {
private enum SplitterType {
PARTITION,
SEQUENCE
}
private String splitSeq;
private boolean omitEmptyStrings = false;
private int partition;
private int limit;
private final SplitterType splitterType;
private static Splitter splitter;
private Splitter(String seq) {
this.splitSeq = seq;
this.splitterType = SplitterType.SEQUENCE;
}
private Splitter(int partition) {
this.partition = partition;
this.splitterType = SplitterType.PARTITION;
}
/**
* Remove empty string from the resulting lists
* @return this
*/
public Splitter omitEmptyStrings() {
this.omitEmptyStrings = true;
return this;
}
/**
* Return a subset of the resulting list
* @param limit how many items to retrieve
* @return this
*/
public Splitter withLimit(int limit) {
this.limit = limit;
return this;
}
public MapSplitter withKeyValueSeparator(String separator) {
checkArguments(!Utils.isEmptyString(separator), "Key value separator cannot be empty or null");
return new MapSplitter(this, separator);
}
/**
* Return an array instead of a list
* @param sentence the string sentence to split
* @return this
*/
public String[] splitToArray(String sentence) {
List<String> list = split(sentence);
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) {
checkArguments(!Utils.isEmptyString(sentence), "provided value is null or empty");
List<String> list = new ArrayList<>();
if (!Utils.isEmptyString(splitSeq))
populateForTrimmer(sentence, list);
else
if (splitterType == SplitterType.PARTITION) {
populateForFixedWidth(sentence, list);
} else {
populateForTrimmer(sentence, list);
}
return limit > 0 ? list.subList(0, limit) : list;
}
private void populateForFixedWidth(String sentence, List<String> list) {
checkArguments(partition > 0, "Partition should be greater than 0");
int strLength = sentence.length();
for (int i = 0; i < 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) {
checkNotNull(splitSeq, "Split value provided was null");
for (String s : sentence.split(splitSeq)) {
if (s == null || (omitEmptyStrings && s.trim().isEmpty()))
if (omitEmptyStrings && s.trim().isEmpty())
continue;
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) {
checkArguments(partition > 0, "Partition has to be greater than 0");
splitter = new Splitter(partition);
return splitter;
}
/**
* Split the length on a specified string sequence
* @param split the sequence to split
* @return this
*/
public static Splitter on(String split) {
checkNotNull(split, "Split value provided was null");
splitter = new Splitter(split);
return splitter;
}
/**
* Split on new line sequence
* @return this
*/
public static Splitter onNewLine() {
return on("\\r?\\n");
}
/**
* Split on spaces
* @return this
*/
public static Splitter onSpace() {
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 {
private final Splitter splitter;
private String separator;
private final String separator;
private boolean skipInvalid = false;
private MapSplitter(Splitter splitter, String separator) {

View File

@@ -185,25 +185,18 @@ public class Utils {
throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Object clone = obj.getClass().getDeclaredConstructor().newInstance();
for (Field field : obj.getClass().getDeclaredFields()) {
try {
field.setAccessible(true);
if (field.get(obj) == null || Modifier.isFinal(field.getModifiers())){
continue;
}
field.setAccessible(true);
if (field.get(obj) == null || Modifier.isFinal(field.getModifiers())){
continue;
}
Class<?> type = field.getType();
Class<?> type = field.getType();
if (type.isPrimitive() || getWrapperTypes().contains(type)) {
field.set(clone, field.get(obj));
} else {
Object childObj = field.get(obj);
if (childObj == obj) {
field.set(clone, clone);
} else {
field.set(clone, cloneObject(field.get(obj)));
}
}
} catch (NullPointerException ignored) { }
if (isPrimitiveOrWrapper(type)) {
field.set(clone, field.get(obj));
} else {
field.set(clone, cloneObject(field.get(obj)));
}
}
return (O) clone;
}
@@ -279,8 +272,8 @@ public class Utils {
* @param <K> The expected class of the Iterable list
* @return The converted list
*/
public static <E, K> List<E> extractFieldToList(String fieldName, Iterable<K> list) {
return setToList(extractFieldToSet(fieldName, list));
public static <E, K> Set<E> extractFieldToSet(String fieldName, Iterable<K> list) {
return listToSet(extractFieldToList(fieldName, list));
}
/**
@@ -292,10 +285,10 @@ public class Utils {
* @return set with the desired items
*/
@SuppressWarnings("unchecked")
public static <E, K> Set<E> extractFieldToSet(String fieldName, Iterable<K> list) {
Set<E> newSet = new LinkedHashSet<>();
public static <E, K> List<E> extractFieldToList(String fieldName, Iterable<K> list) {
List<E> newSet = new ArrayList<>();
Field field;
for (K item : Utils.safeList(list)) {
for (K item : Utils.safeIterable(list)) {
try {
field = getField(item.getClass(), fieldName);
@@ -327,7 +320,7 @@ public class Utils {
Field field;
for (E item : safeList(list)) {
for (E item : safeIterable(list)) {
try {
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.
*
*/
@SuppressWarnings({ "unchecked", "unlikely-arg-type" })
@SuppressWarnings({"unlikely-arg-type" })
public static <E, K> E findValue(E itemToFind, String fieldName, List<K> list) {
for (K item : Utils.safeList(list)) {
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;
return findValues(itemToFind, fieldName, list).stream().findFirst().orElse(null);
}
/**
@@ -398,29 +373,12 @@ public class Utils {
* @param <K> - The expected class of the Iterable 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) {
List<E> tmpList = new ArrayList<>();
for (K item : Utils.safeList(list)) {
try {
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;
List<E> extracted = extractFieldToList(fieldName, list);
return extracted.stream()
.filter(Objects::nonNull)
.filter(v -> v.equals(valueToFind)).collect(Collectors.toList());
}
/**
@@ -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 <V> the type parameter
@@ -589,7 +547,7 @@ public class Utils {
Field field = getField(value.getClass(), keyFieldName);
K key = (K) field.get(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()));
}
}
@@ -712,7 +670,7 @@ public class Utils {
*/
public static <E> Set<E> makeSet(Iterable<E> collection) {
Set<E> set = new HashSet<>();
for (E item : safeList(collection)) {
for (E item : safeIterable(collection)) {
set.add(item);
}
return set;
@@ -756,15 +714,26 @@ public class Utils {
* @return map with reverse key value pairs
*/
public static <K,V> Map<K, V> reverseMap(Map<V, K> map) {
Map<K, V> reverseMap = new HashMap<>();
for(Map.Entry<V, K> entry : map.entrySet()) {
reverseMap.put(entry.getValue(), entry.getKey());
}
return reverseMap;
return map.entrySet().stream().collect(Collectors.toMap(Entry::getValue, Entry::getKey));
}
/**
* 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
*
@@ -780,21 +749,6 @@ public class Utils {
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
@@ -804,22 +758,7 @@ public class Utils {
*
* @return an empty list if list is invalid or the list if its valid
*/
public static <E> Iterable<E> safeList(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) {
public static <E> Iterable<E> safeIterable(Iterable<E> list) {
if (!validateValue(list)) {
return new ArrayList<>();
}
@@ -907,7 +846,6 @@ public class Utils {
*/
@SafeVarargs
public static <E> Set<E> toSet(E... values) {
return new LinkedHashSet<>(Arrays.asList(values));
}
@@ -1002,7 +940,12 @@ public class Utils {
*/
@SafeVarargs
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
* @return true if the values are valid, false otherwise
*/
@SafeVarargs
public static <V> boolean validateValues(boolean or, V... objectsToValidate) {
public static <V> boolean validateValues(boolean or, List<V> objectsToValidate) {
boolean valid = !or;
try {
for (Object obj : objectsToValidate) {
if (or && validateValue(obj)) {
return true;
}
valid &= validateValue(obj);
for (Object obj : objectsToValidate) {
if (or && validateValue(obj)) {
return true;
}
} catch (Exception ex) {
return false;
valid &= validateValue(obj);
}
return valid;