Skip to content

Commit

Permalink
remove powermock dependency and update test to use mockito when possible
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <[email protected]>
  • Loading branch information
harshit-gangal committed Jan 21, 2025
1 parent 0df00ef commit 0e60aa0
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 1,110 deletions.
19 changes: 0 additions & 19 deletions java/jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,6 @@
<version>3.12.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 6 additions & 10 deletions java/jdbc/src/test/java/io/vitess/jdbc/VitessConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down
Loading

0 comments on commit 0e60aa0

Please sign in to comment.