Skip to content

Commit

Permalink
fix InvocationContextTest and InterceptorBindingInheritanceTest
Browse files Browse the repository at this point in the history
This is to cater for implementations that implement interception by proxying,
in which case the contextual instance of a bean is an interception proxy,
while `InvocationContext.getTarget()` returns the proxied object (an "actual"
instance of the bean).
  • Loading branch information
Ladicek authored and manovotn committed Nov 28, 2023
1 parent 652830b commit d78bfc7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void testGetTargetMethod() {
SimpleBean instance = getContextualReference(SimpleBean.class);
instance.setId(10);
assertEquals(instance.getId(), 10);
assertSame(Interceptor1.getTarget(), instance);
assertEquals(Interceptor1.getTarget().getId(), 10);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,54 @@ public static WebArchive createTestArchive() {
@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER)
@SpecAssertions({ @SpecAssertion(section = TYPE_LEVEL_INHERITANCE, id = "ad"), @SpecAssertion(section = TYPE_LEVEL_INHERITANCE, id = "ada") })
public void testInterceptorBindingDirectlyInheritedFromManagedBean(Larch larch) throws Exception {
Plant.clearInspections();
larch.pong();
assertTrue(Plant.inspectedBy(larch, squirrel));
assertFalse(Plant.inspectedBy(larch, woodpecker));
assertTrue(Plant.inspectedBy(squirrel));
assertFalse(Plant.inspectedBy(woodpecker));
}

@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER)
@SpecAssertions({ @SpecAssertion(section = TYPE_LEVEL_INHERITANCE, id = "aj"), @SpecAssertion(section = TYPE_LEVEL_INHERITANCE, id = "aja") })
public void testInterceptorBindingIndirectlyInheritedFromManagedBean(@European Larch europeanLarch) throws Exception {
Plant.clearInspections();
europeanLarch.pong();
assertTrue(europeanLarch instanceof EuropeanLarch);
assertTrue(Plant.inspectedBy(europeanLarch, squirrel));
assertFalse(Plant.inspectedBy(europeanLarch, woodpecker));
assertTrue(Plant.inspectedBy(squirrel));
assertFalse(Plant.inspectedBy(woodpecker));
}

@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER)
@SpecAssertion(section = MEMBER_LEVEL_INHERITANCE, id = "ka")
public void testMethodInterceptorBindingDirectlyInheritedFromManagedBean(Herb herb) {
Plant.clearInspections();
herb.pong();
assertTrue(Plant.inspectedBy(herb, squirrel));
assertTrue(Plant.inspectedBy(squirrel));
}

@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER)
@SpecAssertion(section = MEMBER_LEVEL_INHERITANCE, id = "kc")
public void testMethodInterceptorBindingIndirectlyInheritedFromManagedBean(@Culinary Herb thyme) {
Plant.clearInspections();
thyme.pong();
assertTrue(thyme instanceof Thyme);
assertTrue(Plant.inspectedBy(thyme, squirrel));
assertTrue(Plant.inspectedBy(squirrel));
}

@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER)
@SpecAssertion(section = MEMBER_LEVEL_INHERITANCE, id = "ka")
public void testMethodInterceptorBindingDirectlyNotInheritedFromManagedBean(Shrub shrub) {
Plant.clearInspections();
shrub.pong();
assertFalse(Plant.inspectedBy(shrub, squirrel));
assertFalse(Plant.inspectedBy(squirrel));
}

@Test(dataProvider = ARQUILLIAN_DATA_PROVIDER)
@SpecAssertion(section = MEMBER_LEVEL_INHERITANCE, id = "kc")
public void testMethodInterceptorBindingIndirectlyNotInheritedFromManagedBean(@Culinary Shrub rosehip) {
Plant.clearInspections();
rosehip.pong();
assertTrue(rosehip instanceof Rosehip);
assertFalse(Plant.inspectedBy(rosehip, squirrel));
assertFalse(Plant.inspectedBy(squirrel));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@

public abstract class Plant implements Ping {

private List<String> inspections = new ArrayList<String>();
private static List<String> inspections = new ArrayList<String>();

// all beans of type `Plant` are `@Dependent`, so accessing the field is OK
public static void clearInspections() {
inspections.clear();
}

public static void inspect(Plant plant, String id) {
plant.inspections.add(id);
public static void inspect(String id) {
inspections.add(id);
}

public static boolean inspectedBy(Plant plant, String id) {
return plant.inspections.contains(id);
public static boolean inspectedBy(String id) {
return inspections.contains(id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Object intercept(InvocationContext ctx) throws Exception {
Object target = ctx.getTarget();

if (target instanceof Plant) {
Plant.inspect((Plant) target, SquirrelInterceptor.class.getName());
Plant.inspect(SquirrelInterceptor.class.getName());
}
return ctx.proceed();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Object intercept(InvocationContext ctx) throws Exception {
Object target = ctx.getTarget();

if (target instanceof Plant) {
Plant.inspect((Plant) target, WoodpeckerInterceptor.class.getName());
Plant.inspect(WoodpeckerInterceptor.class.getName());
}
return ctx.proceed();
}
Expand Down

0 comments on commit d78bfc7

Please sign in to comment.