cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1126269 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit: ./ di/client/ di/server/
Date Sun, 22 May 2011 22:52:36 GMT
Author: aadamchik
Date: Sun May 22 22:52:35 2011
New Revision: 1126269

URL: http://svn.apache.org/viewvc?rev=1126269&view=rev
Log:
unit test refactoring

attempt at injectable CayenneResources

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesProvider.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceInfoProvider.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/CayenneResources.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/client/ClientCase.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesAccessStackAdapterProvider.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesDbAdapterProvider.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/di/server/ServerCaseDataSourceFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseModule.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseSharedDataSourceProvider.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerRuntimeProvider.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/CayenneResources.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/CayenneResources.java?rev=1126269&r1=1126268&r2=1126269&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/CayenneResources.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/CayenneResources.java
Sun May 22 22:52:35 2011
@@ -19,7 +19,6 @@
 
 package org.apache.cayenne.unit;
 
-import java.io.InputStream;
 import java.sql.SQLException;
 import java.util.Map;
 
@@ -34,80 +33,20 @@ import org.apache.cayenne.conn.PoolManag
 import org.apache.cayenne.dba.JdbcAdapter;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.BeanFactory;
-import org.springframework.beans.factory.BeanFactoryAware;
-import org.springframework.beans.factory.xml.XmlBeanFactory;
-import org.springframework.core.io.InputStreamResource;
 
 /**
  * Initializes connections for Cayenne unit tests.
  */
-public class CayenneResources implements BeanFactoryAware {
+public class CayenneResources {
 
     private static Log logger = LogFactory.getLog(CayenneResources.class);
 
-    public static final String TEST_RESOURCES_DESCRIPTOR = "spring-test-resources.xml";
-
-    public static final String CONNECTION_NAME_KEY = "cayenneTestConnection";
-    public static final String SKIP_SCHEMA_KEY = "cayenne.test.schema.skip";
-
-    public static final String SCHEMA_SETUP_STACK = "SchemaSetupStack";
     public static final String SQL_TEMPLATE_CUSTOMIZER = "SQLTemplateCustomizer";
-    public static final String DEFAULT_CONNECTION_KEY = "internal_embedded_datasource";
-
-    private static CayenneResources resources;
-
-    private static CayenneResources loadResources() {
-
-        InputStream in = Thread
-                .currentThread()
-                .getContextClassLoader()
-                .getResourceAsStream(TEST_RESOURCES_DESCRIPTOR);
-
-        if (in == null) {
-            logger.error("Can't locate resource: " + TEST_RESOURCES_DESCRIPTOR);
-            throw new RuntimeException(
-                    "Can't locate resource descriptor in the ClassLoader: "
-                            + TEST_RESOURCES_DESCRIPTOR);
-        }
-
-        BeanFactory factory = new XmlBeanFactory(new InputStreamResource(in));
-        CayenneResources resources = (CayenneResources) factory.getBean(
-                "TestResources",
-                CayenneResources.class);
-
-        resources.setConnectionKey(System.getProperty(CONNECTION_NAME_KEY));
-
-        return resources;
-    }
 
     protected DataSourceInfo connectionInfo;
     protected DataSource dataSource;
-    protected BeanFactory beanFactory;
     protected Map<String, AccessStackAdapter> adapterMap;
 
-    /**
-     * Returns shared test resource instance.
-     */
-    public static CayenneResources getResources() {
-        if (resources == null) {
-            resources = loadResources();
-
-            // rebuild schema after the resources static var is initialized so that after
-            // possible initial failure we don't attempt rebuilding schema in subsequent
-            // tests
-            try {
-                resources.rebuildSchema();
-            }
-            catch (Exception ex) {
-                logger.error("Error generating schema...", ex);
-                throw new RuntimeException("Error generating schema");
-            }
-        }
-        return resources;
-    }
-
     public CayenneResources(Map<String, AccessStackAdapter> adapterMap) {
         this.adapterMap = adapterMap;
 
@@ -116,65 +55,12 @@ public class CayenneResources implements
         for (AccessStackAdapter adapter : adapterMap.values()) {
             ((JdbcAdapter) adapter.getAdapter()).setBatchQueryBuilderFactory(factory);
         }
-    }
-
-    /**
-     * Completely rebuilds test schema.
-     */
-    void rebuildSchema() throws Exception {
-
-        if ("true".equalsIgnoreCase(System.getProperty(SKIP_SCHEMA_KEY))) {
-            logger.info("skipping schema generation... ");
-            return;
-        }
-
-        // generate schema using a special AccessStack that
-        // combines all DataMaps that require schema support
-        // schema generation is done like that instead of
-        // per stack on demand, to avoid conflicts when
-        // dropping and generating PK objects.
-        AccessStack stack = getAccessStack(SCHEMA_SETUP_STACK);
-
-        stack.dropSchema();
-        stack.dropPKSupport();
-        stack.createSchema();
-        stack.createPKSupport();
-    }
-
-    public void setConnectionKey(String connectionKey) {
-
-        connectionInfo = ConnectionProperties.getInstance().getConnectionInfo(
-                connectionKey);
-
-        // attempt default if invalid key is specified
-        if (connectionInfo == null) {
-
-            logger.info("Invalid connection key '"
-                    + connectionKey
-                    + "', trying default: "
-                    + DEFAULT_CONNECTION_KEY);
-
-            connectionInfo = ConnectionProperties.getInstance().getConnectionInfo(
-                    DEFAULT_CONNECTION_KEY);
-        }
 
-        if (connectionInfo == null) {
-            throw new RuntimeException("Null connection info for key: " + connectionKey);
-        }
-
-        logger.info("test connection info: " + connectionInfo);
-        this.dataSource = createDataSource();
-    }
-
-    /**
-     * BeanFactoryAware implementation to store BeanFactory.
-     */
-    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
-        this.beanFactory = beanFactory;
     }
 
-    public AccessStack getAccessStack(String name) {
-        return (AccessStack) beanFactory.getBean(name, AccessStack.class);
+    public void setConnectionInfo(DataSourceInfo connectionInfo) {
+        this.connectionInfo = connectionInfo;
+        this.dataSource = createDataSource();
     }
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/client/ClientCase.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/client/ClientCase.java?rev=1126269&r1=1126268&r2=1126269&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/client/ClientCase.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/client/ClientCase.java
Sun May 22 22:52:35 2011
@@ -21,7 +21,6 @@ package org.apache.cayenne.unit.di.clien
 import org.apache.cayenne.di.DIBootstrap;
 import org.apache.cayenne.di.Injector;
 import org.apache.cayenne.di.spi.DefaultScope;
-import org.apache.cayenne.unit.CayenneResources;
 import org.apache.cayenne.unit.di.DICase;
 import org.apache.cayenne.unit.di.server.ServerCaseModule;
 
@@ -34,10 +33,9 @@ public class ClientCase extends DICase {
     private static final Injector injector;
 
     static {
-        final CayenneResources resources = CayenneResources.getResources();
         DefaultScope testScope = new DefaultScope();
         injector = DIBootstrap.createInjector(
-                new ServerCaseModule(resources, testScope),
+                new ServerCaseModule(testScope),
                 new ClientCaseModule(testScope));
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesAccessStackAdapterProvider.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesAccessStackAdapterProvider.java?rev=1126269&r1=1126268&r2=1126269&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesAccessStackAdapterProvider.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesAccessStackAdapterProvider.java
Sun May 22 22:52:35 2011
@@ -20,20 +20,24 @@ package org.apache.cayenne.unit.di.serve
 
 import org.apache.cayenne.ConfigurationException;
 import org.apache.cayenne.conn.DataSourceInfo;
+import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.di.Provider;
 import org.apache.cayenne.unit.AccessStackAdapter;
 import org.apache.cayenne.unit.CayenneResources;
 
-public class CayenneResourcesAccessStackAdapterProvider implements Provider<AccessStackAdapter>
{
+public class CayenneResourcesAccessStackAdapterProvider implements
+        Provider<AccessStackAdapter> {
 
-    protected CayenneResources resources;
+    private CayenneResources resources;
+    private DataSourceInfo dataSourceInfo;
 
-    public CayenneResourcesAccessStackAdapterProvider(CayenneResources resources) {
+    public CayenneResourcesAccessStackAdapterProvider(@Inject CayenneResources resources,
+            @Inject DataSourceInfo dataSourceInfo) {
+        this.dataSourceInfo = dataSourceInfo;
         this.resources = resources;
     }
 
     public AccessStackAdapter get() throws ConfigurationException {
-        DataSourceInfo connectionInfo = resources.getConnectionInfo();
-        return resources.getAccessStackAdapter(connectionInfo.getAdapterClassName());
+        return resources.getAccessStackAdapter(dataSourceInfo.getAdapterClassName());
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesDbAdapterProvider.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesDbAdapterProvider.java?rev=1126269&r1=1126268&r2=1126269&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesDbAdapterProvider.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesDbAdapterProvider.java
Sun May 22 22:52:35 2011
@@ -21,22 +21,25 @@ package org.apache.cayenne.unit.di.serve
 import org.apache.cayenne.ConfigurationException;
 import org.apache.cayenne.conn.DataSourceInfo;
 import org.apache.cayenne.dba.DbAdapter;
+import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.di.Provider;
 import org.apache.cayenne.unit.AccessStackAdapter;
 import org.apache.cayenne.unit.CayenneResources;
 
 public class CayenneResourcesDbAdapterProvider implements Provider<DbAdapter> {
 
-    protected CayenneResources resources;
+    private CayenneResources resources;
+    private DataSourceInfo dataSourceInfo;
 
-    public CayenneResourcesDbAdapterProvider(CayenneResources resources) {
+    public CayenneResourcesDbAdapterProvider(@Inject CayenneResources resources,
+            @Inject DataSourceInfo dataSourceInfo) {
+        this.dataSourceInfo = dataSourceInfo;
         this.resources = resources;
     }
 
     public DbAdapter get() throws ConfigurationException {
 
-        DataSourceInfo connectionInfo = resources.getConnectionInfo();
-        AccessStackAdapter adapter = resources.getAccessStackAdapter(connectionInfo
+        AccessStackAdapter adapter = resources.getAccessStackAdapter(dataSourceInfo
                 .getAdapterClassName());
 
         return adapter.getAdapter();

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesProvider.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesProvider.java?rev=1126269&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesProvider.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/CayenneResourcesProvider.java
Sun May 22 22:52:35 2011
@@ -0,0 +1,110 @@
+/*****************************************************************
+ *   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.unit.di.server;
+
+import java.io.InputStream;
+
+import org.apache.cayenne.ConfigurationException;
+import org.apache.cayenne.conn.DataSourceInfo;
+import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.di.Provider;
+import org.apache.cayenne.unit.AccessStack;
+import org.apache.cayenne.unit.CayenneResources;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.xml.XmlBeanFactory;
+import org.springframework.core.io.InputStreamResource;
+
+public class CayenneResourcesProvider implements Provider<CayenneResources> {
+
+    private static Log logger = LogFactory.getLog(CayenneResourcesProvider.class);
+
+    public static final String TEST_RESOURCES_DESCRIPTOR = "spring-test-resources.xml";
+
+    public static final String CONNECTION_NAME_KEY = "cayenneTestConnection";
+    public static final String DEFAULT_CONNECTION_KEY = "internal_embedded_datasource";
+
+    public static final String SKIP_SCHEMA_KEY = "cayenne.test.schema.skip";
+    public static final String SCHEMA_SETUP_STACK = "SchemaSetupStack";
+
+    @Inject
+    private DataSourceInfo dataSourceInfo;
+
+    public CayenneResources get() throws ConfigurationException {
+
+        InputStream in = Thread
+                .currentThread()
+                .getContextClassLoader()
+                .getResourceAsStream(TEST_RESOURCES_DESCRIPTOR);
+
+        if (in == null) {
+            logger.error("Can't locate resource: " + TEST_RESOURCES_DESCRIPTOR);
+            throw new RuntimeException(
+                    "Can't locate resource descriptor in the ClassLoader: "
+                            + TEST_RESOURCES_DESCRIPTOR);
+        }
+
+        BeanFactory factory = new XmlBeanFactory(new InputStreamResource(in));
+        CayenneResources resources = (CayenneResources) factory.getBean(
+                "TestResources",
+                CayenneResources.class);
+
+        resources.setConnectionInfo(dataSourceInfo);
+
+        // rebuild schema after the resources instance is loaded so that after
+        // possible initial failure we don't attempt rebuilding schema in subsequent
+        // tests
+        try {
+            rebuildSchema(factory);
+        }
+        catch (Exception ex) {
+            logger.error("Error generating schema...", ex);
+            throw new RuntimeException("Error generating schema");
+        }
+
+        return resources;
+    }
+
+    /**
+     * Completely rebuilds test schema.
+     */
+    private void rebuildSchema(BeanFactory beanFactory) throws Exception {
+
+        if ("true".equalsIgnoreCase(System.getProperty(SKIP_SCHEMA_KEY))) {
+            logger.info("skipping schema generation... ");
+            return;
+        }
+
+        // generate schema using a special AccessStack that
+        // combines all DataMaps that require schema support
+        // schema generation is done like that instead of
+        // per stack on demand, to avoid conflicts when
+        // dropping and generating PK objects.
+        AccessStack stack = (AccessStack) beanFactory.getBean(
+                SCHEMA_SETUP_STACK,
+                AccessStack.class);
+
+        stack.dropSchema();
+        stack.dropPKSupport();
+        stack.createSchema();
+        stack.createPKSupport();
+    }
+
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCase.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCase.java?rev=1126269&r1=1126268&r2=1126269&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCase.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCase.java
Sun May 22 22:52:35 2011
@@ -21,7 +21,6 @@ package org.apache.cayenne.unit.di.serve
 import org.apache.cayenne.di.DIBootstrap;
 import org.apache.cayenne.di.Injector;
 import org.apache.cayenne.di.spi.DefaultScope;
-import org.apache.cayenne.unit.CayenneResources;
 import org.apache.cayenne.unit.di.DICase;
 
 public class ServerCase extends DICase {
@@ -42,9 +41,8 @@ public class ServerCase extends DICase {
     private static final Injector injector;
 
     static {
-        CayenneResources resources = CayenneResources.getResources();
         DefaultScope testScope = new DefaultScope();
-        injector = DIBootstrap.createInjector(new ServerCaseModule(resources, testScope));
+        injector = DIBootstrap.createInjector(new ServerCaseModule(testScope));
     }
 
     @Override

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceFactory.java?rev=1126269&r1=1126268&r2=1126269&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceFactory.java
Sun May 22 22:52:35 2011
@@ -30,6 +30,7 @@ import javax.sql.DataSource;
 import org.apache.cayenne.conn.DataSourceInfo;
 import org.apache.cayenne.conn.PoolDataSource;
 import org.apache.cayenne.conn.PoolManager;
+import org.apache.cayenne.di.Inject;
 
 public class ServerCaseDataSourceFactory {
 
@@ -38,17 +39,13 @@ public class ServerCaseDataSourceFactory
     private Map<String, DataSource> dataSources;
     private Set<String> mapsWithDedicatedDataSource;
 
-    ServerCaseDataSourceFactory(DataSourceInfo dataSourceInfo,
-            String... mapsWithDedicatedDataSource) {
-
-        if (mapsWithDedicatedDataSource == null) {
-            mapsWithDedicatedDataSource = new String[0];
-        }
+    public ServerCaseDataSourceFactory(@Inject DataSourceInfo dataSourceInfo) {
 
         this.dataSourceInfo = dataSourceInfo;
         this.dataSources = new HashMap<String, DataSource>();
-        this.mapsWithDedicatedDataSource = new HashSet<String>(Arrays
-                .asList(mapsWithDedicatedDataSource));
+        this.mapsWithDedicatedDataSource = new HashSet<String>(Arrays.asList(
+                "map-db1",
+                "map-db2"));
 
         this.sharedDataSource = createDataSource();
     }

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceInfoProvider.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceInfoProvider.java?rev=1126269&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceInfoProvider.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceInfoProvider.java
Sun May 22 22:52:35 2011
@@ -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.unit.di.server;
+
+import org.apache.cayenne.ConfigurationException;
+import org.apache.cayenne.conn.DataSourceInfo;
+import org.apache.cayenne.di.Provider;
+import org.apache.cayenne.unit.ConnectionProperties;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class ServerCaseDataSourceInfoProvider implements Provider<DataSourceInfo> {
+
+    private static final String CONNECTION_NAME_KEY = "cayenneTestConnection";
+    private static final String DEFAULT_CONNECTION_KEY = "internal_embedded_datasource";
+
+    private static Log logger = LogFactory.getLog(ServerCaseDataSourceInfoProvider.class);
+
+    public DataSourceInfo get() throws ConfigurationException {
+
+        String connectionKey = System.getProperty(CONNECTION_NAME_KEY);
+
+        DataSourceInfo connectionInfo = ConnectionProperties
+                .getInstance()
+                .getConnectionInfo(connectionKey);
+
+        // attempt default if invalid key is specified
+        if (connectionInfo == null) {
+
+            logger.info("Invalid connection key '"
+                    + connectionKey
+                    + "', trying default: "
+                    + DEFAULT_CONNECTION_KEY);
+
+            connectionInfo = ConnectionProperties.getInstance().getConnectionInfo(
+                    DEFAULT_CONNECTION_KEY);
+        }
+
+        if (connectionInfo == null) {
+            throw new RuntimeException("Null connection info for key: " + connectionKey);
+        }
+
+        logger.info("loaded connection info: " + connectionInfo);
+        return connectionInfo;
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseModule.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseModule.java?rev=1126269&r1=1126268&r2=1126269&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseModule.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseModule.java
Sun May 22 22:52:35 2011
@@ -42,46 +42,43 @@ import org.apache.cayenne.unit.util.SQLT
 
 public class ServerCaseModule implements Module {
 
-    protected CayenneResources resources;
     protected DefaultScope testScope;
 
-    public ServerCaseModule(CayenneResources resources, DefaultScope testScope) {
-        this.resources = resources;
+    public ServerCaseModule(DefaultScope testScope) {
         this.testScope = testScope;
     }
 
     public void configure(Binder binder) {
 
-        ServerCaseDataSourceFactory dataSourceFactory = new ServerCaseDataSourceFactory(
-                resources.getConnectionInfo(),
-                "map-db1",
-                "map-db2");
-
         // these are the objects injectable in unit tests that subclass from
         // ServerCase. Note that ServerRuntimeProvider creates ServerRuntime
         // instances complete with their own DI injectors, independent from the
         // unit test injector. ServerRuntime injector contents are customized
         // inside ServerRuntimeProvider.
 
+        binder.bind(CayenneResources.class).toProvider(CayenneResourcesProvider.class);
         binder.bind(JdbcEventLogger.class).to(CommonsJdbcEventLogger.class);
 
         // singleton objects
         binder.bind(UnitTestLifecycleManager.class).toInstance(
                 new ServerCaseLifecycleManager(testScope));
 
-        binder.bind(DataSourceInfo.class).toInstance(resources.getConnectionInfo());
-        binder.bind(DataSource.class).toProviderInstance(
-                new ServerCaseSharedDataSourceProvider(dataSourceFactory));
-        binder.bind(DbAdapter.class).toProviderInstance(
-                new CayenneResourcesDbAdapterProvider(resources));
-        binder.bind(AccessStackAdapter.class).toProviderInstance(
-                new CayenneResourcesAccessStackAdapterProvider(resources));
+        binder.bind(DataSourceInfo.class).toProvider(
+                ServerCaseDataSourceInfoProvider.class);
+        binder
+                .bind(DataSource.class)
+                .toProvider(ServerCaseSharedDataSourceProvider.class);
+        binder.bind(DbAdapter.class).toProvider(CayenneResourcesDbAdapterProvider.class);
+        binder.bind(AccessStackAdapter.class).toProvider(
+                CayenneResourcesAccessStackAdapterProvider.class);
         binder.bind(BatchQueryBuilderFactory.class).toProvider(
                 ServerCaseBatchQueryBuilderFactoryProvider.class);
         binder.bind(DataChannelInterceptor.class).to(
                 ServerCaseDataChannelInterceptor.class);
         binder.bind(SQLTemplateCustomizer.class).toProvider(
                 SQLTemplateCustomizerProvider.class);
+        binder.bind(ServerCaseDataSourceFactory.class).to(
+                ServerCaseDataSourceFactory.class);
 
         // test-scoped objects
         binder.bind(EntityResolver.class).toProvider(
@@ -90,8 +87,8 @@ public class ServerCaseModule implements
                 testScope);
         binder.bind(ServerCaseProperties.class).to(ServerCaseProperties.class).in(
                 testScope);
-        binder.bind(ServerRuntime.class).toProviderInstance(
-                new ServerRuntimeProvider(resources, dataSourceFactory)).in(testScope);
+        binder.bind(ServerRuntime.class).toProvider(ServerRuntimeProvider.class).in(
+                testScope);
         binder
                 .bind(ObjectContext.class)
                 .toProvider(ServerCaseObjectContextProvider.class)
@@ -104,5 +101,4 @@ public class ServerCaseModule implements
         binder.bind(DBHelper.class).toProvider(FlavoredDBHelperProvider.class).in(
                 testScope);
     }
-
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseSharedDataSourceProvider.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseSharedDataSourceProvider.java?rev=1126269&r1=1126268&r2=1126269&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseSharedDataSourceProvider.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseSharedDataSourceProvider.java
Sun May 22 22:52:35 2011
@@ -21,6 +21,7 @@ package org.apache.cayenne.unit.di.serve
 import javax.sql.DataSource;
 
 import org.apache.cayenne.ConfigurationException;
+import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.di.Provider;
 
 public class ServerCaseSharedDataSourceProvider implements Provider<DataSource> {
@@ -28,7 +29,7 @@ public class ServerCaseSharedDataSourceP
     protected ServerCaseDataSourceFactory dataSourceFactory;
 
     public ServerCaseSharedDataSourceProvider(
-            ServerCaseDataSourceFactory dataSourceFactory) {
+            @Inject ServerCaseDataSourceFactory dataSourceFactory) {
         this.dataSourceFactory = dataSourceFactory;
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerRuntimeProvider.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerRuntimeProvider.java?rev=1126269&r1=1126268&r2=1126269&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerRuntimeProvider.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/di/server/ServerRuntimeProvider.java
Sun May 22 22:52:35 2011
@@ -26,20 +26,21 @@ import org.apache.cayenne.di.Binder;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.di.Module;
 import org.apache.cayenne.di.Provider;
-import org.apache.cayenne.unit.CayenneResources;
 
 public class ServerRuntimeProvider implements Provider<ServerRuntime> {
 
-    @Inject
-    protected ServerCaseProperties properties;
-
-    private CayenneResources resources;
+    private ServerCaseProperties properties;
     private ServerCaseDataSourceFactory dataSourceFactory;
 
-    public ServerRuntimeProvider(CayenneResources resources,
-            ServerCaseDataSourceFactory dataSourceFactory) {
-        this.resources = resources;
+    private Provider<DbAdapter> dbAdapterProvider;
+
+    public ServerRuntimeProvider(@Inject ServerCaseDataSourceFactory dataSourceFactory,
+            @Inject ServerCaseProperties properties,
+            @Inject Provider<DbAdapter> dbAdapterProvider) {
+
         this.dataSourceFactory = dataSourceFactory;
+        this.properties = properties;
+        this.dbAdapterProvider = dbAdapterProvider;
     }
 
     public ServerRuntime get() throws ConfigurationException {
@@ -60,8 +61,7 @@ public class ServerRuntimeProvider imple
             // these are the objects overriding standard ServerModule definitions or
             // dependencies needed by such overrides
 
-            binder.bind(DbAdapter.class).toProviderInstance(
-                    new CayenneResourcesDbAdapterProvider(resources));
+            binder.bind(DbAdapter.class).toProviderInstance(dbAdapterProvider);
             binder.bind(DataDomain.class).toProvider(ServerCaseDataDomainProvider.class);
 
             // map DataSources for all test DataNode names



Mime
View raw message