Skip to content

Commit

Permalink
more javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
Suraiya Hameed committed Jan 9, 2017
1 parent 3102bc4 commit 435c925
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 247 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
// ---------------------------------------------------------------------------------------------------------------------------------
// File: BulkCopyTestConnection.java
//
//
// Microsoft JDBC Driver for SQL Server
// Copyright(c) Microsoft Corporation
// All rights reserved.
// MIT License
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"),
// to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense,
// and / or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions :
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ---------------------------------------------------------------------------------------------------------------------------------
/*
* Microsoft JDBC Driver for SQL Server
*
* Copyright(c) 2016 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.bulkCopy;

import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -47,6 +30,9 @@
import com.microsoft.sqlserver.jdbc.SQLServerConnection;
import com.microsoft.sqlserver.jdbc.SQLServerException;

/**
* Test BulkCopy Connection Constructor and BulkCopyOption
*/
@RunWith(JUnitPlatform.class)
@DisplayName("BulkCopy Connection Test")
public class BulkCopyConnectionTest extends BulkCopyTestSetUp {
Expand All @@ -66,7 +52,7 @@ public DynamicTest apply(final BulkCopyTestWrapper datum) {
return DynamicTest.dynamicTest("Testing " + datum.testName, new org.junit.jupiter.api.function.Executable() {
@Override
public void execute() {
BulkCopyTestUtil.performBulkCopy(datum,sourceTable);
BulkCopyTestUtil.performBulkCopy(datum, sourceTable);
}
});
}
Expand All @@ -87,13 +73,16 @@ public DynamicTest apply(final BulkCopyTestWrapper datum) {
return DynamicTest.dynamicTest("Testing " + datum.testName, new org.junit.jupiter.api.function.Executable() {
@Override
public void execute() {
BulkCopyTestUtil.performBulkCopy(datum,sourceTable);
BulkCopyTestUtil.performBulkCopy(datum, sourceTable);
}
});
}
});
}

/**
* BulkCopy:test uninitialized Connection
*/
@Test
@DisplayName("BulkCopy:test uninitialized Connection")
void testInvalidConnection_1() {
Expand All @@ -106,6 +95,9 @@ public void execute() throws SQLServerException {
});
}

/**
* BulkCopy:test uninitialized SQLServerConnection
*/
@Test
@DisplayName("BulkCopy:test uninitialized SQLServerConnection")
void testInvalidConnection_2() {
Expand All @@ -118,6 +110,9 @@ public void execute() throws SQLServerException {
});
}

/**
* BulkCopy:test empty connenction string
*/
@Test
@DisplayName("BulkCopy:test empty connenction string")
void testInvalidConnection_3() {
Expand All @@ -130,6 +125,9 @@ public void execute() throws SQLServerException {
});
}

/**
* BulkCopy:test null connenction string
*/
@Test
@DisplayName("BulkCopy:test null connenction string")
void testInvalidConnection_4() {
Expand All @@ -142,6 +140,9 @@ public void execute() throws SQLServerException {
});
}

/**
* BulkCopy:test null SQLServerBulkCopyOptions
*/
@Test
@DisplayName("BulkCopy:test null SQLServerBulkCopyOptions")
void testEmptyBulkCopyOptions() {
Expand All @@ -150,9 +151,14 @@ void testEmptyBulkCopyOptions() {
SQLServerBulkCopyOptions option = null;
bulkWrapper.useBulkCopyOptions(true);
bulkWrapper.setBulkOptions(option);
BulkCopyTestUtil.performBulkCopy(bulkWrapper,sourceTable);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable);
}


/**
* Generate BulkCopyTestWrapper objects with data for testing BulkCopyConstructor
*
* @return
*/
List<BulkCopyTestWrapper> createTestData_testBulkCopyConstructor() {
String testCaseName = "BulkCopyConstructor ";
List<BulkCopyTestWrapper> testData = new ArrayList<BulkCopyTestWrapper>();
Expand All @@ -169,6 +175,11 @@ List<BulkCopyTestWrapper> createTestData_testBulkCopyConstructor() {
return testData;
}

/**
* Generate BulkCopyTestWrapper objects with data for testing BulkCopyOption
*
* @return
*/
private List<BulkCopyTestWrapper> createTestData_testBulkCopyOption() {
String testCaseName = "BulkCopyOption ";
List<BulkCopyTestWrapper> testData = new ArrayList<BulkCopyTestWrapper>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
// ---------------------------------------------------------------------------------------------------------------------------------
// File: BulkCopyTestSetUp.java
//
//
// Microsoft JDBC Driver for SQL Server
// Copyright(c) Microsoft Corporation
// All rights reserved.
// MIT License
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"),
// to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense,
// and / or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions :
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ---------------------------------------------------------------------------------------------------------------------------------
/*
* Microsoft JDBC Driver for SQL Server
*
* Copyright(c) 2016 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.bulkCopy;

import org.junit.jupiter.api.AfterAll;
Expand All @@ -34,11 +17,17 @@
import com.microsoft.sqlserver.testframework.DBStatement;
import com.microsoft.sqlserver.testframework.DBTable;;

/**
* Create and drop source table needed for testing bulk copy
*/
@RunWith(JUnitPlatform.class)
public class BulkCopyTestSetUp extends AbstractTest {

static DBTable sourceTable;

/**
* Create source table needed for testing bulk copy
*/
@BeforeAll
static void setUpSourceTable() {
DBConnection con = null;
Expand All @@ -55,6 +44,9 @@ static void setUpSourceTable() {
}
}

/**
* drop source table after testing bulk copy
*/
@AfterAll
static void dropSourceTable() {
DBConnection con = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* Microsoft JDBC Driver for SQL Server
*
* Copyright(c) 2016 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.bulkCopy;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.JDBCType;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;

import com.microsoft.sqlserver.jdbc.SQLServerBulkCopy;
import com.microsoft.sqlserver.testframework.DBConnection;
import com.microsoft.sqlserver.testframework.DBResultSet;
import com.microsoft.sqlserver.testframework.DBStatement;
import com.microsoft.sqlserver.testframework.DBTable;

/**
* Utility class
*/
class BulkCopyTestUtil {

/**
* perform bulk copy using source table and validate bulkcopy
*
* @param wrapper
* @param sourceTable
*/
static void performBulkCopy(BulkCopyTestWrapper wrapper, DBTable sourceTable) {
performBulkCopy(wrapper, sourceTable, false);
}

/**
* perform bulk copy using source table
*
* @param wrapper
* @param sourceTable
* @param validateResult
*/
static void performBulkCopy(BulkCopyTestWrapper wrapper, DBTable sourceTable, boolean validateResult) {
DBConnection con = null;
DBStatement stmt = null;
DBTable destinationTable = null;
try {
con = new DBConnection(wrapper.getConnectionString());
stmt = con.createStatement();

destinationTable = sourceTable.cloneSchema();
stmt.createTable(destinationTable);
DBResultSet srcResultSet = stmt.executeQuery("SELECT * FROM " + sourceTable.getEscapedTableName() + ";");
SQLServerBulkCopy bulkCopy;
if (wrapper.isUsingConnection()) {
bulkCopy = new SQLServerBulkCopy((Connection) con.product());
}
else {
bulkCopy = new SQLServerBulkCopy(wrapper.getConnectionString());
}
if (wrapper.isUsingBulkCopyOptions()) {
bulkCopy.setBulkCopyOptions(wrapper.getBulkOptions());
}
bulkCopy.setDestinationTableName(destinationTable.getEscapedTableName());
bulkCopy.writeToServer((ResultSet) srcResultSet.product());
bulkCopy.close();
if (validateResult) {
validateValues(con, sourceTable, destinationTable);
}
}
catch (SQLException ex) {
fail(ex.getMessage());
}
finally {
stmt.dropTable(destinationTable);
con.close();
}
}

/**
* validate if same values are in both source and destination table
*
* @param con
* @param sourceTable
* @param destinationTable
* @throws SQLException
*/
static void validateValues(DBConnection con, DBTable sourceTable, DBTable destinationTable) throws SQLException {
DBStatement srcStmt = con.createStatement();
DBStatement dstStmt = con.createStatement();
DBResultSet srcResultSet = srcStmt.executeQuery("SELECT * FROM " + sourceTable.getEscapedTableName() + ";");
DBResultSet dstResultSet = dstStmt.executeQuery("SELECT * FROM " + destinationTable.getEscapedTableName() + ";");
ResultSetMetaData destMeta = ((ResultSet) dstResultSet.product()).getMetaData();
int totalColumns = destMeta.getColumnCount();

// verify data from sourceType and resultSet
while (srcResultSet.next() && dstResultSet.next())
for (int i = 1; i <= totalColumns; i++) {
// TODO: check row and column count in both the tables

Object srcValue, dstValue;
srcValue = srcResultSet.getObject(i);
dstValue = dstResultSet.getObject(i);
// Bulkcopy doesn't guarantee order of insertion -
// if we need to test several rows either use primary key or
// validate result based on sql JOIN
switch (destMeta.getColumnType(i)) {
case java.sql.Types.BIGINT:
assertTrue((((Long) srcValue).longValue() == ((Long) dstValue).longValue()), "Unexpected bigint value");
break;

case java.sql.Types.INTEGER:
assertTrue((((Integer) srcValue).intValue() == ((Integer) dstValue).intValue()), "Unexpected int value");
break;

case java.sql.Types.SMALLINT:
case java.sql.Types.TINYINT:
assertTrue((((Short) srcValue).shortValue() == ((Short) dstValue).shortValue()), "Unexpected smallint/tinyint value");
break;

case java.sql.Types.BIT:
assertTrue((((Boolean) srcValue).booleanValue() == ((Boolean) dstValue).booleanValue()), "Unexpected bit value");
break;

case java.sql.Types.DECIMAL:
case java.sql.Types.NUMERIC:
assertTrue(0 == (((BigDecimal) srcValue).compareTo((BigDecimal) dstValue)),
"Unexpected decimal/numeric/money/smallmoney value");
break;

case java.sql.Types.DOUBLE:
assertTrue((((Double) srcValue).doubleValue() == ((Double) dstValue).doubleValue()), "Unexpected float value");
break;

case java.sql.Types.REAL:
assertTrue((((Float) srcValue).floatValue() == ((Float) dstValue).floatValue()), "Unexpected real value");
break;

case java.sql.Types.VARCHAR:
case java.sql.Types.NVARCHAR:
assertTrue((((String) srcValue).equals((String) dstValue)), "Unexpected varchar/nvarchar value ");
break;

case java.sql.Types.CHAR:
case java.sql.Types.NCHAR:
assertTrue((((String) srcValue).equals((String) dstValue)), "Unexpected char/nchar value ");
break;

case java.sql.Types.TIMESTAMP:
assertTrue((((Timestamp) srcValue).getTime() == (((Timestamp) dstValue).getTime())),
"Unexpected datetime/smalldatetime/datetime2 value");
break;

case java.sql.Types.DATE:
assertTrue((((Date) srcValue).getTime() == (((Date) dstValue).getTime())), "Unexpected datetime value");
break;

case java.sql.Types.TIME:
assertTrue(((Time) srcValue).getTime() == ((Time) dstValue).getTime(), "Unexpected time value ");
break;

case microsoft.sql.Types.DATETIMEOFFSET:
assertTrue(0 == ((microsoft.sql.DateTimeOffset) srcValue).compareTo((microsoft.sql.DateTimeOffset) dstValue),
"Unexpected time value ");
break;

default:
fail("Unhandled JDBCType " + JDBCType.valueOf(destMeta.getColumnType(i)));
break;
}
}
}
}
Loading

0 comments on commit 435c925

Please sign in to comment.