db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject svn commit: r431730 - in /db/ojb/trunk/src/test/org/apache/ojb: broker/AllTests.java broker/StoredProcedureMaxDBTest.java broker/StoredProcedureTest.java repository_junit.xml
Date Tue, 15 Aug 2006 23:17:03 GMT
Author: arminw
Date: Tue Aug 15 16:17:03 2006
New Revision: 431730

URL: http://svn.apache.org/viewvc?rev=431730&view=rev
Log:
add test: persistence via stored procedures

Added:
    db/ojb/trunk/src/test/org/apache/ojb/broker/StoredProcedureMaxDBTest.java
    db/ojb/trunk/src/test/org/apache/ojb/broker/StoredProcedureTest.java
Modified:
    db/ojb/trunk/src/test/org/apache/ojb/broker/AllTests.java
    db/ojb/trunk/src/test/org/apache/ojb/repository_junit.xml

Modified: db/ojb/trunk/src/test/org/apache/ojb/broker/AllTests.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/src/test/org/apache/ojb/broker/AllTests.java?rev=431730&r1=431729&r2=431730&view=diff
==============================================================================
--- db/ojb/trunk/src/test/org/apache/ojb/broker/AllTests.java (original)
+++ db/ojb/trunk/src/test/org/apache/ojb/broker/AllTests.java Tue Aug 15 16:17:03 2006
@@ -147,6 +147,7 @@
         suite.addTestSuite(UnwrapHelperTest.class);
         suite.addTestSuite(LOBTest.class);
         suite.addTestSuite(JdbcJavaObjectTest.class);
+        suite.addTestSuite(StoredProcedureMaxDBTest.class);
         suite.addTestSuite(CreationTest.class);
         suite.addTestSuite(IdentityPerformanceTest.class);
 

Added: db/ojb/trunk/src/test/org/apache/ojb/broker/StoredProcedureMaxDBTest.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/src/test/org/apache/ojb/broker/StoredProcedureMaxDBTest.java?rev=431730&view=auto
==============================================================================
--- db/ojb/trunk/src/test/org/apache/ojb/broker/StoredProcedureMaxDBTest.java (added)
+++ db/ojb/trunk/src/test/org/apache/ojb/broker/StoredProcedureMaxDBTest.java Tue Aug 15 16:17:03
2006
@@ -0,0 +1,71 @@
+package org.apache.ojb.broker;
+
+/* Copyright 2002-2006 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.
+ */
+
+import org.apache.ojb.broker.platforms.PlatformMaxDBImpl;
+import org.apache.ojb.broker.platforms.PlatformSapdbImpl;
+
+/**
+ * This class
+ *
+ * @version $Id: $
+ */
+public class StoredProcedureMaxDBTest extends StoredProcedureTest
+{
+    public static void main(String[] args)
+    {
+        String[] arr = {StoredProcedureMaxDBTest.class.getName()};
+        junit.textui.TestRunner.main(arr);
+    }
+
+    public StoredProcedureMaxDBTest(String name)
+    {
+        super(name);
+    }
+
+    protected void initDBDependend() throws Exception
+    {
+        databaseName = "SapDB/MaxDB";
+        String schemaName = broker.serviceConnectionManager().getConnectionDescriptor().getUserName()
+ ".";
+
+        createTable = "CREATE TABLE SP_CUSTOMER(ID_ SERIAL PRIMARY KEY, NAME VARCHAR(150))";
+        deleteTable = "DROP TABLE SP_CUSTOMER CASCADE";
+
+        insertProcedure =
+                "CREATE DBPROC SP_CUSTOMER_INSERT (IN NAME VARCHAR(150), OUT CUSTOMER_ID
FIXED(10)) " +
+                        "AS SET CUSTOMER_ID = 0;" +
+                        "INSERT INTO " + schemaName + "SP_CUSTOMER (NAME) VALUES (:NAME);"
+
+                        "BEGIN " +
+                        "SET CUSTOMER_ID=SP_CUSTOMER.CURRVAL;" +
+                        "END;";
+        updateProcedure = "CREATE DBPROC SP_CUSTOMER_UPDATE" +
+                " (IN ID_ FIXED(10), IN NAME VARCHAR(150)) AS " +
+                "UPDATE " + schemaName + "SP_CUSTOMER SET NAME=:NAME WHERE ID_=:ID_;";
+        deleteProcedure = "CREATE DBPROC SP_CUSTOMER_DELETE" +
+                " (IN ID_ FIXED(10)) AS " +
+                "DELETE FROM " + schemaName + "SP_CUSTOMER WHERE ID_=:ID_;";
+
+        dropInsertProcedure = "DROP DBPROC SP_CUSTOMER_INSERT";
+        dropUpdateProcedure = "DROP DBPROC SP_CUSTOMER_UPDATE";
+        dropDeleteProcedure = "DROP DBPROC SP_CUSTOMER_DELETE";
+    }
+
+    protected boolean isSupportedDatabase()
+    {
+        return broker.serviceConnectionManager().getSupportedPlatform() instanceof PlatformSapdbImpl
+                || broker.serviceConnectionManager().getSupportedPlatform() instanceof PlatformMaxDBImpl;
+    }
+}

Added: db/ojb/trunk/src/test/org/apache/ojb/broker/StoredProcedureTest.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/src/test/org/apache/ojb/broker/StoredProcedureTest.java?rev=431730&view=auto
==============================================================================
--- db/ojb/trunk/src/test/org/apache/ojb/broker/StoredProcedureTest.java (added)
+++ db/ojb/trunk/src/test/org/apache/ojb/broker/StoredProcedureTest.java Tue Aug 15 16:17:03
2006
@@ -0,0 +1,278 @@
+package org.apache.ojb.broker;
+
+/* Copyright 2002-2006 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.
+ */
+
+import java.sql.Connection;
+import java.sql.Statement;
+
+import org.apache.ojb.junit.PBTestCase;
+
+/**
+ * Abstract base class to test OJB insert, update, delete operations
+ * via stored procedures. Extend this class to implement database specific
+ * tests. 
+ *
+ * @version $Id: $
+ */
+abstract public class StoredProcedureTest extends PBTestCase
+{
+    protected String databaseName;
+
+    protected String createTable;
+    protected String deleteTable;
+
+
+    protected String insertProcedure;
+    protected String updateProcedure;
+    protected String deleteProcedure;
+
+
+    protected String dropInsertProcedure;
+    protected String dropUpdateProcedure;
+    protected String dropDeleteProcedure;
+
+    public StoredProcedureTest(String name)
+    {
+        super(name);
+    }
+
+    public void setUp() throws Exception
+    {
+        super.setUp();
+
+        initDBDependend();
+
+        try{executeStatement(dropInsertProcedure);}catch(Exception e){}
+        try{executeStatement(dropDeleteProcedure);}catch(Exception e){}
+        try{executeStatement(dropUpdateProcedure);}catch(Exception e){}
+        try{executeStatement(deleteTable);}catch(Exception e){}
+        executeStatement(createTable);
+        executeStatement(insertProcedure);
+        executeStatement(updateProcedure);
+        executeStatement(deleteProcedure);
+    }
+
+    public void tearDown() throws Exception
+    {
+        super.tearDown();
+    }
+
+    /**
+     * Override this method to implement other databases.
+     * @throws Exception
+     */
+    abstract protected void initDBDependend() throws Exception;
+
+    /**
+     * Override this check for each database dependend implementation.
+     * @return Return <em>true</em> if the
+     */
+    abstract protected boolean isSupportedDatabase();
+
+    protected void executeStatement(String statement) throws Exception
+    {
+        try
+        {
+            broker.beginTransaction();
+            Connection con = broker.serviceConnectionManager().getConnection();
+            Statement stmt = con.createStatement();
+            stmt.execute(statement);
+            stmt.close();
+            broker.commitTransaction();
+        }
+        catch(Exception e)
+        {
+            if(broker != null) broker.abortTransaction();
+            throw e;
+        }
+    }
+
+    public void testInsert()
+    {
+        if(!isSupportedDatabase())
+        {
+            this.ojbSkipTestMessage("Skip " + databaseName + " specific test");
+            return;
+        }
+
+        Customer c1 = createCustomer();
+        c1.setName("testInsert_1");
+        Customer c2 = createCustomer();
+        c2.setName("testInsert_2");
+        broker.beginTransaction();
+        try
+        {
+            broker.store(c1);
+            broker.store(c2);
+            broker.commitTransaction();
+        }
+        catch(PersistenceBrokerException e)
+        {
+            if(broker != null) broker.abortTransaction();
+            throw e;
+        }
+        assertNotNull(c1.getId());
+        assertNotNull(c2.getId());
+//        System.out.println("ID: " + c1.getId());
+//        System.out.println("ID: " + c2.getId());
+    }
+
+    public void testUpdate()
+    {
+        if(!isSupportedDatabase())
+        {
+            this.ojbSkipTestMessage("Skip " + databaseName + " specific test");
+            return;
+        }
+
+        Customer c1 = createCustomer();
+        c1.setName("testUpdate_1");
+        Customer c2 = createCustomer();
+        c2.setName("testUpdate_2");
+        broker.beginTransaction();
+        try
+        {
+            broker.store(c1);
+            broker.store(c2);
+            broker.commitTransaction();
+        }
+        catch(PersistenceBrokerException e)
+        {
+            if(broker != null) broker.abortTransaction();
+            throw e;
+        }
+        assertNotNull(c1.getId());
+        assertNotNull(c2.getId());
+
+        c1.setName("update_1");
+        broker.beginTransaction();
+        try
+        {
+            broker.store(c1);
+            broker.store(c2);
+            broker.commitTransaction();
+        }
+        catch(PersistenceBrokerException e)
+        {
+            if(broker != null) broker.abortTransaction();
+            throw e;
+        }
+
+        broker.clearCache();
+
+        Identity oid_1 = broker.serviceIdentity().buildIdentity(Customer.class, c1.getId());
+        Identity oid_2 = broker.serviceIdentity().buildIdentity(Customer.class, c2.getId());
+
+        Customer r1 = (Customer) broker.getObjectByIdentity(oid_1);
+        Customer r2 = (Customer) broker.getObjectByIdentity(oid_2);
+
+        assertNotNull(r1);
+        assertNotNull(r2);
+        assertEquals("update_1", r1.getName());
+        assertEquals("testUpdate_2", r2.getName());
+    }
+
+    public void testDelete()
+    {
+        if(!isSupportedDatabase())
+        {
+            this.ojbSkipTestMessage("Skip " + databaseName + " specific test");
+            return;
+        }
+
+        Customer c1 = createCustomer();
+        c1.setName("testDelete_1");
+        Customer c2 = createCustomer();
+        c2.setName("testDelete_2");
+        broker.beginTransaction();
+        try
+        {
+            broker.store(c1);
+            broker.store(c2);
+            broker.commitTransaction();
+        }
+        catch(PersistenceBrokerException e)
+        {
+            if(broker != null) broker.abortTransaction();
+            throw e;
+        }
+        assertNotNull(c1.getId());
+        assertNotNull(c2.getId());
+
+        broker.clearCache();
+
+        Identity oid_1 = broker.serviceIdentity().buildIdentity(Customer.class, c1.getId());
+        Identity oid_2 = broker.serviceIdentity().buildIdentity(Customer.class, c2.getId());
+
+        Customer r1 = (Customer) broker.getObjectByIdentity(oid_1);
+        Customer r2 = (Customer) broker.getObjectByIdentity(oid_2);
+
+        assertNotNull(r1);
+        assertNotNull(r2);
+        assertEquals("testDelete_1", r1.getName());
+        assertEquals("testDelete_2", r2.getName());
+
+        broker.beginTransaction();
+        try
+        {
+            broker.delete(c1);
+            broker.delete(c2);
+            broker.commitTransaction();
+        }
+        catch(PersistenceBrokerException e)
+        {
+            if(broker != null) broker.abortTransaction();
+            throw e;
+        }
+
+        r1 = (Customer) broker.getObjectByIdentity(oid_1);
+        r2 = (Customer) broker.getObjectByIdentity(oid_2);
+
+        assertNull(r1);
+        assertNull(r2);
+    }
+
+    protected StoredProcedureTest.Customer createCustomer()
+    {
+        return new StoredProcedureTest.Customer();
+    }
+
+    public static class Customer
+    {
+        private Integer id;
+        private String name;
+
+        public Integer getId()
+        {
+            return id;
+        }
+
+        public void setId(Integer id)
+        {
+            this.id = id;
+        }
+
+        public String getName()
+        {
+            return name;
+        }
+
+        public void setName(String name)
+        {
+            this.name = name;
+        }
+    }
+}

Modified: db/ojb/trunk/src/test/org/apache/ojb/repository_junit.xml
URL: http://svn.apache.org/viewvc/db/ojb/trunk/src/test/org/apache/ojb/repository_junit.xml?rev=431730&r1=431729&r2=431730&view=diff
==============================================================================
--- db/ojb/trunk/src/test/org/apache/ojb/repository_junit.xml (original)
+++ db/ojb/trunk/src/test/org/apache/ojb/repository_junit.xml Tue Aug 15 16:17:03 2006
@@ -3563,4 +3563,38 @@
              jdbc-type="JAVA_OBJECT"
         />
     </class-descriptor>
+
+
+    <class-descriptor
+        class="org.apache.ojb.broker.StoredProcedureTest$Customer"
+        table="SP_CUSTOMER"
+    >
+        <field-descriptor
+             name="id"
+             column="ID_"
+             jdbc-type="INTEGER"
+             primarykey="true"
+             autoincrement="false"
+        />
+
+        <field-descriptor
+             name="name"
+             column="NAME"
+             jdbc-type="VARCHAR"
+        />
+
+        <insert-procedure name="SP_CUSTOMER_INSERT">
+            <runtime-argument field-ref="name"/>
+            <runtime-argument field-ref="id" return="true"/>
+        </insert-procedure>
+
+        <update-procedure name="SP_CUSTOMER_UPDATE">
+            <runtime-argument field-ref="id"/>
+            <runtime-argument field-ref="name"/>
+        </update-procedure>
+
+        <delete-procedure name="SP_CUSTOMER_DELETE">
+            <runtime-argument field-ref="id"/>
+        </delete-procedure>
+    </class-descriptor>
 <!-- Mapping of classes used in junit tests and tutorials ends here -->



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message