Skip to content
Browse files

MagicQuery test

  • Loading branch information...
1 parent 719919b commit 77c69619008b6b81e5c64e429c64acd2f81d01c7 @poetix poetix committed
Showing with 68 additions and 0 deletions.
  1. +12 −0 src/main/java/com/youdevise/hsd/Sql.java
  2. +56 −0 src/test/java/com/youdevise/hsd/MagicQueryTest.java
View
12 src/main/java/com/youdevise/hsd/Sql.java
@@ -0,0 +1,12 @@
+package com.youdevise.hsd;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Sql {
+ String[] value();
+}
View
56 src/test/java/com/youdevise/hsd/MagicQueryTest.java
@@ -0,0 +1,56 @@
+package com.youdevise.hsd;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
+import org.junit.Test;
+
+import com.google.common.base.Joiner;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.is;
+
+public class MagicQueryTest {
+
+ public static interface TestInterface {
+
+ @Sql({ "SELECT foo",
+ "FROM bar",
+ "WHERE baz=?" })
+ Query selectFoo(int baz);
+ }
+
+ public static class MagicQuery implements InvocationHandler {
+
+ @SuppressWarnings("unchecked")
+ public static <T> T proxying(Class<T> iface) {
+ return (T) Proxy.newProxyInstance(MagicQuery.class.getClassLoader(),
+ new Class<?>[] { iface },
+ new MagicQuery());
+ }
+
+ @Override
+ public Object invoke(Object target, Method method, Object[] params) throws Throwable {
+ if (method.getReturnType().isAssignableFrom(Query.class)) {
+ return new Query(getQuerySql(method), params);
+ }
+ return null;
+ }
+
+ private String getQuerySql(Method method) {
+ return Joiner.on("\n").join(method.getAnnotation(Sql.class).value());
+ }
+ }
+
+ @Test public void
+ proxies_method_call_to_create_query_with_parameters() {
+ TestInterface testInterface = MagicQuery.proxying(TestInterface.class);
+ Query query = testInterface.selectFoo(100);
+
+ assertThat(query.sql(), is("SELECT foo\nFROM bar\nWHERE baz=?"));
+ assertThat(query.parameters(), hasItem(100));
+ }
+
+}

0 comments on commit 77c6961

Please sign in to comment.
Something went wrong with that request. Please try again.