Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

prepared statement

  • Loading branch information...
commit 2c24cc9adc3e09c1a7a254a6f51294b9307a130c 1 parent 8e6c7e9
@ccaner authored
View
5 resultsetmock/pom.xml
@@ -20,6 +20,11 @@
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>11.0.2</version>
+ </dependency>
</dependencies>
View
2  resultsetmock/resultsetmock.iml
@@ -12,6 +12,8 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.google.collections:google-collections:1.0" level="project" />
+ <orderEntry type="library" name="Maven: com.google.guava:guava:11.0.2" level="project" />
+ <orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-test:3.0.6.RELEASE" level="project" />
View
44 resultsetmock/src/main/java/play/resultsetmock/jdbc/MethodBackedCallableStatement.java
@@ -1,44 +0,0 @@
-package play.resultsetmock.jdbc;
-
-import com.google.common.base.Defaults;
-import net.sf.cglib.proxy.MethodInterceptor;
-import net.sf.cglib.proxy.MethodProxy;
-
-import java.lang.reflect.Method;
-import java.sql.CallableStatement;
-
-public abstract class MethodBackedCallableStatement implements CallableStatement {
-
- private Invocation invocation;
-
- protected MethodBackedCallableStatement(Object target, Method method) {
- this.invocation = new Invocation(target, method);
- }
-
- static class Interceptor implements MethodInterceptor {
-
- @Override
- public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
- if (method.getDeclaringClass() == MethodBackedCallableStatement.class) {
- return methodProxy.invokeSuper(obj, args);
- }
-
- return Defaults.defaultValue(method.getReturnType());
- }
-
- }
-
- static class Invocation {
-
- Object obj;
- Method method;
- Object[] args;
- Object result;
-
- Invocation(Object obj, Method method) {
- this.obj = obj;
- this.method = method;
- }
- }
-
-}
View
78 resultsetmock/src/main/java/play/resultsetmock/jdbc/MethodBackedPreparedStatement.java
@@ -0,0 +1,78 @@
+package play.resultsetmock.jdbc;
+
+import com.google.common.base.Defaults;
+import net.sf.cglib.proxy.MethodInterceptor;
+import net.sf.cglib.proxy.MethodProxy;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.List;
+
+public abstract class MethodBackedPreparedStatement implements PreparedStatement {
+
+ private Invocation invocation;
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public ResultSet executeQuery() throws SQLException {
+ try {
+ return MockJdbcFactory.createResultSet((List<Object>) invocation.invoke());
+ } catch (Exception e) {
+ throw new SQLException(e);
+ }
+ }
+
+ protected MethodBackedPreparedStatement(Object target, Method method) {
+ this.invocation = new Invocation(target, method);
+ }
+
+ private void setArgument(int idx, Object val) {
+ invocation.setArgument(idx, val);
+ }
+
+ static class Interceptor implements MethodInterceptor {
+
+ @Override
+ public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
+ if (method.getDeclaringClass() == MethodBackedPreparedStatement.class) {
+ return methodProxy.invokeSuper(obj, args);
+ }
+ MethodBackedPreparedStatement mockPs = (MethodBackedPreparedStatement) obj;
+ String mName = method.getName();
+ Class<?>[] pTypes = method.getParameterTypes();
+ boolean isSetter = mName.startsWith("set") && pTypes.length == 2;
+ if (isSetter && pTypes[0] == Integer.TYPE) { // set by column idx
+ mockPs.setArgument((Integer) args[0], (Integer)args[1]);
+ return null;
+ }
+ return Defaults.defaultValue(method.getReturnType());
+ }
+
+ }
+
+ private static class Invocation {
+
+ final Object obj;
+ final Method method;
+ final Object[] args;
+
+ Invocation(Object obj, Method method) {
+ this.obj = obj;
+ this.method = method;
+ args = new Object[method.getParameterTypes().length];
+ }
+
+ private void setArgument(int index, Object val) {
+ args[index - 1] = val;
+ }
+
+ private Object invoke() throws InvocationTargetException, IllegalAccessException {
+ return method.invoke(obj, args);
+ }
+ }
+
+}
View
50 resultsetmock/src/main/java/play/resultsetmock/jdbc/MockJdbcFactory.java
@@ -1,8 +1,12 @@
package play.resultsetmock.jdbc;
+import com.google.common.base.Defaults;
import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.MethodInterceptor;
+import net.sf.cglib.proxy.MethodProxy;
import javax.sql.DataSource;
+import java.io.ObjectStreamConstants;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
@@ -12,26 +16,27 @@
public abstract class MockJdbcFactory {
- public static DataSource createDataSource(Object model) {
+ public static DataSource createDataSource(final Object model) {
return (DataSource) Proxy.newProxyInstance(MockJdbcFactory.class.getClassLoader(), new Class[]{DataSource.class},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("getConnection")) {
- return createConnection();
+ return createConnection(model);
}
return null;
}
});
}
- public static Connection createConnection() {
+ public static Connection createConnection(Object model) {
+
return (Connection) Proxy.newProxyInstance(MockJdbcFactory.class.getClassLoader(), new Class[]{Connection.class},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- if (method.getName().equals("getConnection")) {
- return createConnection();
+ if (method.getName().equals("prepareStatement")) {
+ return createPreparedStatement(args[0]);
}
return null;
}
@@ -46,4 +51,39 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
Object rs = enhancer.create(new Class[]{List.class}, new Object[]{backingList});
return (ResultSet) rs;
}
+
+ public static ResultSet createPreparedStatement(Object model, Method method) {
+ Enhancer enhancer = new Enhancer();
+ enhancer.setSuperclass(MethodBackedPreparedStatement.class);
+ enhancer.setCallback(new MethodBackedPreparedStatement.Interceptor());
+ Object rs = enhancer.create(new Class[]{Object.class, Method.class}, new Object[]{model, method});
+ return (ResultSet) rs;
+ }
+
+ public static <T> T initializeAbstractClass(final Class<T> clazz) {
+ return initializeAbstractClass(clazz, null, null);
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <T> T initializeAbstractClass(final Class<T> clazz, Class[] paramTypes, Object[] args) {
+ Enhancer enhancer = new Enhancer();
+ enhancer.setSuperclass(clazz);
+ enhancer.setCallback(new MethodInterceptor() {
+ @Override
+ public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
+ if (method.getDeclaringClass() == clazz) {
+ return methodProxy.invokeSuper(obj, args);
+ }
+ return Defaults.defaultValue(method.getReturnType());
+ }
+ });
+ Object instance;
+ if (paramTypes == null) {
+ instance = enhancer.create();
+ } else {
+ instance = enhancer.create(paramTypes, args);
+ }
+ return (T) instance;
+ }
+
}
View
11 resultsetmock/src/main/java/play/resultsetmock/util/AbstractClassInitializer.java
@@ -0,0 +1,11 @@
+package play.resultsetmock.util;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: akpinarc
+ * Date: 5/2/12
+ * Time: 5:30 PM
+ * To change this template use File | Settings | File Templates.
+ */
+public class AbstractClassInitializer {
+}
View
2  resultsetmock/src/test/java/Model.java → ...tmock/src/test/java/play/resultsetmock/Model.java
@@ -1,3 +1,5 @@
+package play.resultsetmock;
+
import com.google.common.collect.Lists;
import play.resultsetmock.annotations.Param;
import play.resultsetmock.annotations.Query;
View
2  resultsetmock/src/test/java/Pet.java → ...setmock/src/test/java/play/resultsetmock/Pet.java
@@ -1,3 +1,5 @@
+package play.resultsetmock;
+
import play.resultsetmock.annotations.RsColumn;
/**
View
4 resultsetmock/src/test/java/Test.java → ...k/src/test/java/play/resultsetmock/jdbc/Test.java
@@ -1,4 +1,8 @@
+package play.resultsetmock.jdbc;
+
import com.google.common.collect.Lists;
+import play.resultsetmock.Model;
+import play.resultsetmock.Pet;
import play.resultsetmock.jdbc.MockJdbcFactory;
import javax.sql.DataSource;
Please sign in to comment.
Something went wrong with that request. Please try again.