-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ARQ-2171): class rules are not invoked in container (#156)
- Loading branch information
1 parent
ac923bc
commit 31fd0ee
Showing
13 changed files
with
553 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...ntainer/src/main/java/org/jboss/arquillian/junit/container/ContainerClassRulesFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...rc/test/java/org/jboss/arquillian/junit/container/ClassWithArquillianRunnerWithRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ontainer/src/test/java/org/jboss/arquillian/junit/container/JUnitIntegrationTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
168 changes: 168 additions & 0 deletions
168
junit/container/src/test/java/org/jboss/arquillian/junit/container/JUnitTestBaseClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ner/src/test/resources/META-INF/services/org.jboss.arquillian.junit.JUnitClassRulesFilter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.jboss.arquillian.junit.container.ContainerClassRulesFilter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.