cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r956353 - in /cayenne/main/trunk: build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/ framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/
Date Sun, 20 Jun 2010 13:06:46 GMT
Author: aadamchik
Date: Sun Jun 20 13:06:46 2010
New Revision: 956353

URL: http://svn.apache.org/viewvc?rev=956353&view=rev
Log:
DI-enabled unit tests

* support for JDBC types in JDBC test utils

Modified:
    cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/DeleteBuilder.java
    cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/SQLBuilder.java
    cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateBuilder.java
    cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateTemplate.java
    cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/WhereBuilder.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextRefreshingTest.java

Modified: cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/DeleteBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/DeleteBuilder.java?rev=956353&r1=956352&r2=956353&view=diff
==============================================================================
--- cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/DeleteBuilder.java
(original)
+++ cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/DeleteBuilder.java
Sun Jun 20 13:06:46 2010
@@ -18,19 +18,20 @@
  ****************************************************************/
 package org.apache.cayenne.test.jdbc;
 
-import java.util.ArrayList;
-
 public class DeleteBuilder extends SQLBuilder {
 
     protected DeleteBuilder(DBHelper dbHelper, String tableName) {
-        super(dbHelper, new StringBuilder(), new ArrayList<Object>());
-
+        super(dbHelper);
         sqlBuffer.append("delete from ").append(dbHelper.quote(tableName));
     }
 
     public WhereBuilder where(String column, Object value) {
-        WhereBuilder where = new WhereBuilder(dbHelper, sqlBuffer, bindings);
-        where.and(column, value);
+        return where(column, value, NO_TYPE);
+    }
+
+    public WhereBuilder where(String column, Object value, int valueType) {
+        WhereBuilder where = new WhereBuilder(dbHelper, sqlBuffer, bindings, bindingTypes);
+        where.and(column, value, valueType);
         return where;
     }
 }

Modified: cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/SQLBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/SQLBuilder.java?rev=956353&r1=956352&r2=956353&view=diff
==============================================================================
--- cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/SQLBuilder.java
(original)
+++ cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/SQLBuilder.java
Sun Jun 20 13:06:46 2010
@@ -19,23 +19,43 @@
 package org.apache.cayenne.test.jdbc;
 
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.Collection;
 
 public abstract class SQLBuilder {
 
+    static final int NO_TYPE = Integer.MIN_VALUE;
+
     protected DBHelper dbHelper;
     protected Collection<Object> bindings;
+    protected Collection<Integer> bindingTypes;
     protected StringBuilder sqlBuffer;
 
+    protected SQLBuilder(DBHelper dbHelper) {
+        this(
+                dbHelper,
+                new StringBuilder(),
+                new ArrayList<Object>(),
+                new ArrayList<Integer>());
+    }
+
     protected SQLBuilder(DBHelper dbHelper, StringBuilder sqlBuffer,
-            Collection<Object> bindings) {
+            Collection<Object> bindings, Collection<Integer> bindingTypes) {
         this.dbHelper = dbHelper;
         this.bindings = bindings;
+        this.bindingTypes = bindingTypes;
         this.sqlBuffer = sqlBuffer;
     }
 
     public int execute() throws SQLException {
-        return new UpdateTemplate(dbHelper).execute(sqlBuffer.toString(), bindings
-                .toArray());
+        return new UpdateTemplate(dbHelper).execute(
+                sqlBuffer.toString(),
+                bindings,
+                bindingTypes);
+    }
+
+    protected void initBinding(Object value, int type) {
+        bindings.add(value);
+        bindingTypes.add(type);
     }
 }

Modified: cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateBuilder.java?rev=956353&r1=956352&r2=956353&view=diff
==============================================================================
--- cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateBuilder.java
(original)
+++ cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateBuilder.java
Sun Jun 20 13:06:46 2010
@@ -18,31 +18,36 @@
  ****************************************************************/
 package org.apache.cayenne.test.jdbc;
 
-import java.util.ArrayList;
-
 public class UpdateBuilder extends SQLBuilder {
 
     protected int setCount;
 
     protected UpdateBuilder(DBHelper dbHelper, String tableName) {
-        super(dbHelper, new StringBuilder(), new ArrayList<Object>());
+        super(dbHelper);
         sqlBuffer.append("update ").append(dbHelper.quote(tableName)).append(" set ");
     }
 
     public UpdateBuilder set(String column, Object value) {
+        return set(column, value, NO_TYPE);
+    }
+
+    public UpdateBuilder set(String column, Object value, int valueType) {
         if (setCount++ > 0) {
             sqlBuffer.append(", ");
         }
 
         sqlBuffer.append(dbHelper.quote(column)).append(" = ?");
-        bindings.add(value);
+        initBinding(value, valueType);
         return this;
     }
 
     public WhereBuilder where(String column, Object value) {
-        WhereBuilder where = new WhereBuilder(dbHelper, sqlBuffer, bindings);
-        where.and(column, value);
-        return where;
+        return where(column, value, NO_TYPE);
     }
 
+    public WhereBuilder where(String column, Object value, int valueType) {
+        WhereBuilder where = new WhereBuilder(dbHelper, sqlBuffer, bindings, bindingTypes);
+        where.and(column, value, valueType);
+        return where;
+    }
 }

Modified: cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateTemplate.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateTemplate.java?rev=956353&r1=956352&r2=956353&view=diff
==============================================================================
--- cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateTemplate.java
(original)
+++ cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateTemplate.java
Sun Jun 20 13:06:46 2010
@@ -21,6 +21,7 @@ package org.apache.cayenne.test.jdbc;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
+import java.util.Collection;
 
 class UpdateTemplate {
 
@@ -30,17 +31,41 @@ class UpdateTemplate {
         this.parent = parent;
     }
 
-    protected void bindParameters(PreparedStatement statement, Object... bindings)
-            throws SQLException {
-
-        if (bindings != null && bindings.length > 0) {
-            for (int i = 0; i < bindings.length; i++) {
-                statement.setObject(i + 1, bindings[i]);
+    protected void bindParameters(
+            PreparedStatement statement,
+            Collection<Object> bindings,
+            Collection<Integer> bindingTypes) throws SQLException {
+
+        if (bindings != null && !bindings.isEmpty()) {
+
+            Object[] values = bindings.toArray();
+            Integer[] types = bindingTypes.toArray(new Integer[bindingTypes.size()]);
+
+            for (int i = 0; i < values.length; i++) {
+
+                if (values[i] == null) {
+                    if (types[i] != SQLBuilder.NO_TYPE) {
+                        statement.setNull(i + 1, types[i]);
+                    }
+                    else {
+                        throw new IllegalStateException(
+                                "No type inmformation for null value at index " + i);
+                    }
+                }
+                else {
+                    if (types[i] != SQLBuilder.NO_TYPE) {
+                        statement.setObject(i + 1, values[i], types[i]);
+                    }
+                    else {
+                        statement.setObject(i + 1, values[i]);
+                    }
+                }
             }
         }
     }
 
-    int execute(String sql, Object... bindings) throws SQLException {
+    int execute(String sql, Collection<Object> bindings, Collection<Integer>
bindingTypes)
+            throws SQLException {
         UtilityLogger.log(sql);
         Connection c = parent.getConnection();
         try {
@@ -48,7 +73,7 @@ class UpdateTemplate {
             PreparedStatement st = c.prepareStatement(sql);
 
             try {
-                bindParameters(st, bindings);
+                bindParameters(st, bindings, bindingTypes);
                 return st.executeUpdate();
             }
             finally {

Modified: cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/WhereBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/WhereBuilder.java?rev=956353&r1=956352&r2=956353&view=diff
==============================================================================
--- cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/WhereBuilder.java
(original)
+++ cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/WhereBuilder.java
Sun Jun 20 13:06:46 2010
@@ -25,31 +25,40 @@ public class WhereBuilder extends SQLBui
     protected int whereCount;
 
     protected WhereBuilder(DBHelper dbHelper, StringBuilder sqlBuffer,
-            Collection<Object> bindings) {
-        super(dbHelper, sqlBuffer, bindings);
+            Collection<Object> bindings, Collection<Integer> bindingTypes) {
+        super(dbHelper, sqlBuffer, bindings, bindingTypes);
         sqlBuffer.append(" where ");
     }
 
     public WhereBuilder and(String column, Object value) {
+        return and(column, value, NO_TYPE);
+    }
+
+    public WhereBuilder and(String column, Object value, int valueType) {
 
         if (whereCount++ > 0) {
             sqlBuffer.append(" and ");
         }
 
         sqlBuffer.append(dbHelper.quote(column)).append(" = ?");
-        bindings.add(value);
+        initBinding(value, valueType);
 
         return this;
     }
 
     public WhereBuilder or(String column, Object value) {
+        return or(column, value, NO_TYPE);
+    }
+
+    public WhereBuilder or(String column, Object value, int valueType) {
         if (whereCount++ > 0) {
             sqlBuffer.append(" or ");
         }
 
         sqlBuffer.append(dbHelper.quote(column)).append(" = ?");
-        bindings.add(value);
+        initBinding(value, valueType);
 
         return this;
     }
+
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextRefreshingTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextRefreshingTest.java?rev=956353&r1=956352&r2=956353&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextRefreshingTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextRefreshingTest.java
Sun Jun 20 13:06:46 2010
@@ -35,6 +35,7 @@ import org.apache.cayenne.unit.di.UnitTe
 import org.apache.cayenne.unit.di.server.DataChannelQueryInterceptor;
 import org.apache.cayenne.unit.di.server.ServerCase;
 import org.apache.cayenne.unit.di.server.UseServerRuntime;
+import org.hsqldb.Types;
 
 /**
  * Test suite covering possible scenarios of refreshing updated objects. This includes
@@ -130,7 +131,7 @@ public class DataContextRefreshingTest e
         assertNotNull(painting.getToArtist());
         assertEquals("artist2", painting.getToArtist().getArtistName());
 
-        assertEquals(1, tPainting.update().set("ARTIST_ID", null).execute());
+        assertEquals(1, tPainting.update().set("ARTIST_ID", null, Types.BIGINT).execute());
 
         // select without prefetch
         painting = (Painting) context
@@ -250,7 +251,7 @@ public class DataContextRefreshingTest e
         assertNotNull(painting.getToArtist());
         assertEquals("artist2", painting.getToArtist().getArtistName());
 
-        assertEquals(1, tPainting.update().set("ARTIST_ID", null).execute());
+        assertEquals(1, tPainting.update().set("ARTIST_ID", null, Types.BIGINT).execute());
 
         context.invalidateObjects(Collections.singletonList(painting));
         assertNull(painting.getToArtist());



Mime
View raw message