| 1 | /* |
| 2 | * JTiger Unit Testing Framework for J2SE 1.5 |
| 3 | * Copyright (C) 2005 Tony Morris |
| 4 | * |
| 5 | * This software is licenced under the |
| 6 | * Common Public Licence version 1.0 |
| 7 | * http://www.opensource.org/licenses/cpl1.0.php |
| 8 | * |
| 9 | * You received a copy of this licence with this software. |
| 10 | */ |
| 11 | package org.jtiger.framework; |
| 12 | |
| 13 | import static org.jtiger.framework.SequenceFactory.newSequence; |
| 14 | import java.lang.reflect.InvocationTargetException; |
| 15 | import java.lang.reflect.Method; |
| 16 | import java.util.LinkedList; |
| 17 | import java.util.List; |
| 18 | import static org.jtiger.framework.TestFailureFactory.newTestFailure; |
| 19 | import static org.jtiger.framework.FixtureResultFactory.newFixtureResult; |
| 20 | import static org.jtiger.framework.MethodCategoryFactory.newMethodCategory; |
| 21 | import static org.jtiger.framework.TestRunnerFactory.newTestRunner; |
| 22 | import static org.jtiger.framework.ClassConstructionFactory.newClassConstruction; |
| 23 | import static org.jtiger.framework.DefaultSetUpTearDownFactory.newDefaultSetUpTearDown; |
| 24 | import static org.jtiger.framework.DefaultTestDefinitionFactory.newDefaultTestDefinition; |
| 25 | |
| 26 | final class FixtureRunnerFactory |
| 27 | { |
| 28 | private FixtureRunnerFactory() |
| 29 | { |
| 30 | |
| 31 | } |
| 32 | |
| 33 | static FixtureRunner newFixtureRunner() |
| 34 | { |
| 35 | return new FixtureRunnerImpl(); |
| 36 | } |
| 37 | |
| 38 | private static final class FixtureRunnerImpl implements FixtureRunner |
| 39 | { |
| 40 | FixtureRunnerImpl() |
| 41 | { |
| 42 | |
| 43 | } |
| 44 | |
| 45 | public FixtureResult run(Class<?> fixtureClass, |
| 46 | final Class<? extends TestDefinition> definitionClass, |
| 47 | final Class<? extends SetUpTearDown> sutdClass, |
| 48 | final boolean haltOnFailure, |
| 49 | ReadOnlyArray<String> categories) |
| 50 | throws RunnerException |
| 51 | { |
| 52 | if(fixtureClass == null) |
| 53 | { |
| 54 | return null; |
| 55 | } |
| 56 | |
| 57 | try |
| 58 | { |
| 59 | TestDefinition definition = newClassConstruction().construct(definitionClass); |
| 60 | SetUpTearDown sutd = newClassConstruction().construct(sutdClass); |
| 61 | |
| 62 | if(definition == null) |
| 63 | { |
| 64 | definition = newDefaultTestDefinition(); |
| 65 | } |
| 66 | |
| 67 | if(sutd == null) |
| 68 | { |
| 69 | sutd = newDefaultSetUpTearDown(); |
| 70 | } |
| 71 | |
| 72 | if(categories == null) |
| 73 | { |
| 74 | categories = newSequence(new String[0]); |
| 75 | } |
| 76 | |
| 77 | final List<TestResult> results = new LinkedList<TestResult>(); |
| 78 | final TestRunner runner = newTestRunner(); |
| 79 | final MethodCategory mc = newMethodCategory(); |
| 80 | |
| 81 | while(fixtureClass != null) |
| 82 | { |
| 83 | final Method[] methods = fixtureClass.getDeclaredMethods(); |
| 84 | |
| 85 | for(Method m : methods) |
| 86 | { |
| 87 | if(definition.isTest(m) && |
| 88 | mc.isInAnyCategories(m, categories)) |
| 89 | { |
| 90 | final Repeat r = m.getAnnotation(Repeat.class); |
| 91 | |
| 92 | final long repeat = (r == null || r.value() < 0 ? 1L : r.value()); |
| 93 | |
| 94 | for(long count = 0; count < repeat; count++) |
| 95 | { |
| 96 | final Object fixture = newClassConstruction().construct(fixtureClass); |
| 97 | |
| 98 | final TestResult result = runner.run(m, fixture, sutd); |
| 99 | |
| 100 | results.add(result); |
| 101 | |
| 102 | final TestFailure tf = newTestFailure(); |
| 103 | |
| 104 | if(haltOnFailure && tf.isFailure(result.getTestResultType())) |
| 105 | { |
| 106 | return newFixtureResult(results); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | fixtureClass = fixtureClass.getSuperclass(); |
| 113 | } |
| 114 | |
| 115 | return newFixtureResult(results); |
| 116 | } |
| 117 | catch(InstantiationException e) |
| 118 | { |
| 119 | throw new RunnerException(e.getMessage(), e); |
| 120 | } |
| 121 | catch(IllegalAccessException e) |
| 122 | { |
| 123 | throw new RunnerException(e.getMessage(), e); |
| 124 | } |
| 125 | catch(InvocationTargetException e) |
| 126 | { |
| 127 | throw new RunnerException(e.getMessage(), e); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |