cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From torehal...@apache.org
Subject svn commit: r613170 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/merge/ test/java/org/apache/cayenne/merge/ test/resources/dml/
Date Fri, 18 Jan 2008 14:23:57 GMT
Author: torehalset
Date: Fri Jan 18 06:23:57 2008
New Revision: 613170

URL: http://svn.apache.org/viewvc?rev=613170&view=rev
Log:
CAY-885: merge changes between model and db
CAY-966: ObjRelationsship's are not removed processing DropColumnToModel

DropColumnToModel will also drop ObjAttribute mapped to that column.
TODO: ObjRelationship

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropColumnToModelTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergeCase.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/dml/merge.MergeCase.xml
Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/dml/merge.MergerFactoryTest.xml
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DbMerger.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergerFactoryTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DbMerger.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DbMerger.java?rev=613170&r1=613169&r2=613170&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DbMerger.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DbMerger.java
Fri Jan 18 06:23:57 2008
@@ -110,8 +110,9 @@
                 }
                 dbEntityToDropByName.remove(detectedEntity.getName());
 
+                checkRelationshipsToDrop(adapter, tokens, dbEntity, detectedEntity);
                 checkRows(tokens, dbEntity, detectedEntity);
-                checkRelationships(adapter, tokens, dbEntity, detectedEntity);
+                checkRelationshipsToAdd(adapter, tokens, dbEntity, detectedEntity);
             }
 
             // drop table
@@ -202,7 +203,7 @@
         }
     }
 
-    private void checkRelationships(
+    private void checkRelationshipsToDrop(
             DbAdapter adapter,
             List<MergerToken> tokens,
             DbEntity dbEntity,
@@ -247,7 +248,14 @@
                 tokens.add(token);
             }
         }
+    }
 
+    private void checkRelationshipsToAdd(
+            DbAdapter adapter,
+            List<MergerToken> tokens,
+            DbEntity dbEntity,
+            DbEntity detectedEntity) {
+        
         // relationships to add
         for (DbRelationship rel : dbEntity.getRelationships()) {
             if (findDbRelationship(detectedEntity, rel) == null) {
@@ -260,7 +268,7 @@
             }
         }
     }
-
+    
     /**
      * case insensitive search for a {@link DbEntity} in a {@link DataMap} by name
      */

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java?rev=613170&r1=613169&r2=613170&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java
Fri Jan 18 06:23:57 2008
@@ -20,9 +20,11 @@
 
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
 
 /**
- * A {@link MergerToken} to remove a {@link DbAttribute} from a {@link DbEntity}
+ * A {@link MergerToken} to remove a {@link DbAttribute} from a {@link DbEntity}.
  * 
  * @author halset
  */
@@ -41,6 +43,17 @@
     }
 
     public void execute(MergerContext mergerContext) {
+        // remove ObjAttribute mapped to same column
+        for (ObjEntity objEntity : entity.getDataMap().getObjEntities()) {
+            if (objEntity.getDbEntity().equals(entity)) {
+                ObjAttribute objAttribute = objEntity.getAttributeForDbAttribute(column);
+                if (objAttribute != null) {
+                    objEntity.removeAttribute(objAttribute.getName());
+                }
+            }
+        }
+
+        // remove DbAttribute
         entity.removeAttribute(column.getName());
     }
 

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropColumnToModelTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropColumnToModelTest.java?rev=613170&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropColumnToModelTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropColumnToModelTest.java
Fri Jan 18 06:23:57 2008
@@ -0,0 +1,211 @@
+/*****************************************************************
+ *   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.merge;
+
+import java.sql.Types;
+import java.util.List;
+
+import org.apache.cayenne.access.DataContext;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbJoin;
+import org.apache.cayenne.map.DbRelationship;
+import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
+
+public class DropColumnToModelTest extends MergeCase {
+
+    public void XXtestSimpleColumn() throws Exception {
+        dropTableIfPresent(node, "NEW_TABLE");
+
+        assertTokensAndExecute(node, map, 0, 0);
+
+        DbEntity dbEntity = new DbEntity("NEW_TABLE");
+
+        DbAttribute column1 = new DbAttribute("ID", Types.INTEGER, dbEntity);
+        column1.setMandatory(true);
+        column1.setPrimaryKey(true);
+        dbEntity.addAttribute(column1);
+
+        DbAttribute column2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity);
+        column2.setMaxLength(10);
+        column2.setMandatory(false);
+        dbEntity.addAttribute(column2);
+
+        map.addDbEntity(dbEntity);
+
+        assertTokensAndExecute(node, map, 1, 0);
+        assertTokensAndExecute(node, map, 0, 0);
+
+        ObjEntity objEntity = new ObjEntity("NewTable");
+        objEntity.setDbEntity(dbEntity);
+        ObjAttribute oatr1 = new ObjAttribute("name");
+        oatr1.setDbAttributePath(column2.getName());
+        oatr1.setType("java.lang.String");
+        objEntity.addAttribute(oatr1);
+        map.addObjEntity(objEntity);
+
+        // force drop name column in db
+        MergerToken token = mergerFactory().createDropColumToDb(dbEntity, column2);
+        execute(token);
+
+        List<MergerToken> tokens = createMergeTokens();
+        assertEquals(1, tokens.size());
+        token = tokens.get(0);
+        if (token.getDirection().isToDb()) {
+            token = token.createReverse(mergerFactory());
+        }
+        assertTrue(token instanceof DropColumnToModel);
+        execute(token);
+        assertNull(dbEntity.getAttribute(column2.getName()));
+        assertNull(objEntity.getAttribute(oatr1.getName()));
+
+        DataContext ctxt = createDataContext();
+
+        // clear up
+        map.removeObjEntity(objEntity.getName(), true);
+        map.removeDbEntity(dbEntity.getName(), true);
+        ctxt.getEntityResolver().clearCache();
+        assertNull(map.getObjEntity(objEntity.getName()));
+        assertNull(map.getDbEntity(dbEntity.getName()));
+        assertFalse(map.getDbEntities().contains(dbEntity));
+
+        assertTokensAndExecute(node, map, 1, 0);
+        assertTokensAndExecute(node, map, 0, 0);
+    }
+
+    public void testForreignKey() throws Exception {
+        dropTableIfPresent(node, "NEW_TABLE");
+        dropTableIfPresent(node, "NEW_TABLE2");
+
+        assertTokensAndExecute(node, map, 0, 0);
+
+        DbEntity dbEntity1 = new DbEntity("NEW_TABLE");
+
+        DbAttribute e1col1 = new DbAttribute("ID", Types.INTEGER, dbEntity1);
+        e1col1.setMandatory(true);
+        e1col1.setPrimaryKey(true);
+        dbEntity1.addAttribute(e1col1);
+
+        DbAttribute e1col2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity1);
+        e1col2.setMaxLength(10);
+        e1col2.setMandatory(false);
+        dbEntity1.addAttribute(e1col2);
+
+        map.addDbEntity(dbEntity1);
+
+        DbEntity dbEntity2 = new DbEntity("NEW_TABLE2");
+        DbAttribute e2col1 = new DbAttribute("ID", Types.INTEGER, dbEntity2);
+        e2col1.setMandatory(true);
+        e2col1.setPrimaryKey(true);
+        dbEntity2.addAttribute(e2col1);
+        DbAttribute e2col2 = new DbAttribute("FK", Types.INTEGER, dbEntity2);
+        dbEntity2.addAttribute(e2col2);
+        DbAttribute e2col3 = new DbAttribute("NAME", Types.VARCHAR, dbEntity2);
+        e2col3.setMaxLength(10);
+        dbEntity2.addAttribute(e2col3);
+
+        map.addDbEntity(dbEntity2);
+
+        // create db relationships
+        DbRelationship rel1To2 = new DbRelationship("rel1To2");
+        rel1To2.setSourceEntity(dbEntity1);
+        rel1To2.setTargetEntity(dbEntity2);
+        rel1To2.setToMany(true);
+        rel1To2.addJoin(new DbJoin(rel1To2, e1col1.getName(), e2col2.getName()));
+        dbEntity1.addRelationship(rel1To2);
+        DbRelationship rel2To1 = new DbRelationship("rel2To1");
+        rel2To1.setSourceEntity(dbEntity2);
+        rel2To1.setTargetEntity(dbEntity1);
+        rel2To1.setToMany(false);
+        rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
+        dbEntity2.addRelationship(rel2To1);
+
+        assertTokensAndExecute(node, map, 4, 0);
+        assertTokensAndExecute(node, map, 0, 0);
+
+        ObjEntity objEntity1 = new ObjEntity("NewTable");
+        objEntity1.setDbEntity(dbEntity1);
+        ObjAttribute oatr1 = new ObjAttribute("name");
+        oatr1.setDbAttributePath(e1col2.getName());
+        oatr1.setType("java.lang.String");
+        objEntity1.addAttribute(oatr1);
+        map.addObjEntity(objEntity1);
+
+        ObjEntity objEntity2 = new ObjEntity("NewTable2");
+        objEntity2.setDbEntity(dbEntity1);
+        ObjAttribute o2a1 = new ObjAttribute("name");
+        o2a1.setDbAttributePath(e1col2.getName());
+        o2a1.setType("java.lang.String");
+        objEntity2.addAttribute(o2a1);
+        map.addObjEntity(objEntity1);
+
+        // remove relationship and fk from model, merge to db and add to model
+        dbEntity2.removeRelationship(rel2To1.getName());
+        dbEntity1.removeRelationship(rel1To2.getName());
+        dbEntity2.removeAttribute(e2col2.getName());
+        List<MergerToken> tokens = createMergeTokens();
+        assertTokens(tokens, 2, 1);
+        for (MergerToken token : tokens) {
+            if (token.getDirection().isToDb()) {
+                execute(token);
+            }
+        }
+        assertTokensAndExecute(0, 0);
+        dbEntity2.addRelationship(rel2To1);
+        dbEntity1.addRelationship(rel1To2);
+        dbEntity2.addAttribute(e2col2);
+        
+        // try do use the merger to remove the relationship in the model
+        tokens = createMergeTokens();
+        assertTokens(tokens, 2, 0);
+        // TODO: reversing the following two tokens should also reverse the order
+        MergerToken token0 = tokens.get(0).createReverse(mergerFactory());
+        MergerToken token1 = tokens.get(1).createReverse(mergerFactory());
+        assertTrue(token0 instanceof DropColumnToModel);
+        assertTrue(token1 instanceof DropRelationshipToModel);
+        execute(token1);
+        execute(token0);
+        assertNull(dbEntity2.getAttribute(e2col2.getName()));
+        // TODO: assertTrue(dbEntity1.getRelationships().isEmpty());
+        assertTrue(dbEntity2.getRelationships().isEmpty());
+        // assertNull(objEntity.getAttribute(oatr1.getName()));
+        // TODO: test that relationship is gone
+        
+
+        // clear up
+        DataContext ctxt = createDataContext();
+        dbEntity1.removeRelationship(rel1To2.getName());
+        dbEntity2.removeRelationship(rel2To1.getName());
+        map.removeObjEntity(objEntity1.getName(), true);
+        map.removeDbEntity(dbEntity1.getName(), true);
+        map.removeObjEntity(objEntity2.getName(), true);
+        map.removeDbEntity(dbEntity2.getName(), true);
+        ctxt.getEntityResolver().clearCache();
+        assertNull(map.getObjEntity(objEntity1.getName()));
+        assertNull(map.getDbEntity(dbEntity1.getName()));
+        assertNull(map.getObjEntity(objEntity2.getName()));
+        assertNull(map.getDbEntity(dbEntity2.getName()));
+        assertFalse(map.getDbEntities().contains(dbEntity1));
+        assertFalse(map.getDbEntities().contains(dbEntity2));
+
+        assertTokensAndExecute(2, 0);
+        assertTokensAndExecute(0, 0);
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergeCase.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergeCase.java?rev=613170&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergeCase.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergeCase.java
Fri Jan 18 06:23:57 2008
@@ -0,0 +1,242 @@
+/*****************************************************************
+ *   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.merge;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.cayenne.access.DataNode;
+import org.apache.cayenne.access.QueryLogger;
+import org.apache.cayenne.dba.DbAdapter;
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.unit.CayenneCase;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class MergeCase extends CayenneCase {
+
+    protected DataNode node;
+    protected DataMap map;
+
+    private final Log logObj = LogFactory.getLog(getClass());
+
+    private static List<String> tableNames = Arrays.asList(
+            "ARTIST",
+            "PAINTING",
+            "NEW_TABLE",
+            "NEW_TABLE2");
+
+    public DbMerger createMerger() {
+        return new DbMerger() {
+
+            @Override
+            public boolean includeTableName(String tableName) {
+                return tableNames.contains(tableName.toUpperCase());
+            }
+        };
+    }
+    
+    public List<MergerToken> createMergeTokens() {
+        return createMerger().createMergeTokens(node, map);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        // getAccessStack().dropSchema();
+        // getAccessStack().dropPKSupport();
+
+        deleteTestData();
+        createTestData("testArtists");
+        node = getDomain().getDataNodes().iterator().next();
+        map = getDomain().getMap("testmap");
+
+        filterDataMap(node, map);
+
+        List<MergerToken> tokens = createMergeTokens();
+        execute(tokens);
+
+        assertTokensAndExecute(0, 0);
+    }
+
+    /**
+     * Remote binary pk {@link DbEntity} for {@link DbAdapter} not supporting that and so
+     * on.
+     */
+    private void filterDataMap(DataNode node, DataMap map) {
+        // copied from AbstractAccessStack.dbEntitiesInInsertOrder
+        boolean excludeLOB = !getAccessStackAdapter().supportsLobs();
+        boolean excludeBinPK = !getAccessStackAdapter().supportsBinaryPK();
+
+        if (!(excludeLOB || excludeBinPK)) {
+            return;
+        }
+
+        List<DbEntity> entitiesToRemove = new ArrayList<DbEntity>();
+
+        for (DbEntity ent : map.getDbEntities()) {
+
+            if (excludeBinPK) {
+                for (DbAttribute attr : ent.getAttributes()) {
+                    // check for BIN PK or FK to BIN Pk
+                    if (attr.getType() == Types.BINARY
+                            || attr.getType() == Types.VARBINARY
+                            || attr.getType() == Types.LONGVARBINARY) {
+
+                        if (attr.isPrimaryKey() || attr.isForeignKey()) {
+                            entitiesToRemove.add(ent);
+                            break;
+                        }
+                    }
+                }
+
+            }
+        }
+
+        for (DbEntity e : entitiesToRemove) {
+            logObj.info("filter away " + e.getName());
+            map.removeDbEntity(e.getName(), true);
+        }
+
+    }
+
+    protected void execute(List<MergerToken> tokens) throws Exception {
+        MergerContext mergerContext = new ExecutingMergerContext(map, node);
+        for (MergerToken tok : tokens) {
+            tok.execute(mergerContext);
+        }
+    }
+
+    protected void execute(MergerToken token) throws Exception {
+        MergerContext mergerContext = new ExecutingMergerContext(map, node);
+        token.execute(mergerContext);
+    }
+
+    protected void executeSql(String sql) throws Exception {
+        Connection conn = null;
+        Statement st = null;
+        try {
+            QueryLogger.log(sql);
+            conn = getConnection();
+            st = conn.createStatement();
+            st.execute(sql);
+        }
+        catch (SQLException e) {
+            QueryLogger.logQueryError(e);
+            throw e;
+        }
+        finally {
+            if (st != null) {
+                st.close();
+            }
+            if (conn != null) {
+                conn.close();
+            }
+        }
+    }
+
+    protected void assertTokens(
+            List<MergerToken> tokens,
+            int expectedToDb,
+            int expectedToModel) {
+        int actualToDb = 0;
+        int actualToModel = 0;
+        for (MergerToken token : tokens) {
+            if (token.getDirection().isToDb()) {
+                actualToDb++;
+            }
+            else if (token.getDirection().isToModel()) {
+                actualToModel++;
+            }
+        }
+        logTokens(tokens);
+        assertEquals("tokens to db", expectedToDb, actualToDb);
+        assertEquals("tokens to model", expectedToModel, actualToModel);
+    }
+
+    protected void assertTokensAndExecute(
+            DataNode node,
+            DataMap map,
+            int expectedToDb,
+            int expectedToModel) {
+        List<MergerToken> tokens = createMergeTokens();
+
+        assertTokens(tokens, expectedToDb, expectedToModel);
+        if (!tokens.isEmpty()) {
+            try {
+                execute(tokens);
+            }
+            catch (Exception e) {
+                fail(e.getMessage());
+            }
+        }
+    }
+
+    protected void assertTokensAndExecute(int expectedToDb, int expectedToModel) {
+        List<MergerToken> tokens = createMergeTokens();
+
+        assertTokens(tokens, expectedToDb, expectedToModel);
+        if (!tokens.isEmpty()) {
+            try {
+                execute(tokens);
+            }
+            catch (Exception e) {
+                fail(e.getMessage());
+            }
+        }
+    }
+
+    protected void logTokens(List<MergerToken> tokens) {
+        for (MergerToken token : tokens) {
+            logObj.info("token: " + token.toString());
+        }
+    }
+
+    protected MergerFactory mergerFactory() {
+        return node.getAdapter().mergerFactory();
+    }
+
+    protected void dropTableIfPresent(DataNode node, String tableName) {
+        DbEntity entity = new DbEntity(tableName);
+        AbstractToDbToken t = (AbstractToDbToken) mergerFactory().createDropTableToDb(
+                entity);
+        try {
+            for (String sql : t.createSql(node.getAdapter())) {
+                executeSql(sql);
+            }
+        }
+        catch (Exception e) {
+        }
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        // TODO: make this work deleteTestData();
+    }
+
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergerFactoryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergerFactoryTest.java?rev=613170&r1=613169&r2=613170&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergerFactoryTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergerFactoryTest.java
Fri Jan 18 06:23:57 2008
@@ -18,69 +18,18 @@
  ****************************************************************/
 package org.apache.cayenne.merge;
 
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.sql.Statement;
 import java.sql.Types;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
 
 import org.apache.cayenne.CayenneDataObject;
 import org.apache.cayenne.access.DataContext;
-import org.apache.cayenne.access.DataNode;
-import org.apache.cayenne.access.QueryLogger;
-import org.apache.cayenne.dba.DbAdapter;
-import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.DbJoin;
 import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.map.ObjAttribute;
 import org.apache.cayenne.map.ObjEntity;
-import org.apache.cayenne.unit.CayenneCase;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-public class MergerFactoryTest extends CayenneCase {
-
-    private DataNode node;
-    private DataMap map;
-
-    private List<String> tableNames = Arrays.asList("ARTIST", "PAINTING", "NEW_TABLE");
-    
-    private static final Log logObj = LogFactory.getLog(MergerFactoryTest.class);
-
-    public DbMerger createMerger() {
-        return new DbMerger() {
-
-            @Override
-            public boolean includeTableName(String tableName) {
-                return tableNames.contains(tableName.toUpperCase());
-            }
-        };
-    }
-
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-
-        // getAccessStack().dropSchema();
-        // getAccessStack().dropPKSupport();
-
-        deleteTestData();
-        createTestData("testArtists");
-        node = getDomain().getDataNodes().iterator().next();
-        map = getDomain().getMap("testmap");
-
-        filterDataMap(node, map);
-
-        DbMerger merger = createMerger();
-        List<MergerToken> tokens = merger.createMergeTokens(node, map);
-        execute(map, node, tokens);
 
-        assertTokensAndExecute(node, map, 0, 0);
-    }
+public class MergerFactoryTest extends MergeCase {
 
     public void testAddAndDropColumnToDb() throws Exception {
         DbEntity dbEntity = map.getDbEntity("PAINTING");
@@ -349,139 +298,6 @@
         
         assertTokensAndExecute(node, map, 1, 0);
         assertTokensAndExecute(node, map, 0, 0);
-    }
-
-    private void assertTokensAndExecute(
-            DataNode node,
-            DataMap map,
-            int expectedToDb,
-            int expectedToModel) throws Exception {
-        DbMerger merger = createMerger();
-        List<MergerToken> tokens = merger.createMergeTokens(node, map);
-
-        assertTokens(tokens, expectedToDb, expectedToModel);
-        if (!tokens.isEmpty()) {
-            execute(map, node, tokens);
-        }
-    }
-
-    private void assertTokens(
-            List<MergerToken> tokens,
-            int expectedToDb,
-            int expectedToModel) {
-        int actualToDb = 0;
-        int actualToModel = 0;
-        for (MergerToken token : tokens) {
-            if (token.getDirection().equals(MergeDirection.TO_DB)) {
-                actualToDb++;
-            }
-            else if (token.getDirection().equals(MergeDirection.TO_MODEL)) {
-                actualToModel++;
-            }
-        }
-        logTokens(tokens);
-        assertEquals("tokens to db", expectedToDb, actualToDb);
-        assertEquals("tokens to model", expectedToModel, actualToModel);
-    }
-
-    private void logTokens(List<MergerToken> tokens) {
-        for (MergerToken token : tokens) {
-            logObj.info("token: " + token.toString());
-        }
-    }
-
-    private void dropTableIfPresent(DataNode node, String tableName) {
-        DbEntity entity = new DbEntity(tableName);
-        AbstractToDbToken t = (AbstractToDbToken) node
-                .getAdapter()
-                .mergerFactory()
-                .createDropTableToDb(entity);
-        try {
-            for (String sql : t.createSql(node.getAdapter())) {
-                executeSql(sql);
-            }
-        }
-        catch (Exception e) {
-        }
-    }
-
-    /**
-     * Remote binary pk {@link DbEntity} for {@link DbAdapter} not supporting that and so
-     * on.
-     */
-    private void filterDataMap(DataNode node, DataMap map) {
-        // copied from AbstractAccessStack.dbEntitiesInInsertOrder
-        boolean excludeLOB = !getAccessStackAdapter().supportsLobs();
-        boolean excludeBinPK = !getAccessStackAdapter().supportsBinaryPK();
-
-        if (!(excludeLOB || excludeBinPK)) {
-            return;
-        }
-
-        List<DbEntity> entitiesToRemove = new ArrayList<DbEntity>();
-
-        for (DbEntity ent : map.getDbEntities()) {
-
-            if (excludeBinPK) {
-                for (DbAttribute attr : ent.getAttributes()) {
-                    // check for BIN PK or FK to BIN Pk
-                    if (attr.getType() == Types.BINARY
-                            || attr.getType() == Types.VARBINARY
-                            || attr.getType() == Types.LONGVARBINARY) {
-
-                        if (attr.isPrimaryKey() || attr.isForeignKey()) {
-                            entitiesToRemove.add(ent);
-                            break;
-                        }
-                    }
-                }
-
-            }
-        }
-
-        for (DbEntity e : entitiesToRemove) {
-            logObj.info("filter away " + e.getName());
-            map.removeDbEntity(e.getName(), true);
-        }
-
-    }
-
-    private void execute(DataMap map, DataNode node, List<MergerToken> tokens)
-            throws Exception {
-        MergerContext mergerContext = new ExecutingMergerContext(map, node);
-        for (MergerToken tok : tokens) {
-            tok.execute(mergerContext);
-        }
-
-    }
-
-    private void executeSql(String sql) throws Exception {
-        Connection conn = null;
-        Statement st = null;
-        try {
-            QueryLogger.log(sql);
-            conn = getConnection();
-            st = conn.createStatement();
-            st.execute(sql);
-        }
-        catch (SQLException e) {
-            QueryLogger.logQueryError(e);
-            throw e;
-        }
-        finally {
-            if (st != null) {
-                st.close();
-            }
-            if (conn != null) {
-                conn.close();
-            }
-        }
-    }
-
-    @Override
-    protected void tearDown() throws Exception {
-        super.tearDown();
-        deleteTestData();
-    }
+    } 
 
 }

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/dml/merge.MergeCase.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/dml/merge.MergeCase.xml?rev=613170&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/dml/merge.MergeCase.xml
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/dml/merge.MergeCase.xml
Fri Jan 18 06:23:57 2008
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
+
+<beans default-lazy-init="true">	
+
+	<!-- ARTIST -->
+	<bean id="A1" class="org.apache.cayenne.unit.util.UpdatingSQLTemplate">
+		<constructor-arg type="java.lang.Class"><value>org.apache.art.Artist</value></constructor-arg>
+		<constructor-arg><value>
+		insert into ARTIST (ARTIST_ID, ARTIST_NAME) values (33001, 'artist1')
+		</value></constructor-arg>
+	</bean>
+	
+	<bean id="A2" class="org.apache.cayenne.unit.util.UpdatingSQLTemplate">
+		<constructor-arg type="java.lang.Class"><value>org.apache.art.Artist</value></constructor-arg>
+		<constructor-arg><value>
+		insert into ARTIST (ARTIST_ID, ARTIST_NAME) values (33002, 'artist2')
+		</value></constructor-arg>
+	</bean>
+	
+	<!-- ======================================= -->
+	<!-- Data Sets -->
+	<!-- ======================================= -->	
+	
+	<bean id="testArtists" class="java.util.ArrayList">
+		<constructor-arg>
+			<list>
+				<ref bean="A1"/>
+				<ref bean="A2"/>
+			</list>
+		</constructor-arg>
+	</bean>
+
+
+</beans>
\ No newline at end of file



Mime
View raw message