You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2010/06/20 11:27:38 UTC
svn commit: r956338 [1/3] - 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/
framework/cayenne-jdk1.5-unpublished/src/te...
Author: aadamchik
Date: Sun Jun 20 09:27:37 2010
New Revision: 956338
URL: http://svn.apache.org/viewvc?rev=956338&view=rev
Log:
DI-enabled unit tests
* switching old style unit tests
* extending test utilities with UPDATE and DELETE builders
Added:
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/unit/di/server/CayenneResourcesSQLTemplateCustomizerProvider.java
Removed:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextCase.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/dml/access.DataContextCase.xml
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/dml/access.DataContextSQLTemplateTest.xml
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/dml/access.DataRowUtilsTest.xml
Modified:
cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/DBHelper.java
cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/TableHelper.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPerformQueryAPITest.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPrefetchMultistepTest.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPrefetchTest.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextRefreshingTest.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextTest.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataNodeQueriesTest.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataRowUtilsTest.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/jdbc/SQLTemplateActionTest.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ParsedExpQualifierCompatTest.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCase.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/util/SQLTemplateCustomizer.java
Modified: cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/DBHelper.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/DBHelper.java?rev=956338&r1=956337&r2=956338&view=diff
==============================================================================
--- cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/DBHelper.java (original)
+++ cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/DBHelper.java Sun Jun 20 09:27:37 2010
@@ -23,7 +23,6 @@ import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
@@ -147,23 +146,16 @@ public class DBHelper {
}
}
- public int deleteAll(String table) throws SQLException {
- String sql = "delete from " + quote(table);
- UtilityLogger.log(sql);
-
- Connection c = getConnection();
- try {
+ public int deleteAll(String tableName) throws SQLException {
+ return delete(tableName).execute();
+ }
- Statement st = c.createStatement();
- int count = st.executeUpdate(sql);
- st.close();
- c.commit();
+ public UpdateBuilder update(String tableName) throws SQLException {
+ return new UpdateBuilder(this, tableName);
+ }
- return count;
- }
- finally {
- c.close();
- }
+ public DeleteBuilder delete(String tableName) {
+ return new DeleteBuilder(this, tableName);
}
public int getRowCount(String table) throws SQLException {
@@ -179,6 +171,19 @@ public class DBHelper {
}.execute(sql);
}
+ public String getString(String table, String column) throws SQLException {
+ final String sql = "select " + quote(column) + " from " + quote(table);
+
+ return new RowTemplate<String>(this) {
+
+ @Override
+ String readRow(ResultSet rs, String sql) throws SQLException {
+ return rs.getString(1);
+ }
+
+ }.execute(sql);
+ }
+
public Object getObject(String table, String column) throws SQLException {
final String sql = "select " + quote(column) + " from " + quote(table);
Added: 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=956338&view=auto
==============================================================================
--- cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/DeleteBuilder.java (added)
+++ cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/DeleteBuilder.java Sun Jun 20 09:27:37 2010
@@ -0,0 +1,36 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+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>());
+
+ 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;
+ }
+}
Added: 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=956338&view=auto
==============================================================================
--- cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/SQLBuilder.java (added)
+++ cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/SQLBuilder.java Sun Jun 20 09:27:37 2010
@@ -0,0 +1,41 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.test.jdbc;
+
+import java.sql.SQLException;
+import java.util.Collection;
+
+public abstract class SQLBuilder {
+
+ protected DBHelper dbHelper;
+ protected Collection<Object> bindings;
+ protected StringBuilder sqlBuffer;
+
+ protected SQLBuilder(DBHelper dbHelper, StringBuilder sqlBuffer,
+ Collection<Object> bindings) {
+ this.dbHelper = dbHelper;
+ this.bindings = bindings;
+ this.sqlBuffer = sqlBuffer;
+ }
+
+ public int execute() throws SQLException {
+ return new UpdateTemplate(dbHelper).execute(sqlBuffer.toString(), bindings
+ .toArray());
+ }
+}
Modified: cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/TableHelper.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/TableHelper.java?rev=956338&r1=956337&r2=956338&view=diff
==============================================================================
--- cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/TableHelper.java (original)
+++ cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/TableHelper.java Sun Jun 20 09:27:37 2010
@@ -42,11 +42,18 @@ public class TableHelper {
setColumns(columns);
}
- public TableHelper deleteAll() throws SQLException {
- dbHelper.deleteAll(tableName);
- return this;
+ public UpdateBuilder update() throws SQLException {
+ return dbHelper.update(tableName);
+ }
+
+ public DeleteBuilder delete() {
+ return dbHelper.delete(tableName);
+ }
+
+ public int deleteAll() throws SQLException {
+ return dbHelper.deleteAll(tableName);
}
-
+
public String getTableName() {
return tableName;
}
@@ -112,6 +119,10 @@ public class TableHelper {
return dbHelper.getBoolean(tableName, column);
}
+ public String getString(String column) throws SQLException {
+ return dbHelper.getString(tableName, column);
+ }
+
public java.util.Date getUtilDate(String column) throws SQLException {
return dbHelper.getUtilDate(tableName, column);
}
Added: 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=956338&view=auto
==============================================================================
--- cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateBuilder.java (added)
+++ cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateBuilder.java Sun Jun 20 09:27:37 2010
@@ -0,0 +1,48 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+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>());
+ sqlBuffer.append("update ").append(dbHelper.quote(tableName)).append(" set ");
+ }
+
+ public UpdateBuilder set(String column, Object value) {
+ if (setCount++ > 0) {
+ sqlBuffer.append(", ");
+ }
+
+ sqlBuffer.append(dbHelper.quote(column)).append(" = ?");
+ bindings.add(value);
+ return this;
+ }
+
+ public WhereBuilder where(String column, Object value) {
+ WhereBuilder where = new WhereBuilder(dbHelper, sqlBuffer, bindings);
+ where.and(column, value);
+ return where;
+ }
+
+}
Added: 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=956338&view=auto
==============================================================================
--- cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateTemplate.java (added)
+++ cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/UpdateTemplate.java Sun Jun 20 09:27:37 2010
@@ -0,0 +1,62 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.test.jdbc;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+class UpdateTemplate {
+
+ DBHelper parent;
+
+ public UpdateTemplate(DBHelper parent) {
+ 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]);
+ }
+ }
+ }
+
+ int execute(String sql, Object... bindings) throws SQLException {
+ UtilityLogger.log(sql);
+ Connection c = parent.getConnection();
+ try {
+
+ PreparedStatement st = c.prepareStatement(sql);
+
+ try {
+ bindParameters(st, bindings);
+ return st.executeUpdate();
+ }
+ finally {
+ st.close();
+ }
+ }
+ finally {
+ c.close();
+ }
+ }
+}
Added: 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=956338&view=auto
==============================================================================
--- cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/WhereBuilder.java (added)
+++ cayenne/main/trunk/build-tools/cayenne-test-utilities/src/main/java/org/apache/cayenne/test/jdbc/WhereBuilder.java Sun Jun 20 09:27:37 2010
@@ -0,0 +1,55 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.test.jdbc;
+
+import java.util.Collection;
+
+public class WhereBuilder extends SQLBuilder {
+
+ protected int whereCount;
+
+ protected WhereBuilder(DBHelper dbHelper, StringBuilder sqlBuffer,
+ Collection<Object> bindings) {
+ super(dbHelper, sqlBuffer, bindings);
+ sqlBuffer.append(" where ");
+ }
+
+ public WhereBuilder and(String column, Object value) {
+
+ if (whereCount++ > 0) {
+ sqlBuffer.append(" and ");
+ }
+
+ sqlBuffer.append(dbHelper.quote(column)).append(" = ?");
+ bindings.add(value);
+
+ return this;
+ }
+
+ public WhereBuilder or(String column, Object value) {
+ if (whereCount++ > 0) {
+ sqlBuffer.append(" or ");
+ }
+
+ sqlBuffer.append(dbHelper.quote(column)).append(" = ?");
+ bindings.add(value);
+
+ return this;
+ }
+}
Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPerformQueryAPITest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPerformQueryAPITest.java?rev=956338&r1=956337&r2=956338&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPerformQueryAPITest.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPerformQueryAPITest.java Sun Jun 20 09:27:37 2010
@@ -26,65 +26,113 @@ import java.util.List;
import java.util.Map;
import org.apache.cayenne.ObjectId;
+import org.apache.cayenne.configuration.server.ServerRuntime;
+import org.apache.cayenne.di.Inject;
import org.apache.cayenne.query.SQLTemplate;
+import org.apache.cayenne.test.jdbc.DBHelper;
+import org.apache.cayenne.test.jdbc.TableHelper;
import org.apache.cayenne.testdo.testmap.Artist;
import org.apache.cayenne.testdo.testmap.Painting;
-import org.apache.cayenne.unit.CayenneCase;
+import org.apache.cayenne.unit.AccessStackAdapter;
+import org.apache.cayenne.unit.di.UnitTestClosure;
+import org.apache.cayenne.unit.di.server.DataChannelQueryInterceptor;
+import org.apache.cayenne.unit.di.server.ServerCase;
+import org.apache.cayenne.unit.di.server.UseServerRuntime;
-/**
- */
-public class DataContextPerformQueryAPITest extends CayenneCase {
+@UseServerRuntime(ServerCase.TESTMAP_PROJECT)
+public class DataContextPerformQueryAPITest extends ServerCase {
+
+ @Inject
+ protected DataContext context;
+
+ @Inject
+ protected DBHelper dbHelper;
+
+ @Inject
+ protected ServerRuntime runtime;
+
+ @Inject
+ protected AccessStackAdapter accessStackAdapter;
+
+ @Inject
+ protected DataChannelQueryInterceptor queryInterceptor;
+
+ protected TableHelper tArtist;
+ protected TableHelper tPainting;
@Override
- protected void setUp() throws Exception {
- super.setUp();
- deleteTestData();
+ protected void setUpAfterInjection() throws Exception {
+ dbHelper.deleteAll("PAINTING_INFO");
+ dbHelper.deleteAll("PAINTING");
+ dbHelper.deleteAll("ARTIST_EXHIBIT");
+ dbHelper.deleteAll("ARTIST");
+ dbHelper.deleteAll("GALLERY");
+ dbHelper.deleteAll("EXHIBIT");
+
+ tArtist = new TableHelper(dbHelper, "ARTIST");
+ tArtist.setColumns("ARTIST_ID", "ARTIST_NAME");
+
+ tPainting = new TableHelper(dbHelper, "PAINTING");
+ tPainting.setColumns(
+ "PAINTING_ID",
+ "PAINTING_TITLE",
+ "ARTIST_ID",
+ "ESTIMATED_PRICE");
+ }
+
+ protected void createTwoArtists() throws Exception {
+ tArtist.insert(21, "artist2");
+ tArtist.insert(201, "artist3");
+ }
+
+ protected void createTwoArtistsAndTwoPaintingsDataSet() throws Exception {
+ tArtist.insert(11, "artist2");
+ tArtist.insert(101, "artist3");
+ tPainting.insert(6, "p_artist3", 101, 1000);
+ tPainting.insert(7, "p_artist2", 11, 2000);
}
public void testObjectQueryStringBoolean() throws Exception {
- getAccessStack().createTestData(DataContextCase.class, "testArtists", null);
- getAccessStack().createTestData(DataContextCase.class, "testPaintings", null);
+ createTwoArtistsAndTwoPaintingsDataSet();
- List paintings = createDataContext().performQuery("ObjectQuery", true);
+ List<?> paintings = context.performQuery("ObjectQuery", true);
assertNotNull(paintings);
- assertEquals(25, paintings.size());
+ assertEquals(2, paintings.size());
}
public void testObjectQueryStringMapBoolean() throws Exception {
- getAccessStack().createTestData(DataContextCase.class, "testArtists", null);
- getAccessStack().createTestData(DataContextCase.class, "testPaintings", null);
+ createTwoArtistsAndTwoPaintingsDataSet();
- // fetch artist
- DataContext context = createDataContext();
Artist a = (Artist) context.localObject(new ObjectId(
"Artist",
Artist.ARTIST_ID_PK_COLUMN,
- 33018), null);
- Map parameters = Collections.singletonMap("artist", a);
+ 11), null);
+ Map<String, Artist> parameters = Collections.singletonMap("artist", a);
- List paintings = createDataContextWithSharedCache(false)
- .performQuery("ObjectQuery", parameters, true);
+ List<?> paintings = ((DataContext) runtime.getContext()).performQuery(
+ "ObjectQuery",
+ parameters,
+ true);
assertNotNull(paintings);
assertEquals(1, paintings.size());
}
public void testProcedureQueryStringMapBoolean() throws Exception {
- // Don't run this on MySQL
- if (!getAccessStackAdapter().supportsStoredProcedures()) {
+
+ if (!accessStackAdapter.supportsStoredProcedures()) {
return;
}
- if (!getAccessStackAdapter().canMakeObjectsOutOfProcedures()) {
+ if (!accessStackAdapter.canMakeObjectsOutOfProcedures()) {
return;
}
- getAccessStack().createTestData(DataContextCase.class, "testArtists", null);
- getAccessStack().createTestData(DataContextCase.class, "testPaintings", null);
+ createTwoArtistsAndTwoPaintingsDataSet();
// fetch artist
- Map parameters = Collections.singletonMap("aName", "artist2");
- DataContext context = createDataContext();
- List artists;
+ Map<String, String> parameters = Collections.singletonMap("aName", "artist2");
+
+ List<?> artists;
// Sybase blows whenever a transaction wraps a SP, so turn of transactions
boolean transactionsFlag = context
@@ -108,7 +156,6 @@ public class DataContextPerformQueryAPIT
}
public void testNonSelectingQueryString() throws Exception {
- DataContext context = createDataContext();
int[] counts = context.performNonSelectingQuery("NonSelectingQuery");
@@ -124,10 +171,9 @@ public class DataContextPerformQueryAPIT
}
public void testNonSelectingQueryStringMap() throws Exception {
- DataContext context = createDataContext();
- Map parameters = new HashMap();
- parameters.put("id", new Integer(300));
+ Map<String, Object> parameters = new HashMap<String, Object>();
+ parameters.put("id", 300);
parameters.put("title", "Go Figure");
parameters.put("price", new BigDecimal("22.01"));
@@ -147,7 +193,7 @@ public class DataContextPerformQueryAPIT
}
public void testPerfomQueryNonSelecting() throws Exception {
- DataContext context = createDataContext();
+
Artist a = context.newObject(Artist.class);
a.setArtistName("aa");
context.commitChanges();
@@ -155,47 +201,43 @@ public class DataContextPerformQueryAPIT
SQLTemplate q = new SQLTemplate(Artist.class, "DELETE FROM ARTIST");
// this way of executing a query makes no sense, but it shouldn't blow either...
- List result = context.performQuery(q);
+ List<?> result = context.performQuery(q);
assertNotNull(result);
assertEquals(0, result.size());
}
public void testObjectQueryWithLocalCache() throws Exception {
- getAccessStack().createTestData(DataContextCase.class, "testArtists", null);
+ createTwoArtists();
- DataContext context = createDataContext();
- List artists = context.performQuery("QueryWithLocalCache", true);
- assertEquals(25, artists.size());
+ List<?> artists = context.performQuery("QueryWithLocalCache", true);
+ assertEquals(2, artists.size());
- blockQueries();
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- try {
- List artists1 = context.performQuery("QueryWithLocalCache", false);
- assertEquals(25, artists1.size());
- }
- finally {
- unblockQueries();
- }
+ public void execute() {
+ List<?> artists1 = context.performQuery("QueryWithLocalCache", false);
+ assertEquals(2, artists1.size());
+ }
+ });
}
public void testObjectQueryWithSharedCache() throws Exception {
- getAccessStack().createTestData(DataContextCase.class, "testArtists", null);
+ createTwoArtists();
- DataContext context = createDataContext();
- List artists = context.performQuery("QueryWithSharedCache", true);
- assertEquals(25, artists.size());
+ List<?> artists = context.performQuery("QueryWithSharedCache", true);
+ assertEquals(2, artists.size());
- blockQueries();
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- try {
- List artists1 = createDataContextWithSharedCache(false).performQuery(
- "QueryWithSharedCache",
- false);
- assertEquals(25, artists1.size());
- }
- finally {
- unblockQueries();
- }
+ public void execute() {
+
+ DataContext otherContext = (DataContext) runtime.getContext();
+ List<?> artists1 = otherContext.performQuery(
+ "QueryWithSharedCache",
+ false);
+ assertEquals(2, artists1.size());
+ }
+ });
}
}
Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPrefetchMultistepTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPrefetchMultistepTest.java?rev=956338&r1=956337&r2=956338&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPrefetchMultistepTest.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPrefetchMultistepTest.java Sun Jun 20 09:27:37 2010
@@ -19,6 +19,7 @@
package org.apache.cayenne.access;
+import java.sql.Timestamp;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -29,29 +30,80 @@ import org.apache.cayenne.ObjectId;
import org.apache.cayenne.PersistenceState;
import org.apache.cayenne.Persistent;
import org.apache.cayenne.ValueHolder;
+import org.apache.cayenne.di.Inject;
import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.query.PrefetchTreeNode;
import org.apache.cayenne.query.SelectQuery;
+import org.apache.cayenne.test.jdbc.DBHelper;
+import org.apache.cayenne.test.jdbc.TableHelper;
import org.apache.cayenne.testdo.testmap.ArtistExhibit;
import org.apache.cayenne.testdo.testmap.Exhibit;
import org.apache.cayenne.testdo.testmap.Gallery;
+import org.apache.cayenne.unit.di.server.ServerCase;
+import org.apache.cayenne.unit.di.server.UseServerRuntime;
-/**
- * Testing chained prefetches...
- */
-public class DataContextPrefetchMultistepTest extends DataContextCase {
+@UseServerRuntime(ServerCase.TESTMAP_PROJECT)
+public class DataContextPrefetchMultistepTest extends ServerCase {
+
+ @Inject
+ protected DataContext context;
+
+ @Inject
+ protected DBHelper dbHelper;
+
+ protected TableHelper tArtist;
+ protected TableHelper tExhibit;
+ protected TableHelper tGallery;
+ protected TableHelper tArtistExhibit;
@Override
- protected void setUp() throws Exception {
- super.setUp();
+ protected void setUpAfterInjection() throws Exception {
+ dbHelper.deleteAll("PAINTING_INFO");
+ dbHelper.deleteAll("PAINTING");
+ dbHelper.deleteAll("ARTIST_EXHIBIT");
+ dbHelper.deleteAll("ARTIST");
+ dbHelper.deleteAll("EXHIBIT");
+ dbHelper.deleteAll("GALLERY");
+
+ tArtist = new TableHelper(dbHelper, "ARTIST");
+ tArtist.setColumns("ARTIST_ID", "ARTIST_NAME");
+
+ tExhibit = new TableHelper(dbHelper, "EXHIBIT");
+ tExhibit.setColumns("EXHIBIT_ID", "GALLERY_ID", "OPENING_DATE", "CLOSING_DATE");
+
+ tArtistExhibit = new TableHelper(dbHelper, "ARTIST_EXHIBIT");
+ tArtistExhibit.setColumns("ARTIST_ID", "EXHIBIT_ID");
+
+ tGallery = new TableHelper(dbHelper, "GALLERY");
+ tGallery.setColumns("GALLERY_ID", "GALLERY_NAME");
+ }
- createTestData("testGalleries");
- populateExhibits();
- createTestData("testArtistExhibits");
+ protected void createTwoArtistsWithExhibitsDataSet() throws Exception {
+ tArtist.insert(11, "artist2");
+ tArtist.insert(101, "artist3");
+
+ tGallery.insert(25, "gallery1");
+ tGallery.insert(31, "gallery2");
+ tGallery.insert(45, "gallery3");
+
+ Timestamp now = new Timestamp(System.currentTimeMillis());
+
+ tExhibit.insert(1, 25, now, now);
+ tExhibit.insert(2, 31, now, now);
+ tExhibit.insert(3, 45, now, now);
+ tExhibit.insert(4, 25, now, now);
+
+ tArtistExhibit.insert(11, 2);
+ tArtistExhibit.insert(11, 4);
+ tArtistExhibit.insert(101, 1);
+ tArtistExhibit.insert(101, 2);
+ tArtistExhibit.insert(101, 4);
}
public void testToManyToManyFirstStepUnresolved() throws Exception {
+ createTwoArtistsWithExhibitsDataSet();
+
// since objects for the phantom prefetches are not retained explicitly, they may
// get garbage collected, and we won't be able to detect them
// so ensure ObjectStore uses a regular map just for this test
@@ -60,14 +112,14 @@ public class DataContextPrefetchMultiste
// Check the target ArtistExhibit objects do not exist yet
- Map id1 = new HashMap();
- id1.put("ARTIST_ID", new Integer(33001));
- id1.put("EXHIBIT_ID", new Integer(2));
+ Map<String, Object> id1 = new HashMap<String, Object>();
+ id1.put("ARTIST_ID", 11);
+ id1.put("EXHIBIT_ID", 2);
ObjectId oid1 = new ObjectId("ArtistExhibit", id1);
- Map id2 = new HashMap();
- id2.put("ARTIST_ID", new Integer(33003));
- id2.put("EXHIBIT_ID", new Integer(2));
+ Map<String, Object> id2 = new HashMap<String, Object>();
+ id2.put("ARTIST_ID", 101);
+ id2.put("EXHIBIT_ID", 2);
ObjectId oid2 = new ObjectId("ArtistExhibit", id2);
assertNull(context.getGraphManager().getNode(oid1));
@@ -78,10 +130,10 @@ public class DataContextPrefetchMultiste
.singletonMap("name", "gallery2")));
q.addPrefetch("exhibitArray.artistExhibitArray");
- List galleries = context.performQuery(q);
+ List<Gallery> galleries = context.performQuery(q);
assertEquals(1, galleries.size());
- Gallery g2 = (Gallery) galleries.get(0);
+ Gallery g2 = galleries.get(0);
// this relationship wasn't explicitly prefetched....
Object list = g2.readPropertyDirectly("exhibitArray");
@@ -99,37 +151,42 @@ public class DataContextPrefetchMultiste
public void testToManyToManyFirstStepResolved() throws Exception {
+ createTwoArtistsWithExhibitsDataSet();
+
Expression e = Expression.fromString("galleryName = $name");
SelectQuery q = new SelectQuery(Gallery.class, e.expWithParameters(Collections
.singletonMap("name", "gallery2")));
q.addPrefetch("exhibitArray");
q.addPrefetch("exhibitArray.artistExhibitArray");
- List galleries = context.performQuery(q);
+ List<Gallery> galleries = context.performQuery(q);
assertEquals(1, galleries.size());
- Gallery g2 = (Gallery) galleries.get(0);
+ Gallery g2 = galleries.get(0);
// this relationship should be resolved
assertTrue(g2.readPropertyDirectly("exhibitArray") instanceof ValueHolder);
- List exhibits = (List) g2.readPropertyDirectly("exhibitArray");
+ List<Exhibit> exhibits = (List<Exhibit>) g2.readPropertyDirectly("exhibitArray");
assertFalse(((ValueHolder) exhibits).isFault());
assertEquals(1, exhibits.size());
- Exhibit e1 = (Exhibit) exhibits.get(0);
+ Exhibit e1 = exhibits.get(0);
assertEquals(PersistenceState.COMMITTED, e1.getPersistenceState());
// this to-many must also be resolved
assertTrue(e1.readPropertyDirectly("artistExhibitArray") instanceof ValueHolder);
- List aexhibits = (List) e1.readPropertyDirectly("artistExhibitArray");
+ List<ArtistExhibit> aexhibits = (List<ArtistExhibit>) e1
+ .readPropertyDirectly("artistExhibitArray");
assertFalse(((ValueHolder) aexhibits).isFault());
assertEquals(1, exhibits.size());
- ArtistExhibit ae1 = (ArtistExhibit) aexhibits.get(0);
+ ArtistExhibit ae1 = aexhibits.get(0);
assertEquals(PersistenceState.COMMITTED, ae1.getPersistenceState());
}
- public void testMixedPrefetch1() {
+ public void testMixedPrefetch1() throws Exception {
+
+ createTwoArtistsWithExhibitsDataSet();
Expression e = Expression.fromString("galleryName = $name");
SelectQuery q = new SelectQuery(Gallery.class, e.expWithParameters(Collections
@@ -138,31 +195,34 @@ public class DataContextPrefetchMultiste
PrefetchTreeNode.JOINT_PREFETCH_SEMANTICS);
q.addPrefetch("exhibitArray.artistExhibitArray");
- List galleries = context.performQuery(q);
+ List<Gallery> galleries = context.performQuery(q);
assertEquals(1, galleries.size());
- Gallery g2 = (Gallery) galleries.get(0);
+ Gallery g2 = galleries.get(0);
// this relationship should be resolved
assertTrue(g2.readPropertyDirectly("exhibitArray") instanceof ValueHolder);
- List exhibits = (List) g2.readPropertyDirectly("exhibitArray");
+ List<Exhibit> exhibits = (List<Exhibit>) g2.readPropertyDirectly("exhibitArray");
assertFalse(((ValueHolder) exhibits).isFault());
assertEquals(1, exhibits.size());
- Exhibit e1 = (Exhibit) exhibits.get(0);
+ Exhibit e1 = exhibits.get(0);
assertEquals(PersistenceState.COMMITTED, e1.getPersistenceState());
// this to-many must also be resolved
assertTrue(e1.readPropertyDirectly("artistExhibitArray") instanceof ValueHolder);
- List aexhibits = (List) e1.readPropertyDirectly("artistExhibitArray");
+ List<ArtistExhibit> aexhibits = (List<ArtistExhibit>) e1
+ .readPropertyDirectly("artistExhibitArray");
assertFalse(((ValueHolder) aexhibits).isFault());
assertEquals(2, aexhibits.size());
- ArtistExhibit ae1 = (ArtistExhibit) aexhibits.get(0);
+ ArtistExhibit ae1 = aexhibits.get(0);
assertEquals(PersistenceState.COMMITTED, ae1.getPersistenceState());
}
- public void testMixedPrefetch2() {
+ public void testMixedPrefetch2() throws Exception {
+
+ createTwoArtistsWithExhibitsDataSet();
Expression e = Expression.fromString("galleryName = $name");
SelectQuery q = new SelectQuery(Gallery.class, e.expWithParameters(Collections
@@ -173,27 +233,28 @@ public class DataContextPrefetchMultiste
q.addPrefetch("exhibitArray.artistExhibitArray").setSemantics(
PrefetchTreeNode.JOINT_PREFETCH_SEMANTICS);
- List galleries = context.performQuery(q);
+ List<Gallery> galleries = context.performQuery(q);
assertEquals(1, galleries.size());
- Gallery g2 = (Gallery) galleries.get(0);
+ Gallery g2 = galleries.get(0);
// this relationship should be resolved
assertTrue(g2.readPropertyDirectly("exhibitArray") instanceof ValueHolder);
- List exhibits = (List) g2.readPropertyDirectly("exhibitArray");
+ List<Exhibit> exhibits = (List<Exhibit>) g2.readPropertyDirectly("exhibitArray");
assertFalse(((ValueHolder) exhibits).isFault());
assertEquals(1, exhibits.size());
- Exhibit e1 = (Exhibit) exhibits.get(0);
+ Exhibit e1 = exhibits.get(0);
assertEquals(PersistenceState.COMMITTED, e1.getPersistenceState());
// this to-many must also be resolved
assertTrue(e1.readPropertyDirectly("artistExhibitArray") instanceof ValueHolder);
- List aexhibits = (List) e1.readPropertyDirectly("artistExhibitArray");
+ List<ArtistExhibit> aexhibits = (List<ArtistExhibit>) e1
+ .readPropertyDirectly("artistExhibitArray");
assertFalse(((ValueHolder) aexhibits).isFault());
assertEquals(2, aexhibits.size());
- ArtistExhibit ae1 = (ArtistExhibit) aexhibits.get(0);
+ ArtistExhibit ae1 = aexhibits.get(0);
assertEquals(PersistenceState.COMMITTED, ae1.getPersistenceState());
}
}
Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPrefetchTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPrefetchTest.java?rev=956338&r1=956337&r2=956338&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPrefetchTest.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextPrefetchTest.java Sun Jun 20 09:27:37 2010
@@ -19,17 +19,16 @@
package org.apache.cayenne.access;
-import java.util.Collections;
+import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.cayenne.Cayenne;
-import org.apache.cayenne.CayenneDataObject;
-import org.apache.cayenne.DataObject;
import org.apache.cayenne.PersistenceState;
import org.apache.cayenne.ValueHolder;
+import org.apache.cayenne.di.Inject;
import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.exp.ExpressionFactory;
import org.apache.cayenne.map.ObjEntity;
@@ -38,129 +37,225 @@ import org.apache.cayenne.query.Prefetch
import org.apache.cayenne.query.QueryCacheStrategy;
import org.apache.cayenne.query.SelectQuery;
import org.apache.cayenne.query.SortOrder;
+import org.apache.cayenne.test.jdbc.DBHelper;
+import org.apache.cayenne.test.jdbc.TableHelper;
import org.apache.cayenne.testdo.testmap.ArtGroup;
import org.apache.cayenne.testdo.testmap.Artist;
import org.apache.cayenne.testdo.testmap.ArtistExhibit;
import org.apache.cayenne.testdo.testmap.Painting;
import org.apache.cayenne.testdo.testmap.PaintingInfo;
+import org.apache.cayenne.unit.di.UnitTestClosure;
+import org.apache.cayenne.unit.di.server.DataChannelQueryInterceptor;
+import org.apache.cayenne.unit.di.server.ServerCase;
+import org.apache.cayenne.unit.di.server.UseServerRuntime;
-/**
- */
-public class DataContextPrefetchTest extends DataContextCase {
+@UseServerRuntime(ServerCase.TESTMAP_PROJECT)
+public class DataContextPrefetchTest extends ServerCase {
- /**
- * Test that a to-many relationship is initialized.
- */
- public void testPrefetchToMany() throws Exception {
- createTestData("testPaintings");
+ @Inject
+ protected DataContext context;
+
+ @Inject
+ protected DBHelper dbHelper;
+
+ @Inject
+ protected DataChannelQueryInterceptor queryInterceptor;
+
+ protected TableHelper tArtist;
+ protected TableHelper tPainting;
+ protected TableHelper tPaintingInfo;
+ protected TableHelper tExhibit;
+ protected TableHelper tGallery;
+ protected TableHelper tArtistExhibit;
+
+ @Override
+ protected void setUpAfterInjection() throws Exception {
+ dbHelper.deleteAll("PAINTING_INFO");
+ dbHelper.deleteAll("PAINTING");
+ dbHelper.deleteAll("ARTIST_EXHIBIT");
+ dbHelper.deleteAll("ARTIST");
+ dbHelper.deleteAll("EXHIBIT");
+ dbHelper.deleteAll("GALLERY");
+
+ tArtist = new TableHelper(dbHelper, "ARTIST");
+ tArtist.setColumns("ARTIST_ID", "ARTIST_NAME");
+
+ tPainting = new TableHelper(dbHelper, "PAINTING");
+ tPainting.setColumns(
+ "PAINTING_ID",
+ "PAINTING_TITLE",
+ "ARTIST_ID",
+ "ESTIMATED_PRICE");
+
+ tPaintingInfo = new TableHelper(dbHelper, "PAINTING_INFO");
+ tPaintingInfo.setColumns("PAINTING_ID", "TEXT_REVIEW");
+
+ tExhibit = new TableHelper(dbHelper, "EXHIBIT");
+ tExhibit.setColumns("EXHIBIT_ID", "GALLERY_ID", "OPENING_DATE", "CLOSING_DATE");
+
+ tArtistExhibit = new TableHelper(dbHelper, "ARTIST_EXHIBIT");
+ tArtistExhibit.setColumns("ARTIST_ID", "EXHIBIT_ID");
+
+ tGallery = new TableHelper(dbHelper, "GALLERY");
+ tGallery.setColumns("GALLERY_ID", "GALLERY_NAME");
+ }
+
+ protected void createTwoArtistsAndTwoPaintingsDataSet() throws Exception {
+ tArtist.insert(11, "artist2");
+ tArtist.insert(101, "artist3");
+ tPainting.insert(6, "p_artist3", 101, 1000);
+ tPainting.insert(7, "p_artist2", 11, 2000);
+ }
+
+ protected void createArtistWithTwoPaintingsAndTwoInfosDataSet() throws Exception {
+ tArtist.insert(11, "artist2");
+
+ tPainting.insert(6, "p_artist2", 11, 1000);
+ tPainting.insert(7, "p_artist3", 11, 2000);
+
+ tPaintingInfo.insert(6, "xYs");
+ }
+
+ protected void createTwoArtistsWithExhibitsDataSet() throws Exception {
+ tArtist.insert(11, "artist2");
+ tArtist.insert(101, "artist3");
+
+ tGallery.insert(25, "gallery1");
+ tGallery.insert(31, "gallery2");
+ tGallery.insert(45, "gallery3");
- Map params = new HashMap();
+ Timestamp now = new Timestamp(System.currentTimeMillis());
+
+ tExhibit.insert(1, 25, now, now);
+ tExhibit.insert(2, 31, now, now);
+ tExhibit.insert(3, 45, now, now);
+ tExhibit.insert(4, 25, now, now);
+
+ tArtistExhibit.insert(11, 2);
+ tArtistExhibit.insert(11, 4);
+ tArtistExhibit.insert(101, 1);
+ tArtistExhibit.insert(101, 3);
+ tArtistExhibit.insert(101, 4);
+ }
+
+ public void testPrefetchToMany_WithQualfier() throws Exception {
+ createTwoArtistsAndTwoPaintingsDataSet();
+
+ Map<String, Object> params = new HashMap<String, Object>();
params.put("name1", "artist2");
params.put("name2", "artist3");
Expression e = Expression
.fromString("artistName = $name1 or artistName = $name2");
SelectQuery q = new SelectQuery("Artist", e.expWithParameters(params));
- q.addPrefetch("paintingArray");
+ q.addPrefetch(Artist.PAINTING_ARRAY_PROPERTY);
- List artists = context.performQuery(q);
+ final List<Artist> artists = context.performQuery(q);
- blockQueries();
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- try {
- assertEquals(2, artists.size());
+ public void execute() {
- Artist a1 = (Artist) artists.get(0);
- List toMany = (List) a1.readPropertyDirectly("paintingArray");
- assertNotNull(toMany);
- assertFalse(((ValueHolder) toMany).isFault());
- assertEquals(1, toMany.size());
-
- Painting p1 = (Painting) toMany.get(0);
- assertEquals("P_" + a1.getArtistName(), p1.getPaintingTitle());
-
- Artist a2 = (Artist) artists.get(1);
- List toMany2 = (List) a2.readPropertyDirectly("paintingArray");
- assertNotNull(toMany2);
- assertFalse(((ValueHolder) toMany2).isFault());
- assertEquals(1, toMany2.size());
+ assertEquals(2, artists.size());
- Painting p2 = (Painting) toMany2.get(0);
- assertEquals("P_" + a2.getArtistName(), p2.getPaintingTitle());
- }
- finally {
- unblockQueries();
- }
+ Artist a1 = artists.get(0);
+ List<?> toMany = (List<?>) a1
+ .readPropertyDirectly(Artist.PAINTING_ARRAY_PROPERTY);
+ assertNotNull(toMany);
+ assertFalse(((ValueHolder) toMany).isFault());
+ assertEquals(1, toMany.size());
+
+ Painting p1 = (Painting) toMany.get(0);
+ assertEquals("p_" + a1.getArtistName(), p1.getPaintingTitle());
+
+ Artist a2 = artists.get(1);
+ List<?> toMany2 = (List<?>) a2
+ .readPropertyDirectly(Artist.PAINTING_ARRAY_PROPERTY);
+ assertNotNull(toMany2);
+ assertFalse(((ValueHolder) toMany2).isFault());
+ assertEquals(1, toMany2.size());
+
+ Painting p2 = (Painting) toMany2.get(0);
+ assertEquals("p_" + a2.getArtistName(), p2.getPaintingTitle());
+ }
+ });
}
- /**
- * Test that a to-many relationship is initialized.
- */
public void testPrefetchToManyNoQualifier() throws Exception {
- createTestData("testPaintings");
+ createTwoArtistsAndTwoPaintingsDataSet();
+
SelectQuery q = new SelectQuery(Artist.class);
- q.addPrefetch("paintingArray");
+ q.addPrefetch(Artist.PAINTING_ARRAY_PROPERTY);
- List artists = context.performQuery(q);
+ final List<Artist> artists = context.performQuery(q);
- blockQueries();
- try {
- assertEquals(artistCount, artists.size());
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- for (int i = 0; i < artistCount; i++) {
- Artist a = (Artist) artists.get(i);
- List toMany = (List) a.readPropertyDirectly("paintingArray");
- assertNotNull(toMany);
- assertFalse(((ValueHolder) toMany).isFault());
- assertEquals(1, toMany.size());
+ public void execute() {
+
+ assertEquals(2, artists.size());
- Painting p = (Painting) toMany.get(0);
- assertEquals(
- "Invalid prefetched painting:" + p,
- "P_" + a.getArtistName(),
- p.getPaintingTitle());
+ for (int i = 0; i < 2; i++) {
+ Artist a = artists.get(i);
+ List<?> toMany = (List<?>) a.readPropertyDirectly("paintingArray");
+ assertNotNull(toMany);
+ assertFalse(((ValueHolder) toMany).isFault());
+ assertEquals(1, toMany.size());
+
+ Painting p = (Painting) toMany.get(0);
+ assertEquals("Invalid prefetched painting:" + p, "p_"
+ + a.getArtistName(), p.getPaintingTitle());
+ }
}
- }
- finally {
- unblockQueries();
- }
+ });
}
/**
* Test that a to-many relationship is initialized when a target entity has a compound
* PK only partially involved in relationship.
*/
- public void testPrefetchToManyOnJoinTableDisjoinedPrefetch() throws Exception {
- // setup data
- createTestData("testGalleries");
- populateExhibits();
- createTestData("testArtistExhibits");
+ public void testPrefetchToMany_OnJoinTableDisjoinedPrefetch() throws Exception {
+
+ createTwoArtistsWithExhibitsDataSet();
SelectQuery q = new SelectQuery(Artist.class);
- q.addPrefetch("artistExhibitArray").setSemantics(
+ q.addPrefetch(Artist.ARTIST_EXHIBIT_ARRAY_PROPERTY).setSemantics(
PrefetchTreeNode.DISJOINT_PREFETCH_SEMANTICS);
q.addOrdering(Artist.ARTIST_NAME_PROPERTY, SortOrder.ASCENDING);
- List artists = context.performQuery(q);
+ final List<Artist> artists = context.performQuery(q);
- blockQueries();
- try {
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- assertEquals(artistCount, artists.size());
+ public void execute() {
+ assertEquals(2, artists.size());
- Artist a1 = (Artist) artists.get(0);
- assertEquals("artist1", a1.getArtistName());
- List toMany = (List) a1.readPropertyDirectly("artistExhibitArray");
- assertNotNull(toMany);
- assertFalse(((ValueHolder) toMany).isFault());
- assertEquals(2, toMany.size());
-
- ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0);
- assertEquals(PersistenceState.COMMITTED, artistExhibit.getPersistenceState());
- assertSame(a1, artistExhibit.getToArtist());
- }
- finally {
- unblockQueries();
- }
+ Artist a1 = artists.get(0);
+ assertEquals("artist2", a1.getArtistName());
+ List<?> toMany = (List<?>) a1
+ .readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY_PROPERTY);
+ assertNotNull(toMany);
+ assertFalse(((ValueHolder) toMany).isFault());
+ assertEquals(2, toMany.size());
+
+ ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0);
+ assertEquals(PersistenceState.COMMITTED, artistExhibit
+ .getPersistenceState());
+ assertSame(a1, artistExhibit.getToArtist());
+
+ Artist a2 = artists.get(1);
+ assertEquals("artist3", a2.getArtistName());
+ List<?> toMany2 = (List<?>) a2
+ .readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY_PROPERTY);
+ assertNotNull(toMany2);
+ assertFalse(((ValueHolder) toMany2).isFault());
+ assertEquals(3, toMany2.size());
+
+ ArtistExhibit artistExhibit2 = (ArtistExhibit) toMany2.get(0);
+ assertEquals(PersistenceState.COMMITTED, artistExhibit2
+ .getPersistenceState());
+ assertSame(a2, artistExhibit2.getToArtist());
+ }
+ });
}
/**
@@ -168,45 +263,55 @@ public class DataContextPrefetchTest ext
* PK only partially involved in relationship.
*/
public void testPrefetchToManyOnJoinTableJoinedPrefetch() throws Exception {
- // setup data
- createTestData("testGalleries");
- populateExhibits();
- createTestData("testArtistExhibits");
+ createTwoArtistsWithExhibitsDataSet();
SelectQuery q = new SelectQuery(Artist.class);
q.addPrefetch("artistExhibitArray").setSemantics(
PrefetchTreeNode.JOINT_PREFETCH_SEMANTICS);
q.addOrdering(Artist.ARTIST_NAME_PROPERTY, SortOrder.ASCENDING);
- List artists = context.performQuery(q);
+ final List<Artist> artists = context.performQuery(q);
- blockQueries();
- try {
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- assertEquals(artistCount, artists.size());
+ public void execute() {
- Artist a1 = (Artist) artists.get(0);
- assertEquals("artist1", a1.getArtistName());
- List toMany = (List) a1.readPropertyDirectly("artistExhibitArray");
- assertNotNull(toMany);
- assertFalse(((ValueHolder) toMany).isFault());
- assertEquals(2, toMany.size());
-
- ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0);
- assertEquals(PersistenceState.COMMITTED, artistExhibit.getPersistenceState());
- assertSame(a1, artistExhibit.getToArtist());
- }
- finally {
- unblockQueries();
- }
+ assertEquals(2, artists.size());
+
+ Artist a1 = artists.get(0);
+ assertEquals("artist2", a1.getArtistName());
+ List<?> toMany = (List<?>) a1.readPropertyDirectly("artistExhibitArray");
+ assertNotNull(toMany);
+ assertFalse(((ValueHolder) toMany).isFault());
+ assertEquals(2, toMany.size());
+
+ ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0);
+ assertEquals(PersistenceState.COMMITTED, artistExhibit
+ .getPersistenceState());
+ assertSame(a1, artistExhibit.getToArtist());
+
+ Artist a2 = artists.get(1);
+ assertEquals("artist3", a2.getArtistName());
+ List<?> toMany2 = (List<?>) a2
+ .readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY_PROPERTY);
+ assertNotNull(toMany2);
+ assertFalse(((ValueHolder) toMany2).isFault());
+ assertEquals(3, toMany2.size());
+
+ ArtistExhibit artistExhibit2 = (ArtistExhibit) toMany2.get(0);
+ assertEquals(PersistenceState.COMMITTED, artistExhibit2
+ .getPersistenceState());
+ assertSame(a2, artistExhibit2.getToArtist());
+ }
+ });
}
/**
* Test that a to-many relationship is initialized when there is no inverse
* relationship
*/
- public void testPrefetch3a() throws Exception {
- createTestData("testPaintings");
+ public void testPrefetch_ToManyNoReverse() throws Exception {
+ createTwoArtistsAndTwoPaintingsDataSet();
ObjEntity paintingEntity = context.getEntityResolver().lookupObjEntity(
Painting.class);
@@ -214,37 +319,29 @@ public class DataContextPrefetchTest ext
.getRelationship("toArtist");
paintingEntity.removeRelationship("toArtist");
- SelectQuery q = new SelectQuery("Artist");
- q.addPrefetch("paintingArray");
-
try {
- List result = context.performQuery(q);
-
- blockQueries();
- try {
- assertFalse(result.isEmpty());
- CayenneDataObject a1 = (CayenneDataObject) result.get(0);
- List toMany = (List) a1.readPropertyDirectly("paintingArray");
- assertNotNull(toMany);
- assertFalse(((ValueHolder) toMany).isFault());
- }
- finally {
- unblockQueries();
- }
+ SelectQuery q = new SelectQuery(Artist.class);
+ q.addPrefetch(Artist.PAINTING_ARRAY_PROPERTY);
+ final List<Artist> result = context.performQuery(q);
+
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
+
+ public void execute() {
+ assertFalse(result.isEmpty());
+ Artist a1 = result.get(0);
+ List<?> toMany = (List<?>) a1.readPropertyDirectly("paintingArray");
+ assertNotNull(toMany);
+ assertFalse(((ValueHolder) toMany).isFault());
+ }
+ });
}
finally {
- // Fix it up again, so other tests do not fail
paintingEntity.addRelationship(relationship);
}
-
}
- /**
- * Test that a to-many relationship is initialized when there is no inverse
- * relationship and the root query is qualified
- */
- public void testPrefetchOneWayToMany() throws Exception {
- createTestData("testPaintings");
+ public void testPrefetch_ToManyNoReverseWithQualifier() throws Exception {
+ createTwoArtistsAndTwoPaintingsDataSet();
ObjEntity paintingEntity = context.getEntityResolver().lookupObjEntity(
Painting.class);
@@ -252,97 +349,81 @@ public class DataContextPrefetchTest ext
.getRelationship("toArtist");
paintingEntity.removeRelationship("toArtist");
- SelectQuery q = new SelectQuery("Artist");
- q.setQualifier(ExpressionFactory.matchExp("artistName", "artist1"));
- q.addPrefetch("paintingArray");
-
try {
- List result = context.performQuery(q);
-
- blockQueries();
- try {
- assertFalse(result.isEmpty());
+ SelectQuery q = new SelectQuery(Artist.class);
+ q.setQualifier(ExpressionFactory.matchExp("artistName", "artist2"));
+ q.addPrefetch(Artist.PAINTING_ARRAY_PROPERTY);
+
+ final List<Artist> result = context.performQuery(q);
+
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
+
+ public void execute() {
+ assertFalse(result.isEmpty());
+ Artist a1 = result.get(0);
+ List<?> toMany = (List<?>) a1.readPropertyDirectly("paintingArray");
+ assertNotNull(toMany);
+ assertFalse(((ValueHolder) toMany).isFault());
+ }
+ });
- CayenneDataObject a1 = (CayenneDataObject) result.get(0);
- List toMany = (List) a1.readPropertyDirectly("paintingArray");
- assertNotNull(toMany);
- assertFalse(((ValueHolder) toMany).isFault());
- }
- finally {
- unblockQueries();
- }
}
finally {
- // Fix it up again, so other tests do not fail
paintingEntity.addRelationship(relationship);
}
-
}
- /**
- * Test that a to-one relationship is initialized.
- */
- public void testPrefetch4() throws Exception {
- createTestData("testPaintings");
+ public void testPrefetch_ToOne() throws Exception {
+ createTwoArtistsAndTwoPaintingsDataSet();
- SelectQuery q = new SelectQuery("Painting");
- q.addPrefetch("toArtist");
+ SelectQuery q = new SelectQuery(Painting.class);
+ q.addPrefetch(Painting.TO_ARTIST_PROPERTY);
- List result = context.performQuery(q);
+ final List<Painting> result = context.performQuery(q);
- blockQueries();
- try {
- assertFalse(result.isEmpty());
- DataObject p1 = (DataObject) result.get(0);
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- Object toOnePrefetch = p1.readNestedProperty("toArtist");
- assertNotNull(toOnePrefetch);
- assertTrue(
- "Expected DataObject, got: " + toOnePrefetch.getClass().getName(),
- toOnePrefetch instanceof DataObject);
+ public void execute() {
+ assertFalse(result.isEmpty());
+ Painting p1 = result.get(0);
- DataObject a1 = (DataObject) toOnePrefetch;
- assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
- }
- finally {
- unblockQueries();
- }
+ Object toOnePrefetch = p1.readNestedProperty("toArtist");
+ assertNotNull(toOnePrefetch);
+ assertTrue(
+ "Expected Artist, got: " + toOnePrefetch.getClass().getName(),
+ toOnePrefetch instanceof Artist);
+
+ Artist a1 = (Artist) toOnePrefetch;
+ assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
+ }
+ });
}
- /**
- * Test prefetching with queries using DB_PATH.
- */
- public void testPrefetch5() throws Exception {
- createTestData("testPaintings");
+ public void testPrefetch_ToOne_DbPath() throws Exception {
+ createTwoArtistsAndTwoPaintingsDataSet();
- SelectQuery q = new SelectQuery("Painting");
+ SelectQuery q = new SelectQuery(Painting.class);
+ q.addPrefetch(Painting.TO_ARTIST_PROPERTY);
q.andQualifier(ExpressionFactory.matchDbExp("toArtist.ARTIST_NAME", "artist2"));
- q.addPrefetch("toArtist");
- List results = context.performQuery(q);
+ List<Painting> results = context.performQuery(q);
assertEquals(1, results.size());
}
- /**
- * Test prefetching with queries using OBJ_PATH.
- */
- public void testPrefetch6() throws Exception {
- createTestData("testPaintings");
+ public void testPrefetch_ToOne_ObjPath() throws Exception {
+ createTwoArtistsAndTwoPaintingsDataSet();
- SelectQuery q = new SelectQuery("Painting");
+ SelectQuery q = new SelectQuery(Painting.class);
+ q.addPrefetch(Painting.TO_ARTIST_PROPERTY);
q.andQualifier(ExpressionFactory.matchExp("toArtist.artistName", "artist2"));
- q.addPrefetch("toArtist");
- List results = context.performQuery(q);
+ List<Painting> results = context.performQuery(q);
assertEquals(1, results.size());
}
- /**
- * Test prefetching with the prefetch on a reflexive relationship
- */
- public void testPrefetch7() throws Exception {
+ public void testPrefetch_ReflexiveRelationship() throws Exception {
ArtGroup parent = (ArtGroup) context.newObject("ArtGroup");
parent.setName("parent");
ArtGroup child = (ArtGroup) context.newObject("ArtGroup");
@@ -354,60 +435,51 @@ public class DataContextPrefetchTest ext
q.setQualifier(ExpressionFactory.matchExp("name", "child"));
q.addPrefetch("toParentGroup");
- List results = context.performQuery(q);
+ final List<ArtGroup> results = context.performQuery(q);
- blockQueries();
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- try {
- assertEquals(1, results.size());
+ public void execute() {
+ assertEquals(1, results.size());
- ArtGroup fetchedChild = (ArtGroup) results.get(0);
- // The parent must be fully fetched, not just HOLLOW (a fault)
- assertEquals(PersistenceState.COMMITTED, fetchedChild
- .getToParentGroup()
- .getPersistenceState());
- }
- finally {
- unblockQueries();
- }
+ ArtGroup fetchedChild = results.get(0);
+ // The parent must be fully fetched, not just HOLLOW (a fault)
+ assertEquals(PersistenceState.COMMITTED, fetchedChild
+ .getToParentGroup()
+ .getPersistenceState());
+ }
+ });
}
- /**
- * Test prefetching with qualifier on the root query containing the path to the
- * prefetch.
- */
- public void testPrefetch8() throws Exception {
- createTestData("testPaintings");
- Expression exp = ExpressionFactory.matchExp("toArtist.artistName", "artist1");
+ public void testPrefetch_ToOneWithQualifierOverlappingPrefetchPath() throws Exception {
+ createTwoArtistsAndTwoPaintingsDataSet();
+
+ Expression exp = ExpressionFactory.matchExp("toArtist.artistName", "artist3");
SelectQuery q = new SelectQuery(Painting.class, exp);
- q.addPrefetch("toArtist");
+ q.addPrefetch(Painting.TO_ARTIST_PROPERTY);
- List results = context.performQuery(q);
+ final List<Painting> results = context.performQuery(q);
- blockQueries();
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- try {
- assertEquals(1, results.size());
+ public void execute() {
+ assertEquals(1, results.size());
- Painting painting = (Painting) results.get(0);
+ Painting painting = results.get(0);
- // The parent must be fully fetched, not just HOLLOW (a fault)
- assertEquals(PersistenceState.COMMITTED, painting
- .getToArtist()
- .getPersistenceState());
- }
- finally {
- unblockQueries();
- }
+ // The parent must be fully fetched, not just HOLLOW (a fault)
+ assertEquals(PersistenceState.COMMITTED, painting
+ .getToArtist()
+ .getPersistenceState());
+ }
+ });
}
- /**
- * Test prefetching with qualifier on the root query being the path to the prefetch.
- */
public void testPrefetch9() throws Exception {
- createTestData("testPaintings");
- Expression artistExp = ExpressionFactory.matchExp("artistName", "artist1");
+ createTwoArtistsAndTwoPaintingsDataSet();
+
+ Expression artistExp = ExpressionFactory.matchExp("artistName", "artist3");
SelectQuery artistQuery = new SelectQuery(Artist.class, artistExp);
Artist artist1 = (Artist) context.performQuery(artistQuery).get(0);
@@ -418,63 +490,58 @@ public class DataContextPrefetchTest ext
SelectQuery q = new SelectQuery(Painting.class, exp);
q.addPrefetch("toArtist");
- // run the query ... see that it doesn't blow
- List paintings = context.performQuery(q);
+ final List<Painting> results = context.performQuery(q);
- blockQueries();
- try {
- assertEquals(24, paintings.size());
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- // see that artists are resolved...
+ public void execute() {
+ assertEquals(1, results.size());
- Painting px = (Painting) paintings.get(3);
- Artist ax = (Artist) px.readProperty(Painting.TO_ARTIST_PROPERTY);
- assertEquals(PersistenceState.COMMITTED, ax.getPersistenceState());
- }
- finally {
- unblockQueries();
- }
+ // see that artists are resolved...
+ Painting px = results.get(0);
+ Artist ax = (Artist) px.readProperty(Painting.TO_ARTIST_PROPERTY);
+ assertEquals(PersistenceState.COMMITTED, ax.getPersistenceState());
+ }
+ });
}
- public void testPrefetchOneToOne() throws Exception {
- createTestData("testPaintingInfos");
+ public void testPrefetch_OneToOneWithQualifier() throws Exception {
+ createArtistWithTwoPaintingsAndTwoInfosDataSet();
Expression e = ExpressionFactory.likeExp("toArtist.artistName", "a%");
SelectQuery q = new SelectQuery(Painting.class, e);
q.addPrefetch(Painting.TO_PAINTING_INFO_PROPERTY);
q.addOrdering(Painting.PAINTING_TITLE_PROPERTY, SortOrder.ASCENDING);
- List results = context.performQuery(q);
-
- blockQueries();
+ final List<Painting> results = context.performQuery(q);
- try {
- assertEquals(4, results.size());
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- // testing non-null to-one target
- Painting p2 = (Painting) results.get(1);
- Object o2 = p2.readPropertyDirectly(Painting.TO_PAINTING_INFO_PROPERTY);
- assertTrue(o2 instanceof PaintingInfo);
- PaintingInfo pi2 = (PaintingInfo) o2;
- assertEquals(PersistenceState.COMMITTED, pi2.getPersistenceState());
- assertEquals(Cayenne.intPKForObject(p2), Cayenne
- .intPKForObject(pi2));
-
- // testing null to-one target
- Painting p4 = (Painting) results.get(3);
- assertNull(p4.readPropertyDirectly(Painting.TO_PAINTING_INFO_PROPERTY));
+ public void execute() {
+ assertEquals(2, results.size());
- // there was a bug marking an object as dirty when clearing the relationships
- assertEquals(PersistenceState.COMMITTED, p4.getPersistenceState());
- }
- finally {
- unblockQueries();
- }
+ // testing non-null to-one target
+ Painting p0 = results.get(0);
+ Object o2 = p0.readPropertyDirectly(Painting.TO_PAINTING_INFO_PROPERTY);
+ assertTrue(o2 instanceof PaintingInfo);
+ PaintingInfo pi2 = (PaintingInfo) o2;
+ assertEquals(PersistenceState.COMMITTED, pi2.getPersistenceState());
+ assertEquals(Cayenne.intPKForObject(p0), Cayenne.intPKForObject(pi2));
+
+ // testing null to-one target
+ Painting p1 = results.get(1);
+ assertNull(p1.readPropertyDirectly(Painting.TO_PAINTING_INFO_PROPERTY));
+
+ // there was a bug marking an object as dirty when clearing the
+ // relationships
+ assertEquals(PersistenceState.COMMITTED, p1.getPersistenceState());
+ }
+ });
}
- public void testCAY119() throws Exception {
- createTestData("testPaintings");
+ public void testPrefetchToMany_DateInQualifier() throws Exception {
+ createTwoArtistsAndTwoPaintingsDataSet();
Expression e = ExpressionFactory.matchExp("dateOfBirth", new Date());
SelectQuery q = new SelectQuery(Artist.class, e);
@@ -487,101 +554,91 @@ public class DataContextPrefetchTest ext
public void testPrefetchingToOneNull() throws Exception {
- Painting p1 = context.newObject(Painting.class);
- p1.setPaintingTitle("aaaa");
-
- context.commitChanges();
- context.invalidateObjects(Collections.singleton(p1));
+ tPainting.insert(6, "p_Xty", null, 1000);
SelectQuery q = new SelectQuery(Painting.class);
- q.addPrefetch("toArtist");
+ q.addPrefetch(Painting.TO_ARTIST_PROPERTY);
- // run the query ... see that it doesn't blow
- List paintings = context.performQuery(q);
+ final List<Painting> paintings = context.performQuery(q);
- blockQueries();
- try {
- assertEquals(1, paintings.size());
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- Painting p2 = (Painting) paintings.get(0);
- assertNull(p2.readProperty(Painting.TO_ARTIST_PROPERTY));
- }
- finally {
- unblockQueries();
- }
+ public void execute() {
+ assertEquals(1, paintings.size());
+
+ Painting p2 = paintings.get(0);
+ assertNull(p2.readProperty(Painting.TO_ARTIST_PROPERTY));
+ }
+ });
}
public void testPrefetchToOneSharedCache() throws Exception {
- createTestData("testPaintings");
+ createTwoArtistsAndTwoPaintingsDataSet();
- SelectQuery q = new SelectQuery("Painting");
- q.addPrefetch("toArtist");
- q.setName("__testPrefetchToOneSharedCache__" + System.currentTimeMillis());
+ final SelectQuery q = new SelectQuery(Painting.class);
+ q.addPrefetch(Painting.TO_ARTIST_PROPERTY);
q.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
context.performQuery(q);
- blockQueries();
- try {
- // per CAY-499 second run of a cached query with prefetches (i.e. when the
- // result is served from cache) used to throw an exception...
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- List cachedResult = context.performQuery(q);
+ public void execute() {
+ // per CAY-499 second run of a cached query with prefetches (i.e. when the
+ // result is served from cache) used to throw an exception...
- assertFalse(cachedResult.isEmpty());
- DataObject p1 = (DataObject) cachedResult.get(0);
+ List<Painting> cachedResult = context.performQuery(q);
- Object toOnePrefetch = p1.readNestedProperty("toArtist");
- assertNotNull(toOnePrefetch);
- assertTrue(
- "Expected DataObject, got: " + toOnePrefetch.getClass().getName(),
- toOnePrefetch instanceof DataObject);
+ assertFalse(cachedResult.isEmpty());
+ Painting p1 = cachedResult.get(0);
- DataObject a1 = (DataObject) toOnePrefetch;
- assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
+ Object toOnePrefetch = p1.readNestedProperty("toArtist");
+ assertNotNull(toOnePrefetch);
+ assertTrue(
+ "Expected Artist, got: " + toOnePrefetch.getClass().getName(),
+ toOnePrefetch instanceof Artist);
- // and just in case - run one more time...
- context.performQuery(q);
- }
- finally {
- unblockQueries();
- }
+ Artist a1 = (Artist) toOnePrefetch;
+ assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
+
+ // and just in case - run one more time...
+ context.performQuery(q);
+ }
+ });
}
public void testPrefetchToOneLocalCache() throws Exception {
- createTestData("testPaintings");
+ createTwoArtistsAndTwoPaintingsDataSet();
- SelectQuery q = new SelectQuery("Painting");
- q.addPrefetch("toArtist");
- q.setName("__testPrefetchToOneLocalCache__" + System.currentTimeMillis());
+ final SelectQuery q = new SelectQuery(Painting.class);
+ q.addPrefetch(Painting.TO_ARTIST_PROPERTY);
q.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
context.performQuery(q);
- blockQueries();
- try {
- // per CAY-499 second run of a cached query with prefetches (i.e. when the
- // result is served from cache) used to throw an exception...
+ queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
- List cachedResult = context.performQuery(q);
+ public void execute() {
+ // per CAY-499 second run of a cached query with prefetches (i.e. when the
+ // result is served from cache) used to throw an exception...
- assertFalse(cachedResult.isEmpty());
- DataObject p1 = (DataObject) cachedResult.get(0);
+ List<Painting> cachedResult = context.performQuery(q);
- Object toOnePrefetch = p1.readNestedProperty("toArtist");
- assertNotNull(toOnePrefetch);
- assertTrue(
- "Expected DataObject, got: " + toOnePrefetch.getClass().getName(),
- toOnePrefetch instanceof DataObject);
+ assertFalse(cachedResult.isEmpty());
+ Painting p1 = cachedResult.get(0);
- DataObject a1 = (DataObject) toOnePrefetch;
- assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
+ Object toOnePrefetch = p1.readNestedProperty("toArtist");
+ assertNotNull(toOnePrefetch);
+ assertTrue(
+ "Expected Artist, got: " + toOnePrefetch.getClass().getName(),
+ toOnePrefetch instanceof Artist);
- // and just in case - run one more time...
- context.performQuery(q);
- }
- finally {
- unblockQueries();
- }
+ Artist a1 = (Artist) toOnePrefetch;
+ assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
+
+ // and just in case - run one more time...
+ context.performQuery(q);
+ }
+ });
}
}