Skip to content

Commit

Permalink
use junit 5 for all java lib tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jimexist committed Apr 17, 2022
1 parent 90ea2e8 commit 520c43f
Show file tree
Hide file tree
Showing 63 changed files with 1,892 additions and 1,566 deletions.
2 changes: 1 addition & 1 deletion lib/java/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ httpcore.version=4.4.15
slf4j.version=1.7.35
servlet.version=4.0.1
tomcat.embed.version=9.0.43
junit.version=4.13.2
junit.version=5.8.2
mockito.version=1.10.19
javax.annotation.version=1.3.2
commons-lang3.version=3.12
3 changes: 2 additions & 1 deletion lib/java/gradle/environment.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ dependencies {
compile "javax.annotation:javax.annotation-api:${javaxAnnotationVersion}"
compile "org.apache.commons:commons-lang3:3.12.0"

testCompile "junit:junit:${junitVersion}"
testImplementation "org.junit.jupiter:junit-jupiter:${junitVersion}"
testImplementation "org.junit.jupiter:junit-jupiter-params:${junitVersion}"
testCompile "org.mockito:mockito-all:${mockitoVersion}"
testRuntime "org.slf4j:slf4j-log4j12:${slf4jVersion}"
}
8 changes: 7 additions & 1 deletion lib/java/gradle/unitTests.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ test {
include '**/Test*.class'
exclude '**/Test*\$*.class'

// https://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle
useJUnitPlatform() {
// https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution
systemProperty("junit.jupiter.execution.parallel.enabled", "true")
systemProperty("junit.jupiter.execution.parallel.mode.default", "concurrent")
}

maxHeapSize = '512m'
forkEvery = 1

systemProperties = [
'build.test': "${compileTestJava.destinationDir}",
Expand Down
7 changes: 5 additions & 2 deletions lib/java/test/org/apache/thrift/TestDeepCopy.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.apache.thrift;

import junit.framework.TestCase;
import org.junit.jupiter.api.Test;
import thrift.test.DeepCopyBar;
import thrift.test.DeepCopyFoo;

public class TestDeepCopy extends TestCase {
import static org.junit.jupiter.api.Assertions.assertNotSame;

public class TestDeepCopy {

@Test
public void testDeepCopy() throws Exception {
final DeepCopyFoo foo = new DeepCopyFoo();

Expand Down
19 changes: 13 additions & 6 deletions lib/java/test/org/apache/thrift/TestEnumContainers.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.apache.thrift;

import junit.framework.TestCase;
import org.junit.jupiter.api.Test;
import thrift.test.enumcontainers.EnumContainersTestConstants;
import thrift.test.enumcontainers.GodBean;
import thrift.test.enumcontainers.GreekGodGoddess;
Expand All @@ -29,8 +29,14 @@
import java.util.HashMap;
import java.util.HashSet;

public class TestEnumContainers extends TestCase {
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestEnumContainers {

@Test
public void testEnumContainers() throws Exception {
final GodBean b1 = new GodBean();
b1.addToGoddess(GreekGodGoddess.HERA);
Expand All @@ -51,11 +57,11 @@ public void testEnumContainers() throws Exception {
deserializer.deserialize(b3, bytes);
}

assertTrue(b1.getGoddess() != b2.getGoddess());
assertTrue(b1.getPower() != b2.getPower());
assertNotSame(b1.getGoddess(), b2.getGoddess());
assertNotSame(b1.getPower(), b2.getPower());

assertTrue(b1.getGoddess() != b3.getGoddess());
assertTrue(b1.getPower() != b3.getPower());
assertNotSame(b1.getGoddess(), b3.getGoddess());
assertNotSame(b1.getPower(), b3.getPower());

for (GodBean each : new GodBean[]{b1, b2, b3}) {
assertTrue(each.getGoddess().contains(GreekGodGoddess.HERA));
Expand All @@ -71,6 +77,7 @@ public void testEnumContainers() throws Exception {
}
}

@Test
public void testEnumConstants() {
assertEquals("lightning bolt", EnumContainersTestConstants.ATTRIBUTES.get(GreekGodGoddess.ZEUS));
assertTrue(EnumContainersTestConstants.ATTRIBUTES instanceof EnumMap);
Expand Down
18 changes: 9 additions & 9 deletions lib/java/test/org/apache/thrift/TestFullCamel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@

package org.apache.thrift;

import java.util.HashSet;

import junit.framework.TestCase;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TType;

import org.junit.jupiter.api.Test;
import thrift.test.fullcamel.OneOfEachZZ;
import thrift.test.fullcamel.UnderscoreSrv;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

// Sanity check for the code generated by 'fullcamel'.
//
public class TestFullCamel extends TestCase {
public class TestFullCamel {

@Test
public void testCamelCaseSyntax() throws Exception {
TSerializer binarySerializer = new TSerializer(new TBinaryProtocol.Factory());
TDeserializer binaryDeserializer = new TDeserializer(new TBinaryProtocol.Factory());
Expand All @@ -42,10 +41,11 @@ public void testCamelCaseSyntax() throws Exception {
obj.setImFalse(true);
byte[] serBytes = binarySerializer.serialize(obj);
binaryDeserializer.deserialize(obj, serBytes);
assertTrue( obj.getABite() == (byte) 0xae );
assertTrue( obj.isImFalse() == true );
assertEquals(obj.getABite(), (byte) 0xae);
assertEquals(true, obj.isImFalse());
}

@Test
public void testCamelCaseRpcMethods() throws Exception {
final UnderscoreSrv.Iface srv = new UnderscoreSrv.Iface() {
@Override
Expand Down
23 changes: 12 additions & 11 deletions lib/java/test/org/apache/thrift/TestMultiplexedProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,41 @@

package org.apache.thrift;

import org.apache.thrift.protocol.TMessage;
import org.apache.thrift.protocol.TMessageType;
import org.apache.thrift.protocol.TProtocol;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.apache.thrift.protocol.TMessage;
import org.apache.thrift.protocol.TMessageType;
import org.apache.thrift.protocol.TProtocol;
import org.junit.Before;
import org.junit.Test;

public class TestMultiplexedProcessor {
private TMultiplexedProcessor mp;
private TProtocol iprot;
private TProtocol oprot;

@Before
@BeforeEach
public void setUp() throws Exception {
mp = new TMultiplexedProcessor();
iprot = mock(TProtocol.class);
oprot = mock(TProtocol.class);
}

@Test(expected = TException.class)
@Test
public void testWrongMessageType() throws TException {
when (iprot.readMessageBegin()).thenReturn(new TMessage("service:func", TMessageType.REPLY, 42));
mp.process(iprot, oprot);
assertThrows(TException.class, ()->mp.process(iprot, oprot));
}

@Test(expected = TException.class)
@Test
public void testNoSuchService() throws TException {
when(iprot.readMessageBegin()).thenReturn(new TMessage("service:func", TMessageType.CALL, 42));

mp.process(iprot, oprot);
assertThrows(TException.class, ()->mp.process(iprot, oprot));
}

static class StubProcessor implements TProcessor {
Expand Down
13 changes: 11 additions & 2 deletions lib/java/test/org/apache/thrift/TestOptionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@

package org.apache.thrift;

import junit.framework.TestCase;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

// Tests and documents behavior for the "Option<T>" type
public class TestOptionType extends TestCase {
public class TestOptionType {
@Test
public void testSome() {
String name = "Chuck Norris";
Option<String> option = Option.fromNullable(name);
Expand All @@ -34,6 +40,7 @@ public void testSome() {
assertEquals(option.get(),"Chuck Norris");
}

@Test
public void testNone() throws Exception {
String name = null;
Option<String> option = Option.fromNullable(name);
Expand All @@ -53,11 +60,13 @@ public void testNone() throws Exception {
}
}

@Test
public void testMakeSome() throws Exception {
Option<String> some = Option.some("wee");
assertTrue(some instanceof Option.Some);
}

@Test
public void testMakeNone() throws Exception {
Option<Integer> none = Option.none();
assertTrue(none instanceof Option.None);
Expand Down
62 changes: 35 additions & 27 deletions lib/java/test/org/apache/thrift/TestOptionals.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,70 +19,78 @@

package org.apache.thrift;

import junit.framework.TestCase;

import thrift.test.Opt4;
import org.junit.jupiter.api.Test;
import thrift.test.Opt30;
import thrift.test.Opt4;
import thrift.test.Opt64;
import thrift.test.Opt80;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

// Exercises the isSet methods using structs from ManyOptionals.thrift
public class TestOptionals extends TestCase {
public class TestOptionals {
@Test
public void testEncodingUtils() throws Exception {
assertEquals((short)0x8, EncodingUtils.setBit((short)0, 3, true));
assertEquals((short)0, EncodingUtils.setBit((short)0x8, 3, false));
assertEquals(true, EncodingUtils.testBit((short)0x8, 3));
assertEquals(false, EncodingUtils.testBit((short)0x8, 4));
assertTrue(EncodingUtils.testBit((short) 0x8, 3));
assertFalse(EncodingUtils.testBit((short) 0x8, 4));

assertEquals(Short.MIN_VALUE, EncodingUtils.setBit((short)0, 15, true));
assertEquals((short)0, EncodingUtils.setBit(Short.MIN_VALUE, 15, false));
assertEquals(true, EncodingUtils.testBit(Short.MIN_VALUE, 15));
assertEquals(false, EncodingUtils.testBit(Short.MIN_VALUE, 14));
assertTrue(EncodingUtils.testBit(Short.MIN_VALUE, 15));
assertFalse(EncodingUtils.testBit(Short.MIN_VALUE, 14));
}

@Test
public void testOpt4() throws Exception {
Opt4 x = new Opt4();
assertEquals(false, x.isSetDef1());
assertFalse(x.isSetDef1());
x.setDef1(3);
assertEquals(true, x.isSetDef1());
assertEquals(false, x.isSetDef2());
assertTrue(x.isSetDef1());
assertFalse(x.isSetDef2());

Opt4 copy = new Opt4(x);
assertEquals(true, copy.isSetDef1());
assertTrue(copy.isSetDef1());
copy.unsetDef1();
assertEquals(false, copy.isSetDef1());
assertEquals(true, x.isSetDef1());
assertFalse(copy.isSetDef1());
assertTrue(x.isSetDef1());
}

@Test
public void testOpt30() throws Exception {
Opt30 x = new Opt30();
assertEquals(false, x.isSetDef1());
assertFalse(x.isSetDef1());
x.setDef1(3);
assertEquals(true, x.isSetDef1());
assertEquals(false, x.isSetDef2());
assertTrue(x.isSetDef1());
assertFalse(x.isSetDef2());
}

@Test
public void testOpt64() throws Exception {
Opt64 x = new Opt64();
assertEquals(false, x.isSetDef1());
assertFalse(x.isSetDef1());
x.setDef1(3);
assertEquals(true, x.isSetDef1());
assertEquals(false, x.isSetDef2());
assertTrue(x.isSetDef1());
assertFalse(x.isSetDef2());
x.setDef64(22);
assertEquals(true, x.isSetDef64());
assertEquals(false, x.isSetDef63());
assertTrue(x.isSetDef64());
assertFalse(x.isSetDef63());
}

@Test
public void testOpt80() throws Exception {
Opt80 x = new Opt80();
assertEquals(false, x.isSetDef1());
assertFalse(x.isSetDef1());
x.setDef1(3);
assertEquals(true, x.isSetDef1());
assertEquals(false, x.isSetDef2());
assertTrue(x.isSetDef1());
assertFalse(x.isSetDef2());

Opt80 copy = new Opt80(x);
copy.unsetDef1();
assertEquals(false, copy.isSetDef1());
assertEquals(true, x.isSetDef1());
assertFalse(copy.isSetDef1());
assertTrue(x.isSetDef1());
}
}
11 changes: 9 additions & 2 deletions lib/java/test/org/apache/thrift/TestOptionalsWithJdk8.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@

package org.apache.thrift;

import junit.framework.TestCase;
import org.junit.jupiter.api.Test;
import thrift.test.optiontypejdk8.Person;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

// Tests and documents behavior for the JDK8 "Option<T>" type
public class TestOptionalsWithJdk8 extends TestCase {
public class TestOptionalsWithJdk8 {

@Test
public void testConstruction() {
Person person = new Person(1L, "name");
assertFalse(person.getAge().isPresent());
Expand All @@ -42,6 +48,7 @@ public void testConstruction() {
assertEquals(Integer.valueOf(0), person.getPetsSize().orElse(0));
}

@Test
public void testEmpty() {
Person person = new Person();
person.setPhone("phone");
Expand Down
Loading

0 comments on commit 520c43f

Please sign in to comment.