Skip to content

Commit

Permalink
fix(ARQ-2171): class rules are not invoked in container (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatousJobanek authored and bartoszmajsak committed Jan 23, 2018
1 parent ac923bc commit 31fd0ee
Show file tree
Hide file tree
Showing 13 changed files with 553 additions and 5 deletions.
5 changes: 5 additions & 0 deletions junit/container/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jboss.arquillian.junit.container;

import java.util.ArrayList;
import java.util.List;
import org.jboss.arquillian.junit.JUnitClassRulesFilter;
import org.junit.rules.TestRule;

public class ContainerClassRulesFilter implements JUnitClassRulesFilter {

/**
* No @ClassRule should be executed inside of a container since the state between @Test is not kept there.
* This means that particular @ClassRule can be executed multiple times per one test class executed in the container.
*/
public List<TestRule> filter(List<TestRule> scannedRules) {
return new ArrayList<TestRule>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jboss.arquillian.container.test.spi.TestRunner;
import org.jboss.arquillian.container.test.spi.client.deployment.CachedAuxilliaryArchiveAppender;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.JUnitClassRulesFilter;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
Expand Down Expand Up @@ -48,7 +49,10 @@ protected Archive<?> buildArchive() {
.addClass(JUnitRemoteExtension.class)
.addAsServiceProvider(
RemoteLoadableExtension.class,
JUnitRemoteExtension.class);
JUnitRemoteExtension.class)
.addAsServiceProvider(
JUnitClassRulesFilter.class,
ContainerClassRulesFilter.class);
return archive;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.jboss.arquillian.junit.container;

import org.jboss.arquillian.junit.Arquillian;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

import static org.jboss.arquillian.junit.container.JUnitTestBaseClass.Cycle;
import static org.jboss.arquillian.junit.container.JUnitTestBaseClass.wasCalled;

@RunWith(Arquillian.class)
public class ClassWithArquillianRunnerWithRules {

@ClassRule
public static TestRule classRule = new TestRule() {
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
wasCalled(Cycle.BEFORE_CLASS_RULE);
base.evaluate();
wasCalled(Cycle.AFTER_CLASS_RULE);
}
};
}
};

@Rule
public MethodRule rule = new MethodRule() {
@Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
wasCalled(Cycle.BEFORE_RULE);
base.evaluate();
wasCalled(Cycle.AFTER_RULE);
}
};
}
};

@BeforeClass
public static void beforeClass() throws Throwable {
wasCalled(Cycle.BEFORE_CLASS);
}

@AfterClass
public static void afterClass() throws Throwable {
wasCalled(Cycle.AFTER_CLASS);
}

@Before
public void before() throws Throwable {
wasCalled(Cycle.BEFORE);
}

@After
public void after() throws Throwable {
wasCalled(Cycle.AFTER);
}

@Test
public void shouldBeInvoked() throws Throwable {
wasCalled(Cycle.TEST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2009 Red Hat Inc. and/or its affiliates and other contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.arquillian.junit.container;

import org.jboss.arquillian.test.spi.TestRunnerAdaptor;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.Result;

import static org.mockito.Mockito.mock;

public class JUnitIntegrationTestCase extends JUnitTestBaseClass {

@Test
public void should_not_execute_class_rule() throws Exception {
// given
TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class);
executeAllLifeCycles(adaptor);

// when
Result result = run(adaptor, ClassWithArquillianRunnerWithRules.class);

// then
Assert.assertTrue(result.wasSuccessful());
Assert.assertEquals(0, result.getFailureCount());
Assert.assertEquals(0, result.getIgnoreCount());
assertCycle(1, Cycle.BEFORE_RULE, Cycle.BEFORE_CLASS, Cycle.BEFORE, Cycle.TEST, Cycle.AFTER, Cycle.AFTER_CLASS,
Cycle.AFTER_RULE);
assertCycle(0, Cycle.BEFORE_CLASS_RULE, Cycle.AFTER_CLASS_RULE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.arquillian.junit.container;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.jboss.arquillian.junit.event.AfterRules;
import org.jboss.arquillian.junit.event.BeforeRules;
import org.jboss.arquillian.test.spi.LifecycleMethodExecutor;
import org.jboss.arquillian.test.spi.TestMethodExecutor;
import org.jboss.arquillian.test.spi.TestResult;
import org.jboss.arquillian.test.spi.TestRunnerAdaptor;
import org.jboss.arquillian.test.spi.TestRunnerAdaptorBuilder;
import org.jboss.arquillian.test.spi.event.suite.TestLifecycleEvent;
import org.junit.After;
import org.junit.Assert;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.doAnswer;

/**
* JUnitTestBaseClass
*
* @author <a href="mailto:[email protected]">Aslak Knutsen</a>
* @version $Revision: $
*/
public class JUnitTestBaseClass {
/*
* Setup / Clear the static callback info.
*/
private static Map<Cycle, Integer> callbackCount = new HashMap<Cycle, Integer>();
private static Map<Cycle, Throwable> callbackException = new HashMap<Cycle, Throwable>();

static {
for (Cycle tmp : Cycle.values()) {
callbackCount.put(tmp, 0);
}
}

public static void wasCalled(Cycle cycle) throws Throwable {
if (callbackCount.containsKey(cycle)) {
callbackCount.put(cycle, callbackCount.get(cycle) + 1);
} else {
throw new RuntimeException("Unknown callback: " + cycle);
}
if (callbackException.containsKey(cycle)) {
throw callbackException.get(cycle);
}
}

@After
public void clearCallbacks() {
callbackCount.clear();
for (Cycle tmp : Cycle.values()) {
callbackCount.put(tmp, 0);
}
callbackException.clear();
}

/*
* Internal Helpers
*/
protected void executeAllLifeCycles(TestRunnerAdaptor adaptor) throws Exception {
doAnswer(new ExecuteLifecycle()).when(adaptor).fireCustomLifecycle(isA(BeforeRules.class));
doAnswer(new ExecuteLifecycle()).when(adaptor).fireCustomLifecycle(isA(AfterRules.class));
doAnswer(new ExecuteLifecycle()).when(adaptor).beforeClass(any(Class.class), any(LifecycleMethodExecutor.class));
doAnswer(new ExecuteLifecycle()).when(adaptor).afterClass(any(Class.class), any(LifecycleMethodExecutor.class));
doAnswer(new ExecuteLifecycle()).when(adaptor)
.before(any(Object.class), any(Method.class), any(LifecycleMethodExecutor.class));
doAnswer(new ExecuteLifecycle()).when(adaptor)
.after(any(Object.class), any(Method.class), any(LifecycleMethodExecutor.class));
doAnswer(new TestExecuteLifecycle(TestResult.passed())).when(adaptor).test(any(TestMethodExecutor.class));
}

public void assertCycle(int count, Cycle... cycles) {
for (Cycle cycle : cycles) {
Assert.assertEquals("Verify " + cycle + " called N times",
count, (int) callbackCount.get(cycle));
}
}

protected Result run(TestRunnerAdaptor adaptor, Class<?>... classes) throws Exception {
return run(adaptor, null, classes);
}

protected Result run(TestRunnerAdaptor adaptor, RunListener listener, Class<?>... classes)
throws Exception {
try {
setAdaptor(adaptor);
JUnitCore core = new JUnitCore();
if (listener != null) {
core.addListener(listener);
}

return core.run(classes);
} finally {
setAdaptor(null);
}
}

// force set the TestRunnerAdaptor to use
private void setAdaptor(TestRunnerAdaptor adaptor) throws Exception {
Method method = TestRunnerAdaptorBuilder.class.getMethod("set", TestRunnerAdaptor.class);
method.setAccessible(true);
method.invoke(null, adaptor);
}

public enum Cycle

{
BEFORE_CLASS_RULE, BEFORE_RULE, BEFORE_CLASS, BEFORE, TEST, AFTER, AFTER_CLASS, AFTER_RULE, AFTER_CLASS_RULE;
}

/*
* Mockito Answers for invoking the LifeCycle callbacks.
*/
public static class ExecuteLifecycle implements Answer<Object> {
@Override
public Object answer(org.mockito.invocation.InvocationOnMock invocation) throws Throwable {
for (Object argument : invocation.getArguments()) {
if (argument instanceof LifecycleMethodExecutor) {
((LifecycleMethodExecutor) argument).invoke();
} else if (argument instanceof TestMethodExecutor) {
((TestMethodExecutor) argument).invoke();
} else if (argument instanceof TestLifecycleEvent) {
((TestLifecycleEvent) argument).getExecutor().invoke();
}
}
return null;
}
}

public static class TestExecuteLifecycle extends ExecuteLifecycle {
private TestResult result;

public TestExecuteLifecycle(TestResult result) {
this.result = result;
}

@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
super.answer(invocation);
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jboss.arquillian.junit.container.ContainerClassRulesFilter
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.junit.internal.runners.model.MultipleFailureException;
import org.junit.internal.runners.model.ReflectiveCallable;
import org.junit.internal.runners.statements.Fail;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;
Expand Down Expand Up @@ -126,8 +127,25 @@ protected void validatePublicVoidNoArgMethods(Class<? extends Annotation> annota
eachTestMethod.validatePublicVoid(isStatic, errors);
}
}

/*

/**
* Override @ClassRule retrieval to prevent from running them inside of a container
*/
@Override
protected List<TestRule> classRules() {
List<JUnitClassRulesFilter> junitClassRulesFilters =
new JavaSPILoader().all(Arquillian.class.getClassLoader(), JUnitClassRulesFilter.class);
List<TestRule> result = super.classRules();

if (!junitClassRulesFilters.isEmpty()) {
for (final JUnitClassRulesFilter junitClassRulesFilter : junitClassRulesFilters) {
result = junitClassRulesFilter.filter(result);
}
}
return result;
}

/*
* Override BeforeClass/AfterClass and Before/After handling.
*
* Let super create the Before/After chain against a EmptyStatement so our newly created Statement
Expand Down
Loading

0 comments on commit 31fd0ee

Please sign in to comment.