Skip to content

Commit

Permalink
fix: updated implementation to support multiple constructors in steps…
Browse files Browse the repository at this point in the history
… libraries
  • Loading branch information
YamStranger committed Apr 25, 2016
1 parent 3df916a commit 3bbb0c0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.thucydides.core.annotations.Fields;
import net.thucydides.core.guice.Injectors;
import net.thucydides.core.pages.Pages;
import net.thucydides.core.steps.construction.ConstructionStrategy;
import net.thucydides.core.steps.construction.StepLibraryConstructionStrategy;
import net.thucydides.core.steps.construction.StepLibraryType;
import net.thucydides.core.steps.di.DependencyInjectorService;
Expand All @@ -24,6 +25,7 @@

import static com.google.common.collect.ImmutableSet.copyOf;
import static net.thucydides.core.steps.construction.StepLibraryType.ofTypePages;
import static net.thucydides.core.steps.construction.ConstructionStrategy.*;

/**
* Produces an instance of a set of requirement steps for use in the acceptance tests.
Expand Down Expand Up @@ -146,7 +148,7 @@ private <T> T instantiateUniqueStepLibraryFor(Class<T> scenarioStepsClass, Objec
T steps = createProxyStepLibrary(scenarioStepsClass, stepInterceptor, parameters);

instantiateAnyNestedStepLibrariesIn(steps, scenarioStepsClass);

injectOtherDependenciesInto(steps);

return steps;
Expand All @@ -160,11 +162,16 @@ private <T> T createProxyStepLibrary(Class<T> scenarioStepsClass,
e.setSuperclass(scenarioStepsClass);
e.setCallback(interceptor);

switch (StepLibraryConstructionStrategy.forClass(scenarioStepsClass).getStrategy()) {
case STEP_LIBRARY_WITH_WEBDRIVER: return webEnabledStepLibrary(scenarioStepsClass, e);
case STEP_LIBRARY_WITH_PAGES: return stepLibraryWithPages(scenarioStepsClass, e);
case CONSTRUCTOR_WITH_PARAMETERS: return immutableStepLibrary(scenarioStepsClass, e, parameters);
default: return (T) e.create();
final ConstructionStrategy strategy = StepLibraryConstructionStrategy.forClass(scenarioStepsClass)
.getStrategy();
if (STEP_LIBRARY_WITH_WEBDRIVER.equals(strategy)) {
return webEnabledStepLibrary(scenarioStepsClass, e);
} else if (STEP_LIBRARY_WITH_PAGES.equals(strategy)) {
return stepLibraryWithPages(scenarioStepsClass, e);
} else if (CONSTRUCTOR_WITH_PARAMETERS.equals(strategy) && parameters.length > 0) {
return immutableStepLibrary(scenarioStepsClass, e, parameters);
} else{
return (T) e.create();
}
}

Expand All @@ -184,9 +191,13 @@ private Class<?>[] argumentTypesFrom(Class<?> scenarioStepsClass, Object[] param

private boolean parametersMatchFor(Object[] parameters, Class<?>[] parameterTypes) {
int parameterNumber = 0;
for(Class<?> parameterType : parameterTypes) {
if (!parameterType.isAssignableFrom(parameters[parameterNumber++].getClass())) {
return false;
if (parameters.length != parameterTypes.length) {
return false;
} else {
for (Class<?> parameterType : parameterTypes) {
if (!parameterType.isAssignableFrom(parameters[parameterNumber++].getClass())) {
return false;
}
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public ConstructionStrategy getStrategy() {
return ConstructionStrategy.DEFAULT_CONSTRUCTOR;
}

public boolean hasDefaultConstructor(){
return hasAConstructorWithoutParameters(stepLibraryClass);
}


private <T> boolean isWebdriverStepClass(final Class<T> stepLibraryClass) {

Expand All @@ -56,6 +60,12 @@ private <T> boolean hasAConstructorWithParameters(final Class<T> stepLibraryClas

}

private <T> boolean hasAConstructorWithoutParameters(final Class<T> stepLibraryClass) {
ImmutableSet<Constructor<?>> constructors = copyOf(stepLibraryClass.getDeclaredConstructors());
return Iterables.any(constructors, withoutParameters());

}

private <T> boolean hasAPagesField(final Class<T> stepLibraryClass) {
ImmutableSet<Field> fields = copyOf(Fields.of(stepLibraryClass).allFields());
return Iterables.any(fields, ofTypePages());
Expand All @@ -71,6 +81,15 @@ public boolean apply(Constructor<?> constructor) {
};
}

private Predicate<Constructor<?>> withoutParameters() {
return new Predicate<Constructor<?>>() {

public boolean apply(Constructor<?> constructor) {
return ((constructor.getParameterTypes().length == 0));
}
};
}

private Predicate<Constructor<?>> withASinglePagesParameter() {
return new Predicate<Constructor<?>>() {

Expand Down

0 comments on commit 3bbb0c0

Please sign in to comment.