You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ma...@apache.org on 2013/08/06 01:15:33 UTC

svn commit: r1510801 - in /db/derby/code/branches/10.9: ./ java/engine/org/apache/derby/iapi/types/WorkHorseForCollatorDatatypes.java java/testing/org/apache/derbyTesting/functionTests/tests/lang/CollationTest.java

Author: mamta
Date: Mon Aug  5 23:15:32 2013
New Revision: 1510801

URL: http://svn.apache.org/r1510801
Log:
Backporting revision 1430735 to 10.9

Backporting DERBY-6030(Length of escape string in LIKE ... ESCAPE not properly checked with territory based collation) to 10.9



Modified:
    db/derby/code/branches/10.9/   (props changed)
    db/derby/code/branches/10.9/java/engine/org/apache/derby/iapi/types/WorkHorseForCollatorDatatypes.java
    db/derby/code/branches/10.9/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CollationTest.java

Propchange: db/derby/code/branches/10.9/
------------------------------------------------------------------------------
  Merged /db/derby/code/trunk:r1430735,1431259

Modified: db/derby/code/branches/10.9/java/engine/org/apache/derby/iapi/types/WorkHorseForCollatorDatatypes.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.9/java/engine/org/apache/derby/iapi/types/WorkHorseForCollatorDatatypes.java?rev=1510801&r1=1510800&r2=1510801&view=diff
==============================================================================
--- db/derby/code/branches/10.9/java/engine/org/apache/derby/iapi/types/WorkHorseForCollatorDatatypes.java (original)
+++ db/derby/code/branches/10.9/java/engine/org/apache/derby/iapi/types/WorkHorseForCollatorDatatypes.java Mon Aug  5 23:15:32 2013
@@ -153,7 +153,8 @@ final class WorkHorseForCollatorDatatype
 
 		CollationElementsInterface escapeCharacter = (CollationElementsInterface) escape;
 
-		if (!escapeCharacter.hasSingleCollationElement())
+        if (escape.getLength() != 1 ||
+                !escapeCharacter.hasSingleCollationElement())
 		{
 			throw StandardException.newException(SQLState.LANG_INVALID_ESCAPE_CHARACTER,
 					escapeCharacter.toString());

Modified: db/derby/code/branches/10.9/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CollationTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.9/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CollationTest.java?rev=1510801&r1=1510800&r2=1510801&view=diff
==============================================================================
--- db/derby/code/branches/10.9/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CollationTest.java (original)
+++ db/derby/code/branches/10.9/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CollationTest.java Mon Aug  5 23:15:32 2013
@@ -23,16 +23,13 @@ package org.apache.derbyTesting.function
 
 import java.sql.Clob;
 import java.sql.Connection;
-import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
-import java.sql.Types;
 
 import java.text.Collator;
 import java.util.Locale;
-import javax.sql.DataSource;
 
 import junit.framework.Test;
 import junit.framework.TestCase;
@@ -42,15 +39,11 @@ import org.apache.derby.iapi.types.Harmo
 
 import org.apache.derbyTesting.functionTests.tests.jdbcapi.BatchUpdateTest;
 import org.apache.derbyTesting.functionTests.tests.jdbcapi.DatabaseMetaDataTest;
-import org.apache.derbyTesting.functionTests.tests.nist.NistScripts;
 import org.apache.derbyTesting.junit.XML;
-//import org.apache.derby.iapi.types.XML;
 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
 import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
 import org.apache.derbyTesting.junit.Decorator;
 import org.apache.derbyTesting.junit.JDBC;
-import org.apache.derbyTesting.junit.JDBCDataSource;
-import org.apache.derbyTesting.junit.TestConfiguration;
 import org.apache.derbyTesting.functionTests.util.TestUtil;
 
 public class CollationTest extends BaseJDBCTestCase {
@@ -70,7 +63,45 @@ public class CollationTest extends BaseJ
             "\u015Amith",
             "aacorn",
     };
-    
+
+    /** Test cases to run with English case-sensitive collation. */
+    private final static String[] ENGLISH_CASE_SENSITIVE = {
+        "testEnglishCollation",
+        "testUsingClauseAndNaturalJoin",
+        "testNullColumnInInsert",
+        "test_5951",
+    };
+
+    /** Test cases to run with English case-insensitive collation. */
+    private final static String[] ENGLISH_CASE_INSENSITIVE = {
+        "testUsingClauseAndNaturalJoin",
+        "testNullColumnInInsert",
+    };
+
+    /** Test cases to run with Norwegian case-sensitive collation. */
+    private final static String[] NORWEGIAN_CASE_SENSITIVE = {
+        "testNorwayCollation",
+        "testLikeEscapeClauseLengthRestriction",
+    };
+
+    /** Test cases to run with Polish case-sensitive collation. */
+    private final static String[] POLISH_CASE_SENSITIVE = {
+        "testPolishCollation",
+    };
+
+    /** Test cases to run with French case-sensitive collation. */
+    private final static String[] FRENCH_CASE_SENSITIVE = {
+        "testFrenchCollation",
+    };
+
+    /** Test cases to run with Swedish case-insensitive collation. */
+    private final static String[] SWEDISH_CASE_INSENSITIVE = {
+        "testSwedishCaseInsensitiveCollation",
+    };
+
+    /** SQL state that signals invalid escape sequence in LIKE expressions. */
+    private final static String INVALID_ESCAPE = "22019";
+
   /**
    * Test order by with default collation
    * 
@@ -78,8 +109,7 @@ public class CollationTest extends BaseJ
    */
 public void testDefaultCollation() throws SQLException {
 
-      Connection conn = getConnection();
-      conn.setAutoCommit(false);
+      setAutoCommit(false);
       Statement s = createStatement();
       PreparedStatement ps;
       ResultSet rs;
@@ -343,7 +373,7 @@ private void compareAgrave(String dataTy
    */
 public void testPolishCollation() throws SQLException {
 
-      getConnection().setAutoCommit(false);
+      setAutoCommit(false);
       Statement s = createStatement();
       
       setUpTable(s);
@@ -416,7 +446,7 @@ public void testPolishCollation() throws
    */
 public void testNorwayCollation() throws SQLException {
 
-      getConnection().setAutoCommit(false);
+      setAutoCommit(false);
       Statement s = createStatement();
 
       setUpTable(s);
@@ -537,7 +567,7 @@ public void testNullColumnInInsert() thr
    * @throws SQLException
   */
 public void testUsingClauseAndNaturalJoin() throws SQLException {
-      getConnection().setAutoCommit(false);
+      setAutoCommit(false);
       Statement s = createStatement();
       String collation; 
 
@@ -1169,7 +1199,7 @@ private void joinTesting(Statement s, 
   */
 public void testEnglishCollation() throws SQLException {
 
-      getConnection().setAutoCommit(false);
+      setAutoCommit(false);
       Statement s = createStatement();
       setUpTable(s);
 
@@ -1233,7 +1263,7 @@ public void testEnglishCollation() throw
   */
 public void testSwedishCaseInsensitiveCollation() throws SQLException {
 
-      getConnection().setAutoCommit(false);
+      setAutoCommit(false);
       Statement s = createStatement();
       setUpTable(s);
 
@@ -1982,12 +2012,6 @@ private void setUpTable(Statement s) thr
     ps.close();
 }
 
-private void dropTable(Statement s) throws SQLException {
-	
-    s.execute("DROP TABLE APP.CUSTOMER");     
-    s.getConnection().commit();
-}
-
 /**
  * Execute the passed statement and compare the results against the
  * expectedResult 
@@ -2046,11 +2070,8 @@ public void testMissingCollatorSupport()
 
         suite.addTest(new CleanDatabaseTestSetup(
                 new CollationTest("testDefaultCollation")));
-        suite.addTest(collatedSuite("en", false, "testEnglishCollation"));
-        suite.addTest(collatedSuite("en", true, "testUsingClauseAndNaturalJoin"));
-        suite.addTest(collatedSuite("en", false, "testUsingClauseAndNaturalJoin"));
-        suite.addTest(collatedSuite("en", true, "testNullColumnInInsert"));
-        suite.addTest(collatedSuite("en", false, "testNullColumnInInsert"));
+        suite.addTest(collatedSuite("en", false, ENGLISH_CASE_SENSITIVE));
+        suite.addTest(collatedSuite("en", true, ENGLISH_CASE_INSENSITIVE));
          
         // Only add tests for other locales if they are in fact supported 
         // by the jvm.
@@ -2074,16 +2095,16 @@ public void testMissingCollatorSupport()
             }
         }
         if(norwegian) {
-            suite.addTest(collatedSuite("no", false, "testNorwayCollation"));
+            suite.addTest(collatedSuite("no", false, NORWEGIAN_CASE_SENSITIVE));
         }
         if(polish) {
-            suite.addTest(collatedSuite("pl", false, "testPolishCollation"));
+            suite.addTest(collatedSuite("pl", false, POLISH_CASE_SENSITIVE));
         }
         if(french) {
-            suite.addTest(collatedSuite("fr", false, "testFrenchCollation"));
+            suite.addTest(collatedSuite("fr", false, FRENCH_CASE_SENSITIVE));
         }
         if(swedish && !hasBuggySwedishLocale()) {
-            suite.addTest(collatedSuite("sv", true, "testSwedishCaseInsensitiveCollation"));
+            suite.addTest(collatedSuite("sv", true, SWEDISH_CASE_INSENSITIVE));
         }
         return suite;
     }
@@ -2105,35 +2126,26 @@ public void testMissingCollatorSupport()
             return true;
         }
     }
-
-  /**
-     Load the appropriate driver for the current framework
-   */
-  private static void loadDriver()
-  {
-      String driverClass =
-          TestConfiguration.getCurrent().getJDBCClient().getJDBCDriverName();
-      try {
-          Class.forName(driverClass).newInstance();
-      } catch (Exception e) {
-          fail ("could not instantiate driver");
-      }
-  }
  
   /**
    * Return a suite that uses a single use database with
-   * a primary fixture from this test plus potentially other
-   * fixtures.
+   * a set of test cases from this class, plus potentially some other
+   * test cases.
+   *
    * @param locale Locale to use for the database
    * @param caseInsensitive Indicates if the database should use a case insensitive collation.
-   * @param baseFixture Base fixture from this test.
+   * @param testNames the tests to run in the specified locale
    * @return suite of tests to run for the given locale
    */
-  private static Test collatedSuite(String locale, boolean caseInsensitive, String baseFixture)
+  private static Test collatedSuite(
+          String locale, boolean caseInsensitive, String[] testNames)
   {
       TestSuite suite = new TestSuite("CollationTest:territory=" + locale);
-      suite.addTest(new CollationTest(baseFixture));
-      
+
+      for (int i = 0; i < testNames.length; i++) {
+          suite.addTest(new CollationTest(testNames[i]));
+      }
+
       // DMD.getTables() should not fail after the fix to DERBY-2896
       /*
        * The following check condition was added because it would be 
@@ -2160,7 +2172,6 @@ public void testMissingCollatorSupport()
     {
         Statement s = createStatement();
         
-        ResultSet rs = null;
         s.execute("CREATE TABLE derby5951( a clob )");
         s.execute
             (
@@ -2169,11 +2180,10 @@ public void testMissingCollatorSupport()
              "external name 'org.apache.derbyTesting.functionTests.tests.lang.CollationTest.makeClob'\n"
              );
         s.executeUpdate("INSERT INTO derby5951 VALUES( makeClob( 'a' ) )");
-        rs = s.executeQuery("select * from derby5591");
-        JDBC.assertFullResultSet(rs,
-                                 new String[][] {{"a"}});
+        ResultSet rs = s.executeQuery("select * from derby5951");
+        JDBC.assertSingleValueResultSet(rs, "a");
         
-        s.executeUpdate("DROP TABLE derby5591");
+        s.executeUpdate("DROP TABLE derby5951");
         s.executeUpdate("DROP function makeClob");
     }
 
@@ -2183,4 +2193,49 @@ public void testMissingCollatorSupport()
         return new HarmonySerialClob( contents );
     }
 
+    /**
+     * Regression test case for DERBY-6030. The escape sequence in a LIKE
+     * expression should consist of a single character and a single collation
+     * element. Before DERBY-6030, with non-literal escape sequences, Derby
+     * would only check that the sequence consisted of a single collation
+     * element, and might incorrectly accept escape sequences with more than
+     * one character.
+     */
+    public void testLikeEscapeClauseLengthRestriction() throws SQLException {
+        setAutoCommit(false);
+        Statement s = createStatement();
+        s.execute("create table d6030" +
+                  "(x varchar(10), y varchar(10), z varchar(10))");
+        s.execute("insert into d6030 values ('a', 'b', 'c')");
+
+        PreparedStatement select = prepareStatement(
+            "select * from d6030 where x like y escape z");
+
+        PreparedStatement update = prepareStatement("update d6030 set z = ?");
+
+        // Escape clause 'c' is OK.
+        JDBC.assertEmpty(select.executeQuery());
+
+        // Sharp-s is NOT OK, as it has two collation elements.
+        update.setString(1, "\u00df");
+        assertUpdateCount(update, 1);
+        assertStatementError(INVALID_ESCAPE, select);
+
+        // 'aa' is NOT OK, as it has two characters. This used to succeed with
+        // Norwegian collation, which treats 'aa' as a single collation
+        // element. But it should fail since it's two characters.
+        update.setString(1, "aa");
+        assertUpdateCount(update, 1);
+        assertStatementError(INVALID_ESCAPE, select);
+
+        // Also test the same queries with literals in the escape clause.
+        // Those queries follow a different code path, and they produced the
+        // expected results even before the fix.
+        JDBC.assertEmpty(
+            s.executeQuery("select * from d6030 where x like y escape 'c'"));
+        assertStatementError(INVALID_ESCAPE, s,
+                "select * from d6030 where x like y escape '\u00df'");
+        assertStatementError(INVALID_ESCAPE, s,
+                "select * from d6030 where x like y escape 'aa'");
+    }
 }