cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r430616 - in /incubator/cayenne/main/trunk/integration-test/itest-common: ./ src/main/java/org/apache/cayenne/jpa/itest/
Date Fri, 11 Aug 2006 01:43:56 GMT
Author: aadamchik
Date: Thu Aug 10 18:43:56 2006
New Revision: 430616

URL: http://svn.apache.org/viewvc?rev=430616&view=rev
Log:
created a portable integration test setup with one common module and dependent individual
test modules

Added:
    incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDBUtils.java
      - copied, changed from r430559, incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/UnitTestDBUtils.java
    incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDataSourceManager.java
    incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestJpaDataSourceFactory.java
      - copied, changed from r430559, incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/Unit1DataSourceFactory.java
    incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestSetup.java
      - copied, changed from r430559, incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/Unit1TestSetup.java
Removed:
    incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/Unit1DataSource.java
    incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/Unit1DataSourceFactory.java
    incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/Unit1TestSetup.java
    incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/UnitTestDBUtils.java
Modified:
    incubator/cayenne/main/trunk/integration-test/itest-common/   (props changed)
    incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ResultSetTemplate.java

Propchange: incubator/cayenne/main/trunk/integration-test/itest-common/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Aug 10 18:43:56 2006
@@ -0,0 +1,2 @@
+
+target

Copied: incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDBUtils.java
(from r430559, incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/UnitTestDBUtils.java)
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDBUtils.java?p2=incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDBUtils.java&p1=incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/UnitTestDBUtils.java&r1=430559&r2=430616&rev=430616&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/UnitTestDBUtils.java
(original)
+++ incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDBUtils.java
Thu Aug 10 18:43:56 2006
@@ -23,7 +23,7 @@
 import java.sql.SQLException;
 import java.sql.Statement;
 
-public class UnitTestDBUtils {
+public class ItestDBUtils {
 
     public static int deleteAll(String table) throws SQLException {
         String sql = "delete from " + table;
@@ -77,6 +77,6 @@
     }
 
     static Connection getConnection() throws SQLException {
-        return Unit1TestSetup.getInstance().getDataSource().getConnection();
+        return ItestSetup.getInstance().getDataSource().getConnection();
     }
 }

Added: incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDataSourceManager.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDataSourceManager.java?rev=430616&view=auto
==============================================================================
--- incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDataSourceManager.java
(added)
+++ incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDataSourceManager.java
Thu Aug 10 18:43:56 2006
@@ -0,0 +1,140 @@
+/*****************************************************************
+ *   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.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import javax.sql.DataSource;
+
+import org.apache.cayenne.conn.PoolManager;
+
+/**
+ * A helper class that manages test DataSource.
+ * 
+ * @author Andrus Adamchik
+ */
+class ItestDataSourceManager {
+
+    private DataSource dataSource;
+    private String schemaScriptUrl;
+    private String jpaUnit;
+
+    ItestDataSourceManager(String schemaScriptUrl, String jpaUnit) {
+        this.schemaScriptUrl = schemaScriptUrl;
+        this.jpaUnit = jpaUnit;
+    }
+
+    public DataSource getDataSource() {
+        if (this.dataSource == null) {
+            this.dataSource = createDataSource();
+        }
+
+        return dataSource;
+    }
+
+    String getJpaUnit() {
+        return jpaUnit;
+    }
+
+    String getSchemaScriptUrl() {
+        return schemaScriptUrl;
+    }
+
+    /**
+     * Creates DataSource and loads local schema.
+     */
+    private DataSource createDataSource() {
+        DataSource dataSource;
+        try {
+            dataSource = new PoolManager("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:"
+                    + jpaUnit, 1, 2, "sa", null);
+        }
+        catch (SQLException e) {
+            throw new RuntimeException("Error creating DataSource", e);
+        }
+
+        Connection c = null;
+
+        try {
+            c = dataSource.getConnection();
+            loadSchema(c, schemaScriptUrl);
+        }
+        catch (SQLException e) {
+            throw new RuntimeException("Error loading schema", e);
+        }
+        finally {
+            if (c != null) {
+                try {
+                    c.close();
+                }
+                catch (SQLException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+
+        return dataSource;
+    }
+
+    private void loadSchema(Connection c, String schemaFile) throws SQLException {
+        InputStream in = Thread
+                .currentThread()
+                .getContextClassLoader()
+                .getResourceAsStream(schemaFile);
+        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+
+        try {
+
+            String line;
+            while ((line = reader.readLine()) != null) {
+
+                if (line.trim().length() == 0 || line.startsWith("#")) {
+                    continue;
+                }
+
+                Statement st = c.createStatement();
+
+                try {
+                    st.executeUpdate(line.trim());
+                }
+                finally {
+                    st.close();
+                }
+            }
+        }
+        catch (IOException ex) {
+            throw new SQLException("Error reading SQL input: " + ex);
+        }
+        finally {
+            try {
+                reader.close();
+            }
+            catch (IOException e) {
+                // ignore
+            }
+        }
+    }
+
+}

Copied: incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestJpaDataSourceFactory.java
(from r430559, incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/Unit1DataSourceFactory.java)
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestJpaDataSourceFactory.java?p2=incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestJpaDataSourceFactory.java&p1=incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/Unit1DataSourceFactory.java&r1=430559&r2=430616&rev=430616&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/Unit1DataSourceFactory.java
(original)
+++ incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestJpaDataSourceFactory.java
Thu Aug 10 18:43:56 2006
@@ -23,13 +23,18 @@
 
 import org.apache.cayenne.jpa.spi.JpaDataSourceFactory;
 
-public class Unit1DataSourceFactory implements JpaDataSourceFactory {
+/**
+ * A factory that integrates the test environment with Cayenne JPA provider.
+ * 
+ * @author Andrus Adamchik
+ */
+public class ItestJpaDataSourceFactory implements JpaDataSourceFactory {
 
     public DataSource getJtaDataSource(String name, PersistenceUnitInfo info) {
-        return Unit1TestSetup.getInstance().getDataSource();
+        return ItestSetup.getInstance().getDataSource();
     }
 
     public DataSource getNonJtaDataSource(String name, PersistenceUnitInfo info) {
-        return Unit1TestSetup.getInstance().getDataSource();
+        return ItestSetup.getInstance().getDataSource();
     }
 }

Copied: incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestSetup.java
(from r430559, incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/Unit1TestSetup.java)
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestSetup.java?p2=incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestSetup.java&p1=incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/Unit1TestSetup.java&r1=430559&r2=430616&rev=430616&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/Unit1TestSetup.java
(original)
+++ incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestSetup.java
Thu Aug 10 18:43:56 2006
@@ -20,59 +20,67 @@
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Properties;
 
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
-import javax.persistence.spi.PersistenceProvider;
 import javax.sql.DataSource;
 
-import org.apache.cayenne.jpa.cspi.CjpaPersistenceProvider;
-import org.apache.cayenne.jpa.spi.JpaPersistenceProvider;
+import junit.framework.Assert;
 
-public class Unit1TestSetup {
+public class ItestSetup {
 
-	public static final String UNIT_NAME = "ItestUnit1";
+    public static final String SCHEMA_SCRIPT_URL = "itest.schema.script";
+    public static final String JPA_UNIT_NAME = "itest.jpa.unit";
 
-	private static final Unit1TestSetup sharedInstance = new Unit1TestSetup();
+    private static ItestSetup sharedInstance;
 
-	protected EntityManagerFactory sharedFactory;
+    protected EntityManagerFactory sharedFactory;
+    protected ItestDataSourceManager dataSourceManager;
 
-	protected DataSource dataSource;
+    public static void initInstance(Properties properties) {
+        String schemaScript = properties.getProperty(SCHEMA_SCRIPT_URL);
+        Assert.assertNotNull("Null schema script", schemaScript);
 
-	public static Unit1TestSetup getInstance() {
-		return sharedInstance;
-	}
+        String jpaUnit = properties.getProperty(JPA_UNIT_NAME);
+        Assert.assertNotNull("Null jpaUnit", jpaUnit);
 
-	public DataSource getDataSource() {
-		if (dataSource == null) {
-			dataSource = new Unit1DataSource();
-		}
+        sharedInstance = new ItestSetup(schemaScript, jpaUnit);
+    }
 
-		return dataSource;
-	}
+    public static ItestSetup getInstance() {
+        Assert.assertNotNull(
+                "Null shared instance, call 'initInstance' first",
+                sharedInstance);
 
-	public EntityManager createEntityManager() {
-		if (sharedFactory == null) {
-			sharedFactory = createEntityManagerFactory();
-		}
+        return sharedInstance;
+    }
 
-		return sharedFactory.createEntityManager();
-	}
+    protected ItestSetup(String schemaScriptUrl, String jpaUnit) {
+        this.dataSourceManager = new ItestDataSourceManager(schemaScriptUrl, jpaUnit);
+    }
 
-	public EntityManagerFactory createEntityManagerFactory() {
-		Map properties = new HashMap();
-		properties.put("org.apache.cayenne.jpa.jpaDataSourceFactory",
-				Unit1DataSourceFactory.class.getName());
+    public DataSource getDataSource() {
+        return dataSourceManager.getDataSource();
+    }
 
-		System.out.println("CL: " + getClass().getClassLoader());
-		System.out.println("Persistence CL: "
-				+ Persistence.class.getClassLoader());
-		System.out.println("Thread CL: " + Thread.currentThread().getContextClassLoader());
-		System.out.println("PP CL: " + PersistenceProvider.class.getClassLoader());
-		System.out.println("JPP CL: " + JpaPersistenceProvider.class.getClassLoader());
-		System.out.println("CPP CL: " + CjpaPersistenceProvider.class.getClassLoader());
+    public EntityManager createEntityManager() {
+        if (sharedFactory == null) {
+            sharedFactory = createEntityManagerFactory();
+        }
 
-		return Persistence.createEntityManagerFactory(UNIT_NAME, properties);
-	}
+        return sharedFactory.createEntityManager();
+    }
+
+    public EntityManagerFactory createEntityManagerFactory() {
+        Map properties = new HashMap();
+        properties.put(
+                "org.apache.cayenne.jpa.jpaDataSourceFactory",
+                ItestJpaDataSourceFactory.class.getName());
+
+        return Persistence.createEntityManagerFactory(
+                dataSourceManager.getJpaUnit(),
+                properties);
+    }
 }

Modified: incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ResultSetTemplate.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ResultSetTemplate.java?rev=430616&r1=430615&r2=430616&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ResultSetTemplate.java
(original)
+++ incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ResultSetTemplate.java
Thu Aug 10 18:43:56 2006
@@ -28,7 +28,7 @@
     abstract void readResultSet(ResultSet rs, String sql) throws SQLException;
 
     void execute(String sql) throws SQLException {
-        Connection c = UnitTestDBUtils.getConnection();
+        Connection c = ItestDBUtils.getConnection();
         try {
 
             PreparedStatement st = c.prepareStatement(sql);



Mime
View raw message