cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r430228 - in /incubator/cayenne/sandbox/itest-unit1/src: main/java/org/apache/cayenne/jpa/itest1/entity/ test/java/org/apache/cayenne/jpa/itest/ test/java/org/apache/cayenne/jpa/itest/entity/ test/resources/
Date Thu, 10 Aug 2006 03:23:15 GMT
Author: aadamchik
Date: Wed Aug  9 20:23:15 2006
New Revision: 430228

URL: http://svn.apache.org/viewvc?rev=430228&view=rev
Log:
more integration tests; some primitive SQL testing utilities that use JDBC, bypassing Cayenne

Added:
    incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/TransientPersistenceEntity.java
    incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/ResultSetTemplate.java
    incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/RowTemplate.java
    incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1TestSetup.java
      - copied, changed from r430159, incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1TestHelper.java
    incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/UnitTestDBUtils.java
Removed:
    incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1TestHelper.java
Modified:
    incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/FieldPersistenceEntity.java
    incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/PropertyPersistenceEntity.java
    incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1DataSourceFactory.java
    incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/entity/EntityClassTest.java
    incubator/cayenne/sandbox/itest-unit1/src/test/resources/schema-hsqldb.sql

Modified: incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/FieldPersistenceEntity.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/FieldPersistenceEntity.java?rev=430228&r1=430227&r2=430228&view=diff
==============================================================================
--- incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/FieldPersistenceEntity.java
(original)
+++ incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/FieldPersistenceEntity.java
Wed Aug  9 20:23:15 2006
@@ -25,11 +25,13 @@
 
 @Entity
 public class FieldPersistenceEntity {
+    
+    public static final String INITIAL_VALUE = "Init Value";
 
     @Id
     protected int id;
 
-    protected int property1;
+    protected String property1 = INITIAL_VALUE;
 
     public int getProperty1() {
         throw new AssertionFailedError(

Modified: incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/PropertyPersistenceEntity.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/PropertyPersistenceEntity.java?rev=430228&r1=430227&r2=430228&view=diff
==============================================================================
--- incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/PropertyPersistenceEntity.java
(original)
+++ incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/PropertyPersistenceEntity.java
Wed Aug  9 20:23:15 2006
@@ -26,7 +26,7 @@
 
     protected int idx;
 
-    protected int property1x;
+    protected String property1x;
 
     @Id
     public int getId() {
@@ -37,11 +37,11 @@
         this.idx = id;
     }
 
-    public int getProperty1() {
+    public String getProperty1() {
         return property1x;
     }
 
-    public void setProperty1(int property1) {
+    public void setProperty1(String property1) {
         this.property1x = property1;
     }
 }

Added: incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/TransientPersistenceEntity.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/TransientPersistenceEntity.java?rev=430228&view=auto
==============================================================================
--- incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/TransientPersistenceEntity.java
(added)
+++ incubator/cayenne/sandbox/itest-unit1/src/main/java/org/apache/cayenne/jpa/itest1/entity/TransientPersistenceEntity.java
Wed Aug  9 20:23:15 2006
@@ -0,0 +1,26 @@
+/*****************************************************************
+ *   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.jpa.itest1.entity;
+
+import javax.persistence.Entity;
+
+@Entity
+public class TransientPersistenceEntity {
+
+}

Added: incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/ResultSetTemplate.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/ResultSetTemplate.java?rev=430228&view=auto
==============================================================================
--- incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/ResultSetTemplate.java
(added)
+++ incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/ResultSetTemplate.java
Wed Aug  9 20:23:15 2006
@@ -0,0 +1,54 @@
+/*****************************************************************
+ *   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.jpa.itest;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+abstract class ResultSetTemplate {
+
+    abstract void readResultSet(ResultSet rs, String sql) throws SQLException;
+
+    void execute(String sql) throws SQLException {
+        Connection c = UnitTestDBUtils.getConnection();
+        try {
+
+            PreparedStatement st = c.prepareStatement(sql);
+
+            try {
+                ResultSet rs = st.executeQuery();
+                try {
+
+                    readResultSet(rs, sql);
+                }
+                finally {
+                    rs.close();
+                }
+            }
+            finally {
+                st.close();
+            }
+        }
+        finally {
+            c.close();
+        }
+    }
+}
\ No newline at end of file

Added: incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/RowTemplate.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/RowTemplate.java?rev=430228&view=auto
==============================================================================
--- incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/RowTemplate.java
(added)
+++ incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/RowTemplate.java
Wed Aug  9 20:23:15 2006
@@ -0,0 +1,42 @@
+/*****************************************************************
+ *   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.jpa.itest;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+abstract class RowTemplate extends ResultSetTemplate {
+
+    abstract void readRow(ResultSet rs, String sql) throws SQLException;
+
+    @Override
+    void readResultSet(ResultSet rs, String sql) throws SQLException {
+        if (rs.next()) {
+
+            readRow(rs, sql);
+
+            if (rs.next()) {
+                throw new SQLException("More than one result for sql: " + sql);
+            }
+        }
+        else {
+            throw new SQLException("No results for sql: " + sql);
+        }
+    }
+}
\ No newline at end of file

Modified: incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1DataSourceFactory.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1DataSourceFactory.java?rev=430228&r1=430227&r2=430228&view=diff
==============================================================================
--- incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1DataSourceFactory.java
(original)
+++ incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1DataSourceFactory.java
Wed Aug  9 20:23:15 2006
@@ -26,10 +26,10 @@
 public class Unit1DataSourceFactory implements JpaDataSourceFactory {
 
     public DataSource getJtaDataSource(String name, PersistenceUnitInfo info) {
-        return new Unit1DataSource();
+        return Unit1TestSetup.getInstance().getDataSource();
     }
 
     public DataSource getNonJtaDataSource(String name, PersistenceUnitInfo info) {
-        return new Unit1DataSource();
+        return Unit1TestSetup.getInstance().getDataSource();
     }
 }

Copied: incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1TestSetup.java
(from r430159, incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1TestHelper.java)
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1TestSetup.java?p2=incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1TestSetup.java&p1=incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1TestHelper.java&r1=430159&r2=430228&rev=430228&view=diff
==============================================================================
--- incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1TestHelper.java
(original)
+++ incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/Unit1TestSetup.java
Wed Aug  9 20:23:15 2006
@@ -24,20 +24,22 @@
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
+import javax.sql.DataSource;
 
-public class Unit1TestHelper {
+public class Unit1TestSetup {
 
     public static final String UNIT_NAME = "ItestUnit1";
 
-    private static final Unit1TestHelper sharedInstance = new Unit1TestHelper();
+    private static final Unit1TestSetup sharedInstance = new Unit1TestSetup();
 
     protected EntityManagerFactory sharedFactory;
+    protected DataSource dataSource;
 
-    public static Unit1TestHelper getInstance() {
+    public static Unit1TestSetup getInstance() {
         return sharedInstance;
     }
 
-    public Unit1TestHelper() {
+    public Unit1TestSetup() {
 
         // due to Maven class loader weirdness, enhancer
         try {
@@ -54,6 +56,14 @@
         catch (ClassNotFoundException e) {
             e.printStackTrace();
         }
+    }
+
+    public DataSource getDataSource() {
+        if (dataSource == null) {
+            dataSource = new Unit1DataSource();
+        }
+
+        return dataSource;
     }
 
     public EntityManager createEntityManager() {

Added: incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/UnitTestDBUtils.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/UnitTestDBUtils.java?rev=430228&view=auto
==============================================================================
--- incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/UnitTestDBUtils.java
(added)
+++ incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/UnitTestDBUtils.java
Wed Aug  9 20:23:15 2006
@@ -0,0 +1,82 @@
+/*****************************************************************
+ *   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.jpa.itest;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+public class UnitTestDBUtils {
+
+    public static int deleteAll(String table) throws SQLException {
+        String sql = "delete from " + table;
+
+        Connection c = getConnection();
+        try {
+
+            Statement st = c.createStatement();
+            int count = st.executeUpdate(sql);
+            st.close();
+            c.commit();
+
+            return count;
+        }
+        finally {
+            c.close();
+        }
+    }
+
+    public static Object getSingleValue(String table, String column) throws SQLException
{
+        final String sql = "select " + column + " from " + table;
+
+        final Object[] result = new Object[1];
+        RowTemplate template = new RowTemplate() {
+
+            @Override
+            void readRow(ResultSet rs, String sql) throws SQLException {
+                result[0] = rs.getObject(1);
+            }
+        };
+
+        template.execute(sql);
+        return result[0];
+    }
+
+    public static int getSingleInt(String table, String column) throws SQLException {
+        final String sql = "select " + column + " from " + table;
+
+        final int[] result = new int[1];
+
+        RowTemplate template = new RowTemplate() {
+
+            @Override
+            void readRow(ResultSet rs, String sql) throws SQLException {
+                result[0] = rs.getInt(1);
+            }
+        };
+
+        template.execute(sql);
+        return result[0];
+    }
+
+    static Connection getConnection() throws SQLException {
+        return Unit1TestSetup.getInstance().getDataSource().getConnection();
+    }
+}

Modified: incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/entity/EntityClassTest.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/entity/EntityClassTest.java?rev=430228&r1=430227&r2=430228&view=diff
==============================================================================
--- incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/entity/EntityClassTest.java
(original)
+++ incubator/cayenne/sandbox/itest-unit1/src/test/java/org/apache/cayenne/jpa/itest/entity/EntityClassTest.java
Wed Aug  9 20:23:15 2006
@@ -20,35 +20,60 @@
 
 import javax.persistence.EntityManager;
 
+import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
-import org.apache.cayenne.jpa.itest.Unit1TestHelper;
+import org.apache.cayenne.jpa.itest.Unit1TestSetup;
+import org.apache.cayenne.jpa.itest.UnitTestDBUtils;
 import org.apache.cayenne.jpa.itest1.entity.FieldPersistenceEntity;
 import org.apache.cayenne.jpa.itest1.entity.PropertyPersistenceEntity;
 
 public class EntityClassTest extends TestCase {
 
-    public void testFieldBasedPersistence() {
+    public void testFieldBasedPersistence() throws Exception {
+
+        UnitTestDBUtils.deleteAll("FieldPersistenceEntity");
+
+        EntityManager em = Unit1TestSetup.getInstance().createEntityManager();
 
-        EntityManager em = Unit1TestHelper.getInstance().createEntityManager();
-        
         FieldPersistenceEntity o1 = new FieldPersistenceEntity();
         em.persist(o1);
         em.flush();
+
+        assertEquals(FieldPersistenceEntity.INITIAL_VALUE, UnitTestDBUtils
+                .getSingleValue("FieldPersistenceEntity", "property1"));
     }
-    
+
     public void testPropertyBasedPersistence() {
 
-        EntityManager em = Unit1TestHelper.getInstance().createEntityManager();
-        
+        EntityManager em = Unit1TestSetup.getInstance().createEntityManager();
+
         PropertyPersistenceEntity o1 = new PropertyPersistenceEntity();
         em.persist(o1);
         em.flush();
     }
-    
+
     public static void main(String[] args) {
-        new EntityClassTest().testFieldBasedPersistence();
-        new EntityClassTest().testPropertyBasedPersistence();
+
+        try {
+            new EntityClassTest().testFieldBasedPersistence();
+        }
+        catch (AssertionFailedError e) {
+            System.out.print("FAILURE: " + e.getMessage());
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        try {
+            new EntityClassTest().testPropertyBasedPersistence();
+        }
+        catch (AssertionFailedError e) {
+            System.out.print("FAILURE: " + e.getMessage());
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+        }
     }
 
 }

Modified: incubator/cayenne/sandbox/itest-unit1/src/test/resources/schema-hsqldb.sql
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/itest-unit1/src/test/resources/schema-hsqldb.sql?rev=430228&r1=430227&r2=430228&view=diff
==============================================================================
--- incubator/cayenne/sandbox/itest-unit1/src/test/resources/schema-hsqldb.sql (original)
+++ incubator/cayenne/sandbox/itest-unit1/src/test/resources/schema-hsqldb.sql Wed Aug  9
20:23:15 2006
@@ -2,5 +2,5 @@
 insert into AUTO_PK_SUPPORT (TABLE_NAME, NEXT_ID) VALUES ('FieldPersistenceEntity', 1);
 insert into AUTO_PK_SUPPORT (TABLE_NAME, NEXT_ID) VALUES ('PropertyPersistenceEntity', 1);
 
-create table FieldPersistenceEntity (id int not null, property1 int, primary key(id));
-create table PropertyPersistenceEntity (id int not null, property1 int, primary key(id));
\ No newline at end of file
+create table FieldPersistenceEntity (id int not null, property1 VARCHAR(100), primary key(id));
+create table PropertyPersistenceEntity (id int not null, property1 VARCHAR(100), primary
key(id));
\ No newline at end of file



Mime
View raw message