diff --git a/build.gradle b/build.gradle index 768b2cb380..f37e5793fb 100644 --- a/build.gradle +++ b/build.gradle @@ -78,7 +78,7 @@ repositories { } dependencies { - compile 'com.microsoft.azure:azure-keyvault:1.0.0', + compileOnly 'com.microsoft.azure:azure-keyvault:1.0.0', 'com.microsoft.azure:adal4j:1.6.0' testCompile 'junit:junit:4.12', 'org.junit.platform:junit-platform-console:1.2.0', diff --git a/pom.xml b/pom.xml index e08cbe2ca7..f7e59b162b 100644 --- a/pom.xml +++ b/pom.xml @@ -136,7 +136,7 @@ maven-compiler-plugin - 3.6.0 + 3.7.0 **/com/microsoft/sqlserver/jdbc/SQLServerJdbc43.java @@ -169,7 +169,7 @@ maven-compiler-plugin - 3.6.0 + 3.7.0 **/com/microsoft/sqlserver/jdbc/SQLServerJdbc42.java @@ -185,6 +185,9 @@ ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + com.microsoft.sqlserver.jdbc + @@ -302,7 +305,7 @@ org.apache.maven.plugins maven-surefire-plugin - 2.21.0 + 2.22.0 diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/AESetup.java b/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/AESetup.java index 2bb77e4778..50ccf95f6f 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/AESetup.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/AESetup.java @@ -86,7 +86,7 @@ public class AESetup extends AbstractTest { * @throws TestAbortedException */ @BeforeAll - static void setUpConnection() throws TestAbortedException, Exception { + public static void setUpConnection() throws TestAbortedException, Exception { assumeTrue(13 <= new DBConnection(connectionString).getServerVersion(), TestResource.getResource("R_Incompat_SQLServerVersion")); @@ -122,7 +122,7 @@ static void setUpConnection() throws TestAbortedException, Exception { * @throws SQLException */ @AfterAll - private static void dropAll() throws SQLException { + public static void dropAll() throws SQLException { dropTables(stmt); dropCEK(stmt); dropCMK(stmt); diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/CallableStatementTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/CallableStatementTest.java index 8ab4ec0124..52078420bc 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/CallableStatementTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/CallableStatementTest.java @@ -112,7 +112,7 @@ public static void initCallableStatementTest() throws SQLException { } @AfterAll - private static void dropAll() throws SQLException { + public static void dropAll() throws SQLException { dropTables(); } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyCSVTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyCSVTest.java index ebd648a3cf..ec3ebf889f 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyCSVTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyCSVTest.java @@ -64,7 +64,7 @@ public class BulkCopyCSVTest extends AbstractTest { * Create connection, statement and generate path of resource file */ @BeforeAll - static void setUpConnection() { + public static void setUpConnection() { con = new DBConnection(connectionString); stmt = con.createStatement(); filePath = Utils.getCurrentClassPath(); @@ -75,7 +75,7 @@ static void setUpConnection() { */ @Test @DisplayName("Test SQLServerBulkCSVFileRecord") - void testCSV() { + public void testCSV() { try (SQLServerBulkCSVFileRecord fileRecord = new SQLServerBulkCSVFileRecord(filePath + inputFile, encoding, delimiter, true)) { testBulkCopyCSV(fileRecord, true); } @@ -89,7 +89,7 @@ void testCSV() { */ @Test @DisplayName("Test SQLServerBulkCSVFileRecord First line not being column name") - void testCSVFirstLineNotColumnName() { + public void testCSVFirstLineNotColumnName() { try (SQLServerBulkCSVFileRecord fileRecord = new SQLServerBulkCSVFileRecord(filePath + inputFileNoColumnName, encoding, delimiter, false)) { testBulkCopyCSV(fileRecord, false); } @@ -105,7 +105,7 @@ void testCSVFirstLineNotColumnName() { */ @Test @DisplayName("Test SQLServerBulkCSVFileRecord with passing file from url") - void testCSVFromURL() throws SQLException { + public void testCSVFromURL() throws SQLException { try (InputStream csvFileInputStream = new URL( "https://raw.githubusercontent.com/Microsoft/mssql-jdbc/master/src/test/resources/BulkCopyCSVTestInput.csv").openStream(); SQLServerBulkCSVFileRecord fileRecord = new SQLServerBulkCSVFileRecord(csvFileInputStream, encoding, delimiter, true)) { @@ -223,7 +223,7 @@ static void validateValuesFromCSV(DBTable destinationTable, * @throws SQLException */ @AfterAll - static void tearConnection() throws SQLException { + public static void tearConnection() throws SQLException { stmt.close(); con.close(); } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyColumnMappingTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyColumnMappingTest.java index 49e72a880f..de669f17bc 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyColumnMappingTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyColumnMappingTest.java @@ -45,20 +45,20 @@ public class BulkCopyColumnMappingTest extends BulkCopyTestSetUp { * Create connection, statement and generate path of resource file */ @BeforeAll - static void setUpConnection() { + public static void setUpConnection() { con = new DBConnection(connectionString); stmt = con.createStatement(); } @AfterAll - static void closeConnection() throws SQLException { + public static void closeConnection() throws SQLException { stmt.close(); con.close(); } @Test @DisplayName("BulkCopy:test no explicit column mapping") - void testNoExplicitCM() { + public void testNoExplicitCM() { // create dest table DBTable destTable = sourceTable.cloneSchema(); @@ -72,7 +72,7 @@ void testNoExplicitCM() { @Test @DisplayName("BulkCopy:test explicit column mapping") - void testExplicitCM() { + public void testExplicitCM() { // create dest table DBTable destTable = sourceTable.cloneSchema(); @@ -107,7 +107,7 @@ void testExplicitCM() { @Test @DisplayName("BulkCopy:test unicode column mapping") - void testUnicodeCM() { + public void testUnicodeCM() { // create source unicode table DBTable sourceTableUnicode = new DBTable(true, true); @@ -147,7 +147,7 @@ void testUnicodeCM() { @Test @DisplayName("BulkCopy:test repetative column mapping") - void testRepetativeCM() { + public void testRepetativeCM() { // create source table DBTable sourceTable1 = new DBTable(true); @@ -207,7 +207,7 @@ void testRepetativeCM() { @Test @DisplayName("BulkCopy:test implicit mismatched column mapping") - void testImplicitMismatchCM() { + public void testImplicitMismatchCM() { // create non unicode dest table with different schema from source table DBTable destTable = new DBTable(true, false, true); @@ -242,7 +242,7 @@ void testImplicitMismatchCM() { @Test @DisplayName("BulkCopy:test invalid column mapping") - void testInvalidCM() { + public void testInvalidCM() { // create dest table DBTable destTable = sourceTable.cloneSchema(); diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyConnectionTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyConnectionTest.java index bbc3f71009..fe2af2da9d 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyConnectionTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyConnectionTest.java @@ -43,7 +43,7 @@ public class BulkCopyConnectionTest extends BulkCopyTestSetUp { * @return */ @TestFactory - Stream generateBulkCopyConstructorTest() { + public Stream generateBulkCopyConstructorTest() { List testData = createTestDatatestBulkCopyConstructor(); // had to avoid using lambdas as we need to test against java7 return testData.stream().map(new Function() { @@ -65,7 +65,7 @@ public void execute() { * @return */ @TestFactory - Stream generateBulkCopyOptionsTest() { + public Stream generateBulkCopyOptionsTest() { List testData = createTestDatatestBulkCopyOption(); return testData.stream().map(new Function() { @Override @@ -85,7 +85,7 @@ public void execute() { */ @Test @DisplayName("BulkCopy:test uninitialized Connection") - void testInvalidConnection1() { + public void testInvalidConnection1() { assertThrows(SQLException.class, new org.junit.jupiter.api.function.Executable() { @Override public void execute() throws SQLException { @@ -102,7 +102,7 @@ public void execute() throws SQLException { */ @Test @DisplayName("BulkCopy:test uninitialized SQLServerConnection") - void testInvalidConnection2() { + public void testInvalidConnection2() { assertThrows(SQLException.class, new org.junit.jupiter.api.function.Executable() { @Override public void execute() throws SQLException { @@ -119,7 +119,7 @@ public void execute() throws SQLException { */ @Test @DisplayName("BulkCopy:test empty connection string") - void testInvalidConnection3() { + public void testInvalidConnection3() { assertThrows(SQLException.class, new org.junit.jupiter.api.function.Executable() { @Override public void execute() throws SQLException { @@ -136,7 +136,7 @@ public void execute() throws SQLException { */ @Test @DisplayName("BulkCopy:test null connenction string") - void testInvalidConnection4() { + public void testInvalidConnection4() { assertThrows(SQLException.class, new org.junit.jupiter.api.function.Executable() { @Override public void execute() throws SQLException { @@ -153,7 +153,7 @@ public void execute() throws SQLException { */ @Test @DisplayName("BulkCopy:test null SQLServerBulkCopyOptions") - void testEmptyBulkCopyOptions() { + public void testEmptyBulkCopyOptions() { BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString); bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false); SQLServerBulkCopyOptions option = null; diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyISQLServerBulkRecordTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyISQLServerBulkRecordTest.java index 83316cc226..1e9d291148 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyISQLServerBulkRecordTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyISQLServerBulkRecordTest.java @@ -38,7 +38,7 @@ public class BulkCopyISQLServerBulkRecordTest extends AbstractTest { @Test - void testISQLServerBulkRecord() throws SQLException { + public void testISQLServerBulkRecord() throws SQLException { try (DBConnection con = new DBConnection(connectionString); DBStatement stmt = con.createStatement()) { DBTable dstTable = new DBTable(true); stmt.createTable(dstTable); diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyTestSetUp.java b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyTestSetUp.java index f916336de6..2be438ccc0 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyTestSetUp.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyTestSetUp.java @@ -33,7 +33,7 @@ public class BulkCopyTestSetUp extends AbstractTest { * @throws SQLException */ @BeforeAll - static void setUpSourceTable() throws SQLException { + public static void setUpSourceTable() throws SQLException { try (DBConnection con = new DBConnection(connectionString); DBStatement stmt = con.createStatement(); DBPreparedStatement pstmt = new DBPreparedStatement(con);) { @@ -48,7 +48,7 @@ static void setUpSourceTable() throws SQLException { * @throws SQLException */ @AfterAll - static void dropSourceTable() throws SQLException { + public static void dropSourceTable() throws SQLException { try (DBConnection con = new DBConnection(connectionString); DBStatement stmt = con.createStatement()) { stmt.dropTable(sourceTable); diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyTimeoutTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyTimeoutTest.java index 0db8f67a78..047730c84d 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyTimeoutTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyTimeoutTest.java @@ -37,7 +37,7 @@ public class BulkCopyTimeoutTest extends BulkCopyTestSetUp { */ @Test @DisplayName("BulkCopy:test zero timeout") - void testZeroTimeOut() throws SQLException { + public void testZeroTimeOut() throws SQLException { testBulkCopyWithTimeout(0); } @@ -48,7 +48,7 @@ void testZeroTimeOut() throws SQLException { */ @Test @DisplayName("BulkCopy:test negative timeout") - void testNegativeTimeOut() throws SQLException { + public void testNegativeTimeOut() throws SQLException { assertThrows(SQLException.class, new org.junit.jupiter.api.function.Executable() { @Override public void execute() throws SQLException { diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/connection/ConnectionWrapper43Test.java b/src/test/java/com/microsoft/sqlserver/jdbc/connection/ConnectionWrapper43Test.java index 2046739917..2427be5a39 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/connection/ConnectionWrapper43Test.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/connection/ConnectionWrapper43Test.java @@ -58,7 +58,7 @@ public void SQLServerConnection43Test() throws SQLException { } @BeforeAll - private static void setupConnection() throws SQLException { + public static void setupConnection() throws SQLException { connection = DriverManager.getConnection(connectionString); DatabaseMetaData metadata = connection.getMetaData(); @@ -67,7 +67,7 @@ private static void setupConnection() throws SQLException { } @AfterAll - private static void terminateVariation() throws SQLException { + public static void terminateVariation() throws SQLException { if (null != connection) { connection.close(); } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataForeignKeyTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataForeignKeyTest.java index 403d876e23..737a87a992 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataForeignKeyTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataForeignKeyTest.java @@ -47,7 +47,7 @@ public class DatabaseMetaDataForeignKeyTest extends AbstractTest { private static String catalog = null; @BeforeAll - private static void setupVariation() throws SQLException { + public static void setupVariation() throws SQLException { conn = (SQLServerConnection) DriverManager.getConnection(connectionString); SQLServerStatement stmt = (SQLServerStatement) conn.createStatement(); @@ -78,7 +78,7 @@ private static void setupVariation() throws SQLException { } @AfterAll - private static void terminateVariation() throws SQLException { + public static void terminateVariation() throws SQLException { conn = (SQLServerConnection) DriverManager.getConnection(connectionString); stmt = (SQLServerStatement) conn.createStatement(); diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/TVPWithSqlVariantTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/TVPWithSqlVariantTest.java index e62a18dced..6f8375773d 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/TVPWithSqlVariantTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/TVPWithSqlVariantTest.java @@ -458,7 +458,7 @@ private static String[] createNumericValues() { } @BeforeEach - private void testSetup() throws SQLException { + public void testSetup() throws SQLException { conn = (SQLServerConnection) DriverManager.getConnection(connectionString + ";sendStringParametersAsUnicode=true;"); stmt = (SQLServerStatement) conn.createStatement(); @@ -493,7 +493,7 @@ private void createTVPS() throws SQLException { } @AfterEach - private void terminateVariation() throws SQLException { + public void terminateVariation() throws SQLException { Utils.dropProcedureIfExists(procedureName, stmt); Utils.dropTableIfExists(destTable, stmt); dropTVPS(); diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPNumericTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPNumericTest.java index e96496e841..3366ca8507 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPNumericTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPNumericTest.java @@ -63,7 +63,7 @@ public void testNumericPresicionIssue211() throws SQLException { } @BeforeEach - private void testSetup() throws SQLException { + public void testSetup() throws SQLException { conn = new DBConnection(connectionString); stmt = conn.createStatement(); @@ -108,7 +108,7 @@ private void createTVPS() throws SQLException { } @AfterEach - private void terminateVariation() throws SQLException { + public void terminateVariation() throws SQLException { if (null != conn) { conn.close(); } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPResultSetCursorTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPResultSetCursorTest.java index 2557031fb8..d9f20db977 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPResultSetCursorTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPResultSetCursorTest.java @@ -412,7 +412,7 @@ private static void createPreocedure() throws SQLException { } @AfterEach - private void terminateVariation() throws SQLException { + public void terminateVariation() throws SQLException { if (null != conn) { conn.close(); } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPSchemaTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPSchemaTest.java index dd029ee1b0..6cdad7a328 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPSchemaTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPSchemaTest.java @@ -135,7 +135,7 @@ public void testTVPSchemaCallableInsertCommand() throws SQLException, IOExceptio } @BeforeEach - private void testSetup() throws SQLException { + public void testSetup() throws SQLException { conn = new DBConnection(connectionString); stmt = conn.createStatement(); @@ -213,7 +213,7 @@ private void createTVPS() throws SQLException { } @AfterEach - private void terminateVariation() throws SQLException { + public void terminateVariation() throws SQLException { if (null != conn) { conn.close(); } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPTypesTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPTypesTest.java index 5d4ec875cd..6c3e247096 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPTypesTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPTypesTest.java @@ -1,589 +1,588 @@ -/* - * Microsoft JDBC Driver for SQL Server - * - * Copyright(c) Microsoft Corporation All rights reserved. - * - * This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information. - */ -package com.microsoft.sqlserver.jdbc.tvp; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Arrays; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.platform.runner.JUnitPlatform; -import org.junit.runner.RunWith; - -import com.microsoft.sqlserver.jdbc.SQLServerCallableStatement; -import com.microsoft.sqlserver.jdbc.SQLServerDataTable; -import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement; -import com.microsoft.sqlserver.jdbc.SQLServerResultSet; -import com.microsoft.sqlserver.testframework.AbstractTest; - -@RunWith(JUnitPlatform.class) -public class TVPTypesTest extends AbstractTest { - - private static Connection conn = null; - static Statement stmt = null; - static ResultSet rs = null; - static SQLServerDataTable tvp = null; - private static String tvpName = "TVP"; - private static String table = "TVPTable"; - private static String procedureName = "procedureThatCallsTVP"; - private String value = null; - - /** - * Test a longvarchar support - * - * @throws SQLException - */ - @Test - public void testLongVarchar() throws SQLException { - createTables("varchar(max)"); - createTVPS("varchar(max)"); - - StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < 9000; i++) - buffer.append("a"); - - value = buffer.toString(); - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", java.sql.Types.LONGVARCHAR); - tvp.addRow(value); - - SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection - .prepareStatement("INSERT INTO " + table + " select * from ? ;"); - pstmt.setStructured(1, tvpName, tvp); - - pstmt.execute(); - - rs = conn.createStatement().executeQuery("select * from " + table); - while (rs.next()) { - assertEquals(rs.getString(1), value); - } - if (null != pstmt) { - pstmt.close(); - } - } - - /** - * Test longnvarchar - * - * @throws SQLException - */ - @Test - public void testLongNVarchar() throws SQLException { - createTables("nvarchar(max)"); - createTVPS("nvarchar(max)"); - - StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < 8001; i++) - buffer.append("سس"); - - value = buffer.toString(); - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", java.sql.Types.LONGNVARCHAR); - tvp.addRow(value); - - SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection - .prepareStatement("INSERT INTO " + table + " select * from ? ;"); - pstmt.setStructured(1, tvpName, tvp); - - pstmt.execute(); - - rs = conn.createStatement().executeQuery("select * from " + table); - while (rs.next()) { - assertEquals(rs.getString(1), value); - } - - if (null != pstmt) { - pstmt.close(); - } - } - - /** - * Test xml support - * - * @throws SQLException - */ - @Test - public void testXML() throws SQLException { - createTables("xml"); - createTVPS("xml"); - value = "Variable E" + "Variable F" + "API" - + "The following are Japanese chars." - + " Some UTF-8 encoded characters: �������"; - - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", java.sql.Types.SQLXML); - tvp.addRow(value); - - SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection - .prepareStatement("INSERT INTO " + table + " select * from ? ;"); - pstmt.setStructured(1, tvpName, tvp); - - pstmt.execute(); - - Connection con = DriverManager.getConnection(connectionString); - ResultSet rs = con.createStatement().executeQuery("select * from " + table); - while (rs.next()) - assertEquals(rs.getString(1), value); - - if (null != pstmt) { - pstmt.close(); - } - } - - /** - * Test ntext support - * - * @throws SQLException - */ - @Test - public void testnText() throws SQLException { - createTables("ntext"); - createTVPS("ntext"); - StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < 9000; i++) - buffer.append("س"); - value = buffer.toString(); - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", java.sql.Types.LONGNVARCHAR); - tvp.addRow(value); - - SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection - .prepareStatement("INSERT INTO " + table + " select * from ? ;"); - pstmt.setStructured(1, tvpName, tvp); - - pstmt.execute(); - - Connection con = DriverManager.getConnection(connectionString); - ResultSet rs = con.createStatement().executeQuery("select * from " + table); - while (rs.next()) - assertEquals(rs.getString(1), value); - - if (null != pstmt) { - pstmt.close(); - } - } - - /** - * Test text support - * - * @throws SQLException - */ - @Test - public void testText() throws SQLException { - createTables("text"); - createTVPS("text"); - StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < 9000; i++) - buffer.append("a"); - value = buffer.toString(); - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", java.sql.Types.LONGVARCHAR); - tvp.addRow(value); - - SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection - .prepareStatement("INSERT INTO " + table + " select * from ? ;"); - pstmt.setStructured(1, tvpName, tvp); - - pstmt.execute(); - - Connection con = DriverManager.getConnection(connectionString); - ResultSet rs = con.createStatement().executeQuery("select * from " + table); - while (rs.next()) - assertEquals(rs.getString(1), value); - - if (null != pstmt) { - pstmt.close(); - } - } - - /** - * Test text support - * - * @throws SQLException - */ - @Test - public void testImage() throws SQLException { - createTables("varbinary(max)"); - createTVPS("varbinary(max)"); - StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < 10000; i++) - buffer.append("a"); - value = buffer.toString(); - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", java.sql.Types.LONGVARBINARY); - tvp.addRow(value.getBytes()); - - SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection - .prepareStatement("INSERT INTO " + table + " select * from ? ;"); - pstmt.setStructured(1, tvpName, tvp); - - pstmt.execute(); - - Connection con = DriverManager.getConnection(connectionString); - ResultSet rs = con.createStatement().executeQuery("select * from " + table); - - while (rs.next()) - assertTrue(parseByte(rs.getBytes(1), value.getBytes())); - - if (null != pstmt) { - pstmt.close(); - } - } - - /** - * LongVarchar with StoredProcedure - * - * @throws SQLException - */ - @Test - public void testTVPLongVarcharStoredProcedure() throws SQLException { - createTables("varchar(max)"); - createTVPS("varchar(max)"); - createPreocedure(); - - StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < 8001; i++) - buffer.append("a"); - - value = buffer.toString(); - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", java.sql.Types.LONGVARCHAR); - tvp.addRow(value); - - final String sql = "{call " + procedureName + "(?)}"; - - SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql); - P_C_statement.setStructured(1, tvpName, tvp); - P_C_statement.execute(); - - rs = stmt.executeQuery("select * from " + table); - while (rs.next()) - assertEquals(rs.getString(1), value); - - if (null != P_C_statement) { - P_C_statement.close(); - } - } - - /** - * LongNVarchar with StoredProcedure - * - * @throws SQLException - */ - @Test - public void testTVPLongNVarcharStoredProcedure() throws SQLException { - createTables("nvarchar(max)"); - createTVPS("nvarchar(max)"); - createPreocedure(); - - StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < 8001; i++) - buffer.append("سس"); - value = buffer.toString(); - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", java.sql.Types.LONGNVARCHAR); - tvp.addRow(buffer.toString()); - - final String sql = "{call " + procedureName + "(?)}"; - - SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql); - P_C_statement.setStructured(1, tvpName, tvp); - P_C_statement.execute(); - - rs = stmt.executeQuery("select * from " + table); - while (rs.next()) - assertEquals(rs.getString(1), value); - - if (null != P_C_statement) { - P_C_statement.close(); - } - } - - /** - * XML with StoredProcedure - * - * @throws SQLException - */ - @Test - public void testTVPXMLStoredProcedure() throws SQLException { - createTables("xml"); - createTVPS("xml"); - createPreocedure(); - - value = "Variable E" + "Variable F" + "API" - + "The following are Japanese chars." - + " Some UTF-8 encoded characters: �������"; - - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", java.sql.Types.SQLXML); - tvp.addRow(value); - - final String sql = "{call " + procedureName + "(?)}"; - - SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql); - P_C_statement.setStructured(1, tvpName, tvp); - P_C_statement.execute(); - - rs = stmt.executeQuery("select * from " + table); - while (rs.next()) - assertEquals(rs.getString(1), value); - if (null != P_C_statement) { - P_C_statement.close(); - } - } - - /** - * Text with StoredProcedure - * - * @throws SQLException - */ - @Test - public void testTVPTextStoredProcedure() throws SQLException { - createTables("text"); - createTVPS("text"); - createPreocedure(); - - StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < 9000; i++) - buffer.append("a"); - value = buffer.toString(); - - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", java.sql.Types.LONGVARCHAR); - tvp.addRow(value); - - final String sql = "{call " + procedureName + "(?)}"; - - SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql); - P_C_statement.setStructured(1, tvpName, tvp); - P_C_statement.execute(); - - rs = stmt.executeQuery("select * from " + table); - while (rs.next()) - assertEquals(rs.getString(1), value); - if (null != P_C_statement) { - P_C_statement.close(); - } - } - - /** - * Text with StoredProcedure - * - * @throws SQLException - */ - @Test - public void testTVPNTextStoredProcedure() throws SQLException { - createTables("ntext"); - createTVPS("ntext"); - createPreocedure(); - - StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < 9000; i++) - buffer.append("س"); - value = buffer.toString(); - - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", java.sql.Types.LONGNVARCHAR); - tvp.addRow(value); - - final String sql = "{call " + procedureName + "(?)}"; - - SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql); - P_C_statement.setStructured(1, tvpName, tvp); - P_C_statement.execute(); - - rs = stmt.executeQuery("select * from " + table); - while (rs.next()) - assertEquals(rs.getString(1), value); - if (null != P_C_statement) { - P_C_statement.close(); - } - } - - /** - * Image with StoredProcedure acts the same as varbinary(max) - * - * @throws SQLException - */ - @Test - public void testTVPImageStoredProcedure() throws SQLException { - createTables("image"); - createTVPS("image"); - createPreocedure(); - - StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < 9000; i++) - buffer.append("a"); - value = buffer.toString(); - - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", java.sql.Types.LONGVARBINARY); - tvp.addRow(value.getBytes()); - - final String sql = "{call " + procedureName + "(?)}"; - - SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql); - P_C_statement.setStructured(1, tvpName, tvp); - P_C_statement.execute(); - - rs = stmt.executeQuery("select * from " + table); - while (rs.next()) - assertTrue(parseByte(rs.getBytes(1), value.getBytes())); - if (null != P_C_statement) { - P_C_statement.close(); - } - } - - /** - * Test a datetime support - * - * @throws SQLException - */ - @Test - public void testDateTime() throws SQLException { - createTables("datetime"); - createTVPS("datetime"); - - java.sql.Timestamp value = java.sql.Timestamp.valueOf("2007-09-23 10:10:10.123"); - - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", microsoft.sql.Types.DATETIME); - tvp.addRow(value); - - SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection - .prepareStatement("INSERT INTO " + table + " select * from ? ;"); - pstmt.setStructured(1, tvpName, tvp); - - pstmt.execute(); - - rs = conn.createStatement().executeQuery("select * from " + table); - while (rs.next()) { - assertEquals(((SQLServerResultSet) rs).getDateTime(1), value); - } - if (null != pstmt) { - pstmt.close(); - } - } - - /** - * Test a smalldatetime support - * - * @throws SQLException - */ - @Test - public void testSmallDateTime() throws SQLException { - createTables("smalldatetime"); - createTVPS("smalldatetime"); - - java.sql.Timestamp value = java.sql.Timestamp.valueOf("2007-09-23 10:10:10.123"); - java.sql.Timestamp returnValue = java.sql.Timestamp.valueOf("2007-09-23 10:10:00.0"); - - tvp = new SQLServerDataTable(); - tvp.addColumnMetadata("c1", microsoft.sql.Types.SMALLDATETIME); - tvp.addRow(value); - - SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection - .prepareStatement("INSERT INTO " + table + " select * from ? ;"); - pstmt.setStructured(1, tvpName, tvp); - - pstmt.execute(); - - rs = conn.createStatement().executeQuery("select * from " + table); - while (rs.next()) { - assertEquals(((SQLServerResultSet) rs).getSmallDateTime(1), returnValue); - } - if (null != pstmt) { - pstmt.close(); - } - } - - @BeforeEach - private void testSetup() throws SQLException { - conn = DriverManager.getConnection(connectionString); - stmt = conn.createStatement(); - - dropProcedure(); - dropTables(); - dropTVPS(); - } - - @AfterAll - public static void terminate() throws SQLException { - conn = DriverManager.getConnection(connectionString); - stmt = conn.createStatement(); - dropProcedure(); - dropTables(); - dropTVPS(); - } - - private static void dropProcedure() throws SQLException { - String sql = " IF EXISTS (select * from sysobjects where id = object_id(N'" + procedureName + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" - + " DROP PROCEDURE " + procedureName; - stmt.execute(sql); - } - - private static void dropTables() throws SQLException { - stmt.executeUpdate("if object_id('" + table + "','U') is not null" + " drop table " + table); - } - - private static void dropTVPS() throws SQLException { - stmt.executeUpdate("IF EXISTS (SELECT * FROM sys.types WHERE is_table_type = 1 AND name = '" + tvpName + "') " + " drop type " + tvpName); - } - - private static void createPreocedure() throws SQLException { - String sql = "CREATE PROCEDURE " + procedureName + " @InputData " + tvpName + " READONLY " + " AS " + " BEGIN " + " INSERT INTO " + table - + " SELECT * FROM @InputData" + " END"; - - stmt.execute(sql); - } - - private void createTables(String colType) throws SQLException { - String sql = "create table " + table + " (c1 " + colType + " null);"; - stmt.execute(sql); - } - - private void createTVPS(String colType) throws SQLException { - String TVPCreateCmd = "CREATE TYPE " + tvpName + " as table (c1 " + colType + " null)"; - stmt.executeUpdate(TVPCreateCmd); - } - - private boolean parseByte(byte[] expectedData, - byte[] retrieved) { - assertTrue(Arrays.equals(expectedData, Arrays.copyOf(retrieved, expectedData.length)), " unexpected BINARY value, expected"); - for (int i = expectedData.length; i < retrieved.length; i++) { - assertTrue(0 == retrieved[i], "unexpected data BINARY"); - } - return true; - } - - @AfterEach - private void terminateVariation() throws SQLException { - if (null != conn) { - conn.close(); - } - if (null != stmt) { - stmt.close(); - } - if (null != rs) { - rs.close(); - } - if (null != tvp) { - tvp.clear(); - } - } - -} \ No newline at end of file +/* + * Microsoft JDBC Driver for SQL Server + * + * Copyright(c) Microsoft Corporation All rights reserved. + * + * This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information. + */ +package com.microsoft.sqlserver.jdbc.tvp; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Arrays; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; + +import com.microsoft.sqlserver.jdbc.SQLServerCallableStatement; +import com.microsoft.sqlserver.jdbc.SQLServerDataTable; +import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement; +import com.microsoft.sqlserver.jdbc.SQLServerResultSet; +import com.microsoft.sqlserver.testframework.AbstractTest; + +@RunWith(JUnitPlatform.class) +public class TVPTypesTest extends AbstractTest { + + private static Connection conn = null; + static Statement stmt = null; + static ResultSet rs = null; + static SQLServerDataTable tvp = null; + private static String tvpName = "TVP"; + private static String table = "TVPTable"; + private static String procedureName = "procedureThatCallsTVP"; + private String value = null; + + /** + * Test a longvarchar support + * + * @throws SQLException + */ + @Test + public void testLongVarchar() throws SQLException { + createTables("varchar(max)"); + createTVPS("varchar(max)"); + + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < 9000; i++) + buffer.append("a"); + + value = buffer.toString(); + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", java.sql.Types.LONGVARCHAR); + tvp.addRow(value); + + SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection + .prepareStatement("INSERT INTO " + table + " select * from ? ;"); + pstmt.setStructured(1, tvpName, tvp); + + pstmt.execute(); + + rs = conn.createStatement().executeQuery("select * from " + table); + while (rs.next()) { + assertEquals(rs.getString(1), value); + } + if (null != pstmt) { + pstmt.close(); + } + } + + /** + * Test longnvarchar + * + * @throws SQLException + */ + @Test + public void testLongNVarchar() throws SQLException { + createTables("nvarchar(max)"); + createTVPS("nvarchar(max)"); + + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < 8001; i++) + buffer.append("سس"); + + value = buffer.toString(); + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", java.sql.Types.LONGNVARCHAR); + tvp.addRow(value); + + SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection + .prepareStatement("INSERT INTO " + table + " select * from ? ;"); + pstmt.setStructured(1, tvpName, tvp); + + pstmt.execute(); + + rs = conn.createStatement().executeQuery("select * from " + table); + while (rs.next()) { + assertEquals(rs.getString(1), value); + } + + if (null != pstmt) { + pstmt.close(); + } + } + + /** + * Test xml support + * + * @throws SQLException + */ + @Test + public void testXML() throws SQLException { + createTables("xml"); + createTVPS("xml"); + value = "Variable E" + "Variable F" + "API" + + "The following are Japanese chars." + + " Some UTF-8 encoded characters: �������"; + + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", java.sql.Types.SQLXML); + tvp.addRow(value); + + SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection + .prepareStatement("INSERT INTO " + table + " select * from ? ;"); + pstmt.setStructured(1, tvpName, tvp); + + pstmt.execute(); + + Connection con = DriverManager.getConnection(connectionString); + ResultSet rs = con.createStatement().executeQuery("select * from " + table); + while (rs.next()) + assertEquals(rs.getString(1), value); + + if (null != pstmt) { + pstmt.close(); + } + } + + /** + * Test ntext support + * + * @throws SQLException + */ + @Test + public void testnText() throws SQLException { + createTables("ntext"); + createTVPS("ntext"); + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < 9000; i++) + buffer.append("س"); + value = buffer.toString(); + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", java.sql.Types.LONGNVARCHAR); + tvp.addRow(value); + + SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection + .prepareStatement("INSERT INTO " + table + " select * from ? ;"); + pstmt.setStructured(1, tvpName, tvp); + + pstmt.execute(); + + Connection con = DriverManager.getConnection(connectionString); + ResultSet rs = con.createStatement().executeQuery("select * from " + table); + while (rs.next()) + assertEquals(rs.getString(1), value); + + if (null != pstmt) { + pstmt.close(); + } + } + + /** + * Test text support + * + * @throws SQLException + */ + @Test + public void testText() throws SQLException { + createTables("text"); + createTVPS("text"); + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < 9000; i++) + buffer.append("a"); + value = buffer.toString(); + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", java.sql.Types.LONGVARCHAR); + tvp.addRow(value); + + SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection + .prepareStatement("INSERT INTO " + table + " select * from ? ;"); + pstmt.setStructured(1, tvpName, tvp); + + pstmt.execute(); + + Connection con = DriverManager.getConnection(connectionString); + ResultSet rs = con.createStatement().executeQuery("select * from " + table); + while (rs.next()) + assertEquals(rs.getString(1), value); + + if (null != pstmt) { + pstmt.close(); + } + } + + /** + * Test text support + * + * @throws SQLException + */ + @Test + public void testImage() throws SQLException { + createTables("varbinary(max)"); + createTVPS("varbinary(max)"); + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < 10000; i++) + buffer.append("a"); + value = buffer.toString(); + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", java.sql.Types.LONGVARBINARY); + tvp.addRow(value.getBytes()); + + SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection + .prepareStatement("INSERT INTO " + table + " select * from ? ;"); + pstmt.setStructured(1, tvpName, tvp); + + pstmt.execute(); + + Connection con = DriverManager.getConnection(connectionString); + ResultSet rs = con.createStatement().executeQuery("select * from " + table); + + while (rs.next()) + assertTrue(parseByte(rs.getBytes(1), value.getBytes())); + + if (null != pstmt) { + pstmt.close(); + } + } + + /** + * LongVarchar with StoredProcedure + * + * @throws SQLException + */ + @Test + public void testTVPLongVarcharStoredProcedure() throws SQLException { + createTables("varchar(max)"); + createTVPS("varchar(max)"); + createPreocedure(); + + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < 8001; i++) + buffer.append("a"); + + value = buffer.toString(); + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", java.sql.Types.LONGVARCHAR); + tvp.addRow(value); + + final String sql = "{call " + procedureName + "(?)}"; + + SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql); + P_C_statement.setStructured(1, tvpName, tvp); + P_C_statement.execute(); + + rs = stmt.executeQuery("select * from " + table); + while (rs.next()) + assertEquals(rs.getString(1), value); + + if (null != P_C_statement) { + P_C_statement.close(); + } + } + + /** + * LongNVarchar with StoredProcedure + * + * @throws SQLException + */ + @Test + public void testTVPLongNVarcharStoredProcedure() throws SQLException { + createTables("nvarchar(max)"); + createTVPS("nvarchar(max)"); + createPreocedure(); + + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < 8001; i++) + buffer.append("سس"); + value = buffer.toString(); + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", java.sql.Types.LONGNVARCHAR); + tvp.addRow(buffer.toString()); + + final String sql = "{call " + procedureName + "(?)}"; + + SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql); + P_C_statement.setStructured(1, tvpName, tvp); + P_C_statement.execute(); + + rs = stmt.executeQuery("select * from " + table); + while (rs.next()) + assertEquals(rs.getString(1), value); + + if (null != P_C_statement) { + P_C_statement.close(); + } + } + + /** + * XML with StoredProcedure + * + * @throws SQLException + */ + @Test + public void testTVPXMLStoredProcedure() throws SQLException { + createTables("xml"); + createTVPS("xml"); + createPreocedure(); + + value = "Variable E" + "Variable F" + "API" + + "The following are Japanese chars." + + " Some UTF-8 encoded characters: �������"; + + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", java.sql.Types.SQLXML); + tvp.addRow(value); + + final String sql = "{call " + procedureName + "(?)}"; + + SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql); + P_C_statement.setStructured(1, tvpName, tvp); + P_C_statement.execute(); + + rs = stmt.executeQuery("select * from " + table); + while (rs.next()) + assertEquals(rs.getString(1), value); + if (null != P_C_statement) { + P_C_statement.close(); + } + } + + /** + * Text with StoredProcedure + * + * @throws SQLException + */ + @Test + public void testTVPTextStoredProcedure() throws SQLException { + createTables("text"); + createTVPS("text"); + createPreocedure(); + + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < 9000; i++) + buffer.append("a"); + value = buffer.toString(); + + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", java.sql.Types.LONGVARCHAR); + tvp.addRow(value); + + final String sql = "{call " + procedureName + "(?)}"; + + SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql); + P_C_statement.setStructured(1, tvpName, tvp); + P_C_statement.execute(); + + rs = stmt.executeQuery("select * from " + table); + while (rs.next()) + assertEquals(rs.getString(1), value); + if (null != P_C_statement) { + P_C_statement.close(); + } + } + + /** + * Text with StoredProcedure + * + * @throws SQLException + */ + @Test + public void testTVPNTextStoredProcedure() throws SQLException { + createTables("ntext"); + createTVPS("ntext"); + createPreocedure(); + + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < 9000; i++) + buffer.append("س"); + value = buffer.toString(); + + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", java.sql.Types.LONGNVARCHAR); + tvp.addRow(value); + + final String sql = "{call " + procedureName + "(?)}"; + + SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql); + P_C_statement.setStructured(1, tvpName, tvp); + P_C_statement.execute(); + + rs = stmt.executeQuery("select * from " + table); + while (rs.next()) + assertEquals(rs.getString(1), value); + if (null != P_C_statement) { + P_C_statement.close(); + } + } + + /** + * Image with StoredProcedure acts the same as varbinary(max) + * + * @throws SQLException + */ + @Test + public void testTVPImageStoredProcedure() throws SQLException { + createTables("image"); + createTVPS("image"); + createPreocedure(); + + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < 9000; i++) + buffer.append("a"); + value = buffer.toString(); + + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", java.sql.Types.LONGVARBINARY); + tvp.addRow(value.getBytes()); + + final String sql = "{call " + procedureName + "(?)}"; + + SQLServerCallableStatement P_C_statement = (SQLServerCallableStatement) connection.prepareCall(sql); + P_C_statement.setStructured(1, tvpName, tvp); + P_C_statement.execute(); + + rs = stmt.executeQuery("select * from " + table); + while (rs.next()) + assertTrue(parseByte(rs.getBytes(1), value.getBytes())); + if (null != P_C_statement) { + P_C_statement.close(); + } + } + + /** + * Test a datetime support + * + * @throws SQLException + */ + @Test + public void testDateTime() throws SQLException { + createTables("datetime"); + createTVPS("datetime"); + + java.sql.Timestamp value = java.sql.Timestamp.valueOf("2007-09-23 10:10:10.123"); + + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", microsoft.sql.Types.DATETIME); + tvp.addRow(value); + + SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection + .prepareStatement("INSERT INTO " + table + " select * from ? ;"); + pstmt.setStructured(1, tvpName, tvp); + + pstmt.execute(); + + rs = conn.createStatement().executeQuery("select * from " + table); + while (rs.next()) { + assertEquals(((SQLServerResultSet) rs).getDateTime(1), value); + } + if (null != pstmt) { + pstmt.close(); + } + } + + /** + * Test a smalldatetime support + * + * @throws SQLException + */ + @Test + public void testSmallDateTime() throws SQLException { + createTables("smalldatetime"); + createTVPS("smalldatetime"); + + java.sql.Timestamp value = java.sql.Timestamp.valueOf("2007-09-23 10:10:10.123"); + java.sql.Timestamp returnValue = java.sql.Timestamp.valueOf("2007-09-23 10:10:00.0"); + + tvp = new SQLServerDataTable(); + tvp.addColumnMetadata("c1", microsoft.sql.Types.SMALLDATETIME); + tvp.addRow(value); + + SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection + .prepareStatement("INSERT INTO " + table + " select * from ? ;"); + pstmt.setStructured(1, tvpName, tvp); + + pstmt.execute(); + + rs = conn.createStatement().executeQuery("select * from " + table); + while (rs.next()) { + assertEquals(((SQLServerResultSet) rs).getSmallDateTime(1), returnValue); + } + if (null != pstmt) { + pstmt.close(); + } + } + + @BeforeEach + public void testSetup() throws SQLException { + conn = DriverManager.getConnection(connectionString); + stmt = conn.createStatement(); + + dropProcedure(); + dropTables(); + dropTVPS(); + } + + @AfterAll + public static void terminate() throws SQLException { + conn = DriverManager.getConnection(connectionString); + stmt = conn.createStatement(); + dropProcedure(); + dropTables(); + dropTVPS(); + } + + private static void dropProcedure() throws SQLException { + String sql = " IF EXISTS (select * from sysobjects where id = object_id(N'" + procedureName + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + + " DROP PROCEDURE " + procedureName; + stmt.execute(sql); + } + + private static void dropTables() throws SQLException { + stmt.executeUpdate("if object_id('" + table + "','U') is not null" + " drop table " + table); + } + + private static void dropTVPS() throws SQLException { + stmt.executeUpdate("IF EXISTS (SELECT * FROM sys.types WHERE is_table_type = 1 AND name = '" + tvpName + "') " + " drop type " + tvpName); + } + + private static void createPreocedure() throws SQLException { + String sql = "CREATE PROCEDURE " + procedureName + " @InputData " + tvpName + " READONLY " + " AS " + " BEGIN " + " INSERT INTO " + table + + " SELECT * FROM @InputData" + " END"; + + stmt.execute(sql); + } + + private void createTables(String colType) throws SQLException { + String sql = "create table " + table + " (c1 " + colType + " null);"; + stmt.execute(sql); + } + + private void createTVPS(String colType) throws SQLException { + String TVPCreateCmd = "CREATE TYPE " + tvpName + " as table (c1 " + colType + " null)"; + stmt.executeUpdate(TVPCreateCmd); + } + + private boolean parseByte(byte[] expectedData, + byte[] retrieved) { + assertTrue(Arrays.equals(expectedData, Arrays.copyOf(retrieved, expectedData.length)), " unexpected BINARY value, expected"); + for (int i = expectedData.length; i < retrieved.length; i++) { + assertTrue(0 == retrieved[i], "unexpected data BINARY"); + } + return true; + } + + @AfterEach + public void terminateVariation() throws SQLException { + if (null != conn) { + conn.close(); + } + if (null != stmt) { + stmt.close(); + } + if (null != rs) { + rs.close(); + } + if (null != tvp) { + tvp.clear(); + } + } +} diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java index 2b322c47b7..1413bd93fc 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java @@ -2229,7 +2229,7 @@ public class TCUpdateCountWithTriggers { private final String triggerName = "[TCUpdateCountWithTriggersTrigger]"; @BeforeEach - private void setup() throws Exception { + public void setup() throws Exception { Connection con = DriverManager.getConnection(connectionString); con.setAutoCommit(false); Statement stmt = con.createStatement(); @@ -2364,7 +2364,7 @@ public class TCUpdateCountAfterRaiseError { private final String errorMessage50001InSqlAzure = "Error 50001, severity 17, state 1 was raised, but no message with that error number was found in sys.messages. If error is larger than 50000, make sure the user-defined message is added using sp_addmessage."; @BeforeEach - private void setup() throws Exception { + public void setup() throws Exception { Connection con = DriverManager.getConnection(connectionString); con.setAutoCommit(false); Statement stmt = con.createStatement(); @@ -2574,7 +2574,7 @@ public class TCNocount { private static final int NUM_ROWS = 3; @BeforeEach - private void setup() throws Exception { + public void setup() throws Exception { Connection con = DriverManager.getConnection(connectionString); con.setAutoCommit(false); Statement stmt = con.createStatement();