openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r814029 - in /openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique: TestNamedUniqueConstraint.java TestNamedUniqueConstraintWithXMLDescriptor.java
Date Fri, 11 Sep 2009 21:05:32 GMT
Author: faywang
Date: Fri Sep 11 21:05:30 2009
New Revision: 814029

URL: http://svn.apache.org/viewvc?rev=814029&view=rev
Log:
fix unique constraint syntax in the test case for MySQL

Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraint.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraintWithXMLDescriptor.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraint.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraint.java?rev=814029&r1=814028&r2=814029&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraint.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraint.java
Fri Sep 11 21:05:30 2009
@@ -18,18 +18,25 @@
  */
 package org.apache.openjpa.persistence.jdbc.unique;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.StringTokenizer;
 
 import javax.persistence.EntityManager;
 
 import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.sql.DBDictionary;
+import org.apache.openjpa.jdbc.sql.MySQLDictionary;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
 import org.apache.openjpa.persistence.jdbc.SQLSniffer;
 import org.apache.openjpa.persistence.test.SQLListenerTestCase;
 
 public class TestNamedUniqueConstraint extends SQLListenerTestCase {
+
+    DBDictionary dict;
+    
     @Override
     public void setUp(Object... props) {
         super.setUp(DROP_TABLES, NamedUniqueA.class, NamedUniqueB.class);
@@ -50,26 +57,58 @@
         List<String> sqls = super.sql;
         
         assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_A",
-            "uca_f1_f2 UNIQUE .*\\(f1, f2\\)", 
-            "uca_f3_f4 UNIQUE .*\\(f3, f4\\).*");
+            getUniqueConstraint("uca_f1_f2 UNIQUE .*\\(f1, f2\\)"), 
+            getUniqueConstraint("uca_f3_f4 UNIQUE .*\\(f3, f4\\).*"));
         assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_B",
-            "ucb_f1_f2 UNIQUE .*\\(f1, f2\\).*");
+            getUniqueConstraint("ucb_f1_f2 UNIQUE .*\\(f1, f2\\).*"));
         assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_SECONDARY",
-            "uca_sf1 UNIQUE .*\\(sf1\\)");
+            getUniqueConstraint("uca_sf1 UNIQUE .*\\(sf1\\)"));
         assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_GENERATOR",
-            "ucb_gen1_gen2 UNIQUE .*\\(GEN1, GEN2\\)");
+            getUniqueConstraint("ucb_gen1_gen2 UNIQUE .*\\(GEN1, GEN2\\)"));
         assertSQLFragnments(sqls, "CREATE TABLE N_UNIQUE_JOINTABLE",
-            "uca_fka_fkb UNIQUE .*\\(FK_A, FK_B\\)");
+            getUniqueConstraint("uca_fka_fkb UNIQUE .*\\(FK_A, FK_B\\)"));
         assertSQLFragnments(sqls, "CREATE TABLE N_U_COLL_TBL",
-            "ucb_f3 UNIQUE .*\\(f3\\).*");
+            getUniqueConstraint("ucb_f3 UNIQUE .*\\(f3\\).*"));
     }
         
     private boolean supportsUniqueConstraints() {
         OpenJPAEntityManagerFactorySPI emfs = (OpenJPAEntityManagerFactorySPI)emf;
         JDBCConfiguration jdbccfg = (JDBCConfiguration)emfs.getConfiguration();
-        return jdbccfg.getDBDictionaryInstance().supportsUniqueConstraints;
+        dict = jdbccfg.getDBDictionaryInstance();
+        return dict.supportsUniqueConstraints;
     }
 
+    private String getUniqueConstraint(String unique) {
+        if (dict instanceof MySQLDictionary) {
+            //CREATE TABLE N_UNIQUE_A (aid INTEGER NOT NULL, f1 INTEGER NOT NULL, f2 INTEGER
NOT NULL, 
+            //f3 INTEGER NOT NULL, f4 INTEGER NOT NULL, f5 INTEGER, f6 INTEGER, PRIMARY KEY
(aid), 
+            //UNIQUE U_N_UNQU__F1 (f1), 
+            //UNIQUE uca_f1_f2 (f1, f2), 
+            //UNIQUE uca_f3_f4 (f3, f4)) TYPE = innodb
+            return getUniqueConstraint(dict, unique);
+        }
+        return unique;
+    }
+    
+    public static String getUniqueConstraint(DBDictionary dict, String unique) {
+        List<String> tokens = new ArrayList<String>();
+        List<String> tokens1 = new ArrayList<String>();
+        StringTokenizer st = new StringTokenizer(unique);
+        while (st.hasMoreTokens()) {
+            tokens.add(st.nextToken());
+        }
+        tokens1.add(tokens.get(1));
+        tokens1.add(tokens.get(0));
+        for (int i = 2; i < tokens.size(); i++) {
+            tokens1.add(tokens.get(i));
+        }
+        StringBuffer buf = new StringBuffer();
+        for (String token : tokens1) {
+            buf.append(token).append(" ");
+        }
+        return buf.toString().trim();
+    }
+    
     void assertSQLFragnments(List<String> list, String... keys) {
         if (SQLSniffer.matches(list, keys))
             return;

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraintWithXMLDescriptor.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraintWithXMLDescriptor.java?rev=814029&r1=814028&r2=814029&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraintWithXMLDescriptor.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/unique/TestNamedUniqueConstraintWithXMLDescriptor.java
Fri Sep 11 21:05:30 2009
@@ -24,12 +24,16 @@
 import javax.persistence.EntityManager;
 
 import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.sql.DBDictionary;
+import org.apache.openjpa.jdbc.sql.MySQLDictionary;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
 import org.apache.openjpa.persistence.jdbc.SQLSniffer;
 import org.apache.openjpa.persistence.test.SQLListenerTestCase;
 
 public class TestNamedUniqueConstraintWithXMLDescriptor extends SQLListenerTestCase {
+    DBDictionary dict;
+
     @Override
     public void setUp(Object... props) {
         super.setUp(DROP_TABLES, NamedUniqueA.class, NamedUniqueB.class);
@@ -54,27 +58,39 @@
         List<String> sqls = super.sql;
         
         assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_A",
-            "ucxa_f1_f2 UNIQUE .*\\(f1x, f2x\\)", 
-            "ucxa_f3_f4 UNIQUE .*\\(f3x, f4x\\).*");
+            getUniqueConstraint("ucxa_f1_f2 UNIQUE .*\\(f1x, f2x\\)"), 
+            getUniqueConstraint("ucxa_f3_f4 UNIQUE .*\\(f3x, f4x\\).*"));
         assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_B",
-            "ucxb_f1_f2 UNIQUE .*\\(f1x, f2x\\).*");
+            getUniqueConstraint("ucxb_f1_f2 UNIQUE .*\\(f1x, f2x\\).*"));
         assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_SECONDARY",
-            "ucxa_sf1 UNIQUE .*\\(sf1x\\)");
+            getUniqueConstraint("ucxa_sf1 UNIQUE .*\\(sf1x\\)"));
         assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_GENERATOR",
-            "ucxb_gen1_gen2 UNIQUE .*\\(GEN1_XML, GEN2_XML\\)");
+            getUniqueConstraint("ucxb_gen1_gen2 UNIQUE .*\\(GEN1_XML, GEN2_XML\\)"));
         assertSQLFragnments(sqls, "CREATE TABLE NX_UNIQUE_JOINTABLE",
-            "ucxa_fka_fkb UNIQUE .*\\(FK_A_XML, FK_B_XML\\)");
+            getUniqueConstraint("ucxa_fka_fkb UNIQUE .*\\(FK_A_XML, FK_B_XML\\)"));
         assertSQLFragnments(sqls, "CREATE TABLE NX_U_COLL_TBL",
-            "ucxb_f3 UNIQUE .*\\(f3x\\).*");
-
+            getUniqueConstraint("ucxb_f3 UNIQUE .*\\(f3x\\).*"));
     }
         
     private boolean supportsUniqueConstraints() {
         OpenJPAEntityManagerFactorySPI emfs = (OpenJPAEntityManagerFactorySPI)emf;
         JDBCConfiguration jdbccfg = (JDBCConfiguration)emfs.getConfiguration();
-        return jdbccfg.getDBDictionaryInstance().supportsUniqueConstraints;
+        dict = jdbccfg.getDBDictionaryInstance();
+        return dict.supportsUniqueConstraints;
     }
 
+    private String getUniqueConstraint(String unique) {
+        if (dict instanceof MySQLDictionary) {
+            //CREATE TABLE N_UNIQUE_A (aid INTEGER NOT NULL, f1 INTEGER NOT NULL, f2 INTEGER
NOT NULL, 
+            //f3 INTEGER NOT NULL, f4 INTEGER NOT NULL, f5 INTEGER, f6 INTEGER, PRIMARY KEY
(aid), 
+            //UNIQUE U_N_UNQU__F1 (f1), 
+            //UNIQUE uca_f1_f2 (f1, f2), 
+            //UNIQUE uca_f3_f4 (f3, f4)) TYPE = innodb
+            return TestNamedUniqueConstraint.getUniqueConstraint(dict, unique);
+        }
+        return unique;
+    }
+    
     void assertSQLFragnments(List<String> list, String... keys) {
         if (SQLSniffer.matches(list, keys))
             return;



Mime
View raw message