Skip to content

Commit

Permalink
Migrate some unit tests that are using jMock to JUnit Jupiter
Browse files Browse the repository at this point in the history
  • Loading branch information
jeantessier committed Jan 10, 2025
1 parent 4828ddf commit 5ba9d4e
Show file tree
Hide file tree
Showing 19 changed files with 758 additions and 913 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,67 +34,52 @@

import org.jmock.*;
import org.jmock.imposters.*;
import org.jmock.integration.junit4.*;
import org.junit.*;
import org.junit.runner.*;
import org.junit.runners.*;
import org.jmock.junit5.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.params.*;
import org.junit.jupiter.params.provider.*;

import java.io.*;
import java.util.stream.*;

import static org.junit.Assert.*;
import static org.junit.runners.Parameterized.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.params.provider.Arguments.*;

@RunWith(Parameterized.class)
public class TestBootstrapMethodWithOneArgument {
@Parameters(name="BootstrapMethod with {0} argument")
public static Object[][] data() {
return new Object[][] {
{"an integer", 123, 1, com.jeantessier.classreader.Integer_info.class},
{"a float", 234, 2, com.jeantessier.classreader.Float_info.class},
{"a long", 345, 3, com.jeantessier.classreader.Long_info.class},
{"a double", 456, 4, com.jeantessier.classreader.Double_info.class},
{"a Class", 567, 5, com.jeantessier.classreader.Class_info.class},
{"a String", 678, 6, com.jeantessier.classreader.String_info.class},
{"a MethodHandle", 789, 7, com.jeantessier.classreader.MethodHandle_info.class},
{"a MethodType", 890, 8, com.jeantessier.classreader.MethodType_info.class},
{"a Dynamic", 909, 9, com.jeantessier.classreader.Dynamic_info.class},
};
static Stream<Arguments> dataProvider() {
return Stream.of(
arguments("an integer", 123, 1, com.jeantessier.classreader.Integer_info.class),
arguments("a float", 234, 2, com.jeantessier.classreader.Float_info.class),
arguments("a long", 345, 3, com.jeantessier.classreader.Long_info.class),
arguments("a double", 456, 4, com.jeantessier.classreader.Double_info.class),
arguments("a Class", 567, 5, com.jeantessier.classreader.Class_info.class),
arguments("a String", 678, 6, com.jeantessier.classreader.String_info.class),
arguments("a MethodHandle", 789, 7, com.jeantessier.classreader.MethodHandle_info.class),
arguments("a MethodType", 890, 8, com.jeantessier.classreader.MethodType_info.class),
arguments("a Dynamic", 909, 9, com.jeantessier.classreader.Dynamic_info.class)
);
}

@Parameter(0)
public String label;

@Parameter(1)
public int bootstrapMethodRef;

@Parameter(2)
public int argumentIndex;

@Parameter(3)
public Class<? extends ConstantPoolEntry> argumentClass;

@Rule
public JUnitRuleMockery context = new JUnitRuleMockery() {{
@RegisterExtension
JUnit5Mockery context = new JUnit5Mockery() {{
setImposteriser(ByteBuddyClassImposteriser.INSTANCE);
}};

private ConstantPool mockConstantPool;
private DataInput mockIn;
private com.jeantessier.classreader.ConstantPoolEntry mockArgument;

private BootstrapMethods_attribute mockBootstrapMethods;

private BootstrapMethod sut;

@Before
public void setUp() throws IOException {
mockConstantPool = context.mock(ConstantPool.class);
mockIn = context.mock(DataInput.class);
mockArgument = context.mock(argumentClass);
@DisplayName("getArguments")
@ParameterizedTest(name="with {0} argument")
@MethodSource("dataProvider")
void testGetArguments(String variation, int bootstrapMethodRef, int argumentIndex, Class<? extends ConstantPoolEntry> argumentClass) throws Exception {
// Given
var mockConstantPool = context.mock(ConstantPool.class);
var mockIn = context.mock(DataInput.class);
com.jeantessier.classreader.ConstantPoolEntry mockArgument = context.mock(argumentClass);

// and
var mockBootstrapMethods = context.mock(BootstrapMethods_attribute.class);
var mockBootstrapMethod = context.mock(MethodHandle_info.class, "bootstrap method");

// and
context.checking(new Expectations() {{
allowing (mockBootstrapMethods).getConstantPool();
will(returnValue(mockConstantPool));
Expand All @@ -113,12 +98,11 @@ public void setUp() throws IOException {
will(returnValue(mockArgument));
}});

sut = new BootstrapMethod(mockBootstrapMethods, mockIn);
}
// When
var sut = new BootstrapMethod(mockBootstrapMethods, mockIn);

@Test
public void testGetArguments() {
assertEquals("num arguments", 1, sut.getArguments().size());
assertSame("argument", mockArgument, sut.getArguments().stream().findFirst().orElseThrow());
// Then
assertEquals(1, sut.getArguments().size(),"num arguments");
assertSame(mockArgument, sut.getArguments().stream().findFirst().orElseThrow(), "argument");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class TestClass_info {
private static final String FULL_CLASS_NAME = PACKAGE_NAME + "." + SIMPLE_CLASS_NAME;

@RegisterExtension
public JUnit5Mockery context = new JUnit5Mockery() {{
JUnit5Mockery context = new JUnit5Mockery() {{
setImposteriser(ByteBuddyClassImposteriser.INSTANCE);
}};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class TestClassfile {
private static final String TEST_METHOD_SIGNATURE = TEST_CLASS_NAME + ".foo()";

@RegisterExtension
public JUnit5Mockery context = new JUnit5Mockery() {{
JUnit5Mockery context = new JUnit5Mockery() {{
setImposteriser(ByteBuddyClassImposteriser.INSTANCE);
}};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

public class TestClassfile_locateMethodDeclaration {
@RegisterExtension
public JUnit5Mockery context = new JUnit5Mockery() {{
JUnit5Mockery context = new JUnit5Mockery() {{
setImposteriser(ByteBuddyClassImposteriser.INSTANCE);
}};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,127 +34,104 @@

import org.jmock.*;
import org.jmock.imposters.*;
import org.jmock.integration.junit4.*;
import org.junit.*;
import org.junit.runner.*;
import org.junit.runners.*;
import org.jmock.junit5.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.params.*;
import org.junit.jupiter.params.provider.*;

import java.io.*;
import java.util.*;
import java.util.stream.*;

import static org.junit.Assert.assertEquals;
import static org.junit.runners.Parameterized.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.params.provider.Arguments.*;

@RunWith(Parameterized.class)
public class TestFrameType_create {
@Parameters(name="Stack Map Frame from tag {0}")
public static Object[][] data() {
return new Object[][] {
{"SAME (min)", 0, null, null, Collections.emptyList(), null, Collections.emptyList(), SameFrame.class},
{"SAME (max)", 63, null, null, Collections.emptyList(), null, Collections.emptyList(), SameFrame.class},
{"SAME_LOCALS_1_STACK_ITEM (min)", 64, null, null, Collections.emptyList(), null, Collections.singleton("TOP"), SameLocals1StackItemFrame.class},
{"SAME_LOCALS_1_STACK_ITEM (max)", 127, null, null, Collections.emptyList(), null, Collections.singleton("TOP"), SameLocals1StackItemFrame.class},
{"SAME_LOCALS_1_STACK_ITEM_EXTENDED", 247, 456, null, Collections.emptyList(), null, Collections.singleton("TOP"), SameLocals1StackItemFrameExtended.class},
{"CHOP (min)", 248, 456, null, Collections.emptyList(), null, Collections.emptyList(), ChopFrame.class},
{"CHOP (min)", 250, 456, null, Collections.emptyList(), null, Collections.emptyList(), ChopFrame.class},
{"SAME_FRAME_EXTENDED", 251, 456, null, Collections.emptyList(), null, Collections.emptyList(), SameFrameExtended.class},
{"APPEND (252 - 251 = 1)", 252, 456, null, List.of("TOP1"), null, Collections.emptyList(), AppendFrame.class},
{"APPEND (253 - 251 = 2)", 253, 456, null, List.of("TOP1", "TOP2"), null, Collections.emptyList(), AppendFrame.class},
{"APPEND (253 - 251 = 3)", 254, 456, null, List.of("TOP1", "TOP2", "TOP3"), null, Collections.emptyList(), AppendFrame.class},
{"FULL_FRAME (empties)", 255, 456, 0, Collections.emptyList(), 0, Collections.emptyList(), FullFrame.class},
{"FULL_FRAME (locals only)", 255, 456, 2, List.of("TOP1", "TOP2"), 0, Collections.emptyList(), FullFrame.class},
{"FULL_FRAME (stack only)", 255, 456, 0, Collections.emptyList(), 3, List.of("TOP3", "TOP4", "TOP5"), FullFrame.class},
{"FULL_FRAME (locals and stack)", 255, 456, 2, List.of("TOP1", "TOP2"), 3, List.of("TOP3", "TOP4", "TOP5"), FullFrame.class},
};
static Stream<Arguments> dataProvider() {
return Stream.of(
arguments("SAME (min)", 0, null, null, Collections.emptyList(), null, Collections.emptyList(), SameFrame.class),
arguments("SAME (max)", 63, null, null, Collections.emptyList(), null, Collections.emptyList(), SameFrame.class),
arguments("SAME_LOCALS_1_STACK_ITEM (min)", 64, null, null, Collections.emptyList(), null, Collections.singleton("TOP"), SameLocals1StackItemFrame.class),
arguments("SAME_LOCALS_1_STACK_ITEM (max)", 127, null, null, Collections.emptyList(), null, Collections.singleton("TOP"), SameLocals1StackItemFrame.class),
arguments("SAME_LOCALS_1_STACK_ITEM_EXTENDED", 247, 456, null, Collections.emptyList(), null, Collections.singleton("TOP"), SameLocals1StackItemFrameExtended.class),
arguments("CHOP (min)", 248, 456, null, Collections.emptyList(), null, Collections.emptyList(), ChopFrame.class),
arguments("CHOP (min)", 250, 456, null, Collections.emptyList(), null, Collections.emptyList(), ChopFrame.class),
arguments("SAME_FRAME_EXTENDED", 251, 456, null, Collections.emptyList(), null, Collections.emptyList(), SameFrameExtended.class),
arguments("APPEND (252 - 251 = 1)", 252, 456, null, List.of("TOP1"), null, Collections.emptyList(), AppendFrame.class),
arguments("APPEND (253 - 251 = 2)", 253, 456, null, List.of("TOP1", "TOP2"), null, Collections.emptyList(), AppendFrame.class),
arguments("APPEND (253 - 251 = 3)", 254, 456, null, List.of("TOP1", "TOP2", "TOP3"), null, Collections.emptyList(), AppendFrame.class),
arguments("FULL_FRAME (empties)", 255, 456, 0, Collections.emptyList(), 0, Collections.emptyList(), FullFrame.class),
arguments("FULL_FRAME (locals only)", 255, 456, 2, List.of("TOP1", "TOP2"), 0, Collections.emptyList(), FullFrame.class),
arguments("FULL_FRAME (stack only)", 255, 456, 0, Collections.emptyList(), 3, List.of("TOP3", "TOP4", "TOP5"), FullFrame.class),
arguments("FULL_FRAME (locals and stack)", 255, 456, 2, List.of("TOP1", "TOP2"), 3, List.of("TOP3", "TOP4", "TOP5"), FullFrame.class)
);
}

@Parameter(0)
public String label;

@Parameter(1)
public int frameType;

@Parameter(2)
public Integer offsetDelta;

@Parameter(3)
public Integer numberOfLocals;

@Parameter(4)
public Collection<String> locals;

@Parameter(5)
public Integer numberOfStackItems;

@Parameter(6)
public Collection<String> stacks;

@Parameter(7)
public Class<? extends StackMapFrame> expectedClass;

@Rule
public JUnitRuleMockery context = new JUnitRuleMockery() {{
@RegisterExtension
JUnit5Mockery context = new JUnit5Mockery() {{
setImposteriser(ByteBuddyClassImposteriser.INSTANCE);
}};

private VerificationTypeInfoFactory mockVerificationTypeInfoFactory;
private ConstantPool mockConstantPool;
private DataInput mockIn;

private FrameType sut;

@Before
public void setUp() throws IOException {
mockVerificationTypeInfoFactory = context.mock(VerificationTypeInfoFactory.class);
mockConstantPool = context.mock(ConstantPool.class);
mockIn = context.mock(DataInput.class);
@DisplayName("create")
@ParameterizedTest(name="from tag {0}")
@MethodSource("dataProvider")
public void testCreate(String variation, int frameType, Integer offsetDelta, Integer numberOfLocals, Collection<String> locals, Integer numberOfStackItems, Collection<String> stacks, Class<? extends StackMapFrame> expectedClass) throws IOException {
// Given
var mockVerificationTypeInfoFactory = context.mock(VerificationTypeInfoFactory.class);
var mockConstantPool = context.mock(ConstantPool.class);
var mockIn = context.mock(DataInput.class);

// and
if (offsetDelta != null) {
context.checking(new Expectations() {{
oneOf (mockIn).readUnsignedShort();
will(returnValue(offsetDelta));
}});
}

// and
if (numberOfLocals != null) {
context.checking(new Expectations() {{
oneOf (mockIn).readUnsignedShort();
will(returnValue(numberOfLocals));
}});
}

// and
for (String localName : locals) {
final VerificationTypeInfo mockVerificationTypeInfo = context.mock(VerificationTypeInfo.class, localName);
var mockVerificationTypeInfo = context.mock(VerificationTypeInfo.class, localName);
context.checking(new Expectations() {{
oneOf (mockVerificationTypeInfoFactory).create(mockConstantPool, mockIn);
will(returnValue(mockVerificationTypeInfo));
}});
}

// and
if (numberOfStackItems != null) {
context.checking(new Expectations() {{
oneOf (mockIn).readUnsignedShort();
will(returnValue(numberOfStackItems));
}});
}

// and
for (String stackName : stacks) {
final VerificationTypeInfo mockVerificationTypeInfo = context.mock(VerificationTypeInfo.class, stackName);
var mockVerificationTypeInfo = context.mock(VerificationTypeInfo.class, stackName);
context.checking(new Expectations() {{
oneOf (mockVerificationTypeInfoFactory).create(mockConstantPool, mockIn);
will(returnValue(mockVerificationTypeInfo));
}});
}

// And
sut = FrameType.forTag(frameType);
}
// and
var sut = FrameType.forTag(frameType);

@Test
public void testCreate() throws IOException {
// When
var actualStackMapFrame = sut.create(frameType, mockVerificationTypeInfoFactory, mockConstantPool, mockIn);

assertEquals(label, expectedClass, actualStackMapFrame.getClass());
assertEquals(label, frameType, actualStackMapFrame.getFrameType());
// Then
assertEquals(expectedClass, actualStackMapFrame.getClass(), variation);
assertEquals(frameType, actualStackMapFrame.getFrameType(), variation);
}
}
Loading

0 comments on commit 5ba9d4e

Please sign in to comment.