/mycila-testing/tags/mycila-testing-2.6/mycila-testing-api/src/test/java/com/mycila/testing/junit/Junit3FlowTest.java
http://mycila.googlecode.com/ · Java · 133 lines · 96 code · 17 blank · 20 comment · 8 complexity · 727ff1134cbbc8b3814f3fd8df38848a MD5 · raw file
- /**
- * Copyright (C) 2008 Mathieu Carbou <mathieu.carbou@gmail.com>
- *
- * 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 com.mycila.testing.junit;
- import com.mycila.plugin.spi.PluginManager;
- import com.mycila.testing.JDKLogging;
- import com.mycila.testing.core.annot.ConfigureMycilaPlugins;
- import com.mycila.testing.core.annot.MycilaPlugins;
- import com.mycila.testing.core.api.Cache;
- import com.mycila.testing.core.api.TestContext;
- import com.mycila.testing.core.api.TestExecution;
- import com.mycila.testing.core.plugin.DefaultTestPlugin;
- import com.mycila.testing.core.plugin.TestPlugin;
- import com.mycila.testing.ea.Code;
- import static com.mycila.testing.ea.ExtendedAssert.*;
- import java.util.ArrayList;
- import static java.util.Arrays.*;
- import java.util.List;
- /**
- * @author Mathieu Carbou (mathieu.carbou@gmail.com)
- */
- @MycilaPlugins(value = Cache.UNSHARED, descriptor = "")
- public final class Junit3FlowTest extends MycilaJunit3Test {
- static {
- JDKLogging.init();
- }
- List<String> flow = new ArrayList<String>();
- @Override
- protected void setUp() throws Exception {
- flow.add("setUp");
- }
- public void test_method1() throws Exception {
- flow.add("method1");
- assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "method1"), flow);
- }
- public void test_method2() throws Exception {
- flow.add("method2");
- assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "method2"), flow);
- }
- public void test_method3() throws Exception {
- flow.add("method3");
- fail("SHOULD NOT BE EXECUTED - should be skipped - method3 should not be added in flow");
- }
- public void test_method4() throws Exception {
- flow.add("method4");
- assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "method4"), flow);
- throw new AssertionError("METHOD 4 ERROR");
- }
- @Override
- protected void tearDown() throws Exception {
- flow.add("tearDown");
- }
- @ConfigureMycilaPlugins
- void configure(PluginManager<TestPlugin> pluginManager) {
- pluginManager.getCache().registerPlugin("myPlugin", new DefaultTestPlugin() {
- public void prepareTestInstance(TestContext context) throws Exception {
- flow.add("prepare");
- assertEquals(flow.toString(), asList("prepare"), flow);
- }
- public void beforeTest(TestExecution testExecution) throws Exception {
- flow.add("beforeTest");
- System.out.println("beforeTest: " + testExecution.method().getName());
- assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest"), flow);
- if (testExecution.method().getName().equals("test_method3")) {
- testExecution.setSkip(true);
- }
- }
- public void afterTest(final TestExecution testExecution) throws Exception {
- flow.add("afterTest");
- System.out.println("afterTest: " + testExecution.method().getName());
- if (testExecution.method().getName().equals("test_method1")) {
- assertNull(testExecution.throwable());
- assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "method1", "afterTest"), flow);
- } else if (testExecution.method().getName().equals("test_method2")) {
- assertNull(testExecution.throwable());
- assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "method2", "afterTest"), flow);
- } else if (testExecution.method().getName().equals("test_method3")) {
- assertNull(testExecution.throwable());
- assertTrue(testExecution.mustSkip());
- assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "afterTest"), flow);
- } else if (testExecution.method().getName().equals("test_method4")) {
- assertNotNull(testExecution.throwable());
- assertThrow(AssertionError.class).withMessage("METHOD 4 ERROR").whenRunning(new Code() {
- public void run() throws Throwable {
- throw testExecution.throwable();
- }
- });
- assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "method4", "afterTest"), flow);
- // Just to pass the test and not rethrow the exception to junit
- testExecution.setThrowable(null);
- }
- }
- public void afterClass(TestContext context) throws Exception {
- flow.add("afterClass");
- //skipped
- assertFalse(flow.contains("method3"));
- flow.remove("method1");
- flow.remove("method2");
- flow.remove("method4");
- assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "afterTest", "tearDown", "afterClass"), flow);
- }
- });
- }
- }