Author: rhillegas
Date: Sat Aug 1 14:05:42 2015
New Revision: 1693708
URL: http://svn.apache.org/r1693708
Log:
DERBY-6825: Add datatype tests for SimpleJsonVTI. Also, close InputStreams opened by that
VTI. Commit derby-6825-03-aa-datatypeTests.diff.
Added:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/json.dat
(with props)
Modified:
db/derby/code/trunk/java/optional/org/apache/derby/optional/api/SimpleJsonUtils.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/SimpleJsonTest.java
Modified: db/derby/code/trunk/java/optional/org/apache/derby/optional/api/SimpleJsonUtils.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/optional/org/apache/derby/optional/api/SimpleJsonUtils.java?rev=1693708&r1=1693707&r2=1693708&view=diff
==============================================================================
--- db/derby/code/trunk/java/optional/org/apache/derby/optional/api/SimpleJsonUtils.java (original)
+++ db/derby/code/trunk/java/optional/org/apache/derby/optional/api/SimpleJsonUtils.java Sat
Aug 1 14:05:42 2015
@@ -167,7 +167,8 @@ public abstract class SimpleJsonUtils
}
/**
- * Read a JSONArray from an InputStream.
+ * Read a JSONArray from an InputStream. Close the stream
+ * after reading the JSONArray.
*/
public static JSONArray readArrayFromStream
( InputStream inputStream, String characterSetName )
@@ -177,6 +178,13 @@ public abstract class SimpleJsonUtils
return readArray( new InputStreamReader( inputStream, characterSetName ) );
}
catch (UnsupportedEncodingException uee) { throw wrap( uee ); }
+ finally
+ {
+ try {
+ inputStream.close();
+ }
+ catch (IOException ioe) { throw wrap( ioe ); }
+ }
}
/**
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/SimpleJsonTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/SimpleJsonTest.java?rev=1693708&r1=1693707&r2=1693708&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/SimpleJsonTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/SimpleJsonTest.java
Sat Aug 1 14:05:42 2015
@@ -24,6 +24,7 @@ import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
+import java.sql.SQLException;
import java.util.Arrays;
import junit.framework.Test;
@@ -53,6 +54,7 @@ public class SimpleJsonTest extends Base
private static final String TAB = " ";
private static final String USER_ERROR = "38000";
+ private static final String OUT_OF_RANGE = "22003";
private static final String THERMOSTAT_READINGS =
"[\n" +
@@ -97,6 +99,7 @@ public class SimpleJsonTest extends Base
new String[]
{
"functionTests/tests/lang/thermostatReadings.dat",
+ "functionTests/tests/lang/json.dat",
}
);
}
@@ -614,6 +617,140 @@ public class SimpleJsonTest extends Base
goodStatement( conn, "call syscs_util.syscs_register_tool( 'simpleJson', false )"
);
}
+ /**
+ * <p>
+ * Test the datatypes understood by SimpleJsonVTI.
+ * </p>
+ */
+ public void testVTIdatatypes005() throws Exception
+ {
+ Connection conn = getConnection();
+
+ goodStatement( conn, "call syscs_util.syscs_register_tool( 'simpleJson', true )"
);
+
+ vetDatatype_005
+ (
+ conn,
+ "smallint",
+ new String[][]
+ {
+ { "abc","true", "127" },
+ { "def", "false", "1" },
+ { "ghi", null, "345" },
+ { "lmn", "true", "-1" },
+ }
+ );
+ vetDatatype_005
+ (
+ conn,
+ "int",
+ new String[][]
+ {
+ { "abc","true", "127" },
+ { "def", "false", "1" },
+ { "ghi", null, "345" },
+ { "lmn", "true", "-1" },
+ }
+ );
+ vetDatatype_005
+ (
+ conn,
+ "bigint",
+ new String[][]
+ {
+ { "abc","true", "127" },
+ { "def", "false", "1" },
+ { "ghi", null, "345" },
+ { "lmn", "true", "9223372036854775807" },
+ }
+ );
+ vetDatatype_005
+ (
+ conn,
+ "float",
+ new String[][]
+ {
+ { "abc","true", "127.0" },
+ { "def", "false", "1.2" },
+ { "ghi", null, "345.67" },
+ { "lmn", "true", "9.223372036854776E18" },
+ }
+ );
+ vetDatatype_005
+ (
+ conn,
+ "double",
+ new String[][]
+ {
+ { "abc","true", "127.0" },
+ { "def", "false", "1.2" },
+ { "ghi", null, "345.67" },
+ { "lmn", "true", "9.223372036854776E18" },
+ }
+ );
+
+ goodStatement( conn, "call syscs_util.syscs_register_tool( 'simpleJson', false )"
);
+ }
+ private void vetDatatype_005
+ (
+ Connection conn,
+ String datatype,
+ String[][] expectedResults
+ )
+ throws Exception
+ {
+ createFunction_005( conn, datatype );
+
+ PreparedStatement ps = conn.prepareStatement
+ (
+ "select * from table\n" +
+ "( f_" + datatype + "( readArrayFromFile( ?, 'UTF-8' ) )) t\n"
+ );
+ File inputFile = SupportFilesSetup.getReadOnly( "json.dat" );
+ ps.setString( 1, PrivilegedFileOpsForTests.getAbsolutePath( inputFile ) );
+
+ ResultSet rs;
+
+ rs = ps.executeQuery();
+ assertResults(rs, expectedResults, true );
+ rs.close();
+
+ // the first two rows have numeric values which won't raise
+ // truncation exceptions when fetched into tinyint
+ rs = ps.executeQuery();
+ rs.next();
+ assertEquals( (byte) 127, rs.getByte( "NUM_COL" ) );
+ rs.next();
+ assertEquals( (byte) 1, rs.getByte( "NUM_COL" ) );
+ rs.close();
+
+ ps.close();
+
+ dropFunction_005( conn, datatype );
+ }
+ private void createFunction_005( Connection conn, String datatype )
+ throws Exception
+ {
+ goodStatement
+ (
+ conn,
+ "create function f_" + datatype + "( jsonArray JSONArray )\n" +
+ "returns table\n" +
+ "(\n" +
+ " str_col varchar( 10 ),\n" +
+ " bool_col boolean,\n" +
+ " num_col " + datatype + "\n" +
+ ")\n" +
+ "language java parameter style derby_jdbc_result_set contains sql\n" +
+ "external name 'org.apache.derby.optional.api.SimpleJsonVTI.readArray'\n"
+ );
+ }
+ private void dropFunction_005( Connection conn, String datatype )
+ throws Exception
+ {
+ goodStatement( conn, "drop function f_" + datatype );
+ }
+
///////////////////////////////////////////////////////////////////////////////////
//
// FUNCTIONS
Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/json.dat
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/json.dat?rev=1693708&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/json.dat
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/json.dat
Sat Aug 1 14:05:42 2015
@@ -0,0 +1,7 @@
+[
+ { "STR_COL" : "abc", "BOOL_COL" : true, "NUM_COL" : 127 },
+ { "STR_COL" : "def", "BOOL_COL" : false, "NUM_COL" : 1.2 },
+ { "STR_COL" : "ghi", "BOOL_COL" : null, "NUM_COL" : 3.4567e2 },
+ { "STR_COL" : "lmn", "BOOL_COL" : true, "NUM_COL" : 9223372036854775807 }
+]
+
Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/json.dat
------------------------------------------------------------------------------
svn:eol-style = native
|