| 1 | |
package org.jbehave.core.steps.needle; |
| 2 | |
|
| 3 | |
import java.lang.annotation.Annotation; |
| 4 | |
import java.lang.reflect.Method; |
| 5 | |
import java.lang.reflect.Type; |
| 6 | |
import java.util.ArrayList; |
| 7 | |
import java.util.LinkedHashMap; |
| 8 | |
import java.util.List; |
| 9 | |
import java.util.Map; |
| 10 | |
import java.util.Set; |
| 11 | |
|
| 12 | |
import org.jbehave.core.annotations.AsParameterConverter; |
| 13 | |
import org.jbehave.core.configuration.Configuration; |
| 14 | |
import org.jbehave.core.configuration.MostUsefulConfiguration; |
| 15 | |
import org.jbehave.core.steps.AbstractStepsFactory; |
| 16 | |
import org.jbehave.core.steps.CandidateSteps; |
| 17 | |
import org.jbehave.core.steps.InjectableStepsFactory; |
| 18 | |
import org.jbehave.core.steps.ParameterConverters.MethodReturningConverter; |
| 19 | |
import org.jbehave.core.steps.ParameterConverters.ParameterConverter; |
| 20 | |
import org.jbehave.core.steps.Steps; |
| 21 | |
import org.jbehave.core.steps.needle.configuration.CollectInjectionProvidersFromStepsInstance; |
| 22 | |
import org.jbehave.core.steps.needle.configuration.CreateInstanceByDefaultConstructor; |
| 23 | |
import org.jbehave.core.steps.needle.configuration.JBehaveNeedleConfiguration; |
| 24 | |
|
| 25 | |
import de.akquinet.jbosscc.needle.NeedleTestcase; |
| 26 | |
import de.akquinet.jbosscc.needle.injection.InjectionProvider; |
| 27 | |
import de.akquinet.jbosscc.needle.reflection.ReflectionUtil; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
public class NeedleStepsFactory extends NeedleTestcase implements InjectableStepsFactory { |
| 36 | |
|
| 37 | 9 | private final Map<Class<?>, Object> cachedTypeInstances = new LinkedHashMap<Class<?>, Object>(); |
| 38 | |
|
| 39 | |
private final Configuration configuration; |
| 40 | |
private Class<?>[] steps; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public NeedleStepsFactory(final Configuration configuration, final Class<?>... steps) { |
| 50 | 2 | this(configuration, null, steps); |
| 51 | 2 | } |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public NeedleStepsFactory(final Configuration configuration, final Set<InjectionProvider<?>> injectionProviders, final Class<?>... steps) { |
| 63 | 9 | super(setUpInjectionProviders(JBehaveNeedleConfiguration.RESOURCE_JBEHAVE_NEEDLE)); |
| 64 | 9 | if (injectionProviders != null) { |
| 65 | 7 | addInjectionProvider(toArray(injectionProviders)); |
| 66 | |
} |
| 67 | 9 | if (this.configuration == null) { |
| 68 | 9 | this.configuration = new MostUsefulConfiguration(); |
| 69 | |
} else { |
| 70 | 0 | this.configuration = configuration; |
| 71 | |
} |
| 72 | 9 | this.steps = steps; |
| 73 | 9 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public List<CandidateSteps> createCandidateSteps() { |
| 79 | 9 | final List<CandidateSteps> result = new ArrayList<CandidateSteps>(); |
| 80 | 16 | for (final Class<?> type : steps) { |
| 81 | 7 | if (hasAnnotatedMethods(type)) { |
| 82 | 7 | configuration.parameterConverters().addConverters(methodReturningConverters(type)); |
| 83 | 7 | result.add(new Steps(configuration, type, this)); |
| 84 | |
} |
| 85 | |
} |
| 86 | 9 | return result; |
| 87 | |
} |
| 88 | |
|
| 89 | |
public Object createInstanceOfType(final Class<?> type) { |
| 90 | 8 | final Object instance = cachedTypeInstances.get(type); |
| 91 | 8 | if (instance == null) { |
| 92 | |
try { |
| 93 | 7 | final Object stepsInstance = createInstanceUsingNeedleTestCase(type); |
| 94 | 7 | final InjectionProvider<?>[] foundProviders = CollectInjectionProvidersFromStepsInstance.INSTANCE.apply(stepsInstance); |
| 95 | |
|
| 96 | 7 | addInjectionProvider(foundProviders); |
| 97 | |
|
| 98 | 7 | initTestcase(stepsInstance); |
| 99 | |
|
| 100 | 7 | cachedTypeInstances.put(type, stepsInstance); |
| 101 | 7 | return stepsInstance; |
| 102 | 0 | } catch (final Exception e) { |
| 103 | 0 | throw new IllegalStateException(e); |
| 104 | |
} |
| 105 | |
} |
| 106 | 1 | return instance; |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
private Object createInstanceUsingNeedleTestCase(final Class<?> type) throws IllegalStateException { |
| 117 | |
try { |
| 118 | 7 | Object instance = ReflectionUtil.invokeMethod(this, "getInstanceByConstructorInjection", type); |
| 119 | 7 | if (instance == null) { |
| 120 | 7 | instance = CreateInstanceByDefaultConstructor.INSTANCE.apply(type); |
| 121 | |
} |
| 122 | 7 | if (instance == null) { |
| 123 | 0 | throw new IllegalStateException("failed to create instance of type " + type.getCanonicalName()); |
| 124 | |
} |
| 125 | 7 | return instance; |
| 126 | 0 | } catch (final Exception e) { |
| 127 | 0 | throw new IllegalStateException(e); |
| 128 | |
} |
| 129 | |
|
| 130 | |
} |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
private List<ParameterConverter> methodReturningConverters(final Class<?> type) { |
| 137 | 7 | final List<ParameterConverter> converters = new ArrayList<ParameterConverter>(); |
| 138 | 80 | for (final Method method : type.getMethods()) { |
| 139 | 73 | if (method.isAnnotationPresent(AsParameterConverter.class)) { |
| 140 | 0 | converters.add(new MethodReturningConverter(method, type, this)); |
| 141 | |
} |
| 142 | |
} |
| 143 | 7 | return converters; |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
public void addInjectionProviders(final Set<InjectionProvider<?>> providers) { |
| 152 | 0 | if (providers != null) { |
| 153 | 0 | addInjectionProvider(toArray(providers)); |
| 154 | |
} |
| 155 | 0 | } |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
static boolean hasAnnotatedMethods(final Type type) { |
| 166 | 7 | if (type instanceof Class<?>) { |
| 167 | 10 | for (final Method method : ((Class<?>)type).getMethods()) { |
| 168 | 10 | for (final Annotation annotation : method.getAnnotations()) { |
| 169 | 7 | if (annotation.annotationType().getName().startsWith("org.jbehave.core.annotations")) { |
| 170 | 7 | return true; |
| 171 | |
} |
| 172 | |
} |
| 173 | |
} |
| 174 | |
} |
| 175 | 0 | return false; |
| 176 | |
} |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
static InjectionProvider<?>[] setUpInjectionProviders(final String resourceName) { |
| 185 | 9 | return new JBehaveNeedleConfiguration(resourceName).getInjectionProviders(); |
| 186 | |
} |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
static InjectionProvider<?>[] toArray(final Set<InjectionProvider<?>> injectionProviders) { |
| 195 | 7 | return injectionProviders.toArray(new InjectionProvider<?>[injectionProviders.size()]); |
| 196 | |
} |
| 197 | |
|
| 198 | |
} |