cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [1/4] cayenne git commit: CAY-2021 cdbimport: detect when same FK constraint is defined twice
Date Fri, 07 Apr 2017 14:17:19 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master c56019336 -> b451beafb


CAY-2021 cdbimport: detect when same FK constraint is defined twice


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/21832ffa
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/21832ffa
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/21832ffa

Branch: refs/heads/master
Commit: 21832ffa9db0e31dee077399a8257168c43aa596
Parents: 5008dce
Author: Anton Dreka <drekoas@gmail.com>
Authored: Fri Apr 7 16:03:07 2017 +0300
Committer: Anton Dreka <drekoas@gmail.com>
Committed: Fri Apr 7 16:03:07 2017 +0300

----------------------------------------------------------------------
 .../dbsync/reverse/dbload/ExportedKey.java      |  2 +-
 .../reverse/dbload/RelationshipLoader.java      | 27 ++++--
 .../dbsync/reverse/dbload/ExportedKeyTest.java  | 89 ++++++++++++++++++++
 cayenne-server/pom.xml                          | 13 +++
 docs/doc/src/main/resources/RELEASE-NOTES.txt   |  1 +
 5 files changed, 123 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/21832ffa/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKey.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKey.java
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKey.java
index e9c90eb..5443dfc 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKey.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKey.java
@@ -132,7 +132,7 @@ public class ExportedKey implements Comparable {
         private final String column;
         private final String name;
 
-        private KeyData(String catalog, String schema, String table, String column, String
name) {
+        KeyData(String catalog, String schema, String table, String column, String name)
{
             this.catalog = catalog;
             this.schema = schema;
             this.table = table;

http://git-wip-us.apache.org/repos/asf/cayenne/blob/21832ffa/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
index 52de8a6..65260b1 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
@@ -26,10 +26,7 @@ import java.util.Set;
 
 import org.apache.cayenne.dbsync.naming.NameBuilder;
 import org.apache.cayenne.dbsync.naming.ObjectNameGenerator;
-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.*;
 import org.apache.cayenne.util.EqualsBuilder;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -66,7 +63,7 @@ public class RelationshipLoader extends AbstractLoader {
             ExportedKey.KeyData FK = key.getFk();
             DbEntity pkEntity = map.getDbEntity(PK.getTable());
             DbEntity fkEntity = map.getDbEntity(FK.getTable());
-            if(pkEntity == null || fkEntity == null) {
+            if (pkEntity == null || fkEntity == null) {
                 // Check for existence of this entities were made in creation of ExportedKey
                 throw new IllegalStateException();
             }
@@ -143,6 +140,7 @@ public class RelationshipLoader extends AbstractLoader {
 
     private void createAndAppendJoins(Set<ExportedKey> exportedKeys, DbEntity pkEntity,
DbEntity fkEntity,
                                       DbRelationship forwardRelationship, DbRelationship
reverseRelationship) {
+
         for (ExportedKey exportedKey : exportedKeys) {
             // Create and append joins
             String pkName = exportedKey.getPk().getColumn();
@@ -161,8 +159,21 @@ public class RelationshipLoader extends AbstractLoader {
                 continue;
             }
 
-            forwardRelationship.addJoin(new DbJoin(forwardRelationship, pkName, fkName));
-            reverseRelationship.addJoin(new DbJoin(reverseRelationship, fkName, pkName));
+
+            addJoin(forwardRelationship, pkName, fkName);
+            addJoin(reverseRelationship, fkName, pkName);
+
         }
     }
-}
+
+    private void addJoin(DbRelationship relationship, String sourceName, String targetName){
+
+        for (DbJoin join : relationship.getJoins()) {
+            if (join.getSourceName().equals(sourceName) && join.getTargetName().equals(targetName))
{
+                return;
+            }
+        }
+
+        relationship.addJoin(new DbJoin(relationship, sourceName, targetName));
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cayenne/blob/21832ffa/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyTest.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyTest.java
b/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyTest.java
new file mode 100644
index 0000000..4bb41f1
--- /dev/null
+++ b/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.dbsync.reverse.dbload;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * @since 4.0
+ */
+public class ExportedKeyTest {
+
+    @Test
+    public void testEqualsKeyData() throws SQLException {
+
+        ExportedKey.KeyData keyData1 = new ExportedKey.KeyData("Catalog", null, "Table",
"Column", "Name");
+        ExportedKey.KeyData keyData2 = new ExportedKey.KeyData("Catalog", null, "Table",
"Column", "Name");
+
+        Assert.assertTrue(keyData1.equals(keyData2));
+        Assert.assertTrue(keyData2.equals(keyData1));
+
+        Assert.assertEquals(keyData1.hashCode(), keyData2.hashCode());
+    }
+
+    @Test
+    public void testEqualsExportedKey() throws SQLException {
+        ResultSet rs1 = mock(ResultSet.class);
+        when(rs1.getString("PKTABLE_CAT")).thenReturn("PKCatalog");
+        when(rs1.getString("PKTABLE_SCHEM")).thenReturn(null);
+        when(rs1.getString("PKTABLE_NAME")).thenReturn("PKTable");
+        when(rs1.getString("PKCOLUMN_NAME")).thenReturn("PKColumn");
+        when(rs1.getString("PK_NAME")).thenReturn("PKName");
+
+        when(rs1.getString("FKTABLE_CAT")).thenReturn("FKCatalog");
+        when(rs1.getString("FKTABLE_SCHEM")).thenReturn("FKSchema");
+        when(rs1.getString("FKTABLE_NAME")).thenReturn("FKTable");
+        when(rs1.getString("FKCOLUMN_NAME")).thenReturn("FKColumn");
+        when(rs1.getString("FK_NAME")).thenReturn("FKName");
+
+        when(rs1.getShort("KEY_SEQ")).thenReturn((short) 1);
+
+        ExportedKey keyData1 = new ExportedKey(rs1);
+
+        ResultSet rs2 = mock(ResultSet.class);
+        when(rs2.getString("PKTABLE_CAT")).thenReturn("PKCatalog");
+        when(rs2.getString("PKTABLE_SCHEM")).thenReturn(null);
+        when(rs2.getString("PKTABLE_NAME")).thenReturn("PKTable");
+        when(rs2.getString("PKCOLUMN_NAME")).thenReturn("PKColumn");
+        when(rs2.getString("PK_NAME")).thenReturn("PKName");
+
+        when(rs2.getString("FKTABLE_CAT")).thenReturn("FKCatalog");
+        when(rs2.getString("FKTABLE_SCHEM")).thenReturn("FKSchema");
+        when(rs2.getString("FKTABLE_NAME")).thenReturn("FKTable");
+        when(rs2.getString("FKCOLUMN_NAME")).thenReturn("FKColumn");
+        when(rs2.getString("FK_NAME")).thenReturn("FKName");
+
+        when(rs2.getShort("KEY_SEQ")).thenReturn((short)1);
+
+        ExportedKey keyData2 = new ExportedKey(rs2);
+
+        Assert.assertTrue(keyData1.equals(keyData2));
+        Assert.assertTrue(keyData2.equals(keyData1));
+
+        Assert.assertEquals(keyData1.hashCode(), keyData2.hashCode());
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/21832ffa/cayenne-server/pom.xml
----------------------------------------------------------------------
diff --git a/cayenne-server/pom.xml b/cayenne-server/pom.xml
index 0622a58..5ce6f22 100644
--- a/cayenne-server/pom.xml
+++ b/cayenne-server/pom.xml
@@ -307,6 +307,19 @@
                             </execution>
                         </executions>
                     </plugin>
+					<plugin>
+						<groupId>org.apache.maven.plugins</groupId>
+						<artifactId>maven-failsafe-plugin</artifactId>
+						<version>2.17</version>
+						<executions>
+							<execution>
+								<goals>
+									<goal>integration-test</goal>
+									<goal>verify</goal>
+								</goals>
+							</execution>
+						</executions>
+					</plugin>
                 </plugins>
             </build>
         </profile>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/21832ffa/docs/doc/src/main/resources/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/docs/doc/src/main/resources/RELEASE-NOTES.txt b/docs/doc/src/main/resources/RELEASE-NOTES.txt
index c5b1c56..61055f7 100644
--- a/docs/doc/src/main/resources/RELEASE-NOTES.txt
+++ b/docs/doc/src/main/resources/RELEASE-NOTES.txt
@@ -30,6 +30,7 @@ CAY-2272 ColumnSelect: methods to manually control DISTINCT clause
 
 Bug Fixes:
 
+CAY-2021 cdbimport: detect when same FK constraint is defined twice
 CAY-2077 Bug in CayenneRuntimeException using wrong specified string in Formatter
 CAY-2094 SelectById query doesn't work from ROP client
 CAY-2240 Modeler: issue with cursor rendering for EJBQL query


Mime
View raw message