-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from v-suhame/BulkCopyTestCases_v2
BulkCopy additional test cases
- Loading branch information
Showing
6 changed files
with
618 additions
and
231 deletions.
There are no files selected for viewing
217 changes: 217 additions & 0 deletions
217
src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyConnectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyTestSetUp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.