Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix abstractExtends in InterfacePlugin does not support interface #1144

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public <T> InterfacePlugin abstractClassExtends(
Class<T> abstractClassType,
List<Class<? extends T>> implementations
) {
if (!Modifier.isAbstract(abstractClassType.getModifiers())) {
if (!(Modifier.isAbstract(abstractClassType.getModifiers())
&& !Modifier.isInterface(abstractClassType.getModifiers()))) {
throw new IllegalArgumentException(
"abstractClassExtends option first parameter should be abstract class. "
+ abstractClassType.getTypeName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.List;

import lombok.EqualsAndHashCode;
import lombok.Value;

class InterfaceTestSpecs {
Expand Down Expand Up @@ -61,4 +62,14 @@ public List<InterfaceObject> getObject() {
public static class InterfaceWrapperObject {
InterfaceObject value;
}

abstract static class AbstractClassObject {
abstract Object getObject();
}

@EqualsAndHashCode(callSuper = true)
@Value
public static class AbstractClassStringChildObject extends AbstractClassObject {
String object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -92,6 +93,8 @@
import com.navercorp.fixturemonkey.tests.java.ImmutableRecursiveTypeSpecs.SelfRecursiveListObject;
import com.navercorp.fixturemonkey.tests.java.ImmutableRecursiveTypeSpecs.SelfRecursiveMapObject;
import com.navercorp.fixturemonkey.tests.java.ImmutableRecursiveTypeSpecs.SelfRecursiveObject;
import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.AbstractClassObject;
import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.AbstractClassStringChildObject;
import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceIntegerObject;
import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceListObject;
import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceObject;
Expand Down Expand Up @@ -1410,4 +1413,53 @@ void constructorValidator() {

then(actual.getValue()).isEqualTo(100);
}

@RepeatedTest(TEST_COUNT)
void abstractClassExtends() {
FixtureMonkey sut = FixtureMonkey.builder()
.plugin(
new InterfacePlugin()
.abstractClassExtends(
AbstractClassObject.class,
Collections.singletonList(AbstractClassStringChildObject.class))
)
.objectIntrospector(ConstructorPropertiesArbitraryIntrospector.INSTANCE)
.build();

AbstractClassObject actual = sut.giveMeOne(AbstractClassObject.class);

then(actual).isExactlyInstanceOf(AbstractClassStringChildObject.class);
}

@Test
void abstractExtendsInterfaceThrows() {
thenThrownBy(
() -> FixtureMonkey.builder()
.plugin(
new InterfacePlugin()
.abstractClassExtends(
InterfaceObject.class,
Collections.singletonList(InterfaceStringObject.class))
)
.build()
)
.isExactlyInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("should be abstract class");
}

@Test
void interfaceImplementsAbstractClassThrows() {
thenThrownBy(
() -> FixtureMonkey.builder()
.plugin(
new InterfacePlugin()
.interfaceImplements(
AbstractClassObject.class,
Collections.singletonList(AbstractClassStringChildObject.class))
)
.build()
)
.isExactlyInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("should be interface");
}
}
Loading