diff --git a/README.md b/README.md index 739e2a4..bcf1327 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,15 @@ "name" : "H2", "driver" : "org.h2.Driver", "url" : "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", - "username" : "", - "password" : "" + "username" : "****", + "password" : "****" + }, + { + "name" : "ORACLE", + "driver" : "oracle.jdbc.driver.OracleDriver", + "url" : "jdbc:oracle:thin:@server:port:SID", + "username" : "****", + "password" : "****" } ] ``` @@ -28,8 +35,8 @@ @Delete(sql = "delete from users where id = #{id}") public int delete(int id); - @Insert(sql = "insert into users (firstname, lastname, employee_id) values (#{firstname}, #{lastname}, #{employeeID})") - public int insert(@Param("firstname") String first, @Param("lastname") String last, @Param("employeeID") int id); + @Insert(sql = "insert into users (firstname, lastname, employee_id) values (#{firstname}, #{lastname}, #{employeeID})") + public int insert(@Param("firstname") String first, @Param("lastname") String last, @Param("employeeID") int id); } ``` diff --git a/src/main/java/com/ljunggren/storm/StormRepository.java b/src/main/java/com/ljunggren/storm/StormRepository.java index d990c1f..2218ba3 100644 --- a/src/main/java/com/ljunggren/storm/StormRepository.java +++ b/src/main/java/com/ljunggren/storm/StormRepository.java @@ -23,7 +23,7 @@ import com.ljunggren.storm.crud.SelectQuery; import com.ljunggren.storm.crud.UpdateQuery; import com.ljunggren.storm.exception.StormException; -import com.ljunggren.storm.utils.AnnotationUtils; +import com.ljunggren.storm.util.AnnotationUtils; public class StormRepository implements InvocationHandler { diff --git a/src/main/java/com/ljunggren/storm/builders/DeleteQueryBuilder.java b/src/main/java/com/ljunggren/storm/builders/DeleteQueryBuilder.java index 44c04c3..1f56845 100644 --- a/src/main/java/com/ljunggren/storm/builders/DeleteQueryBuilder.java +++ b/src/main/java/com/ljunggren/storm/builders/DeleteQueryBuilder.java @@ -5,7 +5,7 @@ import com.ljunggren.reflectionUtils.ReflectionUtils; import com.ljunggren.storm.annotation.entity.Id; -import com.ljunggren.storm.utils.AnnotationUtils; +import com.ljunggren.storm.util.AnnotationUtils; public class DeleteQueryBuilder extends QueryBuilder { diff --git a/src/main/java/com/ljunggren/storm/builders/InsertBatchQueryBuilder.java b/src/main/java/com/ljunggren/storm/builders/InsertBatchQueryBuilder.java index c43cd94..021f3c2 100644 --- a/src/main/java/com/ljunggren/storm/builders/InsertBatchQueryBuilder.java +++ b/src/main/java/com/ljunggren/storm/builders/InsertBatchQueryBuilder.java @@ -6,8 +6,8 @@ import java.util.stream.Collectors; import com.ljunggren.reflectionUtils.ReflectionUtils; -import com.ljunggren.storm.utils.AnnotationUtils; -import com.ljunggren.storm.utils.ExceptionUtils; +import com.ljunggren.storm.util.AnnotationUtils; +import com.ljunggren.storm.util.ExceptionUtils; public class InsertBatchQueryBuilder extends QueryBuilder { diff --git a/src/main/java/com/ljunggren/storm/builders/InsertQueryBuilder.java b/src/main/java/com/ljunggren/storm/builders/InsertQueryBuilder.java index 2c7c57a..eb34329 100644 --- a/src/main/java/com/ljunggren/storm/builders/InsertQueryBuilder.java +++ b/src/main/java/com/ljunggren/storm/builders/InsertQueryBuilder.java @@ -7,8 +7,8 @@ import org.apache.commons.lang3.StringUtils; import com.ljunggren.reflectionUtils.ReflectionUtils; -import com.ljunggren.storm.utils.AnnotationUtils; -import com.ljunggren.storm.utils.ExceptionUtils; +import com.ljunggren.storm.util.AnnotationUtils; +import com.ljunggren.storm.util.ExceptionUtils; public class InsertQueryBuilder extends QueryBuilder { diff --git a/src/main/java/com/ljunggren/storm/builders/QueryBuilder.java b/src/main/java/com/ljunggren/storm/builders/QueryBuilder.java index 70e6252..8d3ac8c 100644 --- a/src/main/java/com/ljunggren/storm/builders/QueryBuilder.java +++ b/src/main/java/com/ljunggren/storm/builders/QueryBuilder.java @@ -8,7 +8,7 @@ import com.ljunggren.reflectionUtils.ReflectionUtils; import com.ljunggren.storm.annotation.entity.ColumnProperty; import com.ljunggren.storm.annotation.entity.Table; -import com.ljunggren.storm.utils.AnnotationUtils; +import com.ljunggren.storm.util.AnnotationUtils; public abstract class QueryBuilder { diff --git a/src/main/java/com/ljunggren/storm/builders/UpdateQueryBuilder.java b/src/main/java/com/ljunggren/storm/builders/UpdateQueryBuilder.java index e75e69a..4f3dcb9 100644 --- a/src/main/java/com/ljunggren/storm/builders/UpdateQueryBuilder.java +++ b/src/main/java/com/ljunggren/storm/builders/UpdateQueryBuilder.java @@ -8,8 +8,8 @@ import com.ljunggren.reflectionUtils.ReflectionUtils; import com.ljunggren.storm.annotation.entity.Id; -import com.ljunggren.storm.utils.AnnotationUtils; -import com.ljunggren.storm.utils.ExceptionUtils; +import com.ljunggren.storm.util.AnnotationUtils; +import com.ljunggren.storm.util.ExceptionUtils; public class UpdateQueryBuilder extends QueryBuilder { diff --git a/src/main/java/com/ljunggren/storm/context/ContextFactory.java b/src/main/java/com/ljunggren/storm/context/ContextFactory.java index 3297fd0..b753246 100644 --- a/src/main/java/com/ljunggren/storm/context/ContextFactory.java +++ b/src/main/java/com/ljunggren/storm/context/ContextFactory.java @@ -8,7 +8,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.ljunggren.storm.exception.ContextException; -import com.ljunggren.storm.utils.JSONUtils; +import com.ljunggren.storm.util.JSONUtils; public class ContextFactory { diff --git a/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java b/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java index 7aaa2ce..8b31466 100644 --- a/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java @@ -12,7 +12,7 @@ import com.ljunggren.storm.builders.DeleteQueryBuilder; import com.ljunggren.storm.builders.QueryBuilder; import com.ljunggren.storm.context.Context; -import com.ljunggren.storm.utils.ExceptionUtils; +import com.ljunggren.storm.util.ExceptionUtils; public class DeleteQuery extends QueryChain { diff --git a/src/main/java/com/ljunggren/storm/crud/InsertBatchQuery.java b/src/main/java/com/ljunggren/storm/crud/InsertBatchQuery.java index cc407c9..6a429cb 100644 --- a/src/main/java/com/ljunggren/storm/crud/InsertBatchQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/InsertBatchQuery.java @@ -11,7 +11,7 @@ import com.ljunggren.storm.builders.InsertBatchQueryBuilder; import com.ljunggren.storm.builders.QueryBuilder; import com.ljunggren.storm.context.Context; -import com.ljunggren.storm.utils.ExceptionUtils; +import com.ljunggren.storm.util.ExceptionUtils; public class InsertBatchQuery extends QueryChain { diff --git a/src/main/java/com/ljunggren/storm/crud/InsertQuery.java b/src/main/java/com/ljunggren/storm/crud/InsertQuery.java index e5c3c59..42aefac 100644 --- a/src/main/java/com/ljunggren/storm/crud/InsertQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/InsertQuery.java @@ -12,7 +12,7 @@ import com.ljunggren.storm.builders.InsertQueryBuilder; import com.ljunggren.storm.builders.QueryBuilder; import com.ljunggren.storm.context.Context; -import com.ljunggren.storm.utils.ExceptionUtils; +import com.ljunggren.storm.util.ExceptionUtils; public class InsertQuery extends QueryChain { diff --git a/src/main/java/com/ljunggren/storm/crud/QueryChain.java b/src/main/java/com/ljunggren/storm/crud/QueryChain.java index a4fe7df..7519a27 100644 --- a/src/main/java/com/ljunggren/storm/crud/QueryChain.java +++ b/src/main/java/com/ljunggren/storm/crud/QueryChain.java @@ -13,8 +13,9 @@ import java.util.stream.IntStream; import com.ljunggren.storm.context.Context; +import com.ljunggren.storm.exception.StormException; import com.ljunggren.storm.mapper.ResultSetMapper; -import com.ljunggren.storm.utils.ParameterUtils; +import com.ljunggren.storm.util.ParameterUtils; public abstract class QueryChain { @@ -100,6 +101,9 @@ preparedStatement.setObject(1, arguments[0]); return preparedStatement; } + if (parameterArgumentMap.isEmpty() && parameters.length > 1) { + throw new StormException("Multiple Storm parameters must be annotated with @Param"); + } for (int i = 0; i < parameterIds.size(); i++) { preparedStatement.setObject(i + 1, parameterArgumentMap.get(parameterIds.get(i))); } @@ -107,16 +111,16 @@ } - protected int executeBatch(String sql, Context context, Object[] argsArray, Type returnType) throws SQLException { + protected int executeBatch(String sql, Context context, Object[] argumentArray, Type returnType) throws SQLException { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = context.getConnection(); String preparedSql = ParameterUtils.replaceParamaterIdsWithQuestionMarks(sql); preparedStatement = connection.prepareStatement(preparedSql); - for (Object args: argsArray) { + for (Object arguments: argumentArray) { // TODO: Refactor so args does not have to be cast to array - setParameters(preparedStatement, (Object[]) args); + setParameters(preparedStatement, (Object[]) arguments); preparedStatement.addBatch(); if (peek != null) { peek.accept(preparedStatement.toString()); @@ -128,7 +132,7 @@ closeConnection(connection); } } - + private void setParameters(PreparedStatement preparedStatement, Object[] args) throws SQLException { if (args != null) { for (int i = 0; i < args.length; i++) { diff --git a/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java b/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java index b5689c5..d5dbf3a 100644 --- a/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java @@ -12,7 +12,7 @@ import com.ljunggren.storm.builders.QueryBuilder; import com.ljunggren.storm.builders.UpdateQueryBuilder; import com.ljunggren.storm.context.Context; -import com.ljunggren.storm.utils.ExceptionUtils; +import com.ljunggren.storm.util.ExceptionUtils; public class UpdateQuery extends QueryChain { diff --git a/src/main/java/com/ljunggren/storm/mapper/MapperChain.java b/src/main/java/com/ljunggren/storm/mapper/MapperChain.java index 6b44f0a..1873705 100644 --- a/src/main/java/com/ljunggren/storm/mapper/MapperChain.java +++ b/src/main/java/com/ljunggren/storm/mapper/MapperChain.java @@ -12,7 +12,7 @@ import com.ljunggren.reflectionUtils.ReflectionUtils; import com.ljunggren.storm.annotation.entity.ColumnProperty; -import com.ljunggren.storm.utils.AnnotationUtils; +import com.ljunggren.storm.util.AnnotationUtils; public abstract class MapperChain { diff --git a/src/main/java/com/ljunggren/storm/util/AnnotationUtils.java b/src/main/java/com/ljunggren/storm/util/AnnotationUtils.java new file mode 100644 index 0000000..f77e9b4 --- /dev/null +++ b/src/main/java/com/ljunggren/storm/util/AnnotationUtils.java @@ -0,0 +1,52 @@ +package com.ljunggren.storm.util; + +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.List; + +import com.ljunggren.storm.annotation.entity.Id; +import com.ljunggren.storm.annotation.entity.Transient; +import com.ljunggren.storm.entity.Generated; + +public class AnnotationUtils { + + @SuppressWarnings("unchecked") + public static T findFieldByAnnotation(Class annotationClazz, List fields) { + return (T) fields.stream() + .filter(field -> AnnotationUtils.getAnnotationFromField(Id.class, field) != null) + .findFirst() + .orElse(null); + } + + @SuppressWarnings("unchecked") + public static T getAnnotationFromField(Class annotationClazz, Field field) { + return (T) Arrays.stream(field.getAnnotations()) + .filter(annotation -> annotation.annotationType() == annotationClazz) + .findFirst() + .orElse(null); + } + + @SuppressWarnings("unchecked") + public static T getAnnotationFromClass(Class annotationClazz, Class clazz) { + return (T) Arrays.stream(clazz.getAnnotations()) + .filter(annotation -> annotation.annotationType() == annotationClazz) + .findFirst() + .orElse(null); + } + + public static boolean isAutoGeneratedId(Field field) { + if (field == null) { + return false; + } + Id id = getAnnotationFromField(Id.class, field); + if (id == null) { + return false; + } + return id.generated() == Generated.AUTO; + } + + public static boolean isTransient(Field field) { + return getAnnotationFromField(Transient.class, field) != null; + } + +} diff --git a/src/main/java/com/ljunggren/storm/util/ExceptionUtils.java b/src/main/java/com/ljunggren/storm/util/ExceptionUtils.java new file mode 100644 index 0000000..e6113b5 --- /dev/null +++ b/src/main/java/com/ljunggren/storm/util/ExceptionUtils.java @@ -0,0 +1,22 @@ +package com.ljunggren.storm.util; + +import java.util.function.Function; + +public class ExceptionUtils { + + @FunctionalInterface + public interface FunctionWithException { + R apply(T t) throws E; + } + + public static Function rethrowFunction(FunctionWithException fe) { + return arg -> { + try { + return fe.apply(arg); + } catch (Exception e) { + throw new RuntimeException(e); + } + }; + } + +} diff --git a/src/main/java/com/ljunggren/storm/util/JSONUtils.java b/src/main/java/com/ljunggren/storm/util/JSONUtils.java new file mode 100644 index 0000000..29561a9 --- /dev/null +++ b/src/main/java/com/ljunggren/storm/util/JSONUtils.java @@ -0,0 +1,40 @@ +package com.ljunggren.storm.util; + +import java.io.IOException; +import java.util.List; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JSONUtils { + + public static boolean isValidJSON(String json) { + if (json == null) { + return false; + } + try { + final ObjectMapper mapper = new ObjectMapper(); + mapper.readTree(json); + return true; + } catch (IOException e) { + return false; + } + } + + public static String objectToJson(Object object) throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + String json = ""; + json = mapper.writeValueAsString(object); + return json; + } + + @SuppressWarnings("rawtypes") + public static T jsonToListObject(String json, Class clazz) throws JsonMappingException, JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz); + return mapper.readValue(json, type); + } + +} diff --git a/src/main/java/com/ljunggren/storm/util/ParameterUtils.java b/src/main/java/com/ljunggren/storm/util/ParameterUtils.java new file mode 100644 index 0000000..016756c --- /dev/null +++ b/src/main/java/com/ljunggren/storm/util/ParameterUtils.java @@ -0,0 +1,56 @@ +package com.ljunggren.storm.util; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Parameter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.IntStream; + +import com.ljunggren.storm.annotation.Param; + +public class ParameterUtils { + + private final static String parameterRegex = "#\\{[a-zA-Z0-9_]+\\}"; + + public static Map mapArgumentsToParameterNames(Parameter[] parameters, Object[] arguments) { + String[] parameterNames = getParameterNames(parameters); + return IntStream.range(0, parameterNames.length) + .collect(HashMap::new, (m, i) -> m.put(parameterNames[i], arguments[i]), Map::putAll); + } + + public static String[] getParameterNames(Parameter[] parameters) { + return Arrays.stream(parameters) + .map(parameter -> getParameterName(parameter)) + .filter(name -> null != name) + .toArray(String[]::new); + } + + public static String getParameterName(Parameter parameter) { + Annotation[] annotations = parameter.getAnnotations(); + return Arrays.stream(annotations) + .filter(annotation -> annotation.annotationType() == Param.class) + .map(annotation -> ((Param) annotation).value()) + .findFirst() + .orElse(null); + } + + public static List findParameterIds(String sql) { + List parameterIds = new ArrayList<>(); + Pattern pattern = Pattern.compile(parameterRegex); + Matcher matcher = pattern.matcher(sql); + while (matcher.find()) { + parameterIds.add(matcher.group(0).replace("#{", "").replace("}", "")); + } + return parameterIds; + } + + public static String replaceParamaterIdsWithQuestionMarks(String sql) { + return sql.replaceAll(parameterRegex, "?"); + } + +} diff --git a/src/main/java/com/ljunggren/storm/utils/AnnotationUtils.java b/src/main/java/com/ljunggren/storm/utils/AnnotationUtils.java deleted file mode 100644 index d9c0e05..0000000 --- a/src/main/java/com/ljunggren/storm/utils/AnnotationUtils.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.ljunggren.storm.utils; - -import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.List; - -import com.ljunggren.storm.annotation.entity.Id; -import com.ljunggren.storm.annotation.entity.Transient; -import com.ljunggren.storm.entity.Generated; - -public class AnnotationUtils { - - @SuppressWarnings("unchecked") - public static T findFieldByAnnotation(Class annotationClazz, List fields) { - return (T) fields.stream() - .filter(field -> AnnotationUtils.getAnnotationFromField(Id.class, field) != null) - .findFirst() - .orElse(null); - } - - @SuppressWarnings("unchecked") - public static T getAnnotationFromField(Class annotationClazz, Field field) { - return (T) Arrays.stream(field.getAnnotations()) - .filter(annotation -> annotation.annotationType() == annotationClazz) - .findFirst() - .orElse(null); - } - - @SuppressWarnings("unchecked") - public static T getAnnotationFromClass(Class annotationClazz, Class clazz) { - return (T) Arrays.stream(clazz.getAnnotations()) - .filter(annotation -> annotation.annotationType() == annotationClazz) - .findFirst() - .orElse(null); - } - - public static boolean isAutoGeneratedId(Field field) { - if (field == null) { - return false; - } - Id id = getAnnotationFromField(Id.class, field); - if (id == null) { - return false; - } - return id.generated() == Generated.AUTO; - } - - public static boolean isTransient(Field field) { - return getAnnotationFromField(Transient.class, field) != null; - } - -} diff --git a/src/main/java/com/ljunggren/storm/utils/ExceptionUtils.java b/src/main/java/com/ljunggren/storm/utils/ExceptionUtils.java deleted file mode 100644 index 31ab2f9..0000000 --- a/src/main/java/com/ljunggren/storm/utils/ExceptionUtils.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.ljunggren.storm.utils; - -import java.util.function.Function; - -public class ExceptionUtils { - - @FunctionalInterface - public interface FunctionWithException { - R apply(T t) throws E; - } - - public static Function rethrowFunction(FunctionWithException fe) { - return arg -> { - try { - return fe.apply(arg); - } catch (Exception e) { - throw new RuntimeException(e); - } - }; - } - -} diff --git a/src/main/java/com/ljunggren/storm/utils/JSONUtils.java b/src/main/java/com/ljunggren/storm/utils/JSONUtils.java deleted file mode 100644 index 068ae1a..0000000 --- a/src/main/java/com/ljunggren/storm/utils/JSONUtils.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.ljunggren.storm.utils; - -import java.io.IOException; -import java.util.List; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JavaType; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JSONUtils { - - public static boolean isValidJSON(String json) { - if (json == null) { - return false; - } - try { - final ObjectMapper mapper = new ObjectMapper(); - mapper.readTree(json); - return true; - } catch (IOException e) { - return false; - } - } - - public static String objectToJson(Object object) throws JsonProcessingException { - ObjectMapper mapper = new ObjectMapper(); - String json = ""; - json = mapper.writeValueAsString(object); - return json; - } - - @SuppressWarnings("rawtypes") - public static T jsonToListObject(String json, Class clazz) throws JsonMappingException, JsonProcessingException { - ObjectMapper mapper = new ObjectMapper(); - JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz); - return mapper.readValue(json, type); - } - -} diff --git a/src/main/java/com/ljunggren/storm/utils/ParameterUtils.java b/src/main/java/com/ljunggren/storm/utils/ParameterUtils.java deleted file mode 100644 index 089b3a4..0000000 --- a/src/main/java/com/ljunggren/storm/utils/ParameterUtils.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.ljunggren.storm.utils; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Parameter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.IntStream; - -import com.ljunggren.storm.annotation.Param; - -public class ParameterUtils { - - private final static String parameterRegex = "#\\{[a-zA-Z0-9_]+\\}"; - - public static Map mapArgumentsToParameterNames(Parameter[] parameters, Object[] arguments) { - String[] parameterNames = getParameterNames(parameters); - return IntStream.range(0, parameterNames.length) - .collect(HashMap::new, (m, i) -> m.put(parameterNames[i], arguments[i]), Map::putAll); - } - - public static String[] getParameterNames(Parameter[] parameters) { - return Arrays.stream(parameters) - .map(parameter -> getParameterName(parameter)) - .filter(name -> null != name) - .toArray(String[]::new); - } - - public static String getParameterName(Parameter parameter) { - Annotation[] annotations = parameter.getAnnotations(); - return Arrays.stream(annotations) - .filter(annotation -> annotation.annotationType() == Param.class) - .map(annotation -> ((Param) annotation).value()) - .findFirst() - .orElse(null); - } - - public static List findParameterIds(String sql) { - List parameterIds = new ArrayList<>(); - Pattern pattern = Pattern.compile(parameterRegex); - Matcher matcher = pattern.matcher(sql); - while (matcher.find()) { - parameterIds.add(matcher.group(0).replace("#{", "").replace("}", "")); - } - return parameterIds; - } - - public static String replaceParamaterIdsWithQuestionMarks(String sql) { - return sql.replaceAll(parameterRegex, "?"); - } - -} diff --git a/src/test/java/com/ljunggren/storm/util/AnnotationUtilsTest.java b/src/test/java/com/ljunggren/storm/util/AnnotationUtilsTest.java new file mode 100644 index 0000000..374223d --- /dev/null +++ b/src/test/java/com/ljunggren/storm/util/AnnotationUtilsTest.java @@ -0,0 +1,103 @@ +package com.ljunggren.storm.util; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.util.List; + +import org.junit.Test; + +import com.ljunggren.reflectionUtils.ReflectionUtils; +import com.ljunggren.storm.TestUser; +import com.ljunggren.storm.annotation.Database; +import com.ljunggren.storm.annotation.entity.Id; +import com.ljunggren.storm.annotation.entity.Table; +import com.ljunggren.storm.annotation.entity.Transient; +import com.ljunggren.storm.context.Context; +import com.ljunggren.storm.entity.Generated; + +import lombok.Data; + +public class AnnotationUtilsTest { + + @Data + private class TestId { + + @Id(generated = Generated.NONE) + private int id; + + } + + @Test + public void instantiationTest() { + AnnotationUtils annotationUtils = new AnnotationUtils(); + assertTrue(annotationUtils instanceof AnnotationUtils); + } + + @Test + public void findFieldByAnnotationTest() { + List fields = ReflectionUtils.getObjectFields(TestUser.class); + Field field = AnnotationUtils.findFieldByAnnotation(Id.class, fields); + assertTrue(field != null); + } + + @Test + public void findFieldByAnnotationNoFieldTest() { + List fields = ReflectionUtils.getObjectFields(Context.class); + Field field = AnnotationUtils.findFieldByAnnotation(Id.class, fields); + assertTrue(field == null); + } + + @Test + public void getAnnotationFromFieldTest() { + List fields = ReflectionUtils.getObjectFields(TestUser.class); + Field field = AnnotationUtils.findFieldByAnnotation(Id.class, fields); + Annotation annotation = AnnotationUtils.getAnnotationFromField(Id.class, field); + assertTrue(annotation instanceof Id); + } + + @Test + public void getAnnotationFromFieldNoAnnotationTest() { + List fields = ReflectionUtils.getObjectFields(TestUser.class); + Field field = AnnotationUtils.findFieldByAnnotation(Id.class, fields); + Annotation annotation = AnnotationUtils.getAnnotationFromField(Database.class, field); + assertTrue(annotation == null); + } + + @Test + public void getAnnotationFromClassTest() { + Annotation annotation = AnnotationUtils.getAnnotationFromClass(Table.class, TestUser.class); + assertTrue(annotation instanceof Table); + } + + @Test + public void getAnnotationFromClassNoAnnotationTest() { + Annotation annotation = AnnotationUtils.getAnnotationFromClass(Transient.class, TestUser.class); + assertTrue(annotation == null); + } + + @Test + public void isAutoGeneratedIdTest() { + List fields = ReflectionUtils.getObjectFields(TestUser.class); + Field field = AnnotationUtils.findFieldByAnnotation(Id.class, fields); + boolean isAutogenerated = AnnotationUtils.isAutoGeneratedId(field); + assertTrue(isAutogenerated); + } + + @Test + public void isAutoGeneratedIdNotGeneratedTest() { + List fields = ReflectionUtils.getObjectFields(TestId.class); + Field field = AnnotationUtils.findFieldByAnnotation(Id.class, fields); + boolean isAutogenerated = AnnotationUtils.isAutoGeneratedId(field); + assertFalse(isAutogenerated); + } + + @Test + public void isAutoGeneratedIdNullTest() { + boolean isAutogenerated = AnnotationUtils.isAutoGeneratedId(null); + assertFalse(isAutogenerated); + } + +} diff --git a/src/test/java/com/ljunggren/storm/util/ExceptionUtilsTest.java b/src/test/java/com/ljunggren/storm/util/ExceptionUtilsTest.java new file mode 100644 index 0000000..85ade48 --- /dev/null +++ b/src/test/java/com/ljunggren/storm/util/ExceptionUtilsTest.java @@ -0,0 +1,34 @@ +package com.ljunggren.storm.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.stream.Collectors; + +import org.junit.Test; + +public class ExceptionUtilsTest { + + @Test + public void instantiationTest() { + ExceptionUtils exceptionUtils = new ExceptionUtils(); + assertTrue(exceptionUtils instanceof ExceptionUtils); + } + + @Test + public void rethrowFunctionTest() { + int sum = Arrays.stream(new Integer[] {1, 1}) + .map(ExceptionUtils.rethrowFunction(value -> 1/value)) + .collect(Collectors.summingInt(Integer::intValue)); + assertEquals(2, sum); + } + + @Test(expected = Exception.class) + public void rethrowFunctionThrowTest() { + Arrays.stream(new Integer[] {0, 1}) + .map(ExceptionUtils.rethrowFunction(value -> 1/value)) + .collect(Collectors.summingInt(Integer::intValue)); + } + +} diff --git a/src/test/java/com/ljunggren/storm/util/JSONUtilsTest.java b/src/test/java/com/ljunggren/storm/util/JSONUtilsTest.java new file mode 100644 index 0000000..bac8796 --- /dev/null +++ b/src/test/java/com/ljunggren/storm/util/JSONUtilsTest.java @@ -0,0 +1,78 @@ +package com.ljunggren.storm.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.ljunggren.storm.TestUser; + +public class JSONUtilsTest { + + private TestUser getUser() { + TestUser user = new TestUser(); + user.setFirstName("Alex"); + user.setEmployeeID(100); + return user; + } + + @Test + public void instantiationTest() { + JSONUtils jsonUtils = new JSONUtils(); + assertTrue(jsonUtils instanceof JSONUtils); + } + + @Test + public void isValidJsonTest() throws JsonGenerationException, JsonMappingException, IOException { + String json = JSONUtils.objectToJson(getUser()); + boolean actual = JSONUtils.isValidJSON(json); + assertTrue(actual); + } + + @Test + public void isValidJsonFailTest() throws JsonGenerationException, JsonMappingException, IOException { + String json = JSONUtils.objectToJson(getUser()); + String brokenJson = json.substring(0, json.length()/2); + boolean actual = JSONUtils.isValidJSON(brokenJson); + assertFalse(actual); + } + + @Test + public void isValidJsonNullValueTest() throws JsonParseException, IOException { + boolean actual = JSONUtils.isValidJSON(null); + assertFalse(actual); + } + + @Test + public void objectToJsonTest() throws JsonGenerationException, JsonMappingException, IOException { + String json = JSONUtils.objectToJson(getUser()); + Boolean valid = JSONUtils.isValidJSON(json); + assertTrue(valid); + } + + @Test + public void objectToJsonNullTest() throws JsonGenerationException, JsonMappingException, IOException { + String json = JSONUtils.objectToJson(null); + Boolean valid = JSONUtils.isValidJSON(json); + assertTrue(valid); + } + + @Test + public void jsonToListObjectTest() throws JsonMappingException, JsonProcessingException { + List expected = Arrays.asList(new String[] {"Alex", "Christie", "Gage"}); + String json = JSONUtils.objectToJson(expected); + List actual = JSONUtils.jsonToListObject(json, String.class); + assertEquals(expected, actual); + } + + +} diff --git a/src/test/java/com/ljunggren/storm/util/ParameterUtilsTest.java b/src/test/java/com/ljunggren/storm/util/ParameterUtilsTest.java new file mode 100644 index 0000000..0361327 --- /dev/null +++ b/src/test/java/com/ljunggren/storm/util/ParameterUtilsTest.java @@ -0,0 +1,70 @@ +package com.ljunggren.storm.util; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.util.List; +import java.util.Map; + +import org.junit.Test; + +import com.ljunggren.storm.annotation.Param; + +public class ParameterUtilsTest { + + private class DummyClass { + @SuppressWarnings("unused") + public void find(@Param("id") int id, @Param("asc") boolean asc) {} + } + + + @Test + public void mapArgumentsToParameterNamesTest() throws NoSuchMethodException, SecurityException { + Object[] arguments = new Object[] { 1, true}; + Method method = DummyClass.class.getMethod("find", int.class, boolean.class); + Parameter[] parameters = method.getParameters(); + String[] parameterNames = ParameterUtils.getParameterNames(parameters); + Map map = ParameterUtils.mapArgumentsToParameterNames(parameters, arguments); + assertEquals(2, map.size()); + assertEquals(arguments[0], map.get(parameterNames[0])); + assertEquals(arguments[1], map.get(parameterNames[1])); + } + + @Test + public void findParameterIdsTest() { + String sql = "select * from users whesre id = #{id} and active = #{active}"; + List parameterIds = ParameterUtils.findParameterIds(sql); + assertEquals(2, parameterIds.size()); + assertEquals("id", parameterIds.get(0)); + assertEquals("active", parameterIds.get(1)); + } + + @Test + public void getParameterNamesTest() throws NoSuchMethodException, SecurityException { + Method method = DummyClass.class.getMethod("find", int.class, boolean.class); + Parameter[] parameters = method.getParameters(); + String[] parameterNames = ParameterUtils.getParameterNames(parameters); + assertEquals(2, parameterNames.length); + assertEquals("id", parameterNames[0]); + assertEquals("asc", parameterNames[1]); + } + + @Test + public void findParameterIdsWithOutSpacesTest() { + String sql = "select * from users where id in (#{id1},#{id2})"; + List parameterIds = ParameterUtils.findParameterIds(sql); + assertEquals(2, parameterIds.size()); + assertEquals("id1", parameterIds.get(0)); + assertEquals("id2", parameterIds.get(1)); + } + + @Test + public void replaceParamaterIdsWithQuestionMarksTest() { + String sql = "select * from users where id = #{id} and active = #{active}"; + String actual = ParameterUtils.replaceParamaterIdsWithQuestionMarks(sql); + String expected = "select * from users where id = ? and active = ?"; + assertEquals(expected, actual); + } + +} diff --git a/src/test/java/com/ljunggren/storm/utils/AnnotationUtilsTest.java b/src/test/java/com/ljunggren/storm/utils/AnnotationUtilsTest.java deleted file mode 100644 index fe933df..0000000 --- a/src/test/java/com/ljunggren/storm/utils/AnnotationUtilsTest.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.ljunggren.storm.utils; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; -import java.util.List; - -import org.junit.Test; - -import com.ljunggren.reflectionUtils.ReflectionUtils; -import com.ljunggren.storm.TestUser; -import com.ljunggren.storm.annotation.Database; -import com.ljunggren.storm.annotation.entity.Id; -import com.ljunggren.storm.annotation.entity.Table; -import com.ljunggren.storm.annotation.entity.Transient; -import com.ljunggren.storm.context.Context; -import com.ljunggren.storm.entity.Generated; - -import lombok.Data; - -public class AnnotationUtilsTest { - - @Data - private class TestId { - - @Id(generated = Generated.NONE) - private int id; - - } - - @Test - public void instantiationTest() { - AnnotationUtils annotationUtils = new AnnotationUtils(); - assertTrue(annotationUtils instanceof AnnotationUtils); - } - - @Test - public void findFieldByAnnotationTest() { - List fields = ReflectionUtils.getObjectFields(TestUser.class); - Field field = AnnotationUtils.findFieldByAnnotation(Id.class, fields); - assertTrue(field != null); - } - - @Test - public void findFieldByAnnotationNoFieldTest() { - List fields = ReflectionUtils.getObjectFields(Context.class); - Field field = AnnotationUtils.findFieldByAnnotation(Id.class, fields); - assertTrue(field == null); - } - - @Test - public void getAnnotationFromFieldTest() { - List fields = ReflectionUtils.getObjectFields(TestUser.class); - Field field = AnnotationUtils.findFieldByAnnotation(Id.class, fields); - Annotation annotation = AnnotationUtils.getAnnotationFromField(Id.class, field); - assertTrue(annotation instanceof Id); - } - - @Test - public void getAnnotationFromFieldNoAnnotationTest() { - List fields = ReflectionUtils.getObjectFields(TestUser.class); - Field field = AnnotationUtils.findFieldByAnnotation(Id.class, fields); - Annotation annotation = AnnotationUtils.getAnnotationFromField(Database.class, field); - assertTrue(annotation == null); - } - - @Test - public void getAnnotationFromClassTest() { - Annotation annotation = AnnotationUtils.getAnnotationFromClass(Table.class, TestUser.class); - assertTrue(annotation instanceof Table); - } - - @Test - public void getAnnotationFromClassNoAnnotationTest() { - Annotation annotation = AnnotationUtils.getAnnotationFromClass(Transient.class, TestUser.class); - assertTrue(annotation == null); - } - - @Test - public void isAutoGeneratedIdTest() { - List fields = ReflectionUtils.getObjectFields(TestUser.class); - Field field = AnnotationUtils.findFieldByAnnotation(Id.class, fields); - boolean isAutogenerated = AnnotationUtils.isAutoGeneratedId(field); - assertTrue(isAutogenerated); - } - - @Test - public void isAutoGeneratedIdNotGeneratedTest() { - List fields = ReflectionUtils.getObjectFields(TestId.class); - Field field = AnnotationUtils.findFieldByAnnotation(Id.class, fields); - boolean isAutogenerated = AnnotationUtils.isAutoGeneratedId(field); - assertFalse(isAutogenerated); - } - - @Test - public void isAutoGeneratedIdNullTest() { - boolean isAutogenerated = AnnotationUtils.isAutoGeneratedId(null); - assertFalse(isAutogenerated); - } - -} diff --git a/src/test/java/com/ljunggren/storm/utils/ExceptionUtilsTest.java b/src/test/java/com/ljunggren/storm/utils/ExceptionUtilsTest.java deleted file mode 100644 index 273cb5f..0000000 --- a/src/test/java/com/ljunggren/storm/utils/ExceptionUtilsTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.ljunggren.storm.utils; - -import static org.junit.Assert.*; - -import java.util.Arrays; -import java.util.stream.Collectors; - -import org.junit.Test; - -public class ExceptionUtilsTest { - - @Test - public void instantiationTest() { - ExceptionUtils exceptionUtils = new ExceptionUtils(); - assertTrue(exceptionUtils instanceof ExceptionUtils); - } - - @Test - public void rethrowFunctionTest() { - int sum = Arrays.stream(new Integer[] {1, 1}) - .map(ExceptionUtils.rethrowFunction(value -> 1/value)) - .collect(Collectors.summingInt(Integer::intValue)); - assertEquals(2, sum); - } - - @Test(expected = Exception.class) - public void rethrowFunctionThrowTest() { - Arrays.stream(new Integer[] {0, 1}) - .map(ExceptionUtils.rethrowFunction(value -> 1/value)) - .collect(Collectors.summingInt(Integer::intValue)); - } - -} diff --git a/src/test/java/com/ljunggren/storm/utils/JSONUtilsTest.java b/src/test/java/com/ljunggren/storm/utils/JSONUtilsTest.java deleted file mode 100644 index 7b85826..0000000 --- a/src/test/java/com/ljunggren/storm/utils/JSONUtilsTest.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.ljunggren.storm.utils; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.util.Arrays; -import java.util.List; - -import org.junit.Test; - -import com.fasterxml.jackson.core.JsonGenerationException; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.ljunggren.storm.TestUser; - -public class JSONUtilsTest { - - private TestUser getUser() { - TestUser user = new TestUser(); - user.setFirstName("Alex"); - user.setEmployeeID(100); - return user; - } - - @Test - public void instantiationTest() { - JSONUtils jsonUtils = new JSONUtils(); - assertTrue(jsonUtils instanceof JSONUtils); - } - - @Test - public void isValidJsonTest() throws JsonGenerationException, JsonMappingException, IOException { - String json = JSONUtils.objectToJson(getUser()); - boolean actual = JSONUtils.isValidJSON(json); - assertTrue(actual); - } - - @Test - public void isValidJsonFailTest() throws JsonGenerationException, JsonMappingException, IOException { - String json = JSONUtils.objectToJson(getUser()); - String brokenJson = json.substring(0, json.length()/2); - boolean actual = JSONUtils.isValidJSON(brokenJson); - assertFalse(actual); - } - - @Test - public void isValidJsonNullValueTest() throws JsonParseException, IOException { - boolean actual = JSONUtils.isValidJSON(null); - assertFalse(actual); - } - - @Test - public void objectToJsonTest() throws JsonGenerationException, JsonMappingException, IOException { - String json = JSONUtils.objectToJson(getUser()); - Boolean valid = JSONUtils.isValidJSON(json); - assertTrue(valid); - } - - @Test - public void objectToJsonNullTest() throws JsonGenerationException, JsonMappingException, IOException { - String json = JSONUtils.objectToJson(null); - Boolean valid = JSONUtils.isValidJSON(json); - assertTrue(valid); - } - - @Test - public void jsonToListObjectTest() throws JsonMappingException, JsonProcessingException { - List expected = Arrays.asList(new String[] {"Alex", "Christie", "Gage"}); - String json = JSONUtils.objectToJson(expected); - List actual = JSONUtils.jsonToListObject(json, String.class); - assertEquals(expected, actual); - } - - -} diff --git a/src/test/java/com/ljunggren/storm/utils/ParameterUtilsTest.java b/src/test/java/com/ljunggren/storm/utils/ParameterUtilsTest.java deleted file mode 100644 index 43225fb..0000000 --- a/src/test/java/com/ljunggren/storm/utils/ParameterUtilsTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.ljunggren.storm.utils; - -import static org.junit.Assert.assertEquals; - -import java.lang.reflect.Method; -import java.lang.reflect.Parameter; -import java.util.List; -import java.util.Map; - -import org.junit.Test; - -import com.ljunggren.storm.annotation.Param; - -public class ParameterUtilsTest { - - private class DummyClass { - @SuppressWarnings("unused") - public void find(@Param("id") int id, @Param("asc") boolean asc) {} - } - - - @Test - public void mapArgumentsToParameterNamesTest() throws NoSuchMethodException, SecurityException { - Object[] arguments = new Object[] { 1, true}; - Method method = DummyClass.class.getMethod("find", int.class, boolean.class); - Parameter[] parameters = method.getParameters(); - String[] parameterNames = ParameterUtils.getParameterNames(parameters); - Map map = ParameterUtils.mapArgumentsToParameterNames(parameters, arguments); - assertEquals(2, map.size()); - assertEquals(arguments[0], map.get(parameterNames[0])); - assertEquals(arguments[1], map.get(parameterNames[1])); - } - - @Test - public void findParameterIdsTest() { - String sql = "select * from users where id = #{id} and active = #{active}"; - List parameterIds = ParameterUtils.findParameterIds(sql); - assertEquals(2, parameterIds.size()); - assertEquals("id", parameterIds.get(0)); - assertEquals("active", parameterIds.get(1)); - } - - @Test - public void getParameterNamesTest() throws NoSuchMethodException, SecurityException { - Method method = DummyClass.class.getMethod("find", int.class, boolean.class); - Parameter[] parameters = method.getParameters(); - String[] parameterNames = ParameterUtils.getParameterNames(parameters); - assertEquals(2, parameterNames.length); - assertEquals("id", parameterNames[0]); - assertEquals("asc", parameterNames[1]); - } - - @Test - public void findParameterIdsWithOutSpacesTest() { - String sql = "select * from users where id in (#{id1},#{id2})"; - List parameterIds = ParameterUtils.findParameterIds(sql); - assertEquals(2, parameterIds.size()); - assertEquals("id1", parameterIds.get(0)); - assertEquals("id2", parameterIds.get(1)); - } - - @Test - public void replaceParamaterIdsWithQuestionMarksTest() { - String sql = "select * from users where id = #{id} and active = #{active}"; - String actual = ParameterUtils.replaceParamaterIdsWithQuestionMarks(sql); - String expected = "select * from users where id = ? and active = ?"; - assertEquals(expected, actual); - } - -}