cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1407653 - in /cayenne/main/trunk/framework/cayenne-tools/src: main/java/org/apache/cayenne/tools/ main/java/org/apache/cayenne/tools/configuration/ main/java/org/apache/cayenne/tools/dbimport/ test/java/org/apache/cayenne/tools/configurati...
Date Fri, 09 Nov 2012 21:31:32 GMT
Author: aadamchik
Date: Fri Nov  9 21:31:31 2012
New Revision: 1407653

URL: http://svn.apache.org/viewvc?rev=1407653&view=rev
Log:
CAY-1758 cdbimport improvements

* switching driver loader to DI
* unit tests

Added:
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/configuration/DriverDataSourceFactory.java
    cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/configuration/
    cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/configuration/ToolsModuleTest.java
      - copied, changed from r1407641, cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DbImportActionTest.java
Removed:
    cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DbImportActionTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/configuration/ToolsModule.java
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java

Modified: cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java?rev=1407653&r1=1407652&r2=1407653&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java
(original)
+++ cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java
Fri Nov  9 21:31:31 2012
@@ -59,34 +59,30 @@ public class DbGeneratorTask extends Cay
         Log logger = new AntLogger(this);
         Injector injector = DIBootstrap.createInjector(new ToolsModule(logger));
 
-        log(String.format(
-                "connection settings - [driver: %s, url: %s, username: %s]",
-                driver, url, userName), Project.MSG_VERBOSE);
+        log(String.format("connection settings - [driver: %s, url: %s, username: %s]", driver,
url, userName),
+                Project.MSG_VERBOSE);
 
         log(String.format(
                 "generator options - [dropTables: %s, dropPK: %s, createTables: %s, createPK:
%s, createFK: %s]",
-                dropTables, dropPK, createTables, createPK, createFK),
-                Project.MSG_VERBOSE);
+                dropTables, dropPK, createTables, createPK, createFK), Project.MSG_VERBOSE);
 
         validateAttributes();
 
         ClassLoader loader = null;
         try {
             loader = Thread.currentThread().getContextClassLoader();
-            Thread.currentThread().setContextClassLoader(
-                    DbGeneratorTask.class.getClassLoader());
+            Thread.currentThread().setContextClassLoader(DbGeneratorTask.class.getClassLoader());
 
             // Load the data map and run the db generator.
             DataMap dataMap = loadDataMap();
 
             // load driver taking custom CLASSPATH into account...
-            DriverDataSource dataSource = new DriverDataSource((Driver) Class
-                    .forName(driver).newInstance(), url, userName, password);
+            DriverDataSource dataSource = new DriverDataSource((Driver) Class.forName(driver).newInstance(),
url,
+                    userName, password);
 
             DbAdapter adapter = getAdapter(injector, dataSource);
 
-            DbGenerator generator = new DbGenerator(adapter, dataMap,
-                    Collections.<DbEntity> emptyList(), null,
+            DbGenerator generator = new DbGenerator(adapter, dataMap, Collections.<DbEntity>
emptyList(), null,
                     NoopJdbcEventLogger.getInstance());
             generator.setShouldCreateFKConstraints(createFK);
             generator.setShouldCreatePKSupport(createPK);
@@ -112,7 +108,7 @@ public class DbGeneratorTask extends Cay
     }
 
     /**
-     * Validates atttributes that are not related to internal
+     * Validates attributes that are not related to internal
      * DefaultClassGenerator. Throws BuildException if attributes are invalid.
      */
     protected void validateAttributes() throws BuildException {

Added: cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/configuration/DriverDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/configuration/DriverDataSourceFactory.java?rev=1407653&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/configuration/DriverDataSourceFactory.java
(added)
+++ cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/configuration/DriverDataSourceFactory.java
Fri Nov  9 21:31:31 2012
@@ -0,0 +1,53 @@
+/*****************************************************************
+ *   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.tools.configuration;
+
+import java.sql.Driver;
+
+import javax.sql.DataSource;
+
+import org.apache.cayenne.configuration.DataNodeDescriptor;
+import org.apache.cayenne.configuration.server.DataSourceFactory;
+import org.apache.cayenne.conn.DataSourceInfo;
+import org.apache.cayenne.conn.DriverDataSource;
+import org.apache.cayenne.di.AdhocObjectFactory;
+import org.apache.cayenne.di.Inject;
+
+/**
+ * @since 3.2
+ */
+public class DriverDataSourceFactory implements DataSourceFactory {
+
+    private AdhocObjectFactory objectFactory;
+
+    public DriverDataSourceFactory(@Inject AdhocObjectFactory objectFactory) {
+        this.objectFactory = objectFactory;
+    }
+
+    public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception {
+        DataSourceInfo properties = nodeDescriptor.getDataSourceDescriptor();
+        if (properties == null) {
+            throw new IllegalArgumentException("'nodeDescriptor' contains no datasoucre descriptor");
+        }
+
+        Driver driver = objectFactory.newInstance(Driver.class, properties.getJdbcDriver());
+        return new DriverDataSource(driver, properties.getDataSourceUrl(), properties.getUserName(),
+                properties.getPassword());
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/configuration/ToolsModule.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/configuration/ToolsModule.java?rev=1407653&r1=1407652&r2=1407653&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/configuration/ToolsModule.java
(original)
+++ cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/configuration/ToolsModule.java
Fri Nov  9 21:31:31 2012
@@ -23,6 +23,7 @@ import org.apache.cayenne.access.jdbc.De
 import org.apache.cayenne.configuration.Constants;
 import org.apache.cayenne.configuration.DefaultRuntimeProperties;
 import org.apache.cayenne.configuration.RuntimeProperties;
+import org.apache.cayenne.configuration.server.DataSourceFactory;
 import org.apache.cayenne.configuration.server.DbAdapterFactory;
 import org.apache.cayenne.configuration.server.DefaultDbAdapterFactory;
 import org.apache.cayenne.dba.db2.DB2Sniffer;
@@ -80,29 +81,23 @@ public class ToolsModule implements Modu
         binder.bind(AdhocObjectFactory.class).toInstance(objectFactory);
 
         binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
-        binder.bind(BatchQueryBuilderFactory.class).to(
-                DefaultBatchQueryBuilderFactory.class);
+        binder.bind(BatchQueryBuilderFactory.class).to(DefaultBatchQueryBuilderFactory.class);
         binder.bind(JdbcEventLogger.class).to(CommonsJdbcEventLogger.class);
 
         // TODO: this is cloned from ServerModule... figure out how to reuse
         // this list
-        binder.bindList(Constants.SERVER_ADAPTER_DETECTORS_LIST)
-                .add(new OpenBaseSniffer(objectFactory))
-                .add(new FrontBaseSniffer(objectFactory))
-                .add(new IngresSniffer(objectFactory))
-                .add(new SQLiteSniffer(objectFactory))
-                .add(new DB2Sniffer(objectFactory))
-                .add(new H2Sniffer(objectFactory))
-                .add(new HSQLDBSniffer(objectFactory))
-                .add(new SybaseSniffer(objectFactory))
-                .add(new DerbySniffer(objectFactory))
-                .add(new SQLServerSniffer(objectFactory))
-                .add(new OracleSniffer(objectFactory))
-                .add(new PostgresSniffer(objectFactory))
-                .add(new MySQLSniffer(objectFactory));
+        binder.bindList(Constants.SERVER_ADAPTER_DETECTORS_LIST).add(new OpenBaseSniffer(objectFactory))
+                .add(new FrontBaseSniffer(objectFactory)).add(new IngresSniffer(objectFactory))
+                .add(new SQLiteSniffer(objectFactory)).add(new DB2Sniffer(objectFactory))
+                .add(new H2Sniffer(objectFactory)).add(new HSQLDBSniffer(objectFactory))
+                .add(new SybaseSniffer(objectFactory)).add(new DerbySniffer(objectFactory))
+                .add(new SQLServerSniffer(objectFactory)).add(new OracleSniffer(objectFactory))
+                .add(new PostgresSniffer(objectFactory)).add(new MySQLSniffer(objectFactory));
 
         binder.bind(DbAdapterFactory.class).to(DefaultDbAdapterFactory.class);
 
+        binder.bind(DataSourceFactory.class).to(DriverDataSourceFactory.class);
+
     }
 
 }

Modified: cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java?rev=1407653&r1=1407652&r2=1407653&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java
Fri Nov  9 21:31:31 2012
@@ -21,14 +21,14 @@ package org.apache.cayenne.tools.dbimpor
 import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.sql.Driver;
 
 import javax.sql.DataSource;
 
 import org.apache.cayenne.access.DbLoader;
 import org.apache.cayenne.configuration.DataNodeDescriptor;
+import org.apache.cayenne.configuration.server.DataSourceFactory;
 import org.apache.cayenne.configuration.server.DbAdapterFactory;
-import org.apache.cayenne.conn.DriverDataSource;
+import org.apache.cayenne.conn.DataSourceInfo;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.map.DataMap;
@@ -48,12 +48,15 @@ import org.xml.sax.InputSource;
  */
 public class DbImportAction {
 
+    private DataSourceFactory dataSourceFactory;
     private DbAdapterFactory adapterFactory;
     private Log logger;
 
-    public DbImportAction(@Inject Log logger, @Inject DbAdapterFactory adapterFactory) {
+    public DbImportAction(@Inject Log logger, @Inject DbAdapterFactory adapterFactory,
+            @Inject DataSourceFactory dataSourceFactory) {
         this.logger = logger;
         this.adapterFactory = adapterFactory;
+        this.dataSourceFactory = dataSourceFactory;
     }
 
     public void execute(DbImportParameters parameters) throws Exception {
@@ -77,12 +80,18 @@ public class DbImportAction {
             logger.debug("Importer options - namingStrategy: " + parameters.getNamingStrategy());
         }
 
-        // TODO: load via DI
-        DriverDataSource dataSource = new DriverDataSource(
-                (Driver) Class.forName(parameters.getDriver()).newInstance(), parameters.getUrl(),
-                parameters.getUsername(), parameters.getPassword());
+        DataSourceInfo dataSourceInfo = new DataSourceInfo();
+        dataSourceInfo.setDataSourceUrl(parameters.getUrl());
+        dataSourceInfo.setJdbcDriver(parameters.getDriver());
+        dataSourceInfo.setUserName(parameters.getUsername());
+        dataSourceInfo.setPassword(parameters.getPassword());
 
-        DbAdapter adapter = getAdapter(parameters.getAdapter(), dataSource);
+        DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
+        nodeDescriptor.setAdapterType(parameters.getAdapter());
+        nodeDescriptor.setDataSourceDescriptor(dataSourceInfo);
+
+        DataSource dataSource = dataSourceFactory.getDataSource(nodeDescriptor);
+        DbAdapter adapter = adapterFactory.createAdapter(nodeDescriptor, dataSource);
         DataMap dataMap = getDataMap(parameters);
 
         DbImportDbLoaderDelegate loaderDelegate = new DbImportDbLoaderDelegate();
@@ -119,14 +128,6 @@ public class DbImportAction {
         pw.close();
     }
 
-    DbAdapter getAdapter(String adapter, DataSource dataSource) throws Exception {
-
-        DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
-        nodeDescriptor.setAdapterType(adapter);
-
-        return adapterFactory.createAdapter(nodeDescriptor, dataSource);
-    }
-
     DataMap getDataMap(DbImportParameters parameters) throws IOException {
 
         File dataMapFile = parameters.getMap();

Copied: cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/configuration/ToolsModuleTest.java
(from r1407641, cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DbImportActionTest.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/configuration/ToolsModuleTest.java?p2=cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/configuration/ToolsModuleTest.java&p1=cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DbImportActionTest.java&r1=1407641&r2=1407653&rev=1407653&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DbImportActionTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/configuration/ToolsModuleTest.java
Fri Nov  9 21:31:31 2012
@@ -16,41 +16,29 @@
  *  specific language governing permissions and limitations
  *  under the License.
  ****************************************************************/
-package org.apache.cayenne.tools.dbimport;
+package org.apache.cayenne.tools.configuration;
 
 import static org.mockito.Mockito.mock;
-
-import javax.sql.DataSource;
-
 import junit.framework.TestCase;
 
-import org.apache.cayenne.configuration.server.DbAdapterFactory;
-import org.apache.cayenne.dba.AutoAdapter;
-import org.apache.cayenne.dba.DbAdapter;
-import org.apache.cayenne.dba.mysql.MySQLAdapter;
+import org.apache.cayenne.configuration.server.DataSourceFactory;
+import org.apache.cayenne.di.AdhocObjectFactory;
 import org.apache.cayenne.di.DIBootstrap;
 import org.apache.cayenne.di.Injector;
+import org.apache.cayenne.di.spi.DefaultAdhocObjectFactory;
+import org.apache.cayenne.tools.configuration.DriverDataSourceFactory;
 import org.apache.cayenne.tools.configuration.ToolsModule;
-import org.apache.cayenne.tools.dbimport.DbImportAction;
 import org.apache.commons.logging.Log;
 
-public class DbImportActionTest extends TestCase {
-
-    public void testGetAdapter() throws Exception {
-
-        Injector injector = DIBootstrap.createInjector(new ToolsModule(mock(Log.class)));
-
-        DbImportAction action = new DbImportAction(injector.getInstance(Log.class),
-                injector.getInstance(DbAdapterFactory.class));
-
-        DataSource ds = mock(DataSource.class);
+public class ToolsModuleTest extends TestCase {
 
-        DbAdapter adapter = action.getAdapter(null, ds);
-        assertNotNull(adapter);
-        assertTrue(adapter instanceof AutoAdapter);
+    public void testModuleContents() {
 
-        DbAdapter adapter2 = action.getAdapter(MySQLAdapter.class.getName(), ds);
-        assertNotNull(adapter2);
-        assertTrue(adapter2 instanceof MySQLAdapter);
+        Log log = mock(Log.class);
+        Injector i = DIBootstrap.createInjector(new ToolsModule(log));
+        
+        assertSame(log, i.getInstance(Log.class));
+        assertTrue(i.getInstance(DataSourceFactory.class) instanceof DriverDataSourceFactory);
+        assertTrue(i.getInstance(AdhocObjectFactory.class) instanceof DefaultAdhocObjectFactory);
     }
 }



Mime
View raw message