[libpostgresql-jdbc-java] 105/128: fixed DatabaseMetaDataTest as per Sylvain Cuaz

Emmanuel Bourg ebourg-guest at moszumanska.debian.org
Mon Jan 9 10:18:36 UTC 2017


This is an automated email from the git hooks/post-receive script.

ebourg-guest pushed a commit to annotated tag REL9_3_1100
in repository libpostgresql-jdbc-java.

commit 1d75c2f089147f596bafee0f2849bdf977d8e646
Author: Dave Cramer <davecramer at gmail.com>
Date:   Tue Sep 10 09:26:33 2013 -0400

    fixed DatabaseMetaDataTest as per Sylvain Cuaz
---
 .../jdbc2/AbstractJdbc2DatabaseMetaData.java       |  4 +-
 .../test/jdbc2/DatabaseMetaDataTest.java           | 70 ++++++++++++++++++++++
 2 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java b/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java
index 675eead..547edba 100644
--- a/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java
+++ b/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java
@@ -3445,11 +3445,11 @@ public abstract class AbstractJdbc2DatabaseMetaData
 
             if (primaryTable != null)
             {
-                sql += " ORDER BY fkn.nspname,fkc.relname,pos.n";
+                sql += " ORDER BY fkn.nspname,fkc.relname,con.conname,pos.n";
             }
             else
             {
-                sql += " ORDER BY pkn.nspname,pkc.relname,pos.n";
+                sql += " ORDER BY pkn.nspname,pkc.relname, con.conname,pos.n";
             }
 
             return createMetaDataStatement().executeQuery(sql);
diff --git a/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java b/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
index 8fc0d43..543684e 100644
--- a/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
+++ b/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
@@ -10,6 +10,8 @@ package org.postgresql.test.jdbc2;
 import org.postgresql.test.TestUtil;
 import junit.framework.TestCase;
 import java.sql.*;
+import java.util.ArrayList;
+import java.util.List;
 
 /*
  * TestCase to test the internal functionality of org.postgresql.jdbc2.DatabaseMetaData
@@ -263,6 +265,74 @@ public class DatabaseMetaDataTest extends TestCase
         con1.close();
     }
 
+    public void testSameTableForeignKeys() throws Exception
+    {
+        Connection con1 = TestUtil.openDB();
+
+        TestUtil.createTable( con1, "person", "FIRST_NAME character varying(100) NOT NULL,"+
+          "LAST_NAME character varying(100) NOT NULL,"+
+          "FIRST_NAME_PARENT_1 character varying(100),"+
+          "LAST_NAME_PARENT_1 character varying(100),"+
+          "FIRST_NAME_PARENT_2 character varying(100),"+
+          "LAST_NAME_PARENT_2 character varying(100),"+
+          "CONSTRAINT PERSON_pkey PRIMARY KEY (FIRST_NAME , LAST_NAME ),"+
+          "CONSTRAINT PARENT_1_fkey FOREIGN KEY (FIRST_NAME_PARENT_1, LAST_NAME_PARENT_1)"+
+              "REFERENCES PERSON (FIRST_NAME, LAST_NAME) MATCH SIMPLE "+
+              "ON UPDATE CASCADE ON DELETE CASCADE,"+
+          "CONSTRAINT PARENT_2_fkey FOREIGN KEY (FIRST_NAME_PARENT_2, LAST_NAME_PARENT_2)"+
+              "REFERENCES PERSON (FIRST_NAME, LAST_NAME) MATCH SIMPLE "+
+              "ON UPDATE CASCADE ON DELETE CASCADE" );
+
+
+        DatabaseMetaData dbmd = con.getMetaData();
+        assertNotNull(dbmd);
+        ResultSet rs = dbmd.getImportedKeys(null, "", "person");
+        
+        final List<String> fkNames = new ArrayList<String>();
+        
+        int lastFieldCount = -1;
+        while (rs.next()) {
+            // destination table (all foreign keys point to the same)
+            String pkTableName = rs.getString("PKTABLE_NAME");
+            assertEquals("person", pkTableName);
+
+            // destination fields
+            String pkColumnName = rs.getString("PKCOLUMN_NAME");
+            assertTrue("first_name".equals(pkColumnName) || "last_name".equals(pkColumnName));
+
+            // source table (all foreign keys are in the same)
+            String fkTableName = rs.getString("FKTABLE_NAME");
+            assertEquals("person", fkTableName);
+
+            // foreign key name
+            String fkName = rs.getString("FK_NAME");
+            // sequence number within the foreign key
+            int seq = rs.getInt("KEY_SEQ");
+            if (seq == 1) {
+                // begin new foreign key
+                assertFalse(fkNames.contains(fkName));
+                fkNames.add(fkName);
+                // all foreign keys have 2 fields
+                assertTrue(lastFieldCount < 0 || lastFieldCount == 2);
+            } else {
+                // continue foreign key, i.e. fkName matches the last foreign key
+                assertEquals(fkNames.get(fkNames.size() - 1), fkName);
+                // see always increases by 1
+                assertTrue(seq == lastFieldCount + 1);
+            }
+            lastFieldCount = seq;
+        }
+        // there's more than one foreign key from a table to another
+        assertEquals(2, fkNames.size());
+
+        TestUtil.dropTable( con1, "person" );
+        TestUtil.closeDB(con1);
+        
+       
+          
+        
+    }
+
     public void testForeignKeys() throws Exception
     {
         Connection con1 = TestUtil.openDB();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git



More information about the pkg-java-commits mailing list