diff --git a/src/main/java/com/ljunggren/storm/StormRepository.java b/src/main/java/com/ljunggren/storm/StormRepository.java index a3051c8..d1dbbdb 100644 --- a/src/main/java/com/ljunggren/storm/StormRepository.java +++ b/src/main/java/com/ljunggren/storm/StormRepository.java @@ -7,6 +7,7 @@ import java.lang.reflect.Type; import java.util.Arrays; import java.util.Iterator; +import java.util.function.Consumer; import com.ljunggren.storm.annotation.Database; import com.ljunggren.storm.context.Context; @@ -21,11 +22,17 @@ public class StormRepository implements InvocationHandler { private Context context; + private Consumer peek; private StormRepository(Class clazz) { this.context = getContextFromClass(clazz); } - + + private StormRepository(Class clazz, Consumer peek) { + this.context = getContextFromClass(clazz); + this.peek = peek; + } + public static T newInstance(Class clazz) { return clazz.cast(Proxy.newProxyInstance( clazz.getClassLoader(), @@ -33,6 +40,13 @@ new StormRepository(clazz))); } + public static T newInstance(Class clazz, Consumer peek) { + return clazz.cast(Proxy.newProxyInstance( + clazz.getClassLoader(), + new Class[] { clazz }, + new StormRepository(clazz, peek))); + } + private Context getContextFromClass(Class clazz) { Annotation[] annotations = clazz.getAnnotations(); Database connection = (Database) Arrays.stream(annotations) @@ -70,10 +84,10 @@ } private QueryChain getQueryChain() { - return new SelectQuery().nextChain( - new DeleteQuery().nextChain( - new UpdateQuery().nextChain( - new InsertQuery() + return new SelectQuery(peek).nextChain( + new DeleteQuery(peek).nextChain( + new UpdateQuery(peek).nextChain( + new InsertQuery(peek) ))); } diff --git a/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java b/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java index 58c56b2..aa77230 100644 --- a/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java @@ -2,10 +2,8 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; import java.util.Arrays; +import java.util.function.Consumer; import java.util.stream.Collectors; import com.ljunggren.storm.annotation.Delete; @@ -16,11 +14,15 @@ public class DeleteQuery extends QueryChain { + public DeleteQuery(Consumer peek) { + super(peek); + } + @Override public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws Exception { if (annotation.annotationType() == Delete.class) { String sql = ((Delete) annotation).sql(); - return sql.isEmpty() ? executeNonNativeQuery(context, args, returnType) : executeQuery(sql, context, args, returnType); + return sql.isEmpty() ? executeNonNativeQuery(context, args, returnType) : executeUpdate(sql, context, args, returnType); } return nextChain.execute(annotation, context, args, returnType); } @@ -37,19 +39,7 @@ QueryBuilder queryBuilder = new QueryBuilder(arg); String sql = queryBuilder.buildDeleteSQL(); Object[] generatedArgs = queryBuilder.getDeleteArgs(); - return executeQuery(sql, context, generatedArgs, returnType); + return executeUpdate(sql, context, generatedArgs, returnType); } - private int executeQuery(String sql, Context context, Object[] args, Type returnType) throws SQLException { - Connection connection = null; - try { - connection = context.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(sql); - setParameters(preparedStatement, args); - return preparedStatement.executeUpdate(); - } finally { - closeConnection(connection); - } - } - } diff --git a/src/main/java/com/ljunggren/storm/crud/InsertQuery.java b/src/main/java/com/ljunggren/storm/crud/InsertQuery.java index 8923480..f9120b7 100644 --- a/src/main/java/com/ljunggren/storm/crud/InsertQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/InsertQuery.java @@ -2,10 +2,9 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; -import java.sql.Connection; -import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Arrays; +import java.util.function.Consumer; import java.util.stream.Collectors; import com.ljunggren.storm.annotation.Insert; @@ -16,11 +15,15 @@ public class InsertQuery extends QueryChain { + public InsertQuery(Consumer peek) { + super(peek); + } + @Override public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws Exception { if (annotation.annotationType() == Insert.class) { String sql = ((Insert) annotation).sql(); - return sql.isEmpty() ? executeNonNativeQuery(context, args, returnType) : executeQuery(sql, context, args, returnType); + return sql.isEmpty() ? executeNonNativeQuery(context, args, returnType) : executeUpdate(sql, context, args, returnType); } return nextChain.execute(annotation, context, args, returnType); } @@ -37,19 +40,7 @@ QueryBuilder queryBuilder = new QueryBuilder(arg); String sql = queryBuilder.buildInsertSQL(); Object[] generatedArgs = queryBuilder.getInsertArgs(); - return executeQuery(sql, context, generatedArgs, returnType); - } - - private int executeQuery(String sql, Context context, Object[] args, Type returnType) throws SQLException { - Connection connection = null; - try { - connection = context.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(sql); - setParameters(preparedStatement, args); - return preparedStatement.executeUpdate(); - } finally { - closeConnection(connection); - } + return executeUpdate(sql, context, generatedArgs, returnType); } } diff --git a/src/main/java/com/ljunggren/storm/crud/QueryChain.java b/src/main/java/com/ljunggren/storm/crud/QueryChain.java index ecfbb82..ab01a09 100644 --- a/src/main/java/com/ljunggren/storm/crud/QueryChain.java +++ b/src/main/java/com/ljunggren/storm/crud/QueryChain.java @@ -4,11 +4,20 @@ import java.lang.reflect.Type; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; +import java.util.function.Consumer; import com.ljunggren.storm.context.Context; +import com.ljunggren.storm.mapper.ResultSetMapper; public abstract class QueryChain { + + private Consumer peek; + + protected QueryChain(Consumer peek) { + this.peek = peek; + } protected QueryChain nextChain; @@ -19,7 +28,40 @@ public abstract Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws Exception; - protected void setParameters(PreparedStatement preparedStatement, Object[] args) throws SQLException { + protected Object executeQuery(String sql, Context context, Object[] args, Type returnType) throws Exception { + Connection connection = null; + PreparedStatement preparedStatement = null; + try { + connection = context.getConnection(); + preparedStatement = connection.prepareStatement(sql); + setParameters(preparedStatement, args); + ResultSet resultSet = preparedStatement.executeQuery(); + return new ResultSetMapper(resultSet, returnType).map(); + } finally { + if (peek != null) { + peek.accept(preparedStatement.toString()); + } + closeConnection(connection); + } + } + + protected int executeUpdate(String sql, Context context, Object[] args, Type returnType) throws SQLException { + Connection connection = null; + PreparedStatement preparedStatement = null; + try { + connection = context.getConnection(); + preparedStatement = connection.prepareStatement(sql); + setParameters(preparedStatement, args); + return preparedStatement.executeUpdate(); + } finally { + if (peek != null) { + peek.accept(preparedStatement.toString()); + } + closeConnection(connection); + } + } + + private void setParameters(PreparedStatement preparedStatement, Object[] args) throws SQLException { if (args != null) { for (int i = 0; i < args.length; i++) { preparedStatement.setObject(i + 1, args[i]); @@ -27,7 +69,7 @@ } } - protected void closeConnection(Connection connection) throws SQLException { + private void closeConnection(Connection connection) throws SQLException { if (connection != null) { connection.close(); } diff --git a/src/main/java/com/ljunggren/storm/crud/SelectQuery.java b/src/main/java/com/ljunggren/storm/crud/SelectQuery.java index 2d28fbd..0a948e6 100644 --- a/src/main/java/com/ljunggren/storm/crud/SelectQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/SelectQuery.java @@ -2,16 +2,17 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; +import java.util.function.Consumer; import com.ljunggren.storm.annotation.Select; import com.ljunggren.storm.context.Context; -import com.ljunggren.storm.mapper.ResultSetMapper; public class SelectQuery extends QueryChain { + public SelectQuery(Consumer peek) { + super(peek); + } + @Override public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws Exception { if (annotation.annotationType() == Select.class) { @@ -21,17 +22,4 @@ return nextChain.execute(annotation, context, args, returnType); } - private Object executeQuery(String sql, Context context, Object[] args, Type returnType) throws Exception { - Connection connection = null; - try { - connection = context.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(sql); - setParameters(preparedStatement, args); - ResultSet resultSet = preparedStatement.executeQuery(); - return new ResultSetMapper(resultSet, returnType).map(); - } finally { - closeConnection(connection); - } - } - } diff --git a/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java b/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java index 76b83cc..eb35bff 100644 --- a/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java @@ -2,10 +2,8 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; import java.util.Arrays; +import java.util.function.Consumer; import java.util.stream.Collectors; import com.ljunggren.storm.annotation.Update; @@ -16,11 +14,15 @@ public class UpdateQuery extends QueryChain { + public UpdateQuery(Consumer peek) { + super(peek); + } + @Override public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws Exception { if (annotation.annotationType() == Update.class) { String sql = ((Update) annotation).sql(); - return sql.isEmpty() ? executeNonNativeQuery(context, args, returnType) : executeQuery(sql, context, args, returnType); + return sql.isEmpty() ? executeNonNativeQuery(context, args, returnType) : executeUpdate(sql, context, args, returnType); } return nextChain.execute(annotation, context, args, returnType); } @@ -37,19 +39,7 @@ QueryBuilder queryBuilder = new QueryBuilder(arg); String sql = queryBuilder.buildUpdateSQL(); Object[] generatedArgs = queryBuilder.getUpdateArgs(); - return executeQuery(sql, context, generatedArgs, returnType); + return executeUpdate(sql, context, generatedArgs, returnType); } - private int executeQuery(String sql, Context context, Object[] args, Type returnType) throws SQLException { - Connection connection = null; - try { - connection = context.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(sql); - setParameters(preparedStatement, args); - return preparedStatement.executeUpdate(); - } finally { - closeConnection(connection); - } - } - } diff --git a/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java b/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java index ba3208a..bbe9f8a 100644 --- a/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertTrue; import java.sql.Statement; +import java.util.function.Consumer; import org.apache.commons.io.IOUtils; import org.junit.Before; @@ -43,6 +44,12 @@ } + private String generatedSQL; + + public void setGeneratedSQL(String generatedSQL) { + this.generatedSQL = generatedSQL; + } + @Before public void setup() throws Exception { Context context = new ContextFactory().getContext("H2"); @@ -90,4 +97,12 @@ repository.nonsense(); } + @Test + public void peekTest() { + Consumer peek = e -> setGeneratedSQL(e); + UserRepository repository = StormRepository.newInstance(UserRepository.class, peek); + repository.deleteById(1); + assertTrue(generatedSQL.contains("delete from users where id = ?")); + } + } diff --git a/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java b/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java index e5d67e3..5abc650 100644 --- a/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertTrue; import java.sql.Statement; +import java.util.function.Consumer; import org.apache.commons.io.IOUtils; import org.junit.Before; @@ -39,7 +40,13 @@ public long count(); } - + + private String generatedSQL; + + public void setGeneratedSQL(String generatedSQL) { + this.generatedSQL = generatedSQL; + } + @Before public void setup() throws Exception { Context context = new ContextFactory().getContext("H2"); @@ -89,5 +96,13 @@ UserRepository repository = StormRepository.newInstance(UserRepository.class); repository.nonsense(); } + + @Test + public void peekTest() { + Consumer peek = e -> setGeneratedSQL(e); + UserRepository repository = StormRepository.newInstance(UserRepository.class, peek); + repository.add("Jane", "Doe", 104); + assertTrue(generatedSQL.contains("insert into users (firstname, lastname, employee_id) values (?, ?, ?)")); + } } diff --git a/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java b/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java index 9331df0..692d17c 100644 --- a/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java @@ -7,6 +7,7 @@ import java.sql.Statement; import java.util.List; import java.util.Set; +import java.util.function.Consumer; import org.apache.commons.io.IOUtils; import org.junit.Before; @@ -48,6 +49,12 @@ } + private String generatedSQL; + + public void setGeneratedSQL(String generatedSQL) { + this.generatedSQL = generatedSQL; + } + @Before public void setup() throws Exception { Context context = new ContextFactory().getContext("H2"); @@ -122,4 +129,13 @@ UserRepository repository = StormRepository.newInstance(UserRepository.class); repository.nonsense(); } + + @Test + public void peekTest() { + Consumer peek = e -> setGeneratedSQL(e); + UserRepository repository = StormRepository.newInstance(UserRepository.class, peek); + repository.fetchAllOrdered(); + assertTrue(generatedSQL.contains("select * from users order by id")); + } + } diff --git a/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java b/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java index e2296a3..aa4633a 100644 --- a/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java @@ -1,8 +1,10 @@ package com.ljunggren.storm.crud; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.sql.Statement; +import java.util.function.Consumer; import org.apache.commons.io.IOUtils; import org.junit.Before; @@ -39,6 +41,12 @@ } + private String generatedSQL; + + public void setGeneratedSQL(String generatedSQL) { + this.generatedSQL = generatedSQL; + } + @Before public void setup() throws Exception { Context context = new ContextFactory().getContext("H2"); @@ -89,4 +97,12 @@ repository.nonsense(); } + @Test + public void peekTest() { + Consumer peek = e -> setGeneratedSQL(e); + UserRepository repository = StormRepository.newInstance(UserRepository.class, peek); + repository.updateFirstName("Bob", 1); + assertTrue(generatedSQL.contains("update users set firstname = ? where id = ?")); + } + } diff --git a/src/test/java/com/ljunggren/storm/utils/ExceptionUtilsTest.java b/src/test/java/com/ljunggren/storm/utils/ExceptionUtilsTest.java new file mode 100644 index 0000000..273cb5f --- /dev/null +++ b/src/test/java/com/ljunggren/storm/utils/ExceptionUtilsTest.java @@ -0,0 +1,33 @@ +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 index 77da151..7b85826 100644 --- a/src/test/java/com/ljunggren/storm/utils/JSONUtilsTest.java +++ b/src/test/java/com/ljunggren/storm/utils/JSONUtilsTest.java @@ -26,6 +26,12 @@ } @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);