/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

  1. /**
  2. * Copyright (C) 2008 Mathieu Carbou <mathieu.carbou@gmail.com>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mycila.testing.junit;
  17. import com.mycila.plugin.spi.PluginManager;
  18. import com.mycila.testing.JDKLogging;
  19. import com.mycila.testing.core.annot.ConfigureMycilaPlugins;
  20. import com.mycila.testing.core.annot.MycilaPlugins;
  21. import com.mycila.testing.core.api.Cache;
  22. import com.mycila.testing.core.api.TestContext;
  23. import com.mycila.testing.core.api.TestExecution;
  24. import com.mycila.testing.core.plugin.DefaultTestPlugin;
  25. import com.mycila.testing.core.plugin.TestPlugin;
  26. import com.mycila.testing.ea.Code;
  27. import static com.mycila.testing.ea.ExtendedAssert.*;
  28. import java.util.ArrayList;
  29. import static java.util.Arrays.*;
  30. import java.util.List;
  31. /**
  32. * @author Mathieu Carbou (mathieu.carbou@gmail.com)
  33. */
  34. @MycilaPlugins(value = Cache.UNSHARED, descriptor = "")
  35. public final class Junit3FlowTest extends MycilaJunit3Test {
  36. static {
  37. JDKLogging.init();
  38. }
  39. List<String> flow = new ArrayList<String>();
  40. @Override
  41. protected void setUp() throws Exception {
  42. flow.add("setUp");
  43. }
  44. public void test_method1() throws Exception {
  45. flow.add("method1");
  46. assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "method1"), flow);
  47. }
  48. public void test_method2() throws Exception {
  49. flow.add("method2");
  50. assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "method2"), flow);
  51. }
  52. public void test_method3() throws Exception {
  53. flow.add("method3");
  54. fail("SHOULD NOT BE EXECUTED - should be skipped - method3 should not be added in flow");
  55. }
  56. public void test_method4() throws Exception {
  57. flow.add("method4");
  58. assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "method4"), flow);
  59. throw new AssertionError("METHOD 4 ERROR");
  60. }
  61. @Override
  62. protected void tearDown() throws Exception {
  63. flow.add("tearDown");
  64. }
  65. @ConfigureMycilaPlugins
  66. void configure(PluginManager<TestPlugin> pluginManager) {
  67. pluginManager.getCache().registerPlugin("myPlugin", new DefaultTestPlugin() {
  68. public void prepareTestInstance(TestContext context) throws Exception {
  69. flow.add("prepare");
  70. assertEquals(flow.toString(), asList("prepare"), flow);
  71. }
  72. public void beforeTest(TestExecution testExecution) throws Exception {
  73. flow.add("beforeTest");
  74. System.out.println("beforeTest: " + testExecution.method().getName());
  75. assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest"), flow);
  76. if (testExecution.method().getName().equals("test_method3")) {
  77. testExecution.setSkip(true);
  78. }
  79. }
  80. public void afterTest(final TestExecution testExecution) throws Exception {
  81. flow.add("afterTest");
  82. System.out.println("afterTest: " + testExecution.method().getName());
  83. if (testExecution.method().getName().equals("test_method1")) {
  84. assertNull(testExecution.throwable());
  85. assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "method1", "afterTest"), flow);
  86. } else if (testExecution.method().getName().equals("test_method2")) {
  87. assertNull(testExecution.throwable());
  88. assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "method2", "afterTest"), flow);
  89. } else if (testExecution.method().getName().equals("test_method3")) {
  90. assertNull(testExecution.throwable());
  91. assertTrue(testExecution.mustSkip());
  92. assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "afterTest"), flow);
  93. } else if (testExecution.method().getName().equals("test_method4")) {
  94. assertNotNull(testExecution.throwable());
  95. assertThrow(AssertionError.class).withMessage("METHOD 4 ERROR").whenRunning(new Code() {
  96. public void run() throws Throwable {
  97. throw testExecution.throwable();
  98. }
  99. });
  100. assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "method4", "afterTest"), flow);
  101. // Just to pass the test and not rethrow the exception to junit
  102. testExecution.setThrowable(null);
  103. }
  104. }
  105. public void afterClass(TestContext context) throws Exception {
  106. flow.add("afterClass");
  107. //skipped
  108. assertFalse(flow.contains("method3"));
  109. flow.remove("method1");
  110. flow.remove("method2");
  111. flow.remove("method4");
  112. assertEquals(flow.toString(), asList("prepare", "setUp", "beforeTest", "afterTest", "tearDown", "afterClass"), flow);
  113. }
  114. });
  115. }
  116. }