Skip to content

Commit

Permalink
Merge pull request #110 from v-suhame/BulkCopyTestCases_v2
Browse files Browse the repository at this point in the history
BulkCopy additional test cases
  • Loading branch information
v-nisidh authored Jan 9, 2017
2 parents 9b0ce65 + 435c925 commit a84c376
Show file tree
Hide file tree
Showing 6 changed files with 618 additions and 231 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/*
* 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;
import static org.junit.jupiter.api.Assertions.fail;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;
import java.util.stream.Stream;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import com.microsoft.sqlserver.jdbc.SQLServerBulkCopy;
import com.microsoft.sqlserver.jdbc.SQLServerBulkCopyOptions;
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 {

/**
* Generate dynamic tests to test all SQLServerBulkCopy constructor
*
* @return
*/
@TestFactory
Stream<DynamicTest> generateBulkCopyConstructorTest() {
List<BulkCopyTestWrapper> testData = createTestData_testBulkCopyConstructor();
// had to avoid using lambdas as we need to test against java7
return testData.stream().map(new Function<BulkCopyTestWrapper, DynamicTest>() {
@Override
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);
}
});
}
});
}

/**
* Generate dynamic tests to test with various SQLServerBulkCopyOptions
*
* @return
*/
@TestFactory
Stream<DynamicTest> generateBulkCopyOptionsTest() {
List<BulkCopyTestWrapper> testData = createTestData_testBulkCopyOption();
return testData.stream().map(new Function<BulkCopyTestWrapper, DynamicTest>() {
@Override
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);
}
});
}
});
}

/**
* BulkCopy:test uninitialized Connection
*/
@Test
@DisplayName("BulkCopy:test uninitialized Connection")
void testInvalidConnection_1() {
assertThrows(SQLServerException.class, new org.junit.jupiter.api.function.Executable() {
@Override
public void execute() throws SQLServerException {
Connection con = null;
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
}
});
}

/**
* BulkCopy:test uninitialized SQLServerConnection
*/
@Test
@DisplayName("BulkCopy:test uninitialized SQLServerConnection")
void testInvalidConnection_2() {
assertThrows(SQLServerException.class, new org.junit.jupiter.api.function.Executable() {
@Override
public void execute() throws SQLServerException {
SQLServerConnection con = null;
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
}
});
}

/**
* BulkCopy:test empty connenction string
*/
@Test
@DisplayName("BulkCopy:test empty connenction string")
void testInvalidConnection_3() {
assertThrows(SQLServerException.class, new org.junit.jupiter.api.function.Executable() {
@Override
public void execute() throws SQLServerException {
String connectionUrl = " ";
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(connectionUrl);
}
});
}

/**
* BulkCopy:test null connenction string
*/
@Test
@DisplayName("BulkCopy:test null connenction string")
void testInvalidConnection_4() {
assertThrows(SQLServerException.class, new org.junit.jupiter.api.function.Executable() {
@Override
public void execute() throws SQLServerException {
String connectionUrl = null;
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(connectionUrl);
}
});
}

/**
* BulkCopy:test null SQLServerBulkCopyOptions
*/
@Test
@DisplayName("BulkCopy:test null SQLServerBulkCopyOptions")
void testEmptyBulkCopyOptions() {
BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
SQLServerBulkCopyOptions option = null;
bulkWrapper.useBulkCopyOptions(true);
bulkWrapper.setBulkOptions(option);
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>();
BulkCopyTestWrapper bulkWrapper1 = new BulkCopyTestWrapper(connectionString);
bulkWrapper1.testName = testCaseName;
bulkWrapper1.setUsingConnection(true);
testData.add(bulkWrapper1);

BulkCopyTestWrapper bulkWrapper2 = new BulkCopyTestWrapper(connectionString);
bulkWrapper2.testName = testCaseName;
bulkWrapper2.setUsingConnection(false);
testData.add(bulkWrapper2);

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>();

Class<SQLServerBulkCopyOptions> bulkOptions = SQLServerBulkCopyOptions.class;
Method[] methods = bulkOptions.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {

// set bulkCopy Option if return is void and input is boolean
if (0 != methods[i].getParameterTypes().length && boolean.class == methods[i].getParameterTypes()[0]) {
try {

BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.testName = testCaseName;
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);

SQLServerBulkCopyOptions option = new SQLServerBulkCopyOptions();
if (!(methods[i].getName()).equalsIgnoreCase("setUseInternalTransaction")
&& !(methods[i].getName()).equalsIgnoreCase("setAllowEncryptedValueModifications")) {
methods[i].invoke(option, true);
bulkWrapper.useBulkCopyOptions(true);
bulkWrapper.setBulkOptions(option);
bulkWrapper.testName += methods[i].getName() + ";";
testData.add(bulkWrapper);
}
}
catch (Exception ex) {
fail(ex.getMessage());
}
}

}
return testData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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;
import org.junit.jupiter.api.BeforeAll;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.DBConnection;
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;
DBStatement stmt = null;
try {
con = new DBConnection(connectionString);
stmt = con.createStatement();
sourceTable = new DBTable(true);
stmt.createTable(sourceTable);
stmt.populateTable(sourceTable);
}
finally {
con.close();
}
}

/**
* drop source table after testing bulk copy
*/
@AfterAll
static void dropSourceTable() {
DBConnection con = null;
DBStatement stmt = null;
try {
con = new DBConnection(connectionString);
stmt = con.createStatement();
stmt.dropTable(sourceTable);
}
finally {
con.close();
}
}

}
Loading

0 comments on commit a84c376

Please sign in to comment.