Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log a warning when more than one IoC dependency is found in the classpath #594

Merged
merged 3 commits into from
Oct 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/src/main/java/cucumber/runtime/NoInstancesException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cucumber.runtime;

public class NoInstancesException extends CucumberException {

public NoInstancesException(Class parentType) {
super(createMessage(parentType));
}

private static String createMessage(Class parentType) {
return String.format("Couldn't find a single implementation of " + parentType);
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/cucumber/runtime/Reflections.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public <T> T instantiateExactlyOneSubclass(Class<T> parentType, String packageNa
if (instances.size() == 1) {
return instances.iterator().next();
} else if (instances.size() == 0) {
throw new CucumberException("Couldn't find a single implementation of " + parentType);
throw new NoInstancesException(parentType);
} else {
throw new CucumberException("Expected only one instance, but found too many: " + instances);
throw new TooManyInstancesException(instances);
}
}

Expand Down
14 changes: 14 additions & 0 deletions core/src/main/java/cucumber/runtime/TooManyInstancesException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cucumber.runtime;

import java.util.Collection;

public class TooManyInstancesException extends CucumberException {

public TooManyInstancesException(Collection instances) {
super(createMessage(instances));
}

private static String createMessage(Collection instances) {
return String.format("Expected only one instance, but found too many: " + instances);
}
}
24 changes: 15 additions & 9 deletions java/src/main/java/cucumber/runtime/java/JavaBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.runtime.Backend;
import cucumber.runtime.ClassFinder;
import cucumber.runtime.CucumberException;
import cucumber.runtime.DuplicateStepDefinitionException;
import cucumber.runtime.Glue;
import cucumber.runtime.Reflections;
import cucumber.runtime.UnreportedStepExecutor;
import cucumber.runtime.Utils;
import cucumber.runtime.*;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;
Expand Down Expand Up @@ -61,7 +54,10 @@ public static ObjectFactory loadObjectFactory(ClassFinder classFinder) {
try {
Reflections reflections = new Reflections(classFinder);
objectFactory = reflections.instantiateExactlyOneSubclass(ObjectFactory.class, "cucumber.runtime", new Class[0], new Object[0]);
} catch (CucumberException ce) {
} catch (TooManyInstancesException e) {
System.out.println(getMultipleObjectFactoryLogMessage());
objectFactory = new DefaultJavaObjectFactory();
} catch (NoInstancesException e) {
objectFactory = new DefaultJavaObjectFactory();
}
return objectFactory;
Expand Down Expand Up @@ -141,4 +137,14 @@ void addHook(Annotation annotation, Method method) {
glue.addAfterHook(new JavaHookDefinition(method, tagExpressions, ((After) annotation).order(), timeout, objectFactory));
}
}

private static String getMultipleObjectFactoryLogMessage() {
StringBuilder sb = new StringBuilder();
sb.append("More than one Cucumber ObjectFactory was found in the classpath\n\n");
sb.append("You probably may have included, for instance, cucumber-spring AND cucumber-guice as part of\n");
sb.append("your dependencies. When this happens, Cucumber falls back to instantiating the\n");
sb.append("DefaultJavaObjectFactory implementation which doesn't provide IoC.\n");
sb.append("In order to enjoy IoC features, please remove the unnecessary dependencies from your classpath.\n");
return sb.toString();
}
}