/mycila-testing/tags/mycila-testing-2.9.ga/mycila-testing-api/src/main/java/com/mycila/testing/junit/MycilaJunit3Test.java

http://mycila.googlecode.com/ · Java · 92 lines · 65 code · 9 blank · 18 comment · 3 complexity · 83b5507e273d3b02ff240867078bf74e 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.log.Logger;
  18. import com.mycila.log.Loggers;
  19. import com.mycila.testing.core.Mycila;
  20. import com.mycila.testing.core.MycilaTesting;
  21. import com.mycila.testing.core.api.TestExecution;
  22. import com.mycila.testing.core.api.TestNotifier;
  23. import com.mycila.testing.core.util.Closeable;
  24. import com.mycila.testing.core.util.ShutdownHook;
  25. import junit.framework.TestCase;
  26. import java.lang.reflect.Method;
  27. import java.lang.reflect.Modifier;
  28. /**
  29. * @author Mathieu Carbou (mathieu.carbou@gmail.com)
  30. */
  31. public abstract class MycilaJunit3Test extends TestCase {
  32. private static final Logger LOGGER = Loggers.get(MycilaJunit3Test.class);
  33. public MycilaJunit3Test() {
  34. super();
  35. }
  36. public MycilaJunit3Test(String name) {
  37. super(name);
  38. }
  39. @Override
  40. public final void runBare() throws Throwable {
  41. final TestNotifier testNotifier = MycilaTesting.from(getClass()).configure(this).createNotifier(this);
  42. ShutdownHook.get().add(new Closeable() {
  43. public void close() throws Exception {
  44. testNotifier.shutdown();
  45. }
  46. });
  47. testNotifier.prepare();
  48. try {
  49. setUp();
  50. testNotifier.fireBeforeTest(getTestMethod());
  51. TestExecution testExecution = (TestExecution) Mycila.currentExecution();
  52. if (!testExecution.mustSkip()) {
  53. try {
  54. LOGGER.debug("Calling test method %s.%s", testExecution.method().getDeclaringClass().getName(), testExecution.method().getName());
  55. super.runTest();
  56. } catch (Throwable t) {
  57. testExecution.setThrowable(t);
  58. }
  59. }
  60. testNotifier.fireAfterTest();
  61. tearDown();
  62. if (testExecution.hasFailed()) {
  63. throw testExecution.throwable().fillInStackTrace();
  64. }
  65. } finally {
  66. testNotifier.fireAfterClass();
  67. }
  68. }
  69. private Method getTestMethod() {
  70. assertNotNull("TestCase.getName() cannot be null", getName());
  71. Method testMethod = null;
  72. try {
  73. testMethod = getClass().getMethod(getName());
  74. }
  75. catch (NoSuchMethodException ex) {
  76. fail("Method \"" + getName() + "\" not found");
  77. }
  78. if (!Modifier.isPublic(testMethod.getModifiers())) {
  79. fail("Method \"" + getName() + "\" should be public");
  80. }
  81. return testMethod;
  82. }
  83. }