cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmen...@apache.org
Subject svn commit: r770804 - in /cayenne/main/trunk/framework: cayenne-tools/src/main/java/org/apache/cayenne/access/ cayenne-tools/src/test/java/org/apache/cayenne/access/ maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/
Date Fri, 01 May 2009 20:05:02 GMT
Author: kmenard
Date: Fri May  1 20:05:02 2009
New Revision: 770804

URL: http://svn.apache.org/viewvc?rev=770804&view=rev
Log:
Refactoring common DbLoaderDelegate code.

Added:
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/access/
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/access/AbstractDbLoaderDelegate.java
    cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/access/
    cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/access/AbstractDbLoaderDelegateTest.java
Modified:
    cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java

Added: cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/access/AbstractDbLoaderDelegate.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/access/AbstractDbLoaderDelegate.java?rev=770804&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/access/AbstractDbLoaderDelegate.java
(added)
+++ cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/access/AbstractDbLoaderDelegate.java
Fri May  1 20:05:02 2009
@@ -0,0 +1,57 @@
+package org.apache.cayenne.access;
+
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.CayenneException;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Collections;
+
+public abstract class AbstractDbLoaderDelegate implements DbLoaderDelegate {
+
+    private List<DbEntity> addedDbEntities = new ArrayList<DbEntity>();
+    private List<DbEntity> removedDbEntities = new ArrayList<DbEntity>();
+    private List<ObjEntity> addedObjEntities = new ArrayList<ObjEntity>();
+    private List<ObjEntity> removedObjEntities = new ArrayList<ObjEntity>();
+
+    public boolean overwriteDbEntity(final DbEntity ent) throws CayenneException {
+        return false;
+    }
+
+    public void dbEntityAdded(final DbEntity ent) {
+        ent.getDataMap().addDbEntity(ent);
+        addedDbEntities.add(ent);
+    }
+
+    public void dbEntityRemoved(final DbEntity ent) {
+        ent.getDataMap().removeDbEntity(ent.getName());
+        removedDbEntities.add(ent);
+    }
+
+    public void objEntityAdded(final ObjEntity ent) {
+        ent.getDataMap().addObjEntity(ent);
+        addedObjEntities.add(ent);
+    }
+
+    public void objEntityRemoved(final ObjEntity ent) {
+        ent.getDataMap().removeObjEntity(ent.getName());
+        removedObjEntities.add(ent);
+    }
+
+    public List<DbEntity> getAddedDbEntities() {
+        return Collections.unmodifiableList(addedDbEntities);
+    }
+
+    public List<DbEntity> getRemovedDbEntities() {
+        return Collections.unmodifiableList(removedDbEntities);
+    }
+
+    public List<ObjEntity> getAddedObjEntities() {
+        return Collections.unmodifiableList(addedObjEntities);
+    }
+
+    public List<ObjEntity> getRemovedObjEntities() {
+        return Collections.unmodifiableList(removedObjEntities);
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/access/AbstractDbLoaderDelegateTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/access/AbstractDbLoaderDelegateTest.java?rev=770804&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/access/AbstractDbLoaderDelegateTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/access/AbstractDbLoaderDelegateTest.java
Fri May  1 20:05:02 2009
@@ -0,0 +1,82 @@
+package org.apache.cayenne.access;
+
+import junit.framework.TestCase;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.CayenneException;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.ArrayList;
+
+public class AbstractDbLoaderDelegateTest extends TestCase {
+
+    final class TestDbLoaderDelegate extends AbstractDbLoaderDelegate {}
+
+    private AbstractDbLoaderDelegate delegate;
+    private DataMap dataMap;
+    private DbEntity dbEntity;
+    private ObjEntity objEntity;
+
+    public void setUp() {
+        delegate = new TestDbLoaderDelegate();
+        dataMap = new DataMap();
+
+        dbEntity = new DbEntity("TestDbEntity");
+        dbEntity.setDataMap(dataMap);
+        
+        objEntity = new ObjEntity("TestObjEntity");
+        objEntity.setDataMap(dataMap);
+    }
+
+    public void testOverwriteDbEntity() throws CayenneException {
+        assertFalse(delegate.overwriteDbEntity(dbEntity));
+    }
+
+    public void testDbEntityAdded() {
+        delegate.dbEntityAdded(dbEntity);
+
+        final List<DbEntity> entities = Arrays.asList(dbEntity);
+
+        assertEquals(1, dataMap.getDbEntities().size());
+        assertTrue(dataMap.getDbEntities().containsAll(entities));
+        
+        assertEquals(entities, delegate.getAddedDbEntities());
+    }
+
+    public void testDbEntityRemoved() {
+        // Make sure the entity is in the datamap to start.
+        dataMap.addDbEntity(dbEntity);
+
+        delegate.dbEntityRemoved(dbEntity);
+
+        // The entity should no longer be in the map.
+        assertEquals(0, dataMap.getDbEntities().size());
+
+        assertEquals(Arrays.asList(dbEntity), delegate.getRemovedDbEntities());
+    }
+
+    public void testObjEntityAdded() {
+        delegate.objEntityAdded(objEntity);
+
+        final List<ObjEntity> entities = Arrays.asList(objEntity);
+
+        assertEquals(1, dataMap.getObjEntities().size());
+        assertTrue(dataMap.getObjEntities().containsAll(entities));
+
+        assertEquals(entities, delegate.getAddedObjEntities());
+    }
+
+    public void testObjEntityRemoved() {
+        // Make sure the entity is in the datamap to start.
+        dataMap.addObjEntity(objEntity);
+
+        delegate.objEntityRemoved(objEntity);
+
+        // The entity should no longer be in the map.
+        assertEquals(0, dataMap.getObjEntities().size());
+
+        assertEquals(Arrays.asList(objEntity), delegate.getRemovedObjEntities());
+    }
+}

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=770804&r1=770803&r2=770804&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 May  1 20:05:02 2009
@@ -10,6 +10,7 @@
 import org.apache.cayenne.map.naming.NamingStrategy;
 import org.apache.cayenne.access.DbLoader;
 import org.apache.cayenne.access.DbLoaderDelegate;
+import org.apache.cayenne.access.AbstractDbLoaderDelegate;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.dba.JdbcAdapter;
 import org.apache.cayenne.util.Util;
@@ -157,8 +158,6 @@
      */
     private File mapFile;
 
-    private List<ObjEntity> addedObjEntities = new ArrayList<ObjEntity>();
-
     public void execute() throws MojoExecutionException, MojoFailureException {
 
         logger = new MavenLogger(this);
@@ -176,7 +175,8 @@
             DriverDataSource dataSource = new DriverDataSource((Driver) Class.forName(driver).newInstance(),
url, username, password);
 
             // Load the data map and run the db importer.
-            final DbLoader loader = new DbLoader(dataSource.getConnection(), adapterInst,
new LoaderDelegate());
+            final LoaderDelegate loaderDelegate = new LoaderDelegate();
+            final DbLoader loader = new DbLoader(dataSource.getConnection(), adapterInst,
loaderDelegate);
             loader.setCreatingMeaningfulPK(meaningfulPk);
 
             if (namingStrategy != null) {
@@ -188,7 +188,7 @@
             final DataMap dataMap = mapFile.exists() ? loadDataMap() : new DataMap();
             loader.loadDataMapFromDB(schemaName, tablePattern, dataMap);
 
-            for (ObjEntity addedObjEntity : addedObjEntities) {
+            for (ObjEntity addedObjEntity : loaderDelegate.getAddedObjEntities()) {
                     DeleteRuleUpdater.updateObjEntity(addedObjEntity);
                 }
 
@@ -215,31 +215,30 @@
         }
     }
 
-    final class LoaderDelegate implements DbLoaderDelegate {
-
+    final class LoaderDelegate extends AbstractDbLoaderDelegate {
+       
         public boolean overwriteDbEntity(final DbEntity ent) throws CayenneException {
             return overwriteExisting;
         }
 
         public void dbEntityAdded(final DbEntity ent) {
+            super.dbEntityAdded(ent);
             logger.info("Added DB entity: " + ent.getName());
-            ent.getDataMap().addDbEntity(ent);
         }
 
         public void dbEntityRemoved(final DbEntity ent) {
+            super.dbEntityRemoved(ent);
             logger.info("Removed DB entity: " + ent.getName());
-            ent.getDataMap().removeDbEntity(ent.getName());
         }
 
         public void objEntityAdded(final ObjEntity ent) {
+            super.objEntityAdded(ent);
             logger.info("Added obj entity: " + ent.getName());
-            addedObjEntities.add(ent);
-            ent.getDataMap().addObjEntity(ent);
         }
 
         public void objEntityRemoved(final ObjEntity ent) {
+            super.objEntityRemoved(ent);
             logger.info("Removed obj entity: " + ent.getName());
-            ent.getDataMap().removeObjEntity(ent.getName());
         }
     }
 



Mime
View raw message