cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1407471 - in /cayenne/main/trunk/framework: cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ maven-cayenne-plugin/ maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/ maven-cayenne-plugin/src/test/java/or...
Date Fri, 09 Nov 2012 14:39:33 GMT
Author: aadamchik
Date: Fri Nov  9 14:39:32 2012
New Revision: 1407471

URL: http://svn.apache.org/viewvc?rev=1407471&view=rev
Log:
CAY-1758 cdbimport improvements: Default adapter (if none specified) must be AutoAdapter,
not JdbcAdapter

Added:
    cayenne/main/trunk/framework/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ToolModule.java
    cayenne/main/trunk/framework/maven-cayenne-plugin/pom.xml
    cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
    cayenne/main/trunk/framework/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/CayenneGeneratorMojoTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ToolModule.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ToolModule.java?rev=1407471&r1=1407470&r2=1407471&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ToolModule.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ToolModule.java
Fri Nov  9 14:39:32 2012
@@ -20,6 +20,21 @@ package org.apache.cayenne.configuration
 
 import org.apache.cayenne.access.jdbc.BatchQueryBuilderFactory;
 import org.apache.cayenne.access.jdbc.DefaultBatchQueryBuilderFactory;
+import org.apache.cayenne.configuration.server.DbAdapterFactory;
+import org.apache.cayenne.configuration.server.DefaultDbAdapterFactory;
+import org.apache.cayenne.dba.db2.DB2Sniffer;
+import org.apache.cayenne.dba.derby.DerbySniffer;
+import org.apache.cayenne.dba.frontbase.FrontBaseSniffer;
+import org.apache.cayenne.dba.h2.H2Sniffer;
+import org.apache.cayenne.dba.hsqldb.HSQLDBSniffer;
+import org.apache.cayenne.dba.ingres.IngresSniffer;
+import org.apache.cayenne.dba.mysql.MySQLSniffer;
+import org.apache.cayenne.dba.openbase.OpenBaseSniffer;
+import org.apache.cayenne.dba.oracle.OracleSniffer;
+import org.apache.cayenne.dba.postgres.PostgresSniffer;
+import org.apache.cayenne.dba.sqlite.SQLiteSniffer;
+import org.apache.cayenne.dba.sqlserver.SQLServerSniffer;
+import org.apache.cayenne.dba.sybase.SybaseSniffer;
 import org.apache.cayenne.di.AdhocObjectFactory;
 import org.apache.cayenne.di.Binder;
 import org.apache.cayenne.di.Module;
@@ -33,20 +48,42 @@ import org.apache.cayenne.log.JdbcEventL
  * @since 3.1
  */
 public class ToolModule implements Module {
-    
-public void configure(Binder binder) {
-        
+
+    public void configure(Binder binder) {
+
         // configure empty global stack properties
         binder.bindMap(Constants.PROPERTIES_MAP);
-        
+
         binder.bindList(Constants.SERVER_DEFAULT_TYPES_LIST);
         binder.bindList(Constants.SERVER_USER_TYPES_LIST);
         binder.bindList(Constants.SERVER_TYPE_FACTORIES_LIST);
 
-        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
+        AdhocObjectFactory objectFactory = new DefaultAdhocObjectFactory();
+        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.bind(DbAdapterFactory.class).to(DefaultDbAdapterFactory.class);
     }
 
 }

Modified: cayenne/main/trunk/framework/maven-cayenne-plugin/pom.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/maven-cayenne-plugin/pom.xml?rev=1407471&r1=1407470&r2=1407471&view=diff
==============================================================================
--- cayenne/main/trunk/framework/maven-cayenne-plugin/pom.xml (original)
+++ cayenne/main/trunk/framework/maven-cayenne-plugin/pom.xml Fri Nov  9 14:39:32 2012
@@ -1,24 +1,17 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!--
-	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.   
--->
+<!-- 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. -->
 
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
 	<parent>
 		<artifactId>cayenne-parent</artifactId>
@@ -35,6 +28,13 @@
 	<name>Cayenne Maven Plugin</name>
 
 	<dependencies>
+
+		<dependency>
+			<groupId>org.mockito</groupId>
+			<artifactId>mockito-all</artifactId>
+			<scope>test</scope>
+		</dependency>
+
 		<dependency>
 			<groupId>org.apache.maven</groupId>
 			<artifactId>maven-plugin-api</artifactId>
@@ -65,13 +65,13 @@
 			<version>${project.version}</version>
 		</dependency>
 
-        <dependency>
+		<dependency>
 			<groupId>org.apache.cayenne</groupId>
 			<artifactId>cayenne-server</artifactId>
 			<version>${project.version}</version>
 		</dependency>
 
-        <dependency>
+		<dependency>
 			<groupId>org.apache.cayenne</groupId>
 			<artifactId>cayenne-tools</artifactId>
 			<version>${project.version}</version>
@@ -88,14 +88,14 @@
 			<artifactId>junit</artifactId>
 			<scope>test</scope>
 		</dependency>
-		
+
 		<dependency>
-      		<groupId>org.apache.maven.shared</groupId>
-      		<artifactId>maven-plugin-testing-harness</artifactId>
-      		<version>1.0-beta-1</version>
-      		<scope>test</scope>
-    	</dependency>
-    
+			<groupId>org.apache.maven.shared</groupId>
+			<artifactId>maven-plugin-testing-harness</artifactId>
+			<version>1.0-beta-1</version>
+			<scope>test</scope>
+		</dependency>
+
 	</dependencies>
 </project>
 

Modified: cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java?rev=1407471&r1=1407470&r2=1407471&view=diff
==============================================================================
--- cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
(original)
+++ cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
Fri Nov  9 14:39:32 2012
@@ -23,14 +23,16 @@ import java.io.File;
 import java.io.PrintWriter;
 import java.sql.Driver;
 
+import javax.sql.DataSource;
+
 import org.apache.cayenne.CayenneException;
 import org.apache.cayenne.access.AbstractDbLoaderDelegate;
 import org.apache.cayenne.access.DbLoader;
+import org.apache.cayenne.configuration.DataNodeDescriptor;
 import org.apache.cayenne.configuration.ToolModule;
+import org.apache.cayenne.configuration.server.DbAdapterFactory;
 import org.apache.cayenne.conn.DriverDataSource;
 import org.apache.cayenne.dba.DbAdapter;
-import org.apache.cayenne.dba.JdbcAdapter;
-import org.apache.cayenne.di.AdhocObjectFactory;
 import org.apache.cayenne.di.DIBootstrap;
 import org.apache.cayenne.di.Injector;
 import org.apache.cayenne.map.DataMap;
@@ -152,11 +154,12 @@ public class DbImporterMojo extends Abst
     private String namingStrategy;
 
     /**
-     * Java class implementing org.apache.cayenne.dba.DbAdapter. While this
-     * attribute is optional (a generic JdbcAdapter is used if not set), it is
-     * highly recommended to specify correct target adapter.
+     * Java class implementing org.apache.cayenne.dba.DbAdapter. This attribute
+     * is optional, the default is AutoAdapter, i.e. Cayenne would try to guess
+     * the DB type.
      * 
      * @parameter expression="${cdbimport.adapter}"
+     *            default-value="org.apache.cayenne.dba.AutoAdapter"
      */
     private String adapter;
 
@@ -225,22 +228,17 @@ public class DbImporterMojo extends Abst
         String schema = getSchema();
 
         Injector injector = DIBootstrap.createInjector(new ToolModule());
-        AdhocObjectFactory objectFactory = injector
-                .getInstance(AdhocObjectFactory.class);
-
-        DbAdapter adapterInst = (adapter == null) ? (DbAdapter) objectFactory
-                .newInstance(DbAdapter.class, JdbcAdapter.class.getName())
-                : (DbAdapter) objectFactory.newInstance(DbAdapter.class,
-                        adapter);
 
         // load driver taking custom CLASSPATH into account...
         DriverDataSource dataSource = new DriverDataSource((Driver) Class
                 .forName(driver).newInstance(), url, username, password);
 
+        DbAdapter adapter = getAdapter(injector, dataSource);
+
         // Load the data map and run the db importer.
         final LoaderDelegate loaderDelegate = new LoaderDelegate();
         final DbLoader loader = new DbLoader(dataSource.getConnection(),
-                adapterInst, loaderDelegate);
+                adapter, loaderDelegate);
         loader.setCreatingMeaningfulPK(meaningfulPk);
 
         if (namingStrategy != null) {
@@ -273,6 +271,18 @@ public class DbImporterMojo extends Abst
         pw.close();
     }
 
+    DbAdapter getAdapter(Injector injector, DataSource dataSource)
+            throws Exception {
+
+        DbAdapterFactory adapterFactory = injector
+                .getInstance(DbAdapterFactory.class);
+
+        DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
+        nodeDescriptor.setAdapterType(adapter);
+
+        return adapterFactory.createAdapter(nodeDescriptor, dataSource);
+    }
+
     private String getSchema() {
         if (schemaName != null) {
             getLog().warn(

Modified: cayenne/main/trunk/framework/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/CayenneGeneratorMojoTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/CayenneGeneratorMojoTest.java?rev=1407471&r1=1407470&r2=1407471&view=diff
==============================================================================
--- cayenne/main/trunk/framework/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/CayenneGeneratorMojoTest.java
(original)
+++ cayenne/main/trunk/framework/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/CayenneGeneratorMojoTest.java
Fri Nov  9 14:39:32 2012
@@ -23,67 +23,50 @@ import java.io.File;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 
 public class CayenneGeneratorMojoTest extends AbstractMojoTestCase {
-	/** {@inheritDoc} */
-	protected void setUp() throws Exception {
-		// required
-		super.setUp();
 
-	}
+    public void testCgenExecution() throws Exception {
 
-	/** {@inheritDoc} */
-	protected void tearDown() throws Exception {
-		// required
-		super.tearDown();
+        File pom = getTestFile("src/test/resources/cgen/project-to-test/pom.xml");
+        assertNotNull(pom);
+        assertTrue(pom.exists());
 
-	}
+        CayenneGeneratorMojo myMojo = (CayenneGeneratorMojo) lookupMojo("cgen",
+                pom);
+        assertNotNull(myMojo);
+        myMojo.execute();
 
-	/**
-	 * @throws Exception
-	 *             if any
-	 */
-	public void testCgenExecution() throws Exception {
+        File superTestEntity = new File(
+                "target/cayenneGeneratedClasses/superPkg/_TestEntity.txt");
+        File testEntity = new File(
+                "target/cayenneGeneratedClasses/pack/TestEntity.txt");
 
-		File pom = getTestFile("src/test/resources/cgen/project-to-test/pom.xml");
-		assertNotNull(pom);
-		assertTrue(pom.exists());
+        File superEmbeddable = new File(
+                "target/cayenneGeneratedClasses/superPkg/_Embeddable.txt");
+        File embeddable = new File(
+                "target/cayenneGeneratedClasses/pack/Embeddable.txt");
 
-		CayenneGeneratorMojo myMojo = (CayenneGeneratorMojo) lookupMojo("cgen",
-				pom);
-		assertNotNull(myMojo);
-		myMojo.execute();
+        File superNotIncludedEntity = new File(
+                "target/cayenneGeneratedClasses/pack/_NotIncludedEntity.txt");
 
-		File superTestEntity = new File(
-				"target/cayenneGeneratedClasses/superPkg/_TestEntity.txt");
-		File testEntity = new File(
-				"target/cayenneGeneratedClasses/pack/TestEntity.txt");
+        File notIncludedEntity = new File(
+                "target/cayenneGeneratedClasses/pack/NotIncludedEntity.txt");
 
-		File superEmbeddable = new File(
-				"target/cayenneGeneratedClasses/superPkg/_Embeddable.txt");
-		File embeddable = new File(
-				"target/cayenneGeneratedClasses/pack/Embeddable.txt");
+        File superExcludedEntity = new File(
+                "target/cayenneGeneratedClasses/pack/_TestExcludedEntity.txt");
+        File excludedEntity = new File(
+                "target/cayenneGeneratedClasses/pack/TestExcludedEntity.txt");
 
-		File superNotIncludedEntity = new File(
-				"target/cayenneGeneratedClasses/pack/_NotIncludedEntity.txt");
+        assertTrue(superTestEntity.exists());
+        assertTrue(testEntity.exists());
 
-		File notIncludedEntity = new File(
-				"target/cayenneGeneratedClasses/pack/NotIncludedEntity.txt");
+        assertTrue(superEmbeddable.exists());
+        assertTrue(embeddable.exists());
 
-		File superExcludedEntity = new File(
-				"target/cayenneGeneratedClasses/pack/_TestExcludedEntity.txt");
-		File excludedEntity = new File(
-				"target/cayenneGeneratedClasses/pack/TestExcludedEntity.txt");
+        assertFalse(superNotIncludedEntity.exists());
+        assertFalse(notIncludedEntity.exists());
 
-		assertTrue(superTestEntity.exists());
-		assertTrue(testEntity.exists());
+        assertFalse(superExcludedEntity.exists());
+        assertFalse(excludedEntity.exists());
 
-		assertTrue(superEmbeddable.exists());
-		assertTrue(embeddable.exists());
-
-		assertFalse(superNotIncludedEntity.exists());
-		assertFalse(notIncludedEntity.exists());
-
-		assertFalse(superExcludedEntity.exists());
-		assertFalse(excludedEntity.exists());
-
-	}
+    }
 }

Added: cayenne/main/trunk/framework/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java?rev=1407471&view=auto
==============================================================================
--- cayenne/main/trunk/framework/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
(added)
+++ cayenne/main/trunk/framework/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
Fri Nov  9 14:39:32 2012
@@ -0,0 +1,56 @@
+/*****************************************************************
+ *   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;
+
+import static org.mockito.Mockito.mock;
+
+import java.lang.reflect.Field;
+
+import javax.sql.DataSource;
+
+import junit.framework.TestCase;
+
+import org.apache.cayenne.configuration.ToolModule;
+import org.apache.cayenne.dba.AutoAdapter;
+import org.apache.cayenne.dba.DbAdapter;
+import org.apache.cayenne.dba.mysql.MySQLAdapter;
+import org.apache.cayenne.di.DIBootstrap;
+import org.apache.cayenne.di.Injector;
+
+public class DbImporterMojoTest extends TestCase {
+
+    public void testGetAdapter() throws Exception {
+
+        DbImporterMojo mojo = new DbImporterMojo();
+
+        DataSource ds = mock(DataSource.class);
+        Injector injector = DIBootstrap.createInjector(new ToolModule());
+        DbAdapter adapter = mojo.getAdapter(injector, ds);
+        assertNotNull(adapter);
+        assertTrue(adapter instanceof AutoAdapter);
+
+        Field adapterField = mojo.getClass().getDeclaredField("adapter");
+        adapterField.setAccessible(true);
+        adapterField.set(mojo, MySQLAdapter.class.getName());
+        
+        DbAdapter adapter2 = mojo.getAdapter(injector, ds);
+        assertNotNull(adapter2);
+        assertTrue(adapter2 instanceof MySQLAdapter);
+    }
+}



Mime
View raw message