sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mlebi...@apache.org
Subject svn commit: r1643155 [6/6] - in /sis/branches/JDK8: core/sis-utility/src/main/java/org/apache/sis/util/logging/ storage/sis-shapefile/src/main/java/org/apache/sis/internal/shapefile/jdbc/ storage/sis-shapefile/src/main/java/org/apache/sis/internal/shap...
Date Fri, 05 Dec 2014 05:53:24 GMT
Added: sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/AbstractTestBaseForInternalJDBC.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/AbstractTestBaseForInternalJDBC.java?rev=1643155&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/AbstractTestBaseForInternalJDBC.java
(added)
+++ sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/AbstractTestBaseForInternalJDBC.java
Fri Dec  5 05:53:23 2014
@@ -0,0 +1,51 @@
+package org.apache.sis.internal.shapefile.jdbc;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.sql.Connection;
+import java.sql.Driver;
+import java.sql.SQLException;
+import java.util.logging.Logger;
+
+import org.apache.sis.storage.shapefile.Database;
+import org.apache.sis.test.TestCase;
+import org.junit.Before;
+
+/**
+ * Base class to settle a common environment to all the JDBC tests : 
+ * all these tests are currently based on the SignedBikeRoute_4326_clipped.dbf DBase 3 file.
+ * @author Marc LE BIHAN
+ */
+abstract public class AbstractTestBaseForInternalJDBC extends TestCase {
+    /** Logger. */
+    protected Logger log = Logger.getLogger(getClass().getName());
+    
+    /** The database file to use for testing purpose.  */
+    protected File dbfFile;
+
+    /**
+     * Connect to test database.
+     * @return Connection to database.
+     * @throws SQLException if the connection failed.
+     */
+    public Connection connect() throws SQLException {
+        final Driver driver = new DBFDriver();
+        return driver.connect(dbfFile.getAbsolutePath(), null);        
+    }
+    
+    /**
+     * Test setup.
+     * @throws URISyntaxException If an error occurred while getting the file to the test
database.
+     */
+    @Before
+    public void setup() throws URISyntaxException {
+        final URL url = Database.class.getResource("SignedBikeRoute_4326_clipped.dbf");
+        assertNotNull("The database file used for testing doesn't exist.", url);
+        dbfFile = new File(url.toURI());
+        assertTrue(dbfFile.isFile());
+    }
+}

Added: sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/DBFConnectionTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/DBFConnectionTest.java?rev=1643155&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/DBFConnectionTest.java
(added)
+++ sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/DBFConnectionTest.java
Fri Dec  5 05:53:23 2014
@@ -0,0 +1,72 @@
+/*
+ * 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.sis.internal.shapefile.jdbc;
+
+import java.sql.*;
+
+import org.apache.sis.internal.shapefile.jdbc.connection.DBFConnection;
+import org.apache.sis.test.DependsOnMethod;
+import org.junit.*;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests {@link DBFConnection}.
+ * @author  Marc Le Bihan
+ * @version 0.5
+ * @since   0.5
+ * @module
+ */
+public class DBFConnectionTest extends AbstractTestBaseForInternalJDBC {
+    /**
+     * Open and close a connection.
+     * @throws SQLException if an error occurred while opening the database.
+     */
+    @Test
+    public void openCloseConnection() throws SQLException {
+        final Driver driver = new DBFDriver();
+        final Connection connection = driver.connect(dbfFile.getAbsolutePath(), null);
+        assertFalse("Connection should be opened", connection.isClosed());
+        assertTrue ("Connection should be valid",  connection.isValid(0));
+
+        connection.close();
+        assertTrue ("Connection should be closed", connection.isClosed());
+        assertFalse("Connection should no more be valid", connection.isValid(0));
+    }
+    
+    /**
+     * An attempt to use a closed connection must fail with the correct exception.
+     * @throws SQLException if an error occurred while opening the database.
+     */
+    @Test(expected=SQLConnectionClosedException.class)
+    @DependsOnMethod("openCloseConnection")
+    public void connectionClosed() throws SQLException {
+        // Open and close an connection.
+        final Driver driver = new DBFDriver();
+        final Connection connection = driver.connect(dbfFile.getAbsolutePath(), null);
+        connection.close();
+        
+        // Then, attempt to use it.
+        try {
+            connection.createStatement();
+        } catch(SQLConnectionClosedException e) {
+            assertEquals("The database name in this exception is not well set.", e.getDatabase().getName(),
dbfFile.getName());
+            throw e;
+        }
+    }
+}

Modified: sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/DBFResultSetTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/DBFResultSetTest.java?rev=1643155&r1=1643154&r2=1643155&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/DBFResultSetTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/DBFResultSetTest.java
[UTF-8] Fri Dec  5 05:53:23 2014
@@ -16,106 +16,189 @@
  */
 package org.apache.sis.internal.shapefile.jdbc;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 import java.sql.*;
-import java.io.File;
-import java.net.URL;
-import java.net.URISyntaxException;
-import org.apache.sis.storage.shapefile.Database;
-import org.apache.sis.test.DependsOnMethod;
-import org.apache.sis.test.TestCase;
-import org.junit.*;
+import java.text.MessageFormat;
+import java.util.ArrayList;
 
-import static org.junit.Assert.*;
+import org.apache.sis.internal.shapefile.jdbc.resultset.DBFRecordBasedResultSet;
+import org.apache.sis.test.DependsOnMethod;
+import org.junit.Test;
 
 
 /**
- * Tests {@link DBFResultSet}.
+ * Tests {@link DBFRecordBasedResultSet}.
  *
  * @author  Marc Le Bihan
  * @version 0.5
  * @since   0.5
  * @module
  */
-public class DBFResultSetTest extends TestCase {
-    /**
-     * The database file to use for testing purpose.
-     */
-    private File dbfFile;
-
-    /**
-     * Test setup.
-     *
-     * @throws URISyntaxException If an error occurred while getting the file to the test
database.
-     */
-    @Before
-    public void setup() throws URISyntaxException {
-        final URL url = Database.class.getResource("SignedBikeRoute_4326_clipped.dbf");
-        assertNotNull("The database file used for testing doesn't exist.", url);
-        dbfFile = new File(url.toURI());
-        assertTrue(dbfFile.isFile());
-    }
-
-    /**
-     * Open and close a connection.
-     *
-     * @throws SQLException if an error occurred while querying the database.
-     */
-    @Test
-    public void openCloseConnection() throws SQLException {
-        final Driver     driver     = new DBFDriver();
-        final Connection connection = driver.connect(dbfFile.getAbsolutePath(), null);
-        assertFalse("Connection should be opened", connection.isClosed());
-        assertTrue ("Connection should be valid",  connection.isValid(0));
-
-        connection.close();
-        assertTrue ("Connection should be closed", connection.isClosed());
-        assertFalse("Connection should no more be valid", connection.isValid(0));
-    }
-
+public class DBFResultSetTest extends AbstractTestBaseForInternalJDBC {
     /**
      * Reads the first record.
-     *
      * @throws SQLException if an error occurred while querying the database.
      */
     @Test
-    @DependsOnMethod("openCloseConnection")
     public void readFirstRecord() throws SQLException {
-        final Driver     driver     = new DBFDriver();
-        final Connection connection = driver.connect(dbfFile.getAbsolutePath(), null);
-        final Statement  stmt       = connection.createStatement();
-        final ResultSet  rs         = stmt.executeQuery("SELECT * FROM SignedBikeRoute");
-        // We don't care currently of the request: we are returning everything each time.
-
-        rs.next();
-        assertEquals("getString(\"ST_NAME\")", "336TH ST", rs.getString("ST_NAME"));
-
-        rs.close();
-        stmt.close();
-        connection.close();
+        try(Connection connection = connect(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM SignedBikeRoute")) {
+            rs.next();
+            assertEquals("getString(\"ST_NAME\")", "36TH ST", rs.getString("ST_NAME")); 
                                  // ST_NAME Character(29)
+            assertEquals("getInt(\"FNODE_\")", 1199, rs.getInt("FNODE_"));              
                                  // FNODE_ Number(10, 0)
+            assertEquals("getDouble(\"SHAPE_LEN\")", 43.0881492571, rs.getDouble("SHAPE_LEN"),
0.1);                       // SHAPE_LEN Number(19, 11)
+            assertEquals("getBigDecimal(\"SHAPE_LEN\")", 43.0881492571, rs.getBigDecimal("SHAPE_LEN").doubleValue(),
0.1); // SHAPE_LEN Number(19, 11)
+            assertEquals("getDate(\"TR_DATE\")", null, rs.getDate("TR_DATE"));          
            // TR_DATE Date(8)
+        }
     }
-
+    
     /**
      * Read all the DBF records.
-     *
      * @throws SQLException if an error occurred while querying the database.
      */
     @Test
     @DependsOnMethod("readFirstRecord")
     public void readAllRecords() throws SQLException {
-        final Driver     driver     = new DBFDriver();
-        final Connection connection = driver.connect(dbfFile.getAbsolutePath(), null);
-        final Statement  stmt       = connection.createStatement();
-        final ResultSet  rs         = stmt.executeQuery("SELECT * FROM SignedBikeRoute");
-        // We don't care currently of the request: we are returning everything each time.
-
-        int count = 0;
-        while (rs.next()) {
-            count++;
+        try(Connection connection = connect(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM SignedBikeRoute")) {
+            int count = 0;
+            
+            while(rs.next()) {
+                ArrayList<Object> record = new ArrayList<>();
+                
+                record.add(rs.getLong("OBJECTID"));         // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getLong("FNODE_"));           // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getLong("TNODE_"));           // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getLong("LPOLY_"));           // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getLong("RPOLY_"));           // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getLong("SCL_"));             // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getLong("SCL_ID"));           // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getLong("SCL_CODE"));         // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getString("DXF_LAYER"));      // Type : Character, Field length
: 16, Decimal positions : 0
+                record.add(rs.getString("SIS_ID"));         // Type : Character, Field length
: 12, Decimal positions : 0
+                record.add(rs.getString("QUAD_CODE"));      // Type : Character, Field length
: 1, Decimal positions : 0
+                record.add(rs.getString("PRIME_ST"));       // Type : Character, Field length
: 4, Decimal positions : 0
+                record.add(rs.getString("INT_SEQ"));        // Type : Character, Field length
: 3, Decimal positions : 0
+                record.add(rs.getString("ST_NAME"));        // Type : Character, Field length
: 29, Decimal positions : 0
+                record.add(rs.getString("FDPRE"));          // Type : Character, Field length
: 2, Decimal positions : 0
+                record.add(rs.getString("FNAME"));          // Type : Character, Field length
: 30, Decimal positions : 0
+                record.add(rs.getString("FTYPE"));          // Type : Character, Field length
: 4, Decimal positions : 0
+                record.add(rs.getString("FDSUF"));          // Type : Character, Field length
: 2, Decimal positions : 0
+                record.add(rs.getLong("LEFTRANGE1"));       // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getLong("LEFTRANGE2"));       // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getLong("RGTRANGE1"));        // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getLong("RGTRANGE2"));        // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getString("STREET"));         // Type : Character, Field length
: 26, Decimal positions : 0
+                record.add(rs.getString("QUAD"));           // Type : Character, Field length
: 2, Decimal positions : 0
+                record.add(rs.getString("FROM_ST"));        // Type : Character, Field length
: 100, Decimal positions : 0
+                record.add(rs.getString("TO_ST"));          // Type : Character, Field length
: 100, Decimal positions : 0
+                record.add(rs.getString("ODD_WARD"));       // Type : Character, Field length
: 1, Decimal positions : 0
+                record.add(rs.getString("EVEN_WARD"));      // Type : Character, Field length
: 1, Decimal positions : 0
+                record.add(rs.getString("WARD"));           // Type : Character, Field length
: 3, Decimal positions : 0
+                record.add(rs.getString("FC"));             // Type : Character, Field length
: 40, Decimal positions : 0
+                record.add(rs.getInt("NHS"));               // Type : Number, Field length
: 5, Decimal positions : 0
+                record.add(rs.getString("SIGNEDRTE1"));     // Type : Character, Field length
: 5, Decimal positions : 0
+                record.add(rs.getString("RTETYPE1"));       // Type : Character, Field length
: 10, Decimal positions : 0
+                record.add(rs.getString("SIGNEDRTE2"));     // Type : Character, Field length
: 5, Decimal positions : 0
+                record.add(rs.getString("RTETYPE2"));       // Type : Character, Field length
: 10, Decimal positions : 0
+                record.add(rs.getBigDecimal("AADT"));       // Type : Number, Field length
: 19, Decimal positions : 8
+                record.add(rs.getBigDecimal("AADT_YEAR"));  // Type : Number, Field length
: 19, Decimal positions : 8
+                record.add(rs.getBigDecimal("COM_SING_P")); // Type : Number, Field length
: 19, Decimal positions : 8
+                record.add(rs.getBigDecimal("COM_SING_A")); // Type : Number, Field length
: 19, Decimal positions : 8
+                record.add(rs.getBigDecimal("COM_COMB_P")); // Type : Number, Field length
: 19, Decimal positions : 8
+                record.add(rs.getBigDecimal("COM_COMB_A")); // Type : Number, Field length
: 19, Decimal positions : 8
+                record.add(rs.getString("IS_ONEWAY"));      // Type : Number, Field length
: 5, Decimal positions : 0
+                record.add(rs.getString("TRAVEL_DIR"));     // Type : Character, Field length
: 20, Decimal positions : 0
+                record.add(rs.getBigDecimal("LEN_MI"));     // Type : Number, Field length
: 19, Decimal positions : 8
+                record.add(rs.getLong("STUDY_NET"));        // Type : Number, Field length
: 10, Decimal positions : 0
+                record.add(rs.getDate("TR_DATE"));          // Type : Date, Field length
: 8, Decimal positions : 0
+                record.add(rs.getBigDecimal("AADT_2"));     // Type : Number, Field length
: 19, Decimal positions : 8
+                record.add(rs.getBigDecimal("AADT_FINAL")); // Type : Number, Field length
: 19, Decimal positions : 8
+                record.add(rs.getBigDecimal("ROUTENET"));   // Type : Number, Field length
: 19, Decimal positions : 8
+                record.add(rs.getString("NOTES"));          // Type : Character, Field length
: 50, Decimal positions : 0
+                record.add(rs.getBigDecimal("LENGTH_MI"));  // Type : Number, Field length
: 19, Decimal positions : 8
+                record.add(rs.getInt("NET_MARCH"));         // Type : Number, Field length
: 5, Decimal positions : 0
+                record.add(rs.getString("SIGNED_JOI"));     // Type : Character, Field length
: 5, Decimal positions : 0
+                record.add(rs.getString("SIGNED_FAC"));     // Type : Character, Field length
: 30, Decimal positions : 0
+                record.add(rs.getString("NEW_USE"));        // Type : Character, Field length
: 30, Decimal positions : 0
+                record.add(rs.getBigDecimal("SHAPE_LEN"));  // Type : Number, Field length
: 19, Decimal positions : 11                
+                
+                count ++;
+                log.info(MessageFormat.format("Record {0,number} : {1}\n", count, record));
+            }
+            
+            assertTrue("Less than one record was readed.", count > 1);
+        }
+    }
+    
+    /**
+     * An attempt to use a closed resultSet must fail with the correct exception and message.
+     * @throws SQLException if an error occurred while opening the database, the statement
or the resultset.
+     */
+    @Test
+    public void resultSetClosed() throws SQLException {
+        // 1) Open a connection, open a statement, open and close a ResultSet.
+        String sql = "SELECT * FROM SignedBikeRoute";
+        
+        try(Connection connection = connect(); Statement stmt = connection.createStatement())
{
+            // Then, attempt to use it.
+            try {
+                ResultSet rs = stmt.executeQuery(sql);
+                rs.close();
+            } 
+            catch(SQLConnectionClosedException e) {
+                assertEquals("The database name in this exception is not well set.", e.getDatabase().getName(),
dbfFile.getName());
+                assertEquals("The SQL Query is exception is not well set.", e.getSQL(), sql);
+            } 
+            catch(SQLException e) {
+                fail("Not the expected exception for using a closed ResultSet.");
+            }
+        }
+        
+        // 2) Same, but we close the connection instead.
+        Connection connection = connect();
+        Statement stmt = connection.createStatement();
+        ResultSet rs = stmt.executeQuery(sql);
+        
+        connection.close(); // At this time, you expect also a warning on the console, telling
that you have one statement and one ResultSet still opened.
+        
+        // Then, attempt to use it.
+        try {
+            rs.next();
+        } 
+        catch(SQLConnectionClosedException e) {
+            assertEquals("The database name is exception message is not well set.", e.getDatabase().getName(),
dbfFile.getName());
+        } 
+        catch(SQLException e) {
+            fail("Not the expected exception for using a closed ResultSet.");
+        }
+        finally {
+            rs.close();
+            stmt.close();
+        }
+        
+        // 3) Same, but we close the statement instead .
+        try(Connection cnt = connect()) {
+            stmt = cnt.createStatement();
+            rs = stmt.executeQuery(sql);
+            
+            stmt.close(); // At this time, you expect also a information message on the console,
telling that the statement has closed its current ResultSet.
+            
+            // Then, attempt to use it.
+            try {
+                rs.next();
+            } 
+            catch(SQLConnectionClosedException e) {
+                assertEquals("The database name is exception message is not well set.", e.getDatabase().getName(),
dbfFile.getName());
+            } 
+            catch(SQLException e) {
+                fail("Not the expected exception for using a closed ResultSet.");
+            }
+            finally {
+                rs.close();
+                stmt.close();
+            }
         }
-        assertTrue("Less than one record was readed.", count > 1);
-
-        rs.close();
-        stmt.close();
-        connection.close();
     }
 }

Added: sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/DBFStatementTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/DBFStatementTest.java?rev=1643155&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/DBFStatementTest.java
(added)
+++ sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/DBFStatementTest.java
Fri Dec  5 05:53:23 2014
@@ -0,0 +1,97 @@
+/*
+ * 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.sis.internal.shapefile.jdbc;
+
+import java.sql.*;
+
+import org.apache.sis.internal.shapefile.jdbc.statement.DBFStatement;
+import org.apache.sis.test.DependsOnMethod;
+import org.junit.*;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests {@link DBFStatement}.
+ * @author  Marc Le Bihan
+ * @version 0.5
+ * @since   0.5
+ * @module
+ */
+public class DBFStatementTest extends AbstractTestBaseForInternalJDBC {
+    /**
+     * Open and close a statement.
+     * @throws SQLException if an error occurred while opening the database or the statement.
+     */
+    @Test
+    public void openCloseStatement() throws SQLException {
+        final Driver driver = new DBFDriver();
+        
+        try(Connection connection = driver.connect(dbfFile.getAbsolutePath(), null)) {
+            final Statement stmt = connection.createStatement();
+            assertFalse("Statement should be opened", stmt.isClosed());
+    
+            stmt.close();
+            assertTrue ("Statement should be closed", stmt.isClosed());
+        }
+    }
+    
+    /**
+     * An attempt to use a closed statement must fail with the correct exception.
+     * @throws SQLException if an error occurred while opening the database or the statement.
+     */
+    @Test
+    @DependsOnMethod("openCloseStatement")
+    public void statementClosed() throws SQLException {
+        // Open a connection, open and close a statement.
+        try(Connection connection = connect()) {
+            final Statement stmt = connection.createStatement();
+            stmt.close();
+            
+            // Then, attempt to use it.
+            try {
+                stmt.executeQuery("Must detect that the statement is closed, and not try
to parse this query.");
+            } 
+            catch(SQLConnectionClosedException e) {
+                assertEquals("The database name in this exception is not well set.", e.getDatabase().getName(),
dbfFile.getName());
+            } 
+            catch(SQLException e) {
+                fail("Not the expected exception for using a closed statement.");
+            }
+        }
+        
+        // Same, but we close the connection instead.
+        Connection connection = connect();
+        final Statement stmt = connection.createStatement();
+        
+        connection.close(); // At this time, you expect also a warning on the console, telling
that you have one statement still opened.
+        
+        // Then, attempt to use it.
+        try {
+            stmt.executeQuery("Must detect that the statement is closed, and not try to parse
this query.");
+        } 
+        catch(SQLConnectionClosedException e) {
+            assertEquals("The database name in this exception is not well set.", e.getDatabase().getName(),
dbfFile.getName());
+        } 
+        catch(SQLException e) {
+            fail("Not the expected exception for using a closed statement.");
+        }
+        finally {
+            stmt.close();
+        }
+    }
+}

Added: sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/readme.txt
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/readme.txt?rev=1643155&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/readme.txt
(added)
+++ sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/readme.txt
Fri Dec  5 05:53:23 2014
@@ -0,0 +1,58 @@
+The SignedBikeRoute_4326_clipped.dbf used for testing in this package has this description
:
+
+Field name : OBJECTID, Type : Number, Field length : 10, Decimal positions : 0
+Field name : FNODE_, Type : Number, Field length : 10, Decimal positions : 0
+Field name : TNODE_, Type : Number, Field length : 10, Decimal positions : 0
+Field name : LPOLY_, Type : Number, Field length : 10, Decimal positions : 0
+Field name : RPOLY_, Type : Number, Field length : 10, Decimal positions : 0
+Field name : SCL_, Type : Number, Field length : 10, Decimal positions : 0
+Field name : SCL_ID, Type : Number, Field length : 10, Decimal positions : 0
+Field name : SCL_CODE, Type : Number, Field length : 10, Decimal positions : 0
+Field name : DXF_LAYER, Type : Character, Field length : 16, Decimal positions : 0
+Field name : SIS_ID, Type : Character, Field length : 12, Decimal positions : 0
+Field name : QUAD_CODE, Type : Character, Field length : 1, Decimal positions : 0
+Field name : PRIME_ST, Type : Character, Field length : 4, Decimal positions : 0
+Field name : INT_SEQ, Type : Character, Field length : 3, Decimal positions : 0
+Field name : ST_NAME, Type : Character, Field length : 29, Decimal positions : 0
+Field name : FDPRE, Type : Character, Field length : 2, Decimal positions : 0
+Field name : FNAME, Type : Character, Field length : 30, Decimal positions : 0
+Field name : FTYPE, Type : Character, Field length : 4, Decimal positions : 0
+Field name : FDSUF, Type : Character, Field length : 2, Decimal positions : 0
+Field name : LEFTRANGE1, Type : Number, Field length : 10, Decimal positions : 0
+Field name : LEFTRANGE2, Type : Number, Field length : 10, Decimal positions : 0
+Field name : RGTRANGE1, Type : Number, Field length : 10, Decimal positions : 0
+Field name : RGTRANGE2, Type : Number, Field length : 10, Decimal positions : 0
+Field name : STREET, Type : Character, Field length : 26, Decimal positions : 0
+Field name : QUAD, Type : Character, Field length : 2, Decimal positions : 0
+Field name : FROM_ST, Type : Character, Field length : 100, Decimal positions : 0
+Field name : TO_ST, Type : Character, Field length : 100, Decimal positions : 0
+Field name : ODD_WARD, Type : Character, Field length : 1, Decimal positions : 0
+Field name : EVEN_WARD, Type : Character, Field length : 1, Decimal positions : 0
+Field name : WARD, Type : Character, Field length : 3, Decimal positions : 0
+Field name : FC, Type : Character, Field length : 40, Decimal positions : 0
+Field name : NHS, Type : Number, Field length : 5, Decimal positions : 0
+Field name : SIGNEDRTE1, Type : Character, Field length : 5, Decimal positions : 0
+Field name : RTETYPE1, Type : Character, Field length : 10, Decimal positions : 0
+Field name : SIGNEDRTE2, Type : Character, Field length : 5, Decimal positions : 0
+Field name : RTETYPE2, Type : Character, Field length : 10, Decimal positions : 0
+Field name : AADT, Type : Number, Field length : 19, Decimal positions : 8
+Field name : AADT_YEAR, Type : Number, Field length : 19, Decimal positions : 8
+Field name : COM_SING_P, Type : Number, Field length : 19, Decimal positions : 8
+Field name : COM_SING_A, Type : Number, Field length : 19, Decimal positions : 8
+Field name : COM_COMB_P, Type : Number, Field length : 19, Decimal positions : 8
+Field name : COM_COMB_A, Type : Number, Field length : 19, Decimal positions : 8
+Field name : IS_ONEWAY, Type : Number, Field length : 5, Decimal positions : 0
+Field name : TRAVEL_DIR, Type : Character, Field length : 20, Decimal positions : 0
+Field name : LEN_MI, Type : Number, Field length : 19, Decimal positions : 8
+Field name : STUDY_NET, Type : Number, Field length : 10, Decimal positions : 0
+Field name : TR_DATE, Type : Date, Field length : 8, Decimal positions : 0
+Field name : AADT_2, Type : Number, Field length : 19, Decimal positions : 8
+Field name : AADT_FINAL, Type : Number, Field length : 19, Decimal positions : 8
+Field name : ROUTENET, Type : Number, Field length : 19, Decimal positions : 8
+Field name : NOTES, Type : Character, Field length : 50, Decimal positions : 0
+Field name : LENGTH_MI, Type : Number, Field length : 19, Decimal positions : 8
+Field name : NET_MARCH, Type : Number, Field length : 5, Decimal positions : 0
+Field name : SIGNED_JOI, Type : Character, Field length : 5, Decimal positions : 0
+Field name : SIGNED_FAC, Type : Character, Field length : 30, Decimal positions : 0
+Field name : NEW_USE, Type : Character, Field length : 30, Decimal positions : 0
+Field name : SHAPE_LEN, Type : Number, Field length : 19, Decimal positions : 11

Added: sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/sql/WhereClauseTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/sql/WhereClauseTest.java?rev=1643155&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/sql/WhereClauseTest.java
(added)
+++ sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/internal/shapefile/jdbc/sql/WhereClauseTest.java
Fri Dec  5 05:53:23 2014
@@ -0,0 +1,124 @@
+package org.apache.sis.internal.shapefile.jdbc.sql;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+
+import java.sql.*;
+
+import org.apache.sis.internal.shapefile.jdbc.AbstractTestBaseForInternalJDBC;
+import org.apache.sis.internal.shapefile.jdbc.resultset.DBFRecordBasedResultSet;
+import org.junit.Test;
+
+/**
+ * Testing of the WHERE clause in SQL Statements.
+ */
+public class WhereClauseTest extends AbstractTestBaseForInternalJDBC {
+    /**
+     * Test operators.
+     * @throws SQLException if a trouble occurs : all tests shall pass.
+     */
+    @Test
+    public void operators() throws SQLException {
+        try(Connection connection = connect(); Statement stmt = connection.createStatement();
DBFRecordBasedResultSet rs = (DBFRecordBasedResultSet)stmt.executeQuery("SELECT * FROM SignedBikeRoute"))
{
+            rs.next();
+            
+            assertTrue("FNODE_ = 1199", new ConditionalClauseResolver("FNODE_", 1199L, "=").isVerified(rs));
+            assertFalse("FNODE_ > 1199", new ConditionalClauseResolver("FNODE_", 1199L,
">").isVerified(rs));
+            assertFalse("FNODE_ < 1199", new ConditionalClauseResolver("FNODE_", 1199L,
"<").isVerified(rs));
+            assertTrue("FNODE_ >= 1199", new ConditionalClauseResolver("FNODE_", 1199L,
">=").isVerified(rs));
+            assertTrue("FNODE_ <= 1199", new ConditionalClauseResolver("FNODE_", 1199L,
"<=").isVerified(rs));
+            
+            assertTrue("FNODE_ > 1198", new ConditionalClauseResolver("FNODE_", 1198L,
">").isVerified(rs));
+            assertFalse("FNODE_ < 1198", new ConditionalClauseResolver("FNODE_", 1198L,
"<").isVerified(rs));
+            assertTrue("FNODE_ >= 1198", new ConditionalClauseResolver("FNODE_", 1198L,
">=").isVerified(rs));
+            assertFalse("FNODE_ <= 1198", new ConditionalClauseResolver("FNODE_", 1198L,
"<=").isVerified(rs));
+            
+            assertFalse("FNODE_ > 1200", new ConditionalClauseResolver("FNODE_", 1200L,
">").isVerified(rs));
+            assertTrue("FNODE_ < 1200", new ConditionalClauseResolver("FNODE_", 1200L,
"<").isVerified(rs));
+            assertFalse("FNODE_ >= 1200", new ConditionalClauseResolver("FNODE_", 1200L,
">=").isVerified(rs));
+            assertTrue("FNODE_ <= 1200", new ConditionalClauseResolver("FNODE_", 1200L,
"<=").isVerified(rs));
+            
+            assertTrue("ST_NAME = '36TH ST'", new ConditionalClauseResolver("ST_NAME", "'36TH
ST'", "=").isVerified(rs));
+            
+            assertTrue("SHAPE_LEN = 43.0881492571", new ConditionalClauseResolver("SHAPE_LEN",
43.0881492571, "=").isVerified(rs));
+            assertTrue("SHAPE_LEN > 43.088", new ConditionalClauseResolver("SHAPE_LEN",
43.088, ">").isVerified(rs));
+            assertFalse("SHAPE_LEN < 43.0881492571", new ConditionalClauseResolver("SHAPE_LEN",
43.0881492571, "<").isVerified(rs));
+        }
+    }
+    
+    /**
+     * Test where conditions : field [operator] integer.
+     * @throws SQLException if a trouble occurs : all tests shall pass.
+     */
+    @Test
+    public void whereCondition_field_literal_int() throws SQLException {
+        checkAndCount("FNODE_ < 2000", rs -> rs.getInt("FNODE_") < 2000, 3);
+    }
+    
+    /**
+     * Test where conditions : field [operator] integer.
+     * @throws SQLException if a trouble occurs : all tests shall pass.
+     */
+    @Test
+    public void whereCondition_field_literal_double() throws SQLException {
+        checkAndCount("SHAPE_LEN < 70.5", rs -> rs.getDouble("SHAPE_LEN") < 70.5,
3);
+    }
+
+    /**
+     * Test where conditions : field [operator] String value.
+     * @throws SQLException if a trouble occurs : all tests shall pass.
+     */
+    @Test
+    public void whereCondition_field_literal_string() throws SQLException {
+        checkAndCount("FNAME = '36TH'", rs -> rs.getString("FNAME").equals("36TH"), 1);
+    }
+
+    /**
+     * Test where conditions : field [operator] field.
+     * @throws SQLException if a trouble occurs : all tests shall pass.
+     */
+    @Test
+    public void whereCondition_field_field() throws SQLException {
+        checkAndCount("FNODE_ < TNODE_", rs -> rs.getInt("FNODE_") < rs.getInt("TNODE_"),
1);
+    }
+    
+    /** 
+     * Trick suggested by AdiGuba (Forum des développeurs) to avoid the exception thrown
by ResultSet:getInt(), 
+     * unhandlable by a simple Predicate.
+     * @param <T> Type used. 
+     */ 
+    @FunctionalInterface
+    public interface ResultSetPredicate<T> {
+        /**
+         * Test a condition.
+         * @param condition Condition.
+         * @return true is the condition passed.
+         * @throws SQLException if a trouble occurs.
+         */
+        boolean test(T condition) throws SQLException;
+    }
+    
+    /**
+     * Check that all records match the conditions and count them.
+     * @param whereCondition The where condition to add to a "SELECT * FROM SignedBikeRoute
WHERE " statement.
+     * @param condition Condition.
+     * @param countExpected Count Expected, -1 if you don't want to count them.
+     * @throws SQLException if a trouble occurs : all tests shall pass.
+     */
+    private void checkAndCount(String whereCondition, ResultSetPredicate<ResultSet>
condition, int countExpected) throws SQLException {
+        String sql = "SELECT * FROM SignedBikeRoute WHERE " + whereCondition;
+        
+        try(Connection connection = connect(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
+            int count = 0;
+            
+            while(rs.next()) {
+                count ++;
+                assertTrue(sql, condition.test(rs));
+            }
+            
+            if (countExpected != -1)
+                assertEquals("Wrong number of records red by : " + sql, countExpected, count);
+        }
+    }
+}

Modified: sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/test/suite/ShapefileTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/test/suite/ShapefileTestSuite.java?rev=1643155&r1=1643154&r2=1643155&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/test/suite/ShapefileTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-shapefile/src/test/java/org/apache/sis/test/suite/ShapefileTestSuite.java
[UTF-8] Fri Dec  5 05:53:23 2014
@@ -26,7 +26,10 @@ import org.junit.BeforeClass;
  */
 @Suite.SuiteClasses({
     org.apache.sis.storage.shapefile.ShapeFileTest.class,
-    org.apache.sis.internal.shapefile.jdbc.DBFResultSetTest.class
+    org.apache.sis.internal.shapefile.jdbc.DBFConnectionTest.class,
+    org.apache.sis.internal.shapefile.jdbc.DBFStatementTest.class,
+    org.apache.sis.internal.shapefile.jdbc.DBFResultSetTest.class,
+    org.apache.sis.internal.shapefile.jdbc.sql.WhereClauseTest.class
 })
 public final strictfp class ShapefileTestSuite extends TestSuite {
     /**



Mime
View raw message