From bd506b1e10ebc2ce025e836f83cb0f77562deba4 Mon Sep 17 00:00:00 2001 From: Mark Payne Date: Fri, 23 Oct 2015 09:52:09 -0400 Subject: [PATCH] NIFI-972: Added additional unit test; deleted lines that were commented out --- .../processors/standard/util/JdbcCommon.java | 8 ++-- .../standard/util/TestJdbcCommon.java | 42 +++++++++++++++++++ .../standard/util/TestJdbcTypesDerby.java | 4 -- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JdbcCommon.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JdbcCommon.java index 9cf93384a5..937dcabb96 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JdbcCommon.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JdbcCommon.java @@ -19,6 +19,7 @@ package org.apache.nifi.processors.standard.util; import static java.sql.Types.ARRAY; import static java.sql.Types.BIGINT; import static java.sql.Types.BINARY; +import static java.sql.Types.BIT; import static java.sql.Types.BLOB; import static java.sql.Types.BOOLEAN; import static java.sql.Types.CHAR; @@ -83,7 +84,7 @@ public class JdbcCommon { if (value == null) { rec.put(i - 1, null); - } else if (javaSqlType==BINARY || javaSqlType==VARBINARY || javaSqlType==LONGVARBINARY || javaSqlType==ARRAY || javaSqlType==BLOB || javaSqlType==CLOB) { + } else if (javaSqlType == BINARY || javaSqlType == VARBINARY || javaSqlType == LONGVARBINARY || javaSqlType == ARRAY || javaSqlType == BLOB || javaSqlType == CLOB) { // bytes requires little bit different handling byte[] bytes = rs.getBytes(i); ByteBuffer bb = ByteBuffer.wrap(bytes); @@ -104,7 +105,7 @@ public class JdbcCommon { // The different types that we support are numbers (int, long, double, float), // as well as boolean values and Strings. Since Avro doesn't provide // timestamp types, we want to convert those to Strings. So we will cast anything other - // than numbers or booleans to strings by using to toString() method. + // than numbers or booleans to strings by using the toString() method. rec.put(i - 1, value.toString()); } } @@ -137,9 +138,10 @@ public class JdbcCommon { builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault(); break; + case BIT: case BOOLEAN: builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().booleanType().endUnion().noDefault(); - break; + break; case INTEGER: case SMALLINT: diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcCommon.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcCommon.java index f54d4bacc3..9c9532f998 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcCommon.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcCommon.java @@ -24,19 +24,26 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Field; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Types; +import java.util.HashSet; +import java.util.Set; import org.apache.avro.Schema; import org.apache.avro.file.DataFileStream; import org.apache.avro.generic.GenericDatumReader; import org.apache.avro.generic.GenericRecord; import org.apache.avro.io.DatumReader; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import org.mockito.Mockito; public class TestJdbcCommon { @@ -138,6 +145,41 @@ public class TestJdbcCommon { } } + + @Test + public void testCreateSchemaTypes() throws SQLException, IllegalArgumentException, IllegalAccessException { + final Set fieldsToIgnore = new HashSet<>(); + fieldsToIgnore.add(Types.NULL); + fieldsToIgnore.add(Types.OTHER); + + final Field[] fieldTypes = Types.class.getFields(); + for (final Field field : fieldTypes) { + final Object fieldObject = field.get(null); + final int type = (int) fieldObject; + + if (fieldsToIgnore.contains(Types.NULL)) { + continue; + } + + final ResultSetMetaData metadata = Mockito.mock(ResultSetMetaData.class); + Mockito.when(metadata.getColumnCount()).thenReturn(1); + Mockito.when(metadata.getColumnType(1)).thenReturn(type); + Mockito.when(metadata.getColumnName(1)).thenReturn(field.getName()); + Mockito.when(metadata.getTableName(1)).thenReturn("table"); + + final ResultSet rs = Mockito.mock(ResultSet.class); + Mockito.when(rs.getMetaData()).thenReturn(metadata); + + try { + JdbcCommon.createSchema(rs); + } catch (final IllegalArgumentException | SQLException sqle) { + sqle.printStackTrace(); + Assert.fail("Failed when using type " + field.getName()); + } + } + } + + // many test use Derby as database, so ensure driver is available @Test public void testDriverLoad() throws ClassNotFoundException { diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcTypesDerby.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcTypesDerby.java index cf3d0c6b22..fc2bccd782 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcTypesDerby.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestJdbcTypesDerby.java @@ -64,10 +64,6 @@ public class TestJdbcTypesDerby { + " active tinyint NOT NULL DEFAULT 0, " + " home_module_id int DEFAULT NULL, " + " PRIMARY KEY (id) ) " ; -// + " UNIQUE email ) " ; -// + " KEY home_module_id (home_module_id) ) " ; -// + " CONSTRAINT users_ibfk_1 FOREIGN KEY (home_module_id) REFERENCES " -// + " modules (id) ON DELETE SET NULL " ; String dropTable = "drop table users";