From 0e60aa016fde8b300b2ed58c4fe6a565ee64f7ba Mon Sep 17 00:00:00 2001 From: Harshit Gangal Date: Tue, 21 Jan 2025 14:57:13 +0530 Subject: [PATCH] remove powermock dependency and update test to use mockito when possible Signed-off-by: Harshit Gangal --- java/jdbc/pom.xml | 19 - .../io/vitess/jdbc/FieldWithMetadataTest.java | 96 +--- .../io/vitess/jdbc/VitessConnectionTest.java | 16 +- .../jdbc/VitessDatabaseMetadataTest.java | 455 +----------------- .../jdbc/VitessParameterMetaDataTest.java | 29 +- .../jdbc/VitessPreparedStatementTest.java | 217 ++++----- .../io/vitess/jdbc/VitessResultSetTest.java | 231 +-------- .../io/vitess/jdbc/VitessStatementTest.java | 329 ++++++------- 8 files changed, 282 insertions(+), 1110 deletions(-) diff --git a/java/jdbc/pom.xml b/java/jdbc/pom.xml index dd554e64501..a52048576a1 100644 --- a/java/jdbc/pom.xml +++ b/java/jdbc/pom.xml @@ -57,25 +57,6 @@ 3.12.4 test - - - org.powermock - powermock-api-mockito2 - 2.0.9 - test - - - org.powermock - powermock-core - 2.0.9 - test - - - org.powermock - powermock-module-junit4 - 2.0.9 - test - diff --git a/java/jdbc/src/test/java/io/vitess/jdbc/FieldWithMetadataTest.java b/java/jdbc/src/test/java/io/vitess/jdbc/FieldWithMetadataTest.java index 318bf44d328..d02dd416d05 100644 --- a/java/jdbc/src/test/java/io/vitess/jdbc/FieldWithMetadataTest.java +++ b/java/jdbc/src/test/java/io/vitess/jdbc/FieldWithMetadataTest.java @@ -28,15 +28,7 @@ import org.junit.Assert; import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.mockito.internal.verification.VerificationModeFactory; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -@PrepareForTest(FieldWithMetadata.class) -@RunWith(PowerMockRunner.class) + public class FieldWithMetadataTest extends BaseTest { @Test @@ -492,92 +484,6 @@ public void testToString() throws SQLException { Assert.assertEquals(result, field.toString()); } - public void testCollations() throws Exception { - VitessConnection conn = getVitessConnection(); - - Query.Field raw = Query.Field.newBuilder().setTable("foo").setType(Query.Type.CHAR) - .setName("foo").setOrgName("foo").setCharset(33).build(); - - FieldWithMetadata fieldWithMetadata = PowerMockito.spy(new FieldWithMetadata(conn, raw)); - String first = fieldWithMetadata.getCollation(); - String second = fieldWithMetadata.getCollation(); - - Assert.assertEquals("utf8_general_ci", first); - Assert.assertEquals("cached response is same as first", first, second); - - PowerMockito.verifyPrivate(fieldWithMetadata, VerificationModeFactory.times(1)) - .invoke("getCollationIndex"); - - try { - raw = raw.toBuilder() - // value chosen because it's obviously out of bounds for the underlying array - .setCharset(Integer.MAX_VALUE).build(); - - fieldWithMetadata = PowerMockito.spy(new FieldWithMetadata(conn, raw)); - fieldWithMetadata.getCollation(); - Assert.fail("Should have received an array index out of bounds because " - + "charset/collationIndex of Int.MAX is well above size of charset array"); - } catch (SQLException e) { - if (e.getCause() instanceof ArrayIndexOutOfBoundsException) { - Assert.assertEquals("CollationIndex '" + Integer.MAX_VALUE + "' out of bounds for " - + "collationName lookup, should be within 0 and " - + CharsetMapping.COLLATION_INDEX_TO_COLLATION_NAME.length, e.getMessage()); - } else { - // just rethrow so we fail that way - throw e; - } - } - - PowerMockito.verifyPrivate(fieldWithMetadata, VerificationModeFactory.times(1)) - .invoke("getCollationIndex"); - //Mockito.verify(fieldWithMetadata, Mockito.times(1)).getCollationIndex(); - - conn.setIncludedFields(Query.ExecuteOptions.IncludedFields.TYPE_AND_NAME); - fieldWithMetadata = PowerMockito.spy(new FieldWithMetadata(conn, raw)); - Assert.assertEquals("null response when not including all fields", null, - fieldWithMetadata.getCollation()); - - // We should not call this at all, because we're short circuiting due to included fields - //Mockito.verify(fieldWithMetadata, Mockito.never()).getCollationIndex(); - PowerMockito.verifyPrivate(fieldWithMetadata, VerificationModeFactory.times(0)) - .invoke("getCollationIndex"); - } - - @Test - public void testMaxBytesPerChar() throws Exception { - VitessConnection conn = PowerMockito.spy(getVitessConnection()); - - Query.Field raw = Query.Field.newBuilder().setTable("foo").setType(Query.Type.CHAR) - .setName("foo").setOrgName("foo").setCharset(33).build(); - - FieldWithMetadata fieldWithMetadata = PowerMockito.spy(new FieldWithMetadata(conn, raw)); - - int first = fieldWithMetadata.getMaxBytesPerCharacter(); - int second = fieldWithMetadata.getMaxBytesPerCharacter(); - - Assert.assertEquals("cached response is same as first", first, second); - // We called getMaxBytesPerCharacter 2 times above, but should only have made 1 call to - // fieldWithMetadata.getMaxBytesPerChar: - // first - call conn - // second - return cached - Mockito.verify(fieldWithMetadata, VerificationModeFactory.times(1)) - .getMaxBytesPerChar(33, "UTF-8"); - PowerMockito.verifyPrivate(fieldWithMetadata, VerificationModeFactory.times(1)) - .invoke("getCollationIndex"); - - conn.setIncludedFields(Query.ExecuteOptions.IncludedFields.TYPE_AND_NAME); - fieldWithMetadata = PowerMockito.spy(new FieldWithMetadata(conn, raw)); - Assert.assertEquals("0 return value when not including all fields", 0, - fieldWithMetadata.getMaxBytesPerCharacter()); - - // We should not call this function because we short circuited due to not including all fields. - Mockito.verify(fieldWithMetadata, VerificationModeFactory.times(0)) - .getMaxBytesPerChar(33, "UTF-8"); - // Should not be called at all, because it's new for just this test - PowerMockito.verifyPrivate(fieldWithMetadata, VerificationModeFactory.times(0)) - .invoke("getCollationIndex"); - } - @Test public void testGetEncodingForIndex() throws SQLException { Query.Field raw = Query.Field.newBuilder().setTable("foo").setType(Query.Type.CHAR) diff --git a/java/jdbc/src/test/java/io/vitess/jdbc/VitessConnectionTest.java b/java/jdbc/src/test/java/io/vitess/jdbc/VitessConnectionTest.java index 415790ed3f4..e10c97c8636 100644 --- a/java/jdbc/src/test/java/io/vitess/jdbc/VitessConnectionTest.java +++ b/java/jdbc/src/test/java/io/vitess/jdbc/VitessConnectionTest.java @@ -40,15 +40,11 @@ import java.util.Properties; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.modules.junit4.PowerMockRunner; /** * Created by harshit.gangal on 19/01/16. */ -@RunWith(PowerMockRunner.class) public class VitessConnectionTest extends BaseTest { @Test @@ -118,13 +114,13 @@ public void testDefaultSetAutoCommitForClose() throws SQLException { @Test public void testCommit() throws Exception { - VTSession mockSession = PowerMockito.mock(VTSession.class); + VTSession mockSession = Mockito.mock(VTSession.class); VitessConnection vitessConnection = getVitessConnection(); Field privateVTSessionField = VitessConnection.class.getDeclaredField("vtSession"); privateVTSessionField.setAccessible(true); privateVTSessionField.set(vitessConnection, mockSession); - PowerMockito.when(mockSession.isInTransaction()).thenReturn(false); - PowerMockito.when(mockSession.isAutoCommit()).thenReturn(false); + Mockito.when(mockSession.isInTransaction()).thenReturn(false); + Mockito.when(mockSession.isAutoCommit()).thenReturn(false); vitessConnection.commit(); } @@ -159,13 +155,13 @@ public void testClosed() throws SQLException { @Test(expected = SQLException.class) public void testClosedForException() throws Exception { - VTSession mockSession = PowerMockito.mock(VTSession.class); + VTSession mockSession = Mockito.mock(VTSession.class); VitessConnection vitessConnection = getVitessConnection(); Field privateVTSessionField = VitessConnection.class.getDeclaredField("vtSession"); privateVTSessionField.setAccessible(true); privateVTSessionField.set(vitessConnection, mockSession); - PowerMockito.when(mockSession.isInTransaction()).thenReturn(true); - PowerMockito.when(mockSession.isAutoCommit()).thenReturn(true); + Mockito.when(mockSession.isInTransaction()).thenReturn(true); + Mockito.when(mockSession.isAutoCommit()).thenReturn(true); vitessConnection.close(); } diff --git a/java/jdbc/src/test/java/io/vitess/jdbc/VitessDatabaseMetadataTest.java b/java/jdbc/src/test/java/io/vitess/jdbc/VitessDatabaseMetadataTest.java index 92d5cfede9b..e1e50e8a6e6 100644 --- a/java/jdbc/src/test/java/io/vitess/jdbc/VitessDatabaseMetadataTest.java +++ b/java/jdbc/src/test/java/io/vitess/jdbc/VitessDatabaseMetadataTest.java @@ -19,37 +19,26 @@ import com.google.common.base.Charsets; import com.google.common.io.CharStreams; import com.google.protobuf.ByteString; - import io.vitess.client.cursor.Cursor; import io.vitess.client.cursor.SimpleCursor; import io.vitess.proto.Query; import io.vitess.util.Constants; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.sql.Types; +import java.sql.*; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.Scanner; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - /** * Created by ashudeep.sharma on 08/03/16. */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({VitessMySQLDatabaseMetadata.class, VitessConnection.class}) public class VitessDatabaseMetadataTest extends BaseTest { private ResultSet resultSet; @@ -860,19 +849,19 @@ public void autoCommitFailureClosesAllResultSetsTest() throws SQLException { @Test public void getUrlTest() throws SQLException { String connectionUrl = "jdbc:vitess://://"; - VitessJDBCUrl mockUrl = PowerMockito.mock(VitessJDBCUrl.class); - PowerMockito.when(mockUrl.getUrl()).thenReturn(connectionUrl); + VitessJDBCUrl mockUrl = Mockito.mock(VitessJDBCUrl.class); + Mockito.when(mockUrl.getUrl()).thenReturn(connectionUrl); - VitessConnection mockConn = PowerMockito.mock(VitessConnection.class); - PowerMockito.when(mockConn.getUrl()).thenReturn(mockUrl); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + Mockito.when(mockConn.getUrl()).thenReturn(mockUrl); Assert.assertEquals(connectionUrl, mockConn.getUrl().getUrl()); } @Test public void isReadOnlyTest() throws SQLException { - VitessConnection mockConn = PowerMockito.mock(VitessConnection.class); - PowerMockito.when(mockConn.isReadOnly()).thenReturn(false); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + Mockito.when(mockConn.isReadOnly()).thenReturn(false); Assert.assertEquals(false, mockConn.isReadOnly()); } @@ -1022,74 +1011,6 @@ public void supportsStatementPooling() throws SQLException { Assert.assertEquals(false, vitessMariaDBDatabaseMetadata.supportsStatementPooling()); } - @Test - public void getCatalogsTest() throws SQLException, Exception { - String sql = "SHOW DATABASES"; - Cursor mockedCursor = new SimpleCursor(Query.QueryResult.newBuilder().addFields( - Query.Field.newBuilder().setName("TABLE_CAT").setType(Query.Type.VARCHAR).build()).addRows( - Query.Row.newBuilder().addLengths("vitessDB".length()) - .setValues(ByteString.copyFromUtf8("vitessDB"))).addRows( - Query.Row.newBuilder().addLengths("sampleDB".length()) - .setValues(ByteString.copyFromUtf8("sampleDB"))).addRows( - Query.Row.newBuilder().addLengths("testDB".length()) - .setValues(ByteString.copyFromUtf8("testDB"))).addRows( - Query.Row.newBuilder().addLengths("dummyDB".length()) - .setValues(ByteString.copyFromUtf8("dummyDB"))).build()); - - VitessStatement vitessStatement = PowerMockito.mock(VitessStatement.class); - PowerMockito.whenNew(VitessStatement.class).withAnyArguments().thenReturn(vitessStatement); - PowerMockito.when(vitessStatement.executeQuery(sql)) - .thenReturn(new VitessResultSet(mockedCursor)); - - VitessDatabaseMetaData vitessDatabaseMetaData = new VitessMySQLDatabaseMetadata(null); - ResultSet resultSet = vitessDatabaseMetaData.getCatalogs(); - ArrayList resultSetList = new ArrayList(); - while (resultSet.next()) { - resultSetList.add(resultSet.getString(1)); - } - Assert.assertEquals("dummyDB", resultSetList.get(0)); - Assert.assertEquals("sampleDB", resultSetList.get(1)); - Assert.assertEquals("testDB", resultSetList.get(2)); - Assert.assertEquals("vitessDB", resultSetList.get(3)); - } - - @Test - public void getTablesTest() throws SQLException, Exception { - - String sql = "SHOW FULL TABLES FROM `vt` LIKE '%'"; - Cursor mockedCursor = getTablesCursor(); - - VitessStatement vitessStatement = PowerMockito.mock(VitessStatement.class); - PowerMockito.whenNew(VitessStatement.class).withAnyArguments().thenReturn(vitessStatement); - PowerMockito.when(vitessStatement.executeQuery(sql)) - .thenReturn(new VitessResultSet(mockedCursor)); - - VitessDatabaseMetaData vitessDatabaseMetaData = new VitessMySQLDatabaseMetadata( - getVitessConnection()); - ResultSet actualResultSet = vitessDatabaseMetaData.getTables("vt", null, null, null); - ResultSet expectedResultSet = new VitessResultSet(mockedCursor); - - assertResultSetEquals(actualResultSet, expectedResultSet); - } - - @Test - public void getTablesProperResultTypeTest() throws SQLException, Exception { - - String sql = "SHOW FULL TABLES FROM `vt` LIKE '%'"; - Cursor mockedCursor = getTablesCursor(); - - VitessStatement vitessStatement = PowerMockito.mock(VitessStatement.class); - PowerMockito.whenNew(VitessStatement.class).withAnyArguments().thenReturn(vitessStatement); - PowerMockito.when(vitessStatement.executeQuery(sql)) - .thenReturn(new VitessResultSet(mockedCursor)); - - VitessDatabaseMetaData vitessDatabaseMetaData = new VitessMySQLDatabaseMetadata( - getVitessConnection()); - ResultSet actualResultSet = vitessDatabaseMetaData.getTables("vt", null, null, null); - actualResultSet.next(); - Assert.assertEquals(String.class, actualResultSet.getObject("TABLE_CAT").getClass()); - } - private Cursor getTablesCursor() throws Exception { return new SimpleCursor(Query.QueryResult.newBuilder() .addFields(Query.Field.newBuilder().setName("TABLE_CAT").setType(Query.Type.VARCHAR)) @@ -1131,231 +1052,6 @@ private Cursor getTablesCursor() throws Exception { .build()); } - @Test - public void getColumnsTest() throws SQLException, Exception { - - String sql = "SHOW FULL COLUMNS FROM `sampleTable1` FROM `TestDB1` LIKE '%'"; - Cursor mockedTablecursor = new SimpleCursor(Query.QueryResult.newBuilder() - .addFields(Query.Field.newBuilder().setName("TABLE_CAT").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("TABLE_SCHEM").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("TABLE_NAME").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("TABLE_TYPE").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("REMARKS").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("TYPE_CAT").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("TYPE_SCHEM").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("TYPE_NAME").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("SELF_REFERENCING_COL_NAME") - .setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("REF_GENERATION").setType(Query.Type.VARCHAR)) - .addRows(Query.Row.newBuilder().addLengths("TestDB1".length()).addLengths("".length()) - .addLengths("sampleTable1".length()).addLengths("TABLE".length()) - .addLengths("".length()).addLengths("".length()).addLengths("".length()) - .addLengths("".length()).addLengths("".length()).addLengths("".length()) - .setValues(ByteString.copyFromUtf8("TestDB1sampleTable1TABLE"))).build()); - - Cursor actualCursor = new SimpleCursor(Query.QueryResult.newBuilder() - .addFields(Query.Field.newBuilder().setName("Field").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Type").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Collation").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Null").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Key").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Default").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Extra").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Privileges").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Comment").setType(Query.Type.VARCHAR)).addRows( - Query.Row.newBuilder().addLengths("shipmentid".length()).addLengths("bigint".length()) - .addLengths("NULL".length()).addLengths("NO".length()).addLengths("PRI".length()) - .addLengths("NULL".length()).addLengths("".length()) - .addLengths("select,insert,update,references".length()).addLengths("".length()) - .setValues(ByteString - .copyFromUtf8("shipmentidbigintNULLNOPRINULLselect,insert,update,references"))) - .addRows( - Query.Row.newBuilder().addLengths("trackingid".length()).addLengths("varchar".length()) - .addLengths("utf8_general_ci".length()).addLengths("YES".length()) - .addLengths("".length()).addLengths("NULL".length()).addLengths("".length()) - .addLengths("select,insert,update,references".length()).addLengths("".length()) - .setValues(ByteString.copyFromUtf8( - "trackingidvarcharutf8_general_ciYESNULLselect,insert,update,references"))) - .build()); - Cursor expectedCursor = new SimpleCursor(Query.QueryResult.newBuilder() - .addFields(Query.Field.newBuilder().setName("TABLE_CAT").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("TABLE_SCHEM").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("TABLE_NAME").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("COLUMN_NAME").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("DATA_TYPE").setType(Query.Type.INT32)) - .addFields(Query.Field.newBuilder().setName("TYPE_NAME").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("COLUMN_SIZE").setType(Query.Type.INT32)) - .addFields(Query.Field.newBuilder().setName("BUFFER_LENGTH").setType(Query.Type.INT32)) - .addFields(Query.Field.newBuilder().setName("DECIMAL_DIGITS").setType(Query.Type.INT32)) - .addFields(Query.Field.newBuilder().setName("NUM_PREC_RADIX").setType(Query.Type.INT32)) - .addFields(Query.Field.newBuilder().setName("NULLABLE").setType(Query.Type.INT32)) - .addFields(Query.Field.newBuilder().setName("REMARKS").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("COLUMN_DEF").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("SQL_DATA_TYPE").setType(Query.Type.INT32)) - .addFields(Query.Field.newBuilder().setName("SQL_DATETIME_SUB").setType(Query.Type.INT32)) - .addFields(Query.Field.newBuilder().setName("CHAR_OCTET_LENGTH").setType(Query.Type.INT32)) - .addFields(Query.Field.newBuilder().setName("ORDINAL_POSITION").setType(Query.Type.INT32)) - .addFields(Query.Field.newBuilder().setName("ISNULLABLE").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("SCOPE_CATALOG").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("SCOPE_SCHEMA").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("SCOPE_TABLE").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("SOURCE_DATA_TYPE").setType(Query.Type.INT16)) - .addFields(Query.Field.newBuilder().setName("IS_AUTOINCREMENT").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("IS_GENERATEDCOLUMN").setType(Query.Type.CHAR)) - .addRows(Query.Row.newBuilder().addLengths("TestDB1".length()).addLengths(-1) - .addLengths("sampleTable1".length()).addLengths("shipmentid".length()) - .addLengths("-5".length()).addLengths("BIGINT".length()).addLengths("19".length()) - .addLengths("65535".length()).addLengths("0".length()).addLengths("10".length()) - .addLengths("0".length()).addLengths("Comment".length()).addLengths("NULL".length()) - .addLengths("0".length()).addLengths("0".length()).addLengths("0".length()) - .addLengths("1".length()).addLengths("NO".length()).addLengths(-1).addLengths(-1) - .addLengths(-1).addLengths(-1).addLengths("NO".length()).addLengths("NO".length()) - .setValues(ByteString.copyFromUtf8( - "TestDB1sampleTable1shipmentid-5BIGINT19655350100CommentNULL0001NONONO"))).addRows( - Query.Row.newBuilder().addLengths("TestDB1".length()).addLengths(-1) - .addLengths("sampleTable1".length()).addLengths("trackingid".length()) - .addLengths("12".length()).addLengths("VARCHAR".length()).addLengths("255".length()) - .addLengths("65535".length()).addLengths(-1).addLengths("10".length()) - .addLengths("1".length()).addLengths("Comment".length()).addLengths("NULL".length()) - .addLengths("0".length()).addLengths("0".length()).addLengths("255".length()) - .addLengths("2".length()).addLengths("YES".length()).addLengths(-1).addLengths(-1) - .addLengths(-1).addLengths(-1).addLengths("NO".length()).addLengths("NO".length()) - .setValues(ByteString.copyFromUtf8( - "TestDB1sampleTable1trackingid12VARCHAR25565535101CommentNULL002552YESNONO"))) - .build()); - - VitessStatement vitessStatement = PowerMockito.mock(VitessStatement.class); - PowerMockito.whenNew(VitessStatement.class).withAnyArguments().thenReturn(vitessStatement); - PowerMockito.when(vitessStatement.executeQuery(sql)) - .thenReturn(new VitessResultSet(actualCursor)); - - VitessDatabaseMetaData vitessDatabaseMetaData = PowerMockito - .mock(VitessMySQLDatabaseMetadata.class); - PowerMockito.doCallRealMethod().when(vitessDatabaseMetaData) - .getColumns("TestDB1", null, null, null); - PowerMockito.when(vitessDatabaseMetaData.getTables("TestDB1", null, "%", new String[0])) - .thenReturn(new VitessResultSet(mockedTablecursor)); - ResultSet actualResultSet = vitessDatabaseMetaData.getColumns("TestDB1", null, null, null); - ResultSet expectedResultSet = new VitessResultSet(expectedCursor); - - assertResultSetEquals(actualResultSet, expectedResultSet); - } - - @Test - public void getPrimaryKeysTest() throws SQLException, Exception { - - String sql = "SHOW KEYS FROM `shipment` FROM `vt`"; - Cursor mockedCursor = new SimpleCursor(Query.QueryResult.newBuilder() - .addFields(Query.Field.newBuilder().setName("TABLE").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Non_unique").setType(Query.Type.INT64)) - .addFields(Query.Field.newBuilder().setName("Key_name").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Seq_in_index").setType(Query.Type.INT64)) - .addFields(Query.Field.newBuilder().setName("Column_name").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Collation").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Cardinality").setType(Query.Type.INT64)) - .addFields(Query.Field.newBuilder().setName("Sub_part").setType(Query.Type.INT64)) - .addFields(Query.Field.newBuilder().setName("Packed").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Null").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Index_type").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Comment").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Index_comment").setType(Query.Type.VARCHAR)) - .addRows(Query.Row.newBuilder().addLengths("shipment".length()).addLengths("0".length()) - .addLengths("PRIMARY".length()).addLengths("1".length()) - .addLengths("shipmentid".length()).addLengths("A".length()) - .addLengths("434880".length()).addLengths(-1).addLengths(-1).addLengths("".length()) - .addLengths("BTREE".length()).addLengths("".length()).addLengths("".length()) - .setValues(ByteString.copyFromUtf8("shipment0PRIMARY1shipmentidA434880BTREE"))) - .build()); - Cursor expectedcursor = new SimpleCursor(Query.QueryResult.newBuilder() - .addFields(Query.Field.newBuilder().setName("TABLE_CAT").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("TABLE_SCHEM").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("TABLE_NAME").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("COLUMN_NAME").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("KEY_SEQ").setType(Query.Type.INT16)) - .addFields(Query.Field.newBuilder().setName("PK_NAME").setType(Query.Type.CHAR)).addRows( - Query.Row.newBuilder().addLengths("vt".length()).addLengths(-1) - .addLengths("shipment".length()).addLengths("shipmentid".length()) - .addLengths("1".length()).addLengths("PRIMARY".length()) - .setValues(ByteString.copyFromUtf8("vtshipmentshipmentid1PRIMARY"))).build()); - - VitessStatement vitessStatement = PowerMockito.mock(VitessStatement.class); - VitessDatabaseMetaData vitessDatabaseMetaData = PowerMockito - .mock(VitessMySQLDatabaseMetadata.class); - PowerMockito.mock(VitessMySQLDatabaseMetadata.class); - PowerMockito.doCallRealMethod().when(vitessDatabaseMetaData) - .getPrimaryKeys("vt", null, "shipment"); - PowerMockito.whenNew(VitessStatement.class).withAnyArguments().thenReturn(vitessStatement); - PowerMockito.when(vitessStatement.executeQuery(sql)) - .thenReturn(new VitessResultSet(mockedCursor)); - ResultSet expectedResultSet = vitessDatabaseMetaData.getPrimaryKeys("vt", null, "shipment"); - ResultSet actualResultSet = new VitessResultSet(expectedcursor); - - assertResultSetEquals(actualResultSet, expectedResultSet); - } - - @Test - public void getIndexInfoTest() throws SQLException, Exception { - - String sql = "SHOW INDEX FROM `shipment` FROM `vt`"; - Cursor mockedCursor = new SimpleCursor(Query.QueryResult.newBuilder() - .addFields(Query.Field.newBuilder().setName("Table").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Non_unique").setType(Query.Type.INT64)) - .addFields(Query.Field.newBuilder().setName("Key_name").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Seq_in_index").setType(Query.Type.INT64)) - .addFields(Query.Field.newBuilder().setName("Column_name").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Collation").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Cardinality").setType(Query.Type.INT64)) - .addFields(Query.Field.newBuilder().setName("Sub_part").setType(Query.Type.INT64)) - .addFields(Query.Field.newBuilder().setName("Packed").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Null").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Index_type").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Comment").setType(Query.Type.VARCHAR)) - .addFields(Query.Field.newBuilder().setName("Index_comment").setType(Query.Type.VARCHAR)) - .addRows(Query.Row.newBuilder().addLengths("shipment".length()).addLengths("0".length()) - .addLengths("PRIMARY".length()).addLengths("1".length()) - .addLengths("shipmentid".length()).addLengths("A".length()) - .addLengths("434880".length()).addLengths(-1).addLengths(-1).addLengths("".length()) - .addLengths("BTREE".length()).addLengths("".length()).addLengths("".length()) - .setValues(ByteString.copyFromUtf8("shipment0PRIMARY1shipmentidA434880BTREE"))) - .build()); - - Cursor expectedcursor = new SimpleCursor(Query.QueryResult.newBuilder() - .addFields(Query.Field.newBuilder().setName("TABLE_CAT").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("TABLE_SCHEM").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("TABLE_NAME").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("Non_unique").setType(Query.Type.BIT)) - .addFields(Query.Field.newBuilder().setName("INDEX_QUALIFIER").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("INDEX_NAME").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("TYPE").setType(Query.Type.INT16)) - .addFields(Query.Field.newBuilder().setName("ORDINAL_POSITION").setType(Query.Type.INT16)) - .addFields(Query.Field.newBuilder().setName("COLUMN_NAME").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("ASC_OR_DESC").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("CARDINALITY").setType(Query.Type.INT32)) - .addFields(Query.Field.newBuilder().setName("PAGES").setType(Query.Type.INT32)) - .addFields(Query.Field.newBuilder().setName("FILTER_CONDITION").setType(Query.Type.CHAR)) - .addRows(Query.Row.newBuilder().addLengths("vt".length()).addLengths(-1) - .addLengths("shipment".length()).addLengths("false".length()).addLengths("".length()) - .addLengths("PRIMARY".length()).addLengths("3".length()).addLengths("1".length()) - .addLengths("shipmentid".length()).addLengths("A".length()) - .addLengths("434880".length()).addLengths("0".length()).addLengths(-1) - .setValues(ByteString.copyFromUtf8("vtshipmentfalsePRIMARY31shipmentidA4348800"))) - .build()); - VitessStatement vitessStatement = PowerMockito.mock(VitessStatement.class); - VitessDatabaseMetaData vitessDatabaseMetaData = PowerMockito - .mock(VitessMySQLDatabaseMetadata.class); - PowerMockito.mock(VitessMySQLDatabaseMetadata.class); - PowerMockito.doCallRealMethod().when(vitessDatabaseMetaData) - .getIndexInfo("vt", null, "shipment", true, false); - PowerMockito.whenNew(VitessStatement.class).withAnyArguments().thenReturn(vitessStatement); - PowerMockito.when(vitessStatement.executeQuery(sql)) - .thenReturn(new VitessResultSet(mockedCursor)); - ResultSet actualResultSet = vitessDatabaseMetaData - .getIndexInfo("vt", null, "shipment", true, false); - ResultSet expectedResultSet = new VitessResultSet(expectedcursor); - - assertResultSetEquals(actualResultSet, expectedResultSet); - } - private void assertResultSetEquals(ResultSet actualResultSet, ResultSet expectedResultSet) throws SQLException { ResultSetMetaData actualResultSetMetadata = actualResultSet.getMetaData(); @@ -1440,137 +1136,6 @@ public void getUserNameTest() { } } - @Test - public void testCaseSensitivityIdentifierFuncsMySql() throws Exception { - assertCaseSensitivityForDatabaseType(false); - } - - @Test - public void testCaseSensitivityIdentifierFuncsMariaDb() throws Exception { - assertCaseSensitivityForDatabaseType(true); - } - - private void assertCaseSensitivityForDatabaseType(boolean useMariaDb) throws Exception { - VitessConnection connection = new VitessConnection("jdbc:vitess://username@ip1:port1/keyspace", - null); - mockStatementForLowercaseTablesValue("0", useMariaDb); - Assert.assertEquals(true, connection.getMetaData().supportsMixedCaseIdentifiers()); - Assert.assertEquals(true, connection.getMetaData().supportsMixedCaseQuotedIdentifiers()); - Assert.assertEquals(false, connection.getMetaData().storesLowerCaseIdentifiers()); - Assert.assertEquals(true, connection.getMetaData().storesMixedCaseIdentifiers()); - Assert.assertEquals(false, connection.getMetaData().storesLowerCaseQuotedIdentifiers()); - Assert.assertEquals(true, connection.getMetaData().storesMixedCaseQuotedIdentifiers()); - connection.close(); - - connection = new VitessConnection("jdbc:vitess://username@ip1:port1/keyspace", null); - mockStatementForLowercaseTablesValue("1", useMariaDb); - Assert.assertEquals(false, connection.getMetaData().supportsMixedCaseIdentifiers()); - Assert.assertEquals(false, connection.getMetaData().supportsMixedCaseQuotedIdentifiers()); - Assert.assertEquals(true, connection.getMetaData().storesLowerCaseIdentifiers()); - Assert.assertEquals(false, connection.getMetaData().storesMixedCaseIdentifiers()); - Assert.assertEquals(true, connection.getMetaData().storesLowerCaseQuotedIdentifiers()); - Assert.assertEquals(false, connection.getMetaData().storesMixedCaseQuotedIdentifiers()); - connection.close(); - - connection = new VitessConnection("jdbc:vitess://username@ip1:port1/keyspace", null); - mockStatementForLowercaseTablesValue("2", useMariaDb); - Assert.assertEquals(false, connection.getMetaData().supportsMixedCaseIdentifiers()); - Assert.assertEquals(false, connection.getMetaData().supportsMixedCaseQuotedIdentifiers()); - Assert.assertEquals(false, connection.getMetaData().storesLowerCaseIdentifiers()); - Assert.assertEquals(true, connection.getMetaData().storesMixedCaseIdentifiers()); - Assert.assertEquals(false, connection.getMetaData().storesLowerCaseQuotedIdentifiers()); - Assert.assertEquals(true, connection.getMetaData().storesMixedCaseQuotedIdentifiers()); - connection.close(); - - connection = new VitessConnection("jdbc:vitess://username@ip1:port1/keyspace", null); - mockStatementForLowercaseTablesValue("something random", useMariaDb); - Assert.assertEquals(true, connection.getMetaData().supportsMixedCaseIdentifiers()); - Assert.assertEquals(true, connection.getMetaData().supportsMixedCaseQuotedIdentifiers()); - Assert.assertEquals(false, connection.getMetaData().storesLowerCaseIdentifiers()); - Assert.assertEquals(true, connection.getMetaData().storesMixedCaseIdentifiers()); - Assert.assertEquals(false, connection.getMetaData().storesLowerCaseQuotedIdentifiers()); - Assert.assertEquals(true, connection.getMetaData().storesMixedCaseQuotedIdentifiers()); - connection.close(); - } - - private void mockStatementForLowercaseTablesValue(String lcTablesValue, boolean useMariaDb) - throws Exception { - String sql = "SHOW VARIABLES WHERE VARIABLE_NAME IN (\'tx_isolation\',\'INNODB_VERSION\', " - + "\'lower_case_table_names\')"; - String versionName = "innodb_version"; - String versionValue = "5.7.16-10"; - if (useMariaDb) { - versionValue = versionValue + "-mariadb"; - } - String txIsoName = "tx_isolation"; - String txIsoValue = "REPEATABLE-READ"; - String lcTablesName = "lower_case_table_names"; - - Cursor mockedCursor = new SimpleCursor(Query.QueryResult.newBuilder().addFields( - Query.Field.newBuilder().setName("Variable_name").setType(Query.Type.VARCHAR).build()) - .addFields(Query.Field.newBuilder().setName("Value").setType(Query.Type.VARCHAR).build()) - .addRows(Query.Row.newBuilder().addLengths(versionName.length()) - .addLengths(versionValue.length()) - .setValues(ByteString.copyFromUtf8(versionName + versionValue))).addRows( - Query.Row.newBuilder().addLengths(txIsoName.length()).addLengths(txIsoValue.length()) - .setValues(ByteString.copyFromUtf8(txIsoName + txIsoValue))).addRows( - Query.Row.newBuilder().addLengths(lcTablesName.length()) - .addLengths(lcTablesValue.length()) - .setValues(ByteString.copyFromUtf8(lcTablesName + lcTablesValue))).build()); - - VitessStatement vitessStatement = PowerMockito.mock(VitessStatement.class); - PowerMockito.whenNew(VitessStatement.class).withAnyArguments().thenReturn(vitessStatement); - PowerMockito.when(vitessStatement.executeQuery(sql)) - .thenReturn(new VitessResultSet(mockedCursor)); - } - - /** - * Tests that we're properly stitching together the results of SHOW CREATE TABLE. See {@link - * #extractForeignKeyForTableTest()} for more thorough testing of the actual parsing - */ - @Test - public void getImportedKeysTest() throws Exception { - try (InputStream resourceAsStream = this.getClass() - .getResourceAsStream("/getImportedKeysTestCase.sql")) { - String table = "testA"; - String showCreate = CharStreams - .toString(new InputStreamReader(resourceAsStream, Charsets.UTF_8)); - - Query.QueryResult queryResult = Query.QueryResult.newBuilder() - .addFields(Query.Field.newBuilder().setName("Table").setType(Query.Type.CHAR)) - .addFields(Query.Field.newBuilder().setName("Create Table").setType(Query.Type.CHAR)) - .addRows(Query.Row.newBuilder().addLengths(table.length()).addLengths(showCreate.length()) - .setValues(ByteString.copyFromUtf8(table + showCreate))).build(); - - String sql = "SHOW CREATE TABLE `testA`"; - VitessConnection vitessConnection = new VitessConnection( - "jdbc:vitess://username@ip1:port1/keyspace", null); - VitessStatement vitessStatement = PowerMockito.spy(new VitessStatement(vitessConnection)); - PowerMockito.whenNew(VitessStatement.class).withAnyArguments().thenReturn(vitessStatement); - PowerMockito.doReturn(new VitessResultSet(new SimpleCursor(queryResult), vitessStatement)) - .when(vitessStatement).executeQuery(sql); - - VitessDatabaseMetaData vitessDatabaseMetaData = new VitessMySQLDatabaseMetadata( - vitessConnection); - ResultSet importedKeys = vitessDatabaseMetaData.getImportedKeys("test", "test", "testA"); - importedKeys.next(); - Assert.assertEquals("test", importedKeys.getString("PKTABLE_CAT")); - Assert.assertEquals(null, importedKeys.getString("PKTABLE_SCHEM")); - Assert.assertEquals("fTable", importedKeys.getString("PKTABLE_NAME")); - Assert.assertEquals("id", importedKeys.getString("PKCOLUMN_NAME")); - Assert.assertEquals("test", importedKeys.getString("FKTABLE_CAT")); - Assert.assertEquals(null, importedKeys.getString("FKTABLE_SCHEM")); - Assert.assertEquals("testA", importedKeys.getString("FKTABLE_NAME")); - Assert.assertEquals("fIdOne", importedKeys.getString("FKCOLUMN_NAME")); - Assert.assertEquals(1, importedKeys.getInt("KEY_SEQ")); - Assert.assertEquals(3, importedKeys.getInt("UPDATE_RULE")); - Assert.assertEquals(3, importedKeys.getInt("DELETE_RULE")); - Assert.assertEquals("fk_testA", importedKeys.getString("FK_NAME")); - Assert.assertEquals(null, importedKeys.getString("PK_NAME")); - Assert.assertEquals(7, importedKeys.getInt("DEFERRABILITY")); - } - } - /** * Tests parsing all the various outputs of SHOW CREATE TABLE for the foreign key constraints. */ diff --git a/java/jdbc/src/test/java/io/vitess/jdbc/VitessParameterMetaDataTest.java b/java/jdbc/src/test/java/io/vitess/jdbc/VitessParameterMetaDataTest.java index 8916063ffaf..890212d5071 100644 --- a/java/jdbc/src/test/java/io/vitess/jdbc/VitessParameterMetaDataTest.java +++ b/java/jdbc/src/test/java/io/vitess/jdbc/VitessParameterMetaDataTest.java @@ -16,20 +16,13 @@ package io.vitess.jdbc; +import org.junit.Assert; +import org.junit.Test; + import java.sql.ParameterMetaData; import java.sql.SQLException; import java.sql.Types; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.internal.verification.VerificationModeFactory; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -@RunWith(PowerMockRunner.class) -@PrepareForTest(VitessParameterMetaData.class) public class VitessParameterMetaDataTest { @Test @@ -70,22 +63,6 @@ public void testOutOfBoundsValidation() { } } - @Test - public void testOutOfBoundCoverage() throws Exception { - int param = 2; - VitessParameterMetaData metaData = PowerMockito.spy(new VitessParameterMetaData(5)); - - metaData.getParameterType(param); - metaData.getPrecision(param); - metaData.getScale(param); - metaData.getParameterClassName(param); - metaData.getParameterTypeName(param); - metaData.isSigned(param); - - PowerMockito.verifyPrivate(metaData, VerificationModeFactory.times(6)) - .invoke("checkBounds", param); - } - @Test(expected = SQLException.class) public void testNullableNotAvailable() throws SQLException { VitessParameterMetaData metaData = new VitessParameterMetaData(5); diff --git a/java/jdbc/src/test/java/io/vitess/jdbc/VitessPreparedStatementTest.java b/java/jdbc/src/test/java/io/vitess/jdbc/VitessPreparedStatementTest.java index c5a9b4cbd33..2ebadf371ec 100644 --- a/java/jdbc/src/test/java/io/vitess/jdbc/VitessPreparedStatementTest.java +++ b/java/jdbc/src/test/java/io/vitess/jdbc/VitessPreparedStatementTest.java @@ -18,12 +18,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; import static org.mockito.Matchers.nullable; -import static org.mockito.Matchers.anyMap; -import static org.mockito.Matchers.anyString; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; import com.google.common.collect.ImmutableMap; @@ -59,19 +54,13 @@ import org.junit.Assert; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Matchers; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; /** * Created by harshit.gangal on 09/02/16. */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({VTGateConnection.class, Vtrpc.RPCError.class}) public class VitessPreparedStatementTest { private String sqlSelect = "select 1 from test_table"; @@ -81,7 +70,7 @@ public class VitessPreparedStatementTest { @Test public void testStatementExecute() { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessPreparedStatement preparedStatement; try { preparedStatement = new VitessPreparedStatement(mockConn, sqlShow); @@ -110,17 +99,17 @@ public void testStatementExecute() { @Test public void testExecuteQuery() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))). + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))). thenReturn(mockSqlFutureCursor); - when(mockConn.getExecuteType()).thenReturn(Constants.QueryExecuteType.SIMPLE); - when(mockConn.isSimpleExecute()).thenReturn(true); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockConn.getExecuteType()).thenReturn(Constants.QueryExecuteType.SIMPLE); + Mockito.when(mockConn.isSimpleExecute()).thenReturn(true); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); VitessPreparedStatement preparedStatement; try { @@ -156,7 +145,7 @@ public void testExecuteQuery() throws SQLException { try { //when returned cursor is null - when(mockSqlFutureCursor.checkedGet()).thenReturn(null); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(null); preparedStatement.executeQuery(); fail("Should have thrown exception for cursor null"); } catch (SQLException ex) { @@ -170,19 +159,19 @@ public void testExecuteQuery() throws SQLException { @Test public void testExecuteQueryWithStream() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn .streamExecute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockCursor); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockConn.getExecuteType()).thenReturn(Constants.QueryExecuteType.STREAM); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockConn.getExecuteType()).thenReturn(Constants.QueryExecuteType.STREAM); VitessPreparedStatement preparedStatement; try { @@ -218,7 +207,7 @@ public void testExecuteQueryWithStream() throws SQLException { try { //when returned cursor is null - when(mockVtGateConn + Mockito.when(mockVtGateConn .streamExecute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(null); preparedStatement.executeQuery(); @@ -235,17 +224,17 @@ public void testExecuteQueryWithStream() throws SQLException { @Test public void testExecuteUpdate() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); - List fieldList = mock(ArrayList.class); - - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); + List fieldList = Mockito.mock(ArrayList.class); + + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); VitessPreparedStatement preparedStatement; try { @@ -257,14 +246,14 @@ public void testExecuteUpdate() throws SQLException { assertEquals(0, updateCount); //tx is null & autoCommit is true - when(mockConn.getAutoCommit()).thenReturn(true); + Mockito.when(mockConn.getAutoCommit()).thenReturn(true); preparedStatement = new VitessPreparedStatement(mockConn, sqlUpdate); updateCount = preparedStatement.executeUpdate(); assertEquals(0, updateCount); //cursor fields is not null - when(mockCursor.getFields()).thenReturn(fieldList); - when(fieldList.isEmpty()).thenReturn(false); + Mockito.when(mockCursor.getFields()).thenReturn(fieldList); + Mockito.when(fieldList.isEmpty()).thenReturn(false); try { preparedStatement.executeUpdate(); fail("Should have thrown exception for field not null"); @@ -273,7 +262,7 @@ public void testExecuteUpdate() throws SQLException { } //cursor is null - when(mockSqlFutureCursor.checkedGet()).thenReturn(null); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(null); try { preparedStatement.executeUpdate(); fail("Should have thrown exception for cursor null"); @@ -282,7 +271,7 @@ public void testExecuteUpdate() throws SQLException { } //read only - when(mockConn.isReadOnly()).thenReturn(true); + Mockito.when(mockConn.isReadOnly()).thenReturn(true); try { preparedStatement.executeUpdate(); fail("Should have thrown exception for read only"); @@ -291,7 +280,7 @@ public void testExecuteUpdate() throws SQLException { } //read only - when(mockConn.isReadOnly()).thenReturn(true); + Mockito.when(mockConn.isReadOnly()).thenReturn(true); try { preparedStatement.executeBatch(); fail("Should have thrown exception for read only"); @@ -306,31 +295,31 @@ public void testExecuteUpdate() throws SQLException { @Test public void testExecute() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); - List mockFieldList = PowerMockito.spy(new ArrayList<>()); - - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); + List mockFieldList = Mockito.spy(new ArrayList<>()); + + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); - when(mockConn.getExecuteType()).thenReturn(Constants.QueryExecuteType.SIMPLE); - when(mockConn.isSimpleExecute()).thenReturn(true); + Mockito.when(mockConn.getExecuteType()).thenReturn(Constants.QueryExecuteType.SIMPLE); + Mockito.when(mockConn.isSimpleExecute()).thenReturn(true); - when(mockConn.getAutoCommit()).thenReturn(true); + Mockito.when(mockConn.getAutoCommit()).thenReturn(true); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockCursor.getFields()).thenReturn(mockFieldList); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockCursor.getFields()).thenReturn(mockFieldList); VitessPreparedStatement preparedStatement = new VitessPreparedStatement(mockConn, sqlSelect, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); try { int fieldSize = 5; - when(mockCursor.getFields()).thenReturn(mockFieldList); - PowerMockito.doReturn(fieldSize).when(mockFieldList).size(); - PowerMockito.doReturn(false).when(mockFieldList).isEmpty(); + Mockito.when(mockCursor.getFields()).thenReturn(mockFieldList); + Mockito.doReturn(fieldSize).when(mockFieldList).size(); + Mockito.doReturn(false).when(mockFieldList).isEmpty(); boolean hasResultSet = preparedStatement.execute(); Assert.assertTrue(hasResultSet); Assert.assertNotNull(preparedStatement.getResultSet()); @@ -341,9 +330,9 @@ public void testExecute() throws SQLException { Assert.assertNotNull(preparedStatement.getResultSet()); int mockUpdateCount = 10; - when(mockCursor.getFields()) + Mockito.when(mockCursor.getFields()) .thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); - when(mockCursor.getRowsAffected()).thenReturn((long) mockUpdateCount); + Mockito.when(mockCursor.getRowsAffected()).thenReturn((long) mockUpdateCount); preparedStatement = new VitessPreparedStatement(mockConn, sqlUpdate); hasResultSet = preparedStatement.execute(); Assert.assertFalse(hasResultSet); @@ -351,7 +340,7 @@ public void testExecute() throws SQLException { assertEquals(mockUpdateCount, preparedStatement.getUpdateCount()); //cursor is null - when(mockSqlFutureCursor.checkedGet()).thenReturn(null); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(null); try { preparedStatement = new VitessPreparedStatement(mockConn, sqlShow); preparedStatement.execute(); @@ -375,19 +364,19 @@ public void testExecuteFetchSizeAsStreaming() throws SQLException { private void testExecute(int fetchSize, boolean simpleExecute, boolean shouldRunExecute, boolean shouldRunStreamExecute) throws SQLException { - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); - VitessConnection mockConn = mock(VitessConnection.class); - when(mockConn.isSimpleExecute()).thenReturn(simpleExecute); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + Mockito.when(mockConn.isSimpleExecute()).thenReturn(simpleExecute); + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); - when(mockVtGateConn + Mockito.when(mockVtGateConn .streamExecute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockCursor); @@ -408,32 +397,32 @@ private void testExecute(int fetchSize, boolean simpleExecute, boolean shouldRun @Test public void testGetUpdateCount() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFuture = mock(SQLFuture.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFuture = Mockito.mock(SQLFuture.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFuture); - when(mockSqlFuture.checkedGet()).thenReturn(mockCursor); - when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); + Mockito.when(mockSqlFuture.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); VitessPreparedStatement preparedStatement = new VitessPreparedStatement(mockConn, sqlSelect); try { - when(mockCursor.getRowsAffected()).thenReturn(10L); + Mockito.when(mockCursor.getRowsAffected()).thenReturn(10L); int updateCount = preparedStatement.executeUpdate(); assertEquals(10L, updateCount); assertEquals(10L, preparedStatement.getUpdateCount()); // Truncated Update Count - when(mockCursor.getRowsAffected()).thenReturn((long) Integer.MAX_VALUE + 10); + Mockito.when(mockCursor.getRowsAffected()).thenReturn((long) Integer.MAX_VALUE + 10); updateCount = preparedStatement.executeUpdate(); assertEquals(Integer.MAX_VALUE, updateCount); assertEquals(Integer.MAX_VALUE, preparedStatement.getUpdateCount()); - when(mockConn.isSimpleExecute()).thenReturn(true); + Mockito.when(mockConn.isSimpleExecute()).thenReturn(true); preparedStatement.executeQuery(); assertEquals(-1, preparedStatement.getUpdateCount()); @@ -444,7 +433,7 @@ public void testGetUpdateCount() throws SQLException { @Test public void testSetParameters() throws Exception { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); Mockito.when(mockConn.getTreatUtilDateAsTimestamp()).thenReturn(true); VitessPreparedStatement preparedStatement = new VitessPreparedStatement(mockConn, sqlSelect); Boolean boolValue = true; @@ -565,7 +554,7 @@ public void testSetParameters() throws Exception { @Test public void testTreatUtilDateAsTimestamp() throws Exception { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessPreparedStatement preparedStatement = new VitessPreparedStatement(mockConn, sqlSelect); java.util.Date utilDateValue = new java.util.Date(System.currentTimeMillis()); @@ -593,24 +582,24 @@ public void testTreatUtilDateAsTimestamp() throws Exception { @Test public void testAutoGeneratedKeys() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); try { long expectedFirstGeneratedId = 121; long[] expectedGeneratedIds = {121, 122}; int expectedAffectedRows = 2; - when(mockCursor.getInsertId()).thenReturn(expectedFirstGeneratedId); - when(mockCursor.getRowsAffected()).thenReturn(Long.valueOf(expectedAffectedRows)); + Mockito.when(mockCursor.getInsertId()).thenReturn(expectedFirstGeneratedId); + Mockito.when(mockCursor.getRowsAffected()).thenReturn(Long.valueOf(expectedAffectedRows)); //Executing Insert Statement VitessPreparedStatement preparedStatement = new VitessPreparedStatement(mockConn, sqlInsert, @@ -632,7 +621,7 @@ public void testAutoGeneratedKeys() throws SQLException { @Test public void testAddBatch() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessPreparedStatement statement = new VitessPreparedStatement(mockConn, sqlInsert); try { statement.addBatch(this.sqlInsert); @@ -656,7 +645,7 @@ public void testAddBatch() throws SQLException { @Test public void testClearBatch() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessPreparedStatement statement = new VitessPreparedStatement(mockConn, sqlInsert); statement.setString(1, "string1"); statement.addBatch(); @@ -674,25 +663,25 @@ public void testClearBatch() throws SQLException { @Test public void testExecuteBatch() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessPreparedStatement statement = new VitessPreparedStatement(mockConn, sqlInsert); int[] updateCounts = statement.executeBatch(); assertEquals(0, updateCounts.length); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockConn.getAutoCommit()).thenReturn(true); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockConn.getAutoCommit()).thenReturn(true); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); - when(mockVtGateConn.executeBatch(nullable(Context.class), Matchers.anyList(), Matchers.anyList(), + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); + Mockito.when(mockVtGateConn.executeBatch(nullable(Context.class), Matchers.anyList(), Matchers.anyList(), nullable(VTSession.class))).thenReturn(mockSqlFutureCursor); List mockCursorWithErrorList = new ArrayList<>(); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursorWithErrorList); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursorWithErrorList); - CursorWithError mockCursorWithError1 = mock(CursorWithError.class); - when(mockCursorWithError1.getError()).thenReturn(null); - when(mockCursorWithError1.getCursor()).thenReturn(mock(Cursor.class)); + CursorWithError mockCursorWithError1 = Mockito.mock(CursorWithError.class); + Mockito.when(mockCursorWithError1.getError()).thenReturn(null); + Mockito.when(mockCursorWithError1.getCursor()).thenReturn(Mockito.mock(Cursor.class)); mockCursorWithErrorList.add(mockCursorWithError1); statement.setString(1, "string1"); @@ -700,10 +689,10 @@ public void testExecuteBatch() throws SQLException { updateCounts = statement.executeBatch(); assertEquals(1, updateCounts.length); - CursorWithError mockCursorWithError2 = mock(CursorWithError.class); + CursorWithError mockCursorWithError2 = Mockito.mock(CursorWithError.class); Vtrpc.RPCError rpcError = Vtrpc.RPCError.newBuilder() .setMessage("preparedStatement execute batch error").build(); - when(mockCursorWithError2.getError()).thenReturn(rpcError); + Mockito.when(mockCursorWithError2.getError()).thenReturn(rpcError); mockCursorWithErrorList.add(mockCursorWithError2); statement.setString(1, "string1"); statement.addBatch(); @@ -721,7 +710,7 @@ public void testExecuteBatch() throws SQLException { @Test public void testStatementCount() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); Map testCases = ImmutableMap.builder() .put("select * from foo where a = ?", 1).put("select * from foo where a = ? and b = ?", 2) .put("select * from foo where a = ? and b = \"?\"", 1) diff --git a/java/jdbc/src/test/java/io/vitess/jdbc/VitessResultSetTest.java b/java/jdbc/src/test/java/io/vitess/jdbc/VitessResultSetTest.java index a2be875e0d0..bd753d2fcb5 100644 --- a/java/jdbc/src/test/java/io/vitess/jdbc/VitessResultSetTest.java +++ b/java/jdbc/src/test/java/io/vitess/jdbc/VitessResultSetTest.java @@ -40,18 +40,13 @@ import org.junit.Assert; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Matchers; +import org.mockito.Mockito; import org.mockito.internal.verification.VerificationModeFactory; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; /** * Created by harshit.gangal on 19/01/16. */ -@RunWith(PowerMockRunner.class) -@PrepareForTest(VitessResultSet.class) public class VitessResultSetTest extends BaseTest { public Cursor getCursorWithRows() { @@ -635,230 +630,6 @@ public void testEnhancedFieldsFromCursor() throws Exception { assertEquals(cursor.getFields().size(), vitessResultSet.getFields().size()); } - @Test - public void testGetStringUsesEncoding() throws Exception { - VitessConnection conn = getVitessConnection(); - VitessResultSet resultOne = PowerMockito - .spy(new VitessResultSet(getCursorWithRows(), new VitessStatement(conn))); - resultOne.next(); - // test all ways to get to convertBytesToString - - // Verify that we're going through convertBytesToString for column types that return bytes - // (string-like), - // but not for those that return a real object - //resultOne.getString("col21"); // is a string, should go through convert bytes - //resultOne.getString("col13"); // is a datetime, should not - //PowerMockito.verifyPrivate(resultOne, VerificationModeFactory.times(1)) - // .invoke("convertBytesToString", Matchers.any(byte[].class), Matchers.anyString()); - - conn.setIncludedFields(Query.ExecuteOptions.IncludedFields.TYPE_AND_NAME); - VitessResultSet resultTwo = PowerMockito - .spy(new VitessResultSet(getCursorWithRows(), new VitessStatement(conn))); - resultTwo.next(); - - // neither of these should go through convertBytesToString, because we didn't include all fields - resultTwo.getString("col21"); - resultTwo.getString("col13"); - PowerMockito.verifyPrivate(resultTwo, VerificationModeFactory.times(0)) - .invoke("convertBytesToString", Matchers.any(byte[].class), Matchers.anyString()); - } - - @Test - public void testGetObjectForBitValues() throws Exception { - VitessConnection conn = getVitessConnection(); - - ByteString.Output value = ByteString.newOutput(); - value.write(new byte[]{1}); - value.write(new byte[]{0}); - value.write(new byte[]{1, 2, 3, 4}); - - Query.QueryResult result = Query.QueryResult.newBuilder().addFields( - Query.Field.newBuilder().setName("col1").setColumnLength(1).setType(Query.Type.BIT)) - .addFields( - Query.Field.newBuilder().setName("col2").setColumnLength(1).setType(Query.Type.BIT)) - .addFields( - Query.Field.newBuilder().setName("col3").setColumnLength(4).setType(Query.Type.BIT)) - .addRows(Query.Row.newBuilder().addLengths(1).addLengths(1).addLengths(4) - .setValues(value.toByteString())).build(); - - VitessResultSet vitessResultSet = PowerMockito - .spy(new VitessResultSet(new SimpleCursor(result), new VitessStatement(conn))); - vitessResultSet.next(); - - assertEquals(true, vitessResultSet.getObject(1)); - assertEquals(false, vitessResultSet.getObject(2)); - Assert.assertArrayEquals(new byte[]{1, 2, 3, 4}, (byte[]) vitessResultSet.getObject(3)); - - PowerMockito.verifyPrivate(vitessResultSet, VerificationModeFactory.times(3)) - .invoke("convertBytesIfPossible", Matchers.any(byte[].class), - Matchers.any(FieldWithMetadata.class)); - - conn.setIncludedFields(Query.ExecuteOptions.IncludedFields.TYPE_AND_NAME); - vitessResultSet = PowerMockito - .spy(new VitessResultSet(new SimpleCursor(result), new VitessStatement(conn))); - vitessResultSet.next(); - - Assert.assertArrayEquals(new byte[]{1}, (byte[]) vitessResultSet.getObject(1)); - Assert.assertArrayEquals(new byte[]{0}, (byte[]) vitessResultSet.getObject(2)); - Assert.assertArrayEquals(new byte[]{1, 2, 3, 4}, (byte[]) vitessResultSet.getObject(3)); - - PowerMockito.verifyPrivate(vitessResultSet, VerificationModeFactory.times(0)) - .invoke("convertBytesIfPossible", Matchers.any(byte[].class), - Matchers.any(FieldWithMetadata.class)); - } - - @Test - public void testGetObjectForVarBinLikeValues() throws Exception { - VitessConnection conn = getVitessConnection(); - - ByteString.Output value = ByteString.newOutput(); - - byte[] binary = new byte[]{1, 2, 3, 4}; - byte[] varbinary = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; - byte[] blob = new byte[MysqlDefs.LENGTH_BLOB]; - for (int i = 0; i < blob.length; i++) { - blob[i] = 1; - } - byte[] fakeGeometry = new byte[]{2, 3, 4}; - - value.write(binary); - value.write(varbinary); - value.write(blob); - value.write(fakeGeometry); - - Query.QueryResult result = Query.QueryResult.newBuilder().addFields( - Query.Field.newBuilder().setName("col1").setColumnLength(4) - .setCharset(CharsetMapping.MYSQL_COLLATION_INDEX_binary).setType(Query.Type.BINARY) - .setFlags(Query.MySqlFlag.BINARY_FLAG_VALUE)).addFields( - Query.Field.newBuilder().setName("col2").setColumnLength(13) - .setCharset(CharsetMapping.MYSQL_COLLATION_INDEX_binary).setType(Query.Type.VARBINARY) - .setFlags(Query.MySqlFlag.BINARY_FLAG_VALUE)).addFields( - Query.Field.newBuilder().setName("col3") // should go to LONGVARBINARY due to below settings - .setColumnLength(MysqlDefs.LENGTH_BLOB) - .setCharset(CharsetMapping.MYSQL_COLLATION_INDEX_binary) - .setFlags(Query.MySqlFlag.BINARY_FLAG_VALUE).setType(Query.Type.BLOB)).addFields( - Query.Field.newBuilder().setName("col4").setType(Query.Type.GEOMETRY) - .setCharset(CharsetMapping.MYSQL_COLLATION_INDEX_binary).setType(Query.Type.BINARY) - .setFlags(Query.MySqlFlag.BINARY_FLAG_VALUE)).addRows( - Query.Row.newBuilder().addLengths(4).addLengths(13).addLengths(MysqlDefs.LENGTH_BLOB) - .addLengths(3).setValues(value.toByteString())).build(); - - VitessResultSet vitessResultSet = PowerMockito - .spy(new VitessResultSet(new SimpleCursor(result), new VitessStatement(conn))); - vitessResultSet.next(); - - // All of these types should pass straight through, returning the direct bytes - Assert.assertArrayEquals(binary, (byte[]) vitessResultSet.getObject(1)); - Assert.assertArrayEquals(varbinary, (byte[]) vitessResultSet.getObject(2)); - Assert.assertArrayEquals(blob, (byte[]) vitessResultSet.getObject(3)); - Assert.assertArrayEquals(fakeGeometry, (byte[]) vitessResultSet.getObject(4)); - - // We should still call the function 4 times - PowerMockito.verifyPrivate(vitessResultSet, VerificationModeFactory.times(4)) - .invoke("convertBytesIfPossible", Matchers.any(byte[].class), - Matchers.any(FieldWithMetadata.class)); - - conn.setIncludedFields(Query.ExecuteOptions.IncludedFields.TYPE_AND_NAME); - vitessResultSet = PowerMockito - .spy(new VitessResultSet(new SimpleCursor(result), new VitessStatement(conn))); - vitessResultSet.next(); - - // Same as above since this doesn't really do much but pass right through for the varbinary type - Assert.assertArrayEquals(binary, (byte[]) vitessResultSet.getObject(1)); - Assert.assertArrayEquals(varbinary, (byte[]) vitessResultSet.getObject(2)); - Assert.assertArrayEquals(blob, (byte[]) vitessResultSet.getObject(3)); - Assert.assertArrayEquals(fakeGeometry, (byte[]) vitessResultSet.getObject(4)); - - // Never call because not including all - PowerMockito.verifyPrivate(vitessResultSet, VerificationModeFactory.times(0)) - .invoke("convertBytesIfPossible", Matchers.any(byte[].class), - Matchers.any(FieldWithMetadata.class)); - } - - @Test - public void testGetObjectForStringLikeValues() throws Exception { - ByteString.Output value = ByteString.newOutput(); - - String trimmedCharStr = "wasting space"; - String varcharStr = "i have a variable length!"; - String masqueradingBlobStr = "look at me, im a blob"; - String textStr = "an enthralling string of TEXT in some foreign language"; - String jsonStr = "{\"status\": \"ok\"}"; - - int paddedCharColLength = 20; - byte[] trimmedChar = StringUtils.getBytes(trimmedCharStr, "UTF-16"); - byte[] varchar = StringUtils.getBytes(varcharStr, "UTF-8"); - byte[] masqueradingBlob = StringUtils.getBytes(masqueradingBlobStr, "US-ASCII"); - byte[] text = StringUtils.getBytes(textStr, "ISO8859_8"); - byte[] opaqueBinary = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9}; - byte[] json = StringUtils.getBytes(jsonStr, "UTF-8"); - - value.write(trimmedChar); - value.write(varchar); - value.write(opaqueBinary); - value.write(masqueradingBlob); - value.write(text); - value.write(json); - - Query.QueryResult result = Query.QueryResult.newBuilder() - // This tests CHAR - .addFields(Query.Field.newBuilder().setName("col1").setColumnLength(paddedCharColLength) - .setCharset(/* utf-16 collation index from CharsetMapping */ 54) - .setType(Query.Type.CHAR)) - // This tests VARCHAR - .addFields(Query.Field.newBuilder().setName("col2").setColumnLength(varchar.length) - .setCharset(CharsetMapping.MYSQL_COLLATION_INDEX_utf8).setType(Query.Type.VARCHAR)) - // This tests VARCHAR that is an opaque binary - .addFields(Query.Field.newBuilder().setName("col2").setColumnLength(opaqueBinary.length) - .setCharset(CharsetMapping.MYSQL_COLLATION_INDEX_binary) - .setFlags(Query.MySqlFlag.BINARY_FLAG_VALUE).setType(Query.Type.VARCHAR)) - // This tests LONGVARCHAR - .addFields(Query.Field.newBuilder().setName("col3").setColumnLength(masqueradingBlob.length) - .setCharset(/* us-ascii collation index from CharsetMapping */11) - .setType(Query.Type.BLOB)) - // This tests TEXT, which falls through the default case of the switch - .addFields(Query.Field.newBuilder().setName("col4").setColumnLength(text.length) - .setCharset(/* corresponds to greek, from CharsetMapping */25).setType(Query.Type.TEXT)) - .addFields(Query.Field.newBuilder().setName("col5").setColumnLength(json.length) - .setCharset(CharsetMapping.MYSQL_COLLATION_INDEX_utf8).setType(Query.Type.JSON)) - .addRows(Query.Row.newBuilder().addLengths(trimmedChar.length).addLengths(varchar.length) - .addLengths(opaqueBinary.length).addLengths(masqueradingBlob.length) - .addLengths(text.length).addLengths(json.length).setValues(value.toByteString())) - .build(); - - VitessConnection conn = getVitessConnection(); - VitessResultSet vitessResultSet = PowerMockito - .spy(new VitessResultSet(new SimpleCursor(result), new VitessStatement(conn))); - vitessResultSet.next(); - - assertEquals(trimmedCharStr, vitessResultSet.getObject(1)); - assertEquals(varcharStr, vitessResultSet.getObject(2)); - Assert.assertArrayEquals(opaqueBinary, (byte[]) vitessResultSet.getObject(3)); - assertEquals(masqueradingBlobStr, vitessResultSet.getObject(4)); - assertEquals(textStr, vitessResultSet.getObject(5)); - assertEquals(jsonStr, vitessResultSet.getObject(6)); - - PowerMockito.verifyPrivate(vitessResultSet, VerificationModeFactory.times(6)) - .invoke("convertBytesIfPossible", Matchers.any(byte[].class), - Matchers.any(FieldWithMetadata.class)); - - conn.setIncludedFields(Query.ExecuteOptions.IncludedFields.TYPE_AND_NAME); - vitessResultSet = PowerMockito - .spy(new VitessResultSet(new SimpleCursor(result), new VitessStatement(conn))); - vitessResultSet.next(); - - Assert.assertArrayEquals(trimmedChar, (byte[]) vitessResultSet.getObject(1)); - Assert.assertArrayEquals(varchar, (byte[]) vitessResultSet.getObject(2)); - Assert.assertArrayEquals(opaqueBinary, (byte[]) vitessResultSet.getObject(3)); - Assert.assertArrayEquals(masqueradingBlob, (byte[]) vitessResultSet.getObject(4)); - Assert.assertArrayEquals(text, (byte[]) vitessResultSet.getObject(5)); - Assert.assertArrayEquals(json, (byte[]) vitessResultSet.getObject(6)); - - PowerMockito.verifyPrivate(vitessResultSet, VerificationModeFactory.times(0)) - .invoke("convertBytesIfPossible", Matchers.any(byte[].class), - Matchers.any(FieldWithMetadata.class)); - } - @Test public void testGetClob() throws SQLException { VitessResultSet vitessResultSet = new VitessResultSet(new String[]{"clob"}, diff --git a/java/jdbc/src/test/java/io/vitess/jdbc/VitessStatementTest.java b/java/jdbc/src/test/java/io/vitess/jdbc/VitessStatementTest.java index 3f4bb39a44c..b641cafdefb 100644 --- a/java/jdbc/src/test/java/io/vitess/jdbc/VitessStatementTest.java +++ b/java/jdbc/src/test/java/io/vitess/jdbc/VitessStatementTest.java @@ -22,15 +22,8 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; import static org.mockito.Matchers.nullable; -import static org.mockito.Matchers.anyList; -import static org.mockito.Matchers.anyMap; -import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.verify; -import static org.powermock.api.mockito.PowerMockito.doReturn; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; import io.vitess.client.Context; import io.vitess.client.SQLFuture; @@ -52,18 +45,12 @@ import java.util.Map; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; /** * Created by harshit.gangal on 19/01/16. */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({VTGateConnection.class, Vtrpc.RPCError.class}) public class VitessStatementTest { private String sqlSelect = "select 1 from test_table"; @@ -76,7 +63,7 @@ public class VitessStatementTest { @Test public void testGetConnection() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); assertEquals(mockConn, statement.getConnection()); @@ -84,24 +71,24 @@ public void testGetConnection() throws SQLException { @Test public void testGetResultSet() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); assertEquals(null, statement.getResultSet()); } @Test public void testExecuteQuery() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); - when(mockConn.isSimpleExecute()).thenReturn(true); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); + Mockito.when(mockConn.isSimpleExecute()).thenReturn(true); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); VitessStatement statement = new VitessStatement(mockConn); //Empty Sql Statement @@ -116,13 +103,13 @@ public void testExecuteQuery() throws SQLException { assertEquals(-1, statement.getUpdateCount()); //autocommit is false and not in transaction - when(mockConn.getAutoCommit()).thenReturn(false); - when(mockConn.isInTransaction()).thenReturn(false); + Mockito.when(mockConn.getAutoCommit()).thenReturn(false); + Mockito.when(mockConn.isInTransaction()).thenReturn(false); rs = statement.executeQuery(sqlSelect); assertEquals(-1, statement.getUpdateCount()); //when returned cursor is null - when(mockSqlFutureCursor.checkedGet()).thenReturn(null); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(null); try { statement.executeQuery(sqlSelect); fail("Should have thrown exception for cursor null"); @@ -133,18 +120,18 @@ public void testExecuteQuery() throws SQLException { @Test public void testExecuteQueryWithStreamExecuteType() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn .streamExecute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockCursor); - when(mockConn.getExecuteType()).thenReturn(Constants.QueryExecuteType.STREAM); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); + Mockito.when(mockConn.getExecuteType()).thenReturn(Constants.QueryExecuteType.STREAM); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); VitessStatement statement = new VitessStatement(mockConn); //Empty Sql Statement @@ -164,13 +151,13 @@ public void testExecuteQueryWithStreamExecuteType() throws SQLException { assertEquals(-1, statement.getUpdateCount()); //select on primary when tx is null and autocommit is false - when(mockConn.getAutoCommit()).thenReturn(false); - when(mockConn.isInTransaction()).thenReturn(false); + Mockito.when(mockConn.getAutoCommit()).thenReturn(false); + Mockito.when(mockConn.isInTransaction()).thenReturn(false); rs = statement.executeQuery(sqlSelect); assertEquals(-1, statement.getUpdateCount()); //when returned cursor is null - when(mockVtGateConn + Mockito.when(mockVtGateConn .streamExecute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(null); try { @@ -191,19 +178,19 @@ public void testExecuteFetchSizeAsStreaming() throws SQLException { private void testExecute(int fetchSize, boolean simpleExecute, boolean shouldRunExecute, boolean shouldRunStreamExecute) throws SQLException { - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); - VitessConnection mockConn = mock(VitessConnection.class); - when(mockConn.isSimpleExecute()).thenReturn(simpleExecute); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + Mockito.when(mockConn.isSimpleExecute()).thenReturn(simpleExecute); + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); - when(mockVtGateConn + Mockito.when(mockVtGateConn .streamExecute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockCursor); @@ -224,17 +211,17 @@ private void testExecute(int fetchSize, boolean simpleExecute, boolean shouldRun @Test public void testExecuteUpdate() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); - List fieldList = mock(ArrayList.class); - - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); + List fieldList = Mockito.mock(ArrayList.class); + + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); VitessStatement statement = new VitessStatement(mockConn); //executing dml on primary @@ -242,13 +229,13 @@ public void testExecuteUpdate() throws SQLException { assertEquals(0, updateCount); //tx is null & autoCommit is true - when(mockConn.getAutoCommit()).thenReturn(true); + Mockito.when(mockConn.getAutoCommit()).thenReturn(true); updateCount = statement.executeUpdate(sqlUpdate); assertEquals(0, updateCount); //cursor fields is not null - when(mockCursor.getFields()).thenReturn(fieldList); - when(fieldList.isEmpty()).thenReturn(false); + Mockito.when(mockCursor.getFields()).thenReturn(fieldList); + Mockito.when(fieldList.isEmpty()).thenReturn(false); try { statement.executeUpdate(sqlSelect); fail("Should have thrown exception for field not null"); @@ -257,7 +244,7 @@ public void testExecuteUpdate() throws SQLException { } //cursor is null - when(mockSqlFutureCursor.checkedGet()).thenReturn(null); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(null); try { statement.executeUpdate(sqlUpdate); fail("Should have thrown exception for cursor null"); @@ -266,7 +253,7 @@ public void testExecuteUpdate() throws SQLException { } //read only - when(mockConn.isReadOnly()).thenReturn(true); + Mockito.when(mockConn.isReadOnly()).thenReturn(true); try { statement.execute("UPDATE SET foo = 1 ON mytable WHERE id = 1"); fail("Should have thrown exception for read only"); @@ -275,7 +262,7 @@ public void testExecuteUpdate() throws SQLException { } //read only - when(mockConn.isReadOnly()).thenReturn(true); + Mockito.when(mockConn.isReadOnly()).thenReturn(true); try { statement.executeBatch(); fail("Should have thrown exception for read only"); @@ -286,27 +273,27 @@ public void testExecuteUpdate() throws SQLException { @Test public void testExecute() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); - List mockFieldList = PowerMockito.spy(new ArrayList<>()); - - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); + List mockFieldList = Mockito.spy(new ArrayList<>()); + + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); - when(mockConn.getAutoCommit()).thenReturn(true); - when(mockConn.getExecuteType()).thenReturn(Constants.QueryExecuteType.SIMPLE); - when(mockConn.isSimpleExecute()).thenReturn(true); + Mockito.when(mockConn.getAutoCommit()).thenReturn(true); + Mockito.when(mockConn.getExecuteType()).thenReturn(Constants.QueryExecuteType.SIMPLE); + Mockito.when(mockConn.isSimpleExecute()).thenReturn(true); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockCursor.getFields()).thenReturn(mockFieldList); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockCursor.getFields()).thenReturn(mockFieldList); VitessStatement statement = new VitessStatement(mockConn); int fieldSize = 5; - when(mockCursor.getFields()).thenReturn(mockFieldList); - doReturn(fieldSize).when(mockFieldList).size(); - doReturn(false).when(mockFieldList).isEmpty(); + Mockito.when(mockCursor.getFields()).thenReturn(mockFieldList); + Mockito.doReturn(fieldSize).when(mockFieldList).size(); + Mockito.doReturn(false).when(mockFieldList).isEmpty(); boolean hasResultSet = statement.execute(sqlSelect); assertTrue(hasResultSet); @@ -317,15 +304,15 @@ public void testExecute() throws SQLException { assertNotNull(statement.getResultSet()); int mockUpdateCount = 10; - when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); - when(mockCursor.getRowsAffected()).thenReturn((long) mockUpdateCount); + Mockito.when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); + Mockito.when(mockCursor.getRowsAffected()).thenReturn((long) mockUpdateCount); hasResultSet = statement.execute(sqlUpdate); assertFalse(hasResultSet); assertNull(statement.getResultSet()); assertEquals(mockUpdateCount, statement.getUpdateCount()); //cursor is null - when(mockSqlFutureCursor.checkedGet()).thenReturn(null); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(null); try { statement.execute(sqlUpdate); fail("Should have thrown exception for cursor null"); @@ -336,47 +323,47 @@ public void testExecute() throws SQLException { @Test public void testGetUpdateCount() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFuture = mock(SQLFuture.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFuture = Mockito.mock(SQLFuture.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFuture); - when(mockSqlFuture.checkedGet()).thenReturn(mockCursor); - when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); + Mockito.when(mockSqlFuture.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); VitessStatement statement = new VitessStatement(mockConn); - when(mockCursor.getRowsAffected()).thenReturn(10L); + Mockito.when(mockCursor.getRowsAffected()).thenReturn(10L); int updateCount = statement.executeUpdate(sqlUpdate); assertEquals(10L, updateCount); assertEquals(10L, statement.getUpdateCount()); // Truncated Update Count - when(mockCursor.getRowsAffected()).thenReturn((long) Integer.MAX_VALUE + 10); + Mockito.when(mockCursor.getRowsAffected()).thenReturn((long) Integer.MAX_VALUE + 10); updateCount = statement.executeUpdate(sqlUpdate); assertEquals(Integer.MAX_VALUE, updateCount); assertEquals(Integer.MAX_VALUE, statement.getUpdateCount()); - when(mockConn.isSimpleExecute()).thenReturn(true); + Mockito.when(mockConn.isSimpleExecute()).thenReturn(true); statement.executeQuery(sqlSelect); assertEquals(-1, statement.getUpdateCount()); } @Test public void testClose() throws Exception { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); - when(mockConn.getExecuteType()).thenReturn(Constants.QueryExecuteType.SIMPLE); - when(mockConn.isSimpleExecute()).thenReturn(true); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockConn.getExecuteType()).thenReturn(Constants.QueryExecuteType.SIMPLE); + Mockito.when(mockConn.isSimpleExecute()).thenReturn(true); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); VitessStatement statement = new VitessStatement(mockConn); ResultSet rs = statement.executeQuery(sqlSelect); @@ -391,7 +378,7 @@ public void testClose() throws Exception { @Test public void testGetMaxFieldSize() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); assertEquals(65535, statement.getMaxFieldSize()); @@ -399,7 +386,7 @@ public void testGetMaxFieldSize() throws SQLException { @Test public void testGetMaxRows() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); @@ -417,7 +404,7 @@ public void testGetMaxRows() throws SQLException { @Test public void testGetQueryTimeout() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); Mockito.when(mockConn.getTimeout()).thenReturn((long) Constants.DEFAULT_TIMEOUT); VitessStatement statement = new VitessStatement(mockConn); @@ -426,7 +413,7 @@ public void testGetQueryTimeout() throws SQLException { @Test public void testGetQueryTimeoutZeroDefault() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); Mockito.when(mockConn.getTimeout()).thenReturn(0L); VitessStatement statement = new VitessStatement(mockConn); @@ -435,7 +422,7 @@ public void testGetQueryTimeoutZeroDefault() throws SQLException { @Test public void testSetQueryTimeout() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); Mockito.when(mockConn.getTimeout()).thenReturn((long) Constants.DEFAULT_TIMEOUT); VitessStatement statement = new VitessStatement(mockConn); @@ -457,7 +444,7 @@ public void testSetQueryTimeout() throws SQLException { @Test public void testGetWarnings() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); assertNull(statement.getWarnings()); @@ -465,7 +452,7 @@ public void testGetWarnings() throws SQLException { @Test public void testGetFetchDirection() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); assertEquals(ResultSet.FETCH_FORWARD, statement.getFetchDirection()); @@ -473,7 +460,7 @@ public void testGetFetchDirection() throws SQLException { @Test public void testGetFetchSize() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); assertEquals(0, statement.getFetchSize()); @@ -481,7 +468,7 @@ public void testGetFetchSize() throws SQLException { @Test public void testGetResultSetConcurrency() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); assertEquals(ResultSet.CONCUR_READ_ONLY, statement.getResultSetConcurrency()); @@ -489,7 +476,7 @@ public void testGetResultSetConcurrency() throws SQLException { @Test public void testGetResultSetType() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); assertEquals(ResultSet.TYPE_FORWARD_ONLY, statement.getResultSetType()); @@ -497,7 +484,7 @@ public void testGetResultSetType() throws SQLException { @Test public void testIsClosed() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); assertFalse(statement.isClosed()); @@ -507,23 +494,23 @@ public void testIsClosed() throws SQLException { @Test public void testAutoGeneratedKeys() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockVtGateConn.execute(nullable(Context.class), nullable(String.class), nullable(Map.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); VitessStatement statement = new VitessStatement(mockConn); long expectedFirstGeneratedId = 121; long[] expectedGeneratedIds = {121, 122, 123, 124, 125}; int expectedAffectedRows = 5; - when(mockCursor.getInsertId()).thenReturn(expectedFirstGeneratedId); - when(mockCursor.getRowsAffected()).thenReturn(Long.valueOf(expectedAffectedRows)); + Mockito.when(mockCursor.getInsertId()).thenReturn(expectedFirstGeneratedId); + Mockito.when(mockCursor.getRowsAffected()).thenReturn(Long.valueOf(expectedAffectedRows)); //Executing Insert Statement int updateCount = statement.executeUpdate(sqlInsert, Statement.RETURN_GENERATED_KEYS); @@ -550,7 +537,7 @@ public void testAutoGeneratedKeys() throws SQLException { //Fetching Generated Keys on update query expectedFirstGeneratedId = 0; - when(mockCursor.getInsertId()).thenReturn(expectedFirstGeneratedId); + Mockito.when(mockCursor.getInsertId()).thenReturn(expectedFirstGeneratedId); updateCount = statement.executeUpdate(sqlUpdate, Statement.RETURN_GENERATED_KEYS); assertEquals(expectedAffectedRows, updateCount); @@ -560,7 +547,7 @@ public void testAutoGeneratedKeys() throws SQLException { @Test public void testAddBatch() throws Exception { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); statement.addBatch(sqlInsert); Field privateStringField = VitessStatement.class.getDeclaredField("batchedArgs"); @@ -570,7 +557,7 @@ public void testAddBatch() throws Exception { @Test public void testClearBatch() throws Exception { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); statement.addBatch(sqlInsert); statement.clearBatch(); @@ -581,36 +568,36 @@ public void testClearBatch() throws Exception { @Test public void testExecuteBatch() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); int[] updateCounts = statement.executeBatch(); assertEquals(0, updateCounts.length); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockConn.getAutoCommit()).thenReturn(true); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockConn.getAutoCommit()).thenReturn(true); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); - when( + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); + Mockito.when( mockVtGateConn.executeBatch(nullable(Context.class), nullable(List.class), nullable(List.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); List mockCursorWithErrorList = new ArrayList<>(); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursorWithErrorList); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursorWithErrorList); - CursorWithError mockCursorWithError1 = mock(CursorWithError.class); - when(mockCursorWithError1.getError()).thenReturn(null); - when(mockCursorWithError1.getCursor()).thenReturn(mock(Cursor.class)); + CursorWithError mockCursorWithError1 = Mockito.mock(CursorWithError.class); + Mockito.when(mockCursorWithError1.getError()).thenReturn(null); + Mockito.when(mockCursorWithError1.getCursor()).thenReturn(Mockito.mock(Cursor.class)); mockCursorWithErrorList.add(mockCursorWithError1); statement.addBatch(sqlUpdate); updateCounts = statement.executeBatch(); assertEquals(1, updateCounts.length); - CursorWithError mockCursorWithError2 = mock(CursorWithError.class); + CursorWithError mockCursorWithError2 = Mockito.mock(CursorWithError.class); Vtrpc.RPCError rpcError = Vtrpc.RPCError.newBuilder() .setMessage("statement execute batch error").build(); - when(mockCursorWithError2.getError()).thenReturn(rpcError); + Mockito.when(mockCursorWithError2.getError()).thenReturn(rpcError); mockCursorWithErrorList.add(mockCursorWithError2); statement.addBatch(sqlUpdate); statement.addBatch(sqlUpdate); @@ -627,33 +614,33 @@ public void testExecuteBatch() throws SQLException { @Test public void testBatchGeneratedKeys() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockConn.getAutoCommit()).thenReturn(true); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockConn.getAutoCommit()).thenReturn(true); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); - when( + Mockito.when( mockVtGateConn.executeBatch(nullable(Context.class), nullable(List.class), nullable(List.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); List mockCursorWithErrorList = new ArrayList<>(); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursorWithErrorList); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursorWithErrorList); - CursorWithError mockCursorWithError = mock(CursorWithError.class); - when(mockCursorWithError.getError()).thenReturn(null); - when(mockCursorWithError.getCursor()).thenReturn(mockCursor); + CursorWithError mockCursorWithError = Mockito.mock(CursorWithError.class); + Mockito.when(mockCursorWithError.getError()).thenReturn(null); + Mockito.when(mockCursorWithError.getCursor()).thenReturn(mockCursor); mockCursorWithErrorList.add(mockCursorWithError); long expectedFirstGeneratedId = 121; long[] expectedGeneratedIds = {121, 122, 123, 124, 125}; - when(mockCursor.getInsertId()).thenReturn(expectedFirstGeneratedId); - when(mockCursor.getRowsAffected()).thenReturn(Long.valueOf(expectedGeneratedIds.length)); + Mockito.when(mockCursor.getInsertId()).thenReturn(expectedFirstGeneratedId); + Mockito.when(mockCursor.getRowsAffected()).thenReturn(Long.valueOf(expectedGeneratedIds.length)); statement.addBatch(sqlInsert); statement.executeBatch(); @@ -668,33 +655,33 @@ public void testBatchGeneratedKeys() throws SQLException { @Test public void testBatchUpsertGeneratedKeys() throws SQLException { - VitessConnection mockConn = mock(VitessConnection.class); + VitessConnection mockConn = Mockito.mock(VitessConnection.class); VitessStatement statement = new VitessStatement(mockConn); - Cursor mockCursor = mock(Cursor.class); - SQLFuture mockSqlFutureCursor = mock(SQLFuture.class); + Cursor mockCursor = Mockito.mock(Cursor.class); + SQLFuture mockSqlFutureCursor = Mockito.mock(SQLFuture.class); - VTGateConnection mockVtGateConn = mock(VTGateConnection.class); - when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); - when(mockConn.getAutoCommit()).thenReturn(true); + VTGateConnection mockVtGateConn = Mockito.mock(VTGateConnection.class); + Mockito.when(mockConn.getVtGateConn()).thenReturn(mockVtGateConn); + Mockito.when(mockConn.getAutoCommit()).thenReturn(true); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); - when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursor); + Mockito.when(mockCursor.getFields()).thenReturn(Query.QueryResult.getDefaultInstance().getFieldsList()); - when( + Mockito.when( mockVtGateConn.executeBatch(nullable(Context.class), nullable(List.class), nullable(List.class), nullable(VTSession.class))) .thenReturn(mockSqlFutureCursor); List mockCursorWithErrorList = new ArrayList<>(); - when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursorWithErrorList); + Mockito.when(mockSqlFutureCursor.checkedGet()).thenReturn(mockCursorWithErrorList); - CursorWithError mockCursorWithError = mock(CursorWithError.class); - when(mockCursorWithError.getError()).thenReturn(null); - when(mockCursorWithError.getCursor()).thenReturn(mockCursor); + CursorWithError mockCursorWithError = Mockito.mock(CursorWithError.class); + Mockito.when(mockCursorWithError.getError()).thenReturn(null); + Mockito.when(mockCursorWithError.getCursor()).thenReturn(mockCursor); mockCursorWithErrorList.add(mockCursorWithError); long expectedFirstGeneratedId = 121; long[] expectedGeneratedIds = {121, 122}; - when(mockCursor.getInsertId()).thenReturn(expectedFirstGeneratedId); - when(mockCursor.getRowsAffected()).thenReturn(Long.valueOf(expectedGeneratedIds.length)); + Mockito.when(mockCursor.getInsertId()).thenReturn(expectedFirstGeneratedId); + Mockito.when(mockCursor.getRowsAffected()).thenReturn(Long.valueOf(expectedGeneratedIds.length)); statement.addBatch(sqlUpsert); statement.executeBatch(); @@ -709,8 +696,8 @@ public void testBatchUpsertGeneratedKeys() throws SQLException { } VitessStatement noUpdate = new VitessStatement(mockConn); - when(mockCursor.getInsertId()).thenReturn(0L); - when(mockCursor.getRowsAffected()).thenReturn(1L); + Mockito.when(mockCursor.getInsertId()).thenReturn(0L); + Mockito.when(mockCursor.getRowsAffected()).thenReturn(1L); noUpdate.addBatch(sqlUpsert); noUpdate.executeBatch();