Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra committed Apr 3, 2019
1 parent e14177e commit 58a53c8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void init() throws SQLException {
@TestFactory
public Collection<DynamicTest> executeDynamicTests() {
List<Class<?>> classes = new ArrayList<Class<?>>(
Arrays.asList(Blob.class, Clob.class, DBBinaryStream.class, DBCharacterStream.class));
Arrays.asList(Blob.class, Clob.class, NClob.class, DBBinaryStream.class, DBCharacterStream.class));
List<Boolean> isResultSetTypes = new ArrayList<>(Arrays.asList(true, false));
Collection<DynamicTest> dynamicTests = new ArrayList<>();

Expand Down Expand Up @@ -163,7 +163,7 @@ private void testInvalidLobs(Class<?> lobClass, boolean isResultSet) throws SQLE
} catch (SQLException e) {
boolean verified = false;

if (lobClass == Clob.class)
if (lobClass == Clob.class || lobClass == NClob.class)
streamLength = ((DBInvalidUtil.InvalidClob) lob).length;
else if (lobClass == Blob.class)
streamLength = ((DBInvalidUtil.InvalidBlob) lob).length;
Expand All @@ -180,8 +180,8 @@ else if (lobClass == Blob.class)
}

// Case 2: CharacterStream or Clob.getCharacterStream threw IOException
if (lobClass == DBCharacterStream.class
|| (lobClass == Clob.class && ((DBInvalidUtil.InvalidClob) lob).stream != null)) {
if (lobClass == DBCharacterStream.class || ((lobClass == Clob.class || lobClass == NClob.class)
&& ((DBInvalidUtil.InvalidClob) lob).stream != null)) {
try (DBInvalidUtil.InvalidCharacterStream stream = lobClass == DBCharacterStream.class ? ((DBInvalidUtil.InvalidCharacterStream) lob)
: ((DBInvalidUtil.InvalidClob) lob).stream) {
if (stream.threwException) {
Expand Down Expand Up @@ -670,6 +670,8 @@ else if (lob instanceof DBBinaryStream)
ps.setBinaryStream(index, (InputStream) lob, length);
else if (lob instanceof Clob)
ps.setClob(index, (Clob) lob);
else if (lob instanceof NClob)
ps.setNClob(index, (NClob) lob);
else
ps.setBlob(index, (Blob) lob);
assertEquals(ps.executeUpdate(), 1, TestResource.getResource("R_incorrectUpdateCount"));
Expand All @@ -682,6 +684,8 @@ private void updateResultSet(ResultSet rs, Object lob, int index, int length) th
rs.updateBinaryStream(index, (InputStream) lob, length);
} else if (lob instanceof Clob) {
rs.updateClob(index, (Clob) lob);
} else if (lob instanceof NClob) {
rs.updateNClob(index, (NClob) lob);
} else {
rs.updateBlob(index, (Blob) lob);
}
Expand All @@ -703,7 +707,7 @@ private Object createLob(Class<?> lobClass) {
return new DBInvalidUtil().new InvalidCharacterStream(new String(data), streamLength < -1);
else if (lobClass == DBBinaryStream.class)
return new DBInvalidUtil().new InvalidBinaryStream(data, streamLength < -1);
if (lobClass == Clob.class) {
if (lobClass == Clob.class || lobClass == NClob.class) {
SqlType type = TestUtils.find(String.class);
Object expected = type.createdata(String.class, data);
return new DBInvalidUtil().new InvalidClob(expected, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ public static void setup() throws Exception {
try {
Assertions.assertNotNull(connectionString, TestResource.getResource("R_ConnectionStringNull"));
Class.forName(Constants.MSSQL_JDBC_PACKAGE + ".SQLServerDriver");
connection = getConnection();
if (!SQLServerDriver.isRegistered())
SQLServerDriver.register();
if (null == connection || connection.isClosed()) {
connection = getConnection();
}
isSqlAzureOrAzureDW(connection);
} catch (Exception e) {
throw e;
Expand Down Expand Up @@ -203,19 +207,14 @@ protected static SQLServerConnection getConnection() throws SQLException {
@AfterAll
public static void teardown() throws Exception {
try {
if (connection != null && !connection.isClosed()) {
if (null != connection && !connection.isClosed()) {
connection.close();
}
} finally {
connection = null;
}
}

@BeforeAll
public static void registerDriver() throws Exception {
SQLServerDriver.register();
}

/**
* Read variable from property files if found null try to read from env.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Logger;

import com.microsoft.sqlserver.jdbc.TestUtils.DBBinaryStream;
Expand Down Expand Up @@ -67,8 +66,8 @@ public long length() throws SQLException {
long ret = actualLength;
long actual = ret;
if (invalidLength == -1) {
int choose = ThreadLocalRandom.current().nextInt(5);
int randomInt = 1 + ThreadLocalRandom.current().nextInt(diff);
int choose = Constants.RANDOM.nextInt(5);
int randomInt = 1 + Constants.RANDOM.nextInt(diff);

switch (choose) {
case 0: // more than ret
Expand Down Expand Up @@ -189,8 +188,8 @@ public int read(char[] cbuf, int off, int len) throws IOException {
int ret = super.read(cbuf, off, len);
int actual = ret;
if (!returnValid) {
int choose = ThreadLocalRandom.current().nextInt(5);
int randomInt = 1 + ThreadLocalRandom.current().nextInt(diff);
int choose = Constants.RANDOM.nextInt(5);
int randomInt = 1 + Constants.RANDOM.nextInt(diff);

switch (choose) {
case 0: // more than ret
Expand Down Expand Up @@ -247,8 +246,8 @@ public long length() throws SQLException {
long ret = actualLength;
long actual = ret;
if (invalidLength == -1) {
int choose = ThreadLocalRandom.current().nextInt(5);
int randomInt = 1 + ThreadLocalRandom.current().nextInt(diff);
int choose = Constants.RANDOM.nextInt(5);
int randomInt = 1 + Constants.RANDOM.nextInt(diff);

switch (choose) {
case 0: // more than ret
Expand Down Expand Up @@ -342,7 +341,7 @@ public InputStream getBinaryStream(long pos, long length) throws SQLException {
*/
public class InvalidBinaryStream extends DBBinaryStream {
final int diff = 5;
private boolean _returnValid = false; // Perfom invalid actions at most once
private boolean _returnValid = false; // Perform invalid actions at most once

/**
* Constructor
Expand All @@ -360,8 +359,8 @@ public int read(byte[] bytes, int off, int len) {
int ret = super.read(bytes, off, len);
int actual = ret;
if (!_returnValid) {
int choose = ThreadLocalRandom.current().nextInt(4);
int randomInt = 1 + ThreadLocalRandom.current().nextInt(diff);
int choose = Constants.RANDOM.nextInt(4);
int randomInt = 1 + Constants.RANDOM.nextInt(diff);
switch (choose) {
case 0: // greater than ret
actual = ret + randomInt;
Expand Down

0 comments on commit 58a53c8

Please sign in to comment.