sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1729013 - in /sis/branches/JDK8/core/sis-referencing/src: main/java/org/apache/sis/referencing/factory/sql/ test/java/org/apache/sis/referencing/factory/sql/
Date Sun, 07 Feb 2016 18:59:10 GMT
Author: desruisseaux
Date: Sun Feb  7 18:59:09 2016
New Revision: 1729013

URL: http://svn.apache.org/viewvc?rev=1729013&view=rev
Log:
Test EPSG factory on HSQLDB. The test is skipped by default for the reason explained in Javadoc.

Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGInstaller.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGDataFormatter.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGInstaller.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGInstaller.java?rev=1729013&r1=1729012&r2=1729013&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGInstaller.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/sql/EPSGInstaller.java
[UTF-8] Sun Feb  7 18:59:09 2016
@@ -75,7 +75,7 @@ final class EPSGInstaller extends Script
      *     SET datum_name = replace(datum_name, CHAR(182), CHAR(10));
      * }
      */
-    private static final String REPLACE_STATEMENT =
+    static final String REPLACE_STATEMENT =
             "\\s*UPDATE\\s+[\\w\\.\" ]+\\s+SET\\s+(\\w+)\\s*=\\s*replace\\s*\\(\\s*\\1\\W+.*";
 
     /**
@@ -171,7 +171,8 @@ final class EPSGInstaller extends Script
      * Prepends the given schema or catalog to all table names.
      */
     final void prependNamespace(final String schema) {
-        modifyReplacements((key, value) -> schema + '.' + identifierQuote + value + identifierQuote);
+        modifyReplacements((key, value) -> key.startsWith(SQLTranslator.TABLE_PREFIX)
+                ? schema + '.' + identifierQuote + value + identifierQuote : value);
     }
 
     /**
@@ -222,10 +223,7 @@ final class EPSGInstaller extends Script
      */
     public void run(final Path scriptDirectory) throws SQLException, IOException {
         long time = System.nanoTime();
-        if (scriptDirectory == null) {
-            log(Messages.getResources(null).getLogRecord(Level.INFO, Messages.Keys.CreatingSchema_2,
-                    Constants.EPSG, getConnection().getMetaData().getURL()));
-        }
+        log(Messages.getResources(null).getLogRecord(Level.INFO, Messages.Keys.CreatingSchema_2,
Constants.EPSG, getURL()));
         int numScripts = SCRIPTS.length;
         if (!isGrantOnTableSupported) {
             numScripts--;
@@ -252,6 +250,17 @@ final class EPSGInstaller extends Script
     }
 
     /**
+     * Returns a simplified form of the URL (truncated before the first ? or ; character),
+     * for logging purpose only.
+     */
+    private String getURL() throws SQLException {
+        String url = getConnection().getMetaData().getURL();
+        int s1 = url.indexOf('?'); if (s1 < 0) s1 = url.length();
+        int s2 = url.indexOf(';'); if (s2 < 0) s2 = url.length();
+        return url.substring(0, Math.min(s1, s2));
+    }
+
+    /**
      * Logs a message reporting the failure to create EPSG database.
      */
     final void logFailure(final Locale locale) {

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGDataFormatter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGDataFormatter.java?rev=1729013&r1=1729012&r2=1729013&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGDataFormatter.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGDataFormatter.java
[UTF-8] Sun Feb  7 18:59:09 2016
@@ -77,8 +77,7 @@ import org.apache.sis.internal.metadata.
  *       <li>In the statement creating the {@code change} table,
  *           remove the {@code UNIQUE} constraint on the {@code change_id} column
  *           and add a {@code CONSTRAINT pk_change PRIMARY KEY (change_id)} line instead.</li>
- *       <li>Suppress trailing spaces, format the statement as in the previous version
- *           for reducing the amount of difference and save.</li>
+ *       <li>Suppress trailing spaces and save.</li>
  *     </ul>
  *     <p>Usually this results in no change at all compared to the previous script
(ignoring white spaces),
  *        in which case the maintainer can just revert the changes in order to preserve the
formatting.</p>

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java?rev=1729013&r1=1729012&r2=1729013&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java
[UTF-8] Sun Feb  7 18:59:09 2016
@@ -18,21 +18,32 @@ package org.apache.sis.referencing.facto
 
 import java.util.Map;
 import java.util.HashMap;
+import java.util.regex.Pattern;
+import javax.sql.DataSource;
 import java.sql.Connection;
+import java.sql.Statement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-import javax.sql.DataSource;
 import org.opengis.util.FactoryException;
 import org.opengis.referencing.crs.GeographicCRS;
+import org.opengis.referencing.crs.ProjectedCRS;
 import org.apache.sis.referencing.CommonCRS;
-import org.apache.sis.internal.metadata.sql.TestDatabase;
+import org.apache.sis.internal.system.Loggers;
+import org.apache.sis.util.logging.Logging;
 import org.apache.sis.util.ComparisonMode;
 import org.apache.sis.util.Utilities;
+
+// Test dependencies
+import org.apache.sis.internal.metadata.sql.TestDatabase;
+import org.apache.sis.test.LoggingWatcher;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
+import org.junit.Ignore;
+import org.junit.Rule;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
+import static org.junit.Assume.*;
 
 // Branch-dependent imports
 import java.nio.file.Path;
@@ -54,13 +65,59 @@ import java.nio.file.Path;
 @DependsOn(EPSGFactoryTest.class)
 public final strictfp class EPSGInstallerTest extends TestCase {
     /**
+     * A JUnit rule for listening to log events emitted during execution of tests.
+     * This rule verifies that the log message contains the expected information.
+     *
+     * <p>This field is public because JUnit requires us to do so, but should be considered
+     * as an implementation details (it should have been a private field).</p>
+     */
+    @Rule
+    public final LoggingWatcher listener = new LoggingWatcher(Logging.getLogger(Loggers.CRS_FACTORY))
{
+        @Override protected void verifyMessage(final String message) {
+            log = message;      // Verified later by the tests in the enclosing class.
+        }
+    };
+
+    /**
+     * The logging message caught by the {@link #listener}.
+     */
+    private String log;
+
+    /**
+     * Tests the {@link EPSGInstaller#REPLACE_STATEMENT} pattern.
+     */
+    @Test
+    public void testReplacePattern() {
+        // Statement as in the EPSG scripts since EPSG version 7.06.
+        assertTrue(Pattern.matches(EPSGInstaller.REPLACE_STATEMENT,
+                "UPDATE epsg_datum\n" +
+                "SET datum_name = replace(datum_name, CHR(182), CHR(10))"));
+
+        // Statement as in the EPSG scripts prior to EPSG version 7.06.
+        assertTrue(Pattern.matches(EPSGInstaller.REPLACE_STATEMENT,
+                "UPDATE epsg_datum\n" +
+                "SET datum_name = replace(datum_name, CHAR(182), CHAR(10))"));
+
+        // Modified statement with MS-Access table name in a schema.
+        assertTrue(Pattern.matches(EPSGInstaller.REPLACE_STATEMENT,
+                "UPDATE epsg.\"Alias\"\n" +
+                "SET object_table_name = replace(object_table_name, CHR(182), CHR(10))"));
+
+        // Like above, but the table name contains a space.
+        assertTrue(Pattern.matches(EPSGInstaller.REPLACE_STATEMENT,
+                "UPDATE epsg.\"Coordinate Axis\"\n" +
+                "SET coord_axis_orientation = replace(coord_axis_orientation, CHR(182), CHR(10))"));
+    }
+
+    /**
      * Tests the creation of an EPSG database on Derby.
-     * This test is skipped if no Derby or JavaDB driver has been found.
+     * This test is skipped if Derby/JavaDB is not found, or if the SQL scripts are not found.
      *
      * @throws Exception if an error occurred while creating the database.
      */
     @Test
     public void testCreationOnDerby() throws Exception {
+        assumeTrue("Slow test skipped in non-extensive test mode.", RUN_EXTENSIVE_TESTS);
         final Path scripts = TestDatabase.directory("ExternalSources");
         final DataSource ds = TestDatabase.create("test");
         try {
@@ -68,22 +125,57 @@ public final strictfp class EPSGInstalle
         } finally {
             TestDatabase.drop(ds);
         }
+        assertTrue(log, log.contains("EPSG"));
+        assertTrue(log, log.contains("jdbc:derby:memory:test"));
+    }
+
+    /**
+     * Tests the creation of an EPSG database on HSQLDB.
+     * This test is skipped if the SQL scripts are not found.
+     *
+     * <p>This test is skipped by default because HSQLDB changes the {@code java.util.logging}
configuration,
+     * which causes failures in all Apache SIS tests that verify the logging messages after
execution of this
+     * test. This impact this {@code EPSGInstallerTest} class, but also other test classes.</p>
+     *
+     * @throws Exception if an error occurred while creating the database.
+     */
+    @Test
+    @Ignore("Skipped for protecting java.util.logging configuration against changes.")
+    public void testCreationOnHSQLDB() throws Exception {
+        final Path scripts = TestDatabase.directory("ExternalSources");
+        final DataSource ds = (DataSource) Class.forName("org.hsqldb.jdbc.JDBCDataSource").newInstance();
+        ds.getClass().getMethod("setURL", String.class).invoke(ds, "jdbc:hsqldb:mem:test");
+        try {
+            createAndTest(ds, scripts);
+        } finally {
+            try (Connection c = ds.getConnection(); Statement s = c.createStatement()) {
+                s.execute("SHUTDOWN");
+            }
+        }
+        assertTrue(log, log.contains("EPSG"));
+        assertTrue(log, log.contains("jdbc:hsqldb:mem:test"));
     }
 
     /**
-     * Requests the WGS84 coordinate reference system from the EPSG database at the given
source.
-     * It should trig the creation of the EPSG database.
+     * Requests the "WGS84" and the "WGS72 / UTM zone 15N" coordinate reference systems from
the EPSG database
+     * at the given {@code DataSource}. Those requests should trig the creation of the EPSG
database.
      */
-    private static void createAndTest(final DataSource ds, final Path scripts) throws SQLException,
FactoryException {
+    private void createAndTest(final DataSource ds, final Path scripts) throws SQLException,
FactoryException {
+        listener.maximumLogCount = 100;
         final Map<String,Object> properties = new HashMap<>();
         assertNull(properties.put("dataSource", ds));
         assertNull(properties.put("scriptDirectory", scripts));
         assertEquals("Should not contain EPSG tables before we created them.", 0, countCRSTables(ds));
+        assertEquals("Should not yet have logged anything at this point.", 100, listener.maximumLogCount);
         try (EPSGFactory factory = new EPSGFactory(properties)) {
             final GeographicCRS crs = factory.createGeographicCRS("4326");
             assertTrue(Utilities.deepEquals(CommonCRS.WGS84.geographic(), crs, ComparisonMode.DEBUG));
+
+            final ProjectedCRS p = factory.createProjectedCRS("EPSG:32215");    // UTM zone
15.
+            assertTrue(Utilities.deepEquals(CommonCRS.WGS72.UTM(1, -93), p, ComparisonMode.DEBUG));
         }
         assertEquals("Should contain EPSG tables after we created them.", 1, countCRSTables(ds));
+        assertEquals("Should have logged a message about the database creation.", 99, listener.maximumLogCount);
     }
 
     /**



Mime
View raw message