From 08df2a11e85b1f13dc08799272fcebe368b0c54c Mon Sep 17 00:00:00 2001 From: Ariel Kogan Date: Thu, 19 Sep 2013 13:52:53 +0100 Subject: [PATCH 1/3] Log a warning when more than one IoC dependency is found in the classpath and Cucumber defaults to no IoCs. --- java/pom.xml | 4 ++++ .../java/cucumber/runtime/java/JavaBackend.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/java/pom.xml b/java/pom.xml index 111ad1844b..531972fe23 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -48,6 +48,10 @@ cobertura test + + org.slf4j + slf4j-log4j12 + diff --git a/java/src/main/java/cucumber/runtime/java/JavaBackend.java b/java/src/main/java/cucumber/runtime/java/JavaBackend.java index 49fc2a92bb..a097b6a0d0 100644 --- a/java/src/main/java/cucumber/runtime/java/JavaBackend.java +++ b/java/src/main/java/cucumber/runtime/java/JavaBackend.java @@ -16,6 +16,8 @@ import cucumber.runtime.snippets.FunctionNameSanitizer; import cucumber.runtime.snippets.SnippetGenerator; import gherkin.formatter.model.Step; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.lang.annotation.Annotation; import java.lang.reflect.Method; @@ -30,6 +32,8 @@ public class JavaBackend implements Backend { private final MethodScanner methodScanner; private Glue glue; + private static final Logger logger = LoggerFactory.getLogger(JavaBackend.class); + /** * The constructor called by reflection by default. * @@ -62,6 +66,7 @@ public static ObjectFactory loadObjectFactory(ClassFinder classFinder) { Reflections reflections = new Reflections(classFinder); objectFactory = reflections.instantiateExactlyOneSubclass(ObjectFactory.class, "cucumber.runtime", new Class[0], new Object[0]); } catch (CucumberException ce) { + logger.warn(getMultipleObjectFactoryLogMessage()); objectFactory = new DefaultJavaObjectFactory(); } return objectFactory; @@ -141,4 +146,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(); + } } From fb62e663600d4a88352a1813dbfebef10dcaab5a Mon Sep 17 00:00:00 2001 From: Ariel Kogan Date: Thu, 19 Sep 2013 14:34:02 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Changes=20based=20on=20Aslak=20Helles=C3=B8?= =?UTF-8?q?y=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/src/main/java/cucumber/runtime/Reflections.java | 7 +++++-- java/pom.xml | 4 ---- java/src/main/java/cucumber/runtime/java/JavaBackend.java | 8 +++----- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/cucumber/runtime/Reflections.java b/core/src/main/java/cucumber/runtime/Reflections.java index 6ee929a938..508fd1fa35 100644 --- a/core/src/main/java/cucumber/runtime/Reflections.java +++ b/core/src/main/java/cucumber/runtime/Reflections.java @@ -7,6 +7,9 @@ public class Reflections { private final ClassFinder classFinder; + public static final String NO_INSTANCES_MSG = "Couldn't find a single implementation of "; + public static final String TOO_MANY_INSTANCES_MSG = "Expected only one instance, but found too many"; + public Reflections(ClassFinder classFinder) { this.classFinder = classFinder; } @@ -16,9 +19,9 @@ public T instantiateExactlyOneSubclass(Class 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 CucumberException(NO_INSTANCES_MSG + parentType); } else { - throw new CucumberException("Expected only one instance, but found too many: " + instances); + throw new CucumberException(TOO_MANY_INSTANCES_MSG + ": " + instances); } } diff --git a/java/pom.xml b/java/pom.xml index 531972fe23..111ad1844b 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -48,10 +48,6 @@ cobertura test - - org.slf4j - slf4j-log4j12 - diff --git a/java/src/main/java/cucumber/runtime/java/JavaBackend.java b/java/src/main/java/cucumber/runtime/java/JavaBackend.java index a097b6a0d0..344b7f6d6a 100644 --- a/java/src/main/java/cucumber/runtime/java/JavaBackend.java +++ b/java/src/main/java/cucumber/runtime/java/JavaBackend.java @@ -16,8 +16,6 @@ import cucumber.runtime.snippets.FunctionNameSanitizer; import cucumber.runtime.snippets.SnippetGenerator; import gherkin.formatter.model.Step; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.lang.annotation.Annotation; import java.lang.reflect.Method; @@ -32,8 +30,6 @@ public class JavaBackend implements Backend { private final MethodScanner methodScanner; private Glue glue; - private static final Logger logger = LoggerFactory.getLogger(JavaBackend.class); - /** * The constructor called by reflection by default. * @@ -66,7 +62,9 @@ public static ObjectFactory loadObjectFactory(ClassFinder classFinder) { Reflections reflections = new Reflections(classFinder); objectFactory = reflections.instantiateExactlyOneSubclass(ObjectFactory.class, "cucumber.runtime", new Class[0], new Object[0]); } catch (CucumberException ce) { - logger.warn(getMultipleObjectFactoryLogMessage()); + if (ce.getMessage().contains(Reflections.TOO_MANY_INSTANCES_MSG)) { + System.out.println(getMultipleObjectFactoryLogMessage()); + } objectFactory = new DefaultJavaObjectFactory(); } return objectFactory; From e1b50ca061a548c3cfdd37465a2c31e479581fa2 Mon Sep 17 00:00:00 2001 From: Ariel Kogan Date: Thu, 19 Sep 2013 15:12:50 +0100 Subject: [PATCH 3/3] Changing to strongly typed exceptions --- .../cucumber/runtime/NoInstancesException.java | 12 ++++++++++++ .../main/java/cucumber/runtime/Reflections.java | 7 ++----- .../runtime/TooManyInstancesException.java | 14 ++++++++++++++ .../java/cucumber/runtime/java/JavaBackend.java | 17 +++++------------ 4 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 core/src/main/java/cucumber/runtime/NoInstancesException.java create mode 100644 core/src/main/java/cucumber/runtime/TooManyInstancesException.java diff --git a/core/src/main/java/cucumber/runtime/NoInstancesException.java b/core/src/main/java/cucumber/runtime/NoInstancesException.java new file mode 100644 index 0000000000..152e545a6d --- /dev/null +++ b/core/src/main/java/cucumber/runtime/NoInstancesException.java @@ -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); + } +} diff --git a/core/src/main/java/cucumber/runtime/Reflections.java b/core/src/main/java/cucumber/runtime/Reflections.java index 508fd1fa35..e060ebe8ff 100644 --- a/core/src/main/java/cucumber/runtime/Reflections.java +++ b/core/src/main/java/cucumber/runtime/Reflections.java @@ -7,9 +7,6 @@ public class Reflections { private final ClassFinder classFinder; - public static final String NO_INSTANCES_MSG = "Couldn't find a single implementation of "; - public static final String TOO_MANY_INSTANCES_MSG = "Expected only one instance, but found too many"; - public Reflections(ClassFinder classFinder) { this.classFinder = classFinder; } @@ -19,9 +16,9 @@ public T instantiateExactlyOneSubclass(Class parentType, String packageNa if (instances.size() == 1) { return instances.iterator().next(); } else if (instances.size() == 0) { - throw new CucumberException(NO_INSTANCES_MSG + parentType); + throw new NoInstancesException(parentType); } else { - throw new CucumberException(TOO_MANY_INSTANCES_MSG + ": " + instances); + throw new TooManyInstancesException(instances); } } diff --git a/core/src/main/java/cucumber/runtime/TooManyInstancesException.java b/core/src/main/java/cucumber/runtime/TooManyInstancesException.java new file mode 100644 index 0000000000..463f01211e --- /dev/null +++ b/core/src/main/java/cucumber/runtime/TooManyInstancesException.java @@ -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); + } +} diff --git a/java/src/main/java/cucumber/runtime/java/JavaBackend.java b/java/src/main/java/cucumber/runtime/java/JavaBackend.java index 344b7f6d6a..2cc078e3e0 100644 --- a/java/src/main/java/cucumber/runtime/java/JavaBackend.java +++ b/java/src/main/java/cucumber/runtime/java/JavaBackend.java @@ -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; @@ -61,10 +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) { - if (ce.getMessage().contains(Reflections.TOO_MANY_INSTANCES_MSG)) { - System.out.println(getMultipleObjectFactoryLogMessage()); - } + } catch (TooManyInstancesException e) { + System.out.println(getMultipleObjectFactoryLogMessage()); + objectFactory = new DefaultJavaObjectFactory(); + } catch (NoInstancesException e) { objectFactory = new DefaultJavaObjectFactory(); } return objectFactory;