Author: clr
Date: Wed Mar 8 15:45:25 2006
New Revision: 384378
URL: http://svn.apache.org/viewcvs?rev=384378&view=rev
Log:
JDO-191 Added tests for DataStoreConnection
Added:
db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/api/persistencemanager/DataStoreConnection.java
db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/api/persistencemanager/DataStoreConnectionThrows.java
Modified:
db/jdo/trunk/tck20/src/conf/pm.conf
db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/JDO_Test.java
Modified: db/jdo/trunk/tck20/src/conf/pm.conf
URL:
http://svn.apache.org/viewcvs/db/jdo/trunk/tck20/src/conf/pm.conf?rev=384378&r1=384377&r2=384378&view=diff
==============================================================================
--- db/jdo/trunk/tck20/src/conf/pm.conf (original)
+++ db/jdo/trunk/tck20/src/conf/pm.conf Wed Mar 8 15:45:25 2006
@@ -9,6 +9,8 @@
org.apache.jdo.tck.api.persistencemanager.ConcurrentPersistenceManagersSameClasses
\
org.apache.jdo.tck.api.persistencemanager.CurrentTransaction \
org.apache.jdo.tck.api.persistencemanager.DataStoreCacheTest \
+org.apache.jdo.tck.api.persistencemanager.DataStoreConnection \
+org.apache.jdo.tck.api.persistencemanager.DataStoreConnectionThrows \
org.apache.jdo.tck.api.persistencemanager.DeletePersistent \
org.apache.jdo.tck.api.persistencemanager.DeletePersistentAllFails \
org.apache.jdo.tck.api.persistencemanager.DeletePersistentFailsIfInstanceIsTransient
\
Modified: db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/JDO_Test.java
URL:
http://svn.apache.org/viewcvs/db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/JDO_Test.java?rev=384378&r1=384377&r2=384378&view=diff
==============================================================================
--- db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/JDO_Test.java
(original)
+++ db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/JDO_Test.java Wed
Mar 8 15:45:25 2006
@@ -62,8 +62,10 @@
public static final int PERSISTENT_NEW_DELETED = 7;
public static final int PERSISTENT_DELETED = 8;
public static final int PERSISTENT_NONTRANSACTIONAL = 9;
- public static final int NUM_STATES = 10;
- public static final int ILLEGAL_STATE = 10;
+ public static final int PERSISTENT_NONTRANSACTIONAL_DIRTY = 10;
+ public static final int DETACHED = 11;
+ public static final int NUM_STATES = 12;
+ public static final int ILLEGAL_STATE = 12;
public static final String[] states = {
"transient",
@@ -76,6 +78,8 @@
"persistent-new-deleted",
"persistent-deleted",
"persistent-nontransactional",
+ "persistent-nontransactional-dirty",
+ "detached",
"illegal"
};
private static final int IS_PERSISTENT = 0;
@@ -83,7 +87,8 @@
private static final int IS_DIRTY = 2;
private static final int IS_NEW = 3;
private static final int IS_DELETED = 4;
- private static final int NUM_STATUSES = 5;
+ private static final int IS_DETACHED = 5;
+ private static final int NUM_STATUSES = 6;
/*
* This table indicates the values returned by the status
interrogation
@@ -91,36 +96,42 @@
* state of an object.
*/
private static final boolean state_statuses[][] = {
- // IS_PERSISTENT IS_TRANSACTIONAL IS_DIRTY
IS_NEW IS_DELETED
+ // IS_PERSISTENT IS_TRANSACTIONAL IS_DIRTY
IS_NEW IS_DELETED IS_DETACHED
// transient
- { false, false, false,
false, false},
+ { false, false, false,
false, false, false},
// persistent-new
- { true, true, true,
true, false},
+ { true, true, true,
true, false, false},
// persistent-clean
- { true, true, false,
false, false},
+ { true, true, false,
false, false, false},
// persistent-dirty
- { true, true, true,
false, false},
+ { true, true, true,
false, false, false},
// hollow
- { true, false, false,
false, false},
+ { true, false, false,
false, false, false},
// transient-clean
- { false, true, false,
false, false},
+ { false, true, false,
false, false, false},
// transient-dirty
- { false, true, true,
false, false},
+ { false, true, true,
false, false, false},
// persistent-new-deleted
- { true, true, true,
true, true},
+ { true, true, true,
true, true, false},
// persistent-deleted
- { true, true, true,
false, true},
+ { true, true, true,
false, true, false},
// persistent-nontransactional
- { true, false, false,
false, false}
+ { true, false, false,
false, false, false},
+
+ // persistent-nontransactional-dirty
+ { true, true, false,
false, false, false},
+
+ // detached
+ { false, false, false,
false, false, true}
};
/** identitytype value for applicationidentity. */
@@ -706,6 +717,30 @@
"javax.jdo.query.SQL");
}
+ /** Reports whether getting the DataStoreConnection is
supported. */
+ public boolean isDataStoreConnectionSupported() {
+ return getPMF().supportedOptions().contains(
+ "javax.jdo.option.GetDataStoreConnection");
+ }
+ + /**
+ * Determine if a class is loadable in the current environment.
+ */
+ public static boolean isClassLoadable(String className) {
+ try {
+ Class.forName(className);
+ return true;
+ } catch (ClassNotFoundException ex) {
+ return false;
+ }
+ }
+
+ /** + * Determine if the environment is 1.4 version of JRE
or better.
+ */
+ public static boolean isJRE14orBetter() {
+ return isClassLoadable("java.util.Currency");
+ }
/**
* This utility method returns a <code>String</code> that
indicates the
@@ -741,6 +776,10 @@
if( existingEntries ) buff.append(", ");
buff.append("deleted");
}
+ if( JDOHelper.isDetached(o) ){
+ if( existingEntries ) buff.append(", ");
+ buff.append("detached");
+ }
buff.append("}");
return buff.toString();
}
@@ -750,12 +789,13 @@
*/
public static int currentState(Object o)
{
- boolean[] status = new boolean[5];
+ boolean[] status = new boolean[NUM_STATUSES];
status[IS_PERSISTENT] = JDOHelper.isPersistent(o);
status[IS_TRANSACTIONAL] = JDOHelper.isTransactional(o);
status[IS_DIRTY] = JDOHelper.isDirty(o);
status[IS_NEW] = JDOHelper.isNew(o);
status[IS_DELETED] = JDOHelper.isDeleted(o);
+ status[IS_DETACHED] = JDOHelper.isDetached(o);
int i, j;
outerloop:
for( i = 0; i < NUM_STATES; ++i ){
Added:
db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/api/persistencemanager/DataStoreConnection.java
URL:
http://svn.apache.org/viewcvs/db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/api/persistencemanager/DataStoreConnection.java?rev=384378&view=auto
==============================================================================
---
db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/api/persistencemanager/DataStoreConnection.java
(added)
+++
db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/api/persistencemanager/DataStoreConnection.java
Wed Mar 8 15:45:25 2006
@@ -0,0 +1,130 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * + * Licensed 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.jdo.tck.api.persistencemanager;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+
+import java.util.Collection;
+import java.util.HashSet;
+
+import javax.jdo.datastore.JDOConnection;
+
+import org.apache.jdo.tck.pc.mylib.PCPoint;
+
+import org.apache.jdo.tck.util.BatchTestRunner;
+
+/**
+ *<B>Title:</B> DataStoreConnection
+ *<BR>
+ *<B>Keywords:</B>
+ *<BR>
+ *<B>Assertion ID:</B> A12.5.2-1
+ *<BR>
+ *<B>Assertion Description: </B>
+In order for the application to perform some +datastore-specific
functions, such as to execute +a query that is not directly supported
by JDO, +applications might need access to the +datastore connection
used by the JDO implementation. +This method returns a wrapped
+connection that can be cast to the appropriate +datastore connection
and used by the application. +The capability to get the datastore
connection is +indicated by the optional feature string
+javax.jdo.option.GetDataStoreConnection. +
+ */
+
+public class DataStoreConnection extends PersistenceManagerTest {
+
+ /** */
+ private static final String ASSERTION_FAILED = +
"Assertion A12.5.2-1 (DataStoreConnection) failed: ";
+ + protected PCPoint goldenPoint;
+ + /**
+ * The <code>main</code> is called when the class
+ * is directly executed from the command line.
+ * @param args The arguments passed to the program.
+ */
+ public static void main(String[] args) {
+ BatchTestRunner.run(DataStoreConnection.class);
+ }
+ + /** */
+ protected void localSetUp() {
+ addTearDownClass(PCPoint.class);
+ PCPoint point = new PCPoint(50, 100);
+ goldenPoint = new PCPoint(point.getX(), point.getY());
+ getPM().currentTransaction().begin();
+ pm.makePersistent(point);
+ pm.currentTransaction().commit();
+ }
+
+ /** */
+ public void testDataStoreConnection() {
+ if (!(isDataStoreConnectionSupported() && isSQLSupported())) {
+ printUnsupportedOptionalFeatureNotTested(
+ this.getClass().getName(),
+ "getDataStoreConnection AND SQLSupported.");
+ return;
+ }
+ String schema = getPMFProperty("javax.jdo.mapping.Schema");
+ String sql = "SELECT X, Y FROM " + schema + ".PCPoint";
+ JDOConnection jconn = pm.getDataStoreConnection();
+ try {
+ getPM().currentTransaction().begin();
+ jconn = pm.getDataStoreConnection();
+ Connection conn = (Connection)jconn.getNativeConnection();
+ if (conn.getAutoCommit()) {
+ appendMessage(ASSERTION_FAILED +
+ "Autocommit must not be true in native
connection.");
+ };
+ PreparedStatement ps = conn.prepareStatement(sql);
+ ResultSet rs = ps.executeQuery();
+ Collection actuals = new HashSet();
+ while (rs.next()) {
+ PCPoint p = new PCPoint(rs.getInt(1), rs.getInt(2));
+ actuals.add(p);
+ }
+ if (actuals.size() != 1) {
+ appendMessage(ASSERTION_FAILED + "Wrong size of
result of " +
+ sql + NL + "expected: 1, actual: " +
actuals.size());
+ } else {
+ PCPoint actual = (PCPoint)actuals.iterator().next();
+ if (goldenPoint.getX() != actual.getX() ||
+ !goldenPoint.getY().equals(actual.getY())) {
+ appendMessage(ASSERTION_FAILED +
+ "Wrong values of PCPoint from SQL" +
+ "expected x: " + goldenPoint.getX() +
+ ", y: " + goldenPoint.getX() + NL +
+ "actual x: " + actual.getX() +
+ ", y: " + actual.getX()
+ );
+ }
+ }
+ } catch (Exception ex) {
+ appendMessage(ASSERTION_FAILED + " caught exception:" +
ex);
+ } finally {
+ jconn.close();
+ failOnError();
+ }
+ }
+}
Added:
db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/api/persistencemanager/DataStoreConnectionThrows.java
URL:
http://svn.apache.org/viewcvs/db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/api/persistencemanager/DataStoreConnectionThrows.java?rev=384378&view=auto
==============================================================================
---
db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/api/persistencemanager/DataStoreConnectionThrows.java
(added)
+++
db/jdo/trunk/tck20/src/java/org/apache/jdo/tck/api/persistencemanager/DataStoreConnectionThrows.java
Wed Mar 8 15:45:25 2006
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * + * Licensed 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.jdo.tck.api.persistencemanager;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+
+import javax.jdo.datastore.JDOConnection;
+
+import org.apache.jdo.tck.pc.mylib.PCPoint;
+
+import org.apache.jdo.tck.util.BatchTestRunner;
+
+/**
+ *<B>Title:</B> DataStoreConnectionThrows
+ *<BR>
+ *<B>Keywords:</B>
+ *<BR>
+ *<B>Assertion ID:</B> A12.16-2
+ *<BR>
+ *<B>Assertion Description: </B>
+For portability, a JDBC-based JDO implementation +will return an
instance that implements +java.sql.Connection. The instance +will
throw an exception for any of the +following method calls: commit,
getMetaData, +releaseSavepoint, rollback, setAutoCommit, +setCatalog,
setHoldability, setReadOnly, +setSavepoint, setTransactionIsolation,
and +setTypeMap.
+ */
+
+public class DataStoreConnectionThrows extends PersistenceManagerTest {
+
+ /** */
+ private static final String ASSERTION_FAILED = +
"Assertion A12.16-2 (DataStoreConnectionThrows) failed: ";
+ + protected PCPoint goldenPoint;
+ + /**
+ * The <code>main</code> is called when the class
+ * is directly executed from the command line.
+ * @param args The arguments passed to the program.
+ */
+ public static void main(String[] args) {
+ BatchTestRunner.run(DataStoreConnectionThrows.class);
+ }
+ + /** */
+ protected void checkThrow(Connection conn, Call call) {
+ try {
+ call.execute(conn);
+ appendMessage(ASSERTION_FAILED +
+ "Failed to throw an exception for " +
call.getName());
+ } catch (SQLException ex) {
+ appendMessage(ASSERTION_FAILED +
+ "Threw a SQLException for " + call.getName() +
NL + ex);
+ return;
+ } catch (Exception ex) {
+ return;
+ }
+ }
+
+ /** */
+ interface Call {
+ String getName();
+ void execute(Connection conn) throws SQLException;
+ }
+
+ /** */
+ public void testDataStoreConnectionThrows() {
+ if (!(isDataStoreConnectionSupported() && isSQLSupported())) {
+ printUnsupportedOptionalFeatureNotTested(
+ this.getClass().getName(),
+ "getDataStoreConnection AND SQLSupported.");
+ return;
+ }
+ JDOConnection jconn = getPM().getDataStoreConnection();
+ Connection conn = (Connection)jconn.getNativeConnection();
+ check13Methods(conn);
+ if (isJRE14orBetter()) {
+ check14Methods(conn);
+ }
+ jconn.close();
+ failOnError();
+ }
+
+ /** + * These methods are defined in Java 1.3 Connection.
+ */
+ protected void check13Methods(Connection conn) {
+ checkThrow(conn,
+ new Call() {
+ public String getName() {return "commit";}
+ public void execute(Connection conn)
+ throws SQLException {conn.commit();}
+ }
+ );
+ checkThrow(conn,
+ new Call() {
+ public String getName() {return "rollback";}
+ public void execute(Connection conn)
+ throws SQLException {conn.rollback();}
+ }
+ );
+ checkThrow(conn,
+ new Call() {
+ public String getName() {return
"setTransactionIsolation";}
+ public void execute(Connection conn)
+ throws SQLException {
+ conn.setTransactionIsolation(
+
Connection.TRANSACTION_READ_COMMITTED);}
+ }
+ );
+ checkThrow(conn,
+ new Call() {
+ public String getName() {return "setAutoCommit";}
+ public void execute(Connection conn)
+ throws SQLException {conn.setAutoCommit(true);}
+ }
+ );
+ checkThrow(conn,
+ new Call() {
+ public String getName() {return "setCatalog";}
+ public void execute(Connection conn)
+ throws SQLException {conn.setCatalog("NONE");}
+ }
+ );
+ }
+
+ /**
+ * These methods are defined in Java 1.4 Connection.
+ */
+ protected void check14Methods(Connection conn) {
+ checkThrow(conn,
+ new Call() {
+ public String getName() {return "setSavepoint";}
+ public void execute(Connection conn)
+ throws SQLException {conn.setSavepoint();}
+ }
+ );
+ checkThrow(conn,
+ new Call() {
+ public String getName() {return
"releaseSavepoint";}
+ public void execute(Connection conn)
+ throws SQLException
{conn.releaseSavepoint(null);}
+ }
+ );
+ checkThrow(conn,
+ new Call() {
+ public String getName() {return "setHoldability";}
+ public void execute(Connection conn)
+ throws SQLException {
+ conn.setHoldability(
+ ResultSet.CLOSE_CURSORS_AT_COMMIT);}
+ }
+ );
+ checkThrow(conn,
+ new Call() {
+ public String getName() {return "setTypeMap";}
+ public void execute(Connection conn)
+ throws SQLException {conn.setTypeMap(new
HashMap());}
+ }
+ );
+ }
+}