Skip to content

Commit

Permalink
[Java] Updated java for the use of Gherkin v4.0.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
brasmusson committed Dec 18, 2016
1 parent 5df2263 commit 41c169a
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 128 deletions.
36 changes: 24 additions & 12 deletions java/pom.xml
Expand Up @@ -68,7 +68,8 @@


<groovy><![CDATA[ <groovy><![CDATA[
import groovy.text.SimpleTemplateEngine import groovy.text.SimpleTemplateEngine
import gherkin.I18n import gherkin.GherkinDialect
import gherkin.GherkinDialectProvider
def engine = new SimpleTemplateEngine() def engine = new SimpleTemplateEngine()
def templateSource = new File(project.baseDir, "src${File.separator}main${File.separator}code_generator${File.separator}I18n.java.txt").getText() def templateSource = new File(project.baseDir, "src${File.separator}main${File.separator}code_generator${File.separator}I18n.java.txt").getText()
Expand All @@ -81,37 +82,48 @@ def normalize(s) {
} }
} }
def localeFor(lang) {
languageAndCountry = lang.split("-")
if (languageAndCountry.length == 1) {
return new Locale(lang)
} else {
return new Locale(languageAndCountry[0], languageAndCountry[1])
}
}
// TODO: Need to add i18n.getName() and i18n.getNative() for better names. // TODO: Need to add i18n.getName() and i18n.getNative() for better names.
def package_html = """\ def package_html = """\
<body> <body>
<p> <p>
\${i18n.getLocale().getDisplayLanguage()} \${locale.getDisplayLanguage()}
</p> </p>
</body> </body>
""" """
def unsupported = [] // We used to have problems with "kn", but it seems ok now. def unsupported = ["em"] // The generated files for Emoij do not compile.
def dialectProvider = new GherkinDialectProvider()
I18n.all.each { i18n -> GherkinDialectProvider.DIALECTS.keySet().each { language ->
if (!unsupported.contains(i18n.underscoredIsoCode)) { def dialect = dialectProvider.getDialect(language, null)
i18n.codeKeywords.each { kw -> def normalized_language = dialect.language.replaceAll("[\\s-]", "_").toLowerCase()
def normalized_kw = normalize(kw) if (!unsupported.contains(normalized_language)) {
def binding = ["i18n":i18n, "kw":normalized_kw] dialect.stepKeywords.findAll { !it.contains('*') && !it.matches("^\\d.*") }.unique().each { kw ->
def normalized_kw = normalize(kw.replaceAll("[\\s',!]", ""))
def binding = ["lang":normalized_language, "kw":normalized_kw]
def template = engine.createTemplate(templateSource).make(binding) def template = engine.createTemplate(templateSource).make(binding)
def file = new File(project.baseDir, "target${File.separator}generated-sources${File.separator}i18n${File.separator}java${File.separator}cucumber${File.separator}api${File.separator}java${File.separator}${i18n.underscoredIsoCode}${File.separator}${normalized_kw}.java") def file = new File(project.baseDir, "target${File.separator}generated-sources${File.separator}i18n${File.separator}java${File.separator}cucumber${File.separator}api${File.separator}java${File.separator}${normalized_language}${File.separator}${normalized_kw}.java")
file.parentFile.mkdirs() file.parentFile.mkdirs()
file.write(template.toString(), "UTF-8") file.write(template.toString(), "UTF-8")
} }
// html // html
def binding = ["i18n":i18n] def locale = localeFor(dialect.language)
def binding = ["locale":locale]
def html = engine.createTemplate(package_html).make(binding).toString() def html = engine.createTemplate(package_html).make(binding).toString()
def file = new File(project.baseDir, "target${File.separator}generated-sources${File.separator}i18n${File.separator}java${File.separator}cucumber${File.separator}api${File.separator}java${File.separator}${i18n.underscoredIsoCode}${File.separator}package.html") def file = new File(project.baseDir, "target${File.separator}generated-sources${File.separator}i18n${File.separator}java${File.separator}cucumber${File.separator}api${File.separator}java${File.separator}${normalized_language}${File.separator}package.html")
file.write(html, "UTF-8") file.write(html, "UTF-8")
} }
} }
]]></groovy> ]]></groovy>


</target> </target>
Expand Down
2 changes: 1 addition & 1 deletion java/src/main/code_generator/I18n.java.txt
@@ -1,4 +1,4 @@
package cucumber.api.java.${i18n.underscoredIsoCode}; package cucumber.api.java.${lang};


import cucumber.runtime.java.StepDefAnnotation; import cucumber.runtime.java.StepDefAnnotation;


Expand Down
Expand Up @@ -5,8 +5,8 @@
import cucumber.api.java8.HookNoArgsBody; import cucumber.api.java8.HookNoArgsBody;
import cucumber.runtime.HookDefinition; import cucumber.runtime.HookDefinition;
import cucumber.runtime.Timeout; import cucumber.runtime.Timeout;
import gherkin.TagExpression; import cucumber.runtime.TagExpression;
import gherkin.formatter.model.Tag; import gherkin.pickles.PickleTag;


import java.util.Collection; import java.util.Collection;


Expand Down Expand Up @@ -59,7 +59,7 @@ public Object call() throws Throwable {
} }


@Override @Override
public boolean matches(Collection<Tag> tags) { public boolean matches(Collection<PickleTag> tags) {
return tagExpression.evaluate(tags); return tagExpression.evaluate(tags);
} }


Expand Down
@@ -1,14 +1,13 @@
package cucumber.runtime.java; package cucumber.runtime.java;


import cucumber.api.java8.StepdefBody; import cucumber.api.java8.StepdefBody;
import cucumber.runtime.Argument;
import cucumber.runtime.CucumberException; import cucumber.runtime.CucumberException;
import cucumber.runtime.JdkPatternArgumentMatcher; import cucumber.runtime.JdkPatternArgumentMatcher;
import cucumber.runtime.ParameterInfo; import cucumber.runtime.ParameterInfo;
import cucumber.runtime.StepDefinition; import cucumber.runtime.StepDefinition;
import cucumber.runtime.Utils; import cucumber.runtime.Utils;
import gherkin.I18n; import gherkin.pickles.PickleStep;
import gherkin.formatter.Argument;
import gherkin.formatter.model.Step;


import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
Expand Down Expand Up @@ -91,8 +90,8 @@ private CucumberException withLocation(CucumberException exception) {
} }


@Override @Override
public List<Argument> matchedArguments(Step step) { public List<Argument> matchedArguments(PickleStep step) {
return argumentMatcher.argumentsFrom(step.getName()); return argumentMatcher.argumentsFrom(step.getText());
} }


@Override @Override
Expand All @@ -111,7 +110,7 @@ public ParameterInfo getParameterType(int n, Type argumentType) throws IndexOutO
} }


@Override @Override
public void execute(final I18n i18n, final Object[] args) throws Throwable { public void execute(final String language, final Object[] args) throws Throwable {
Utils.invoke(body, method, timeoutMillis, args); Utils.invoke(body, method, timeoutMillis, args);
} }


Expand Down
6 changes: 3 additions & 3 deletions java/src/main/java/cucumber/runtime/java/JavaBackend.java
Expand Up @@ -21,7 +21,7 @@
import cucumber.runtime.snippets.FunctionNameGenerator; import cucumber.runtime.snippets.FunctionNameGenerator;
import cucumber.runtime.snippets.Snippet; import cucumber.runtime.snippets.Snippet;
import cucumber.runtime.snippets.SnippetGenerator; import cucumber.runtime.snippets.SnippetGenerator;
import gherkin.formatter.model.Step; import gherkin.pickles.PickleStep;


import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
Expand Down Expand Up @@ -141,8 +141,8 @@ public void disposeWorld() {
} }


@Override @Override
public String getSnippet(Step step, FunctionNameGenerator functionNameGenerator) { public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return snippetGenerator.getSnippet(step, functionNameGenerator); return snippetGenerator.getSnippet(step, keyword, functionNameGenerator);
} }


void addStepDefinition(Annotation annotation, Method method) { void addStepDefinition(Annotation annotation, Method method) {
Expand Down
Expand Up @@ -6,8 +6,8 @@
import cucumber.runtime.HookDefinition; import cucumber.runtime.HookDefinition;
import cucumber.runtime.MethodFormat; import cucumber.runtime.MethodFormat;
import cucumber.runtime.Utils; import cucumber.runtime.Utils;
import gherkin.TagExpression; import cucumber.runtime.TagExpression;
import gherkin.formatter.model.Tag; import gherkin.pickles.PickleTag;


import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collection; import java.util.Collection;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void execute(Scenario scenario) throws Throwable {
} }


@Override @Override
public boolean matches(Collection<Tag> tags) { public boolean matches(Collection<PickleTag> tags) {
return tagExpression.evaluate(tags); return tagExpression.evaluate(tags);
} }


Expand Down
11 changes: 5 additions & 6 deletions java/src/main/java/cucumber/runtime/java/JavaStepDefinition.java
@@ -1,14 +1,13 @@
package cucumber.runtime.java; package cucumber.runtime.java;


import cucumber.api.java.ObjectFactory; import cucumber.api.java.ObjectFactory;
import cucumber.runtime.Argument;
import cucumber.runtime.JdkPatternArgumentMatcher; import cucumber.runtime.JdkPatternArgumentMatcher;
import cucumber.runtime.MethodFormat; import cucumber.runtime.MethodFormat;
import cucumber.runtime.ParameterInfo; import cucumber.runtime.ParameterInfo;
import cucumber.runtime.StepDefinition; import cucumber.runtime.StepDefinition;
import cucumber.runtime.Utils; import cucumber.runtime.Utils;
import gherkin.I18n; import gherkin.pickles.PickleStep;
import gherkin.formatter.Argument;
import gherkin.formatter.model.Step;


import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Type; import java.lang.reflect.Type;
Expand All @@ -34,12 +33,12 @@ public JavaStepDefinition(Method method, Pattern pattern, long timeoutMillis, Ob
this.parameterInfos = ParameterInfo.fromMethod(method); this.parameterInfos = ParameterInfo.fromMethod(method);
} }


public void execute(I18n i18n, Object[] args) throws Throwable { public void execute(String language, Object[] args) throws Throwable {
Utils.invoke(objectFactory.getInstance(method.getDeclaringClass()), method, timeoutMillis, args); Utils.invoke(objectFactory.getInstance(method.getDeclaringClass()), method, timeoutMillis, args);
} }


public List<Argument> matchedArguments(Step step) { public List<Argument> matchedArguments(PickleStep step) {
return argumentMatcher.argumentsFrom(step.getName()); return argumentMatcher.argumentsFrom(step.getText());
} }


public String getLocation(boolean detail) { public String getLocation(boolean detail) {
Expand Down
13 changes: 6 additions & 7 deletions java/src/test/java/cucumber/runtime/java/Java8SnippetTest.java
@@ -1,18 +1,17 @@
package cucumber.runtime.java; package cucumber.runtime.java;


import cucumber.runtime.snippets.SnippetGenerator; import cucumber.runtime.snippets.SnippetGenerator;
import gherkin.formatter.model.Comment; import gherkin.pickles.Argument;
import gherkin.formatter.model.Step; import gherkin.pickles.PickleLocation;
import gherkin.pickles.PickleStep;
import org.junit.Test; import org.junit.Test;


import java.util.Collections; import java.util.Collections;
import java.util.List;


import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;


public class Java8SnippetTest { public class Java8SnippetTest {

private static final String GIVEN_KEYWORD = "Given";
private static final List<Comment> NO_COMMENTS = Collections.emptyList();


@Test @Test
public void generatesPlainSnippet() { public void generatesPlainSnippet() {
Expand All @@ -26,7 +25,7 @@ public void generatesPlainSnippet() {
} }


private String snippetFor(String name) { private String snippetFor(String name) {
Step step = new Step(NO_COMMENTS, "Given ", name, 0, null, null); PickleStep step = new PickleStep(name, Collections.<Argument>emptyList(), Collections.<PickleLocation>emptyList());
return new SnippetGenerator(new Java8Snippet()).getSnippet(step, null); return new SnippetGenerator(new Java8Snippet()).getSnippet(step, GIVEN_KEYWORD, null);
} }
} }
5 changes: 2 additions & 3 deletions java/src/test/java/cucumber/runtime/java/JavaBackendTest.java
Expand Up @@ -8,8 +8,7 @@
import cucumber.runtime.StepDefinition; import cucumber.runtime.StepDefinition;
import cucumber.runtime.StepDefinitionMatch; import cucumber.runtime.StepDefinitionMatch;
import cucumber.runtime.java.stepdefs.Stepdefs; import cucumber.runtime.java.stepdefs.Stepdefs;
import gherkin.I18n; import gherkin.pickles.PickleStep;
import gherkin.formatter.model.Step;
import org.junit.Test; import org.junit.Test;


import java.util.ArrayList; import java.util.ArrayList;
Expand Down Expand Up @@ -76,7 +75,7 @@ public List<HookDefinition> getAfterHooks() {
} }


@Override @Override
public StepDefinitionMatch stepDefinitionMatch(String featurePath, Step step, I18n i18n) { public StepDefinitionMatch stepDefinitionMatch(String featurePath, PickleStep step) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }


Expand Down
7 changes: 4 additions & 3 deletions java/src/test/java/cucumber/runtime/java/JavaHookTest.java
Expand Up @@ -9,7 +9,8 @@
import cucumber.runtime.RuntimeGlue; import cucumber.runtime.RuntimeGlue;
import cucumber.runtime.UndefinedStepsTracker; import cucumber.runtime.UndefinedStepsTracker;
import cucumber.runtime.xstream.LocalizedXStreams; import cucumber.runtime.xstream.LocalizedXStreams;
import gherkin.formatter.model.Tag; import gherkin.pickles.PickleLocation;
import gherkin.pickles.PickleTag;
import org.junit.Test; import org.junit.Test;


import java.lang.reflect.Method; import java.lang.reflect.Method;
Expand Down Expand Up @@ -91,7 +92,7 @@ public void matches_matching_tags() {
backend.buildWorld(); backend.buildWorld();
backend.addHook(BEFORE.getAnnotation(Before.class), BEFORE); backend.addHook(BEFORE.getAnnotation(Before.class), BEFORE);
HookDefinition before = glue.getBeforeHooks().get(0); HookDefinition before = glue.getBeforeHooks().get(0);
assertTrue(before.matches(asList(new Tag("@bar", 0), new Tag("@zap", 0)))); assertTrue(before.matches(asList(new PickleTag(mock(PickleLocation.class), "@bar"), new PickleTag(mock(PickleLocation.class), "@zap"))));
} }


@Test @Test
Expand All @@ -100,7 +101,7 @@ public void does_not_match_non_matching_tags() {
backend.buildWorld(); backend.buildWorld();
backend.addHook(BEFORE.getAnnotation(Before.class), BEFORE); backend.addHook(BEFORE.getAnnotation(Before.class), BEFORE);
HookDefinition before = glue.getBeforeHooks().get(0); HookDefinition before = glue.getBeforeHooks().get(0);
assertFalse(before.matches(asList(new Tag("@bar", 0)))); assertFalse(before.matches(asList(new PickleTag(mock(PickleLocation.class), "@bar"))));
} }


@Test @Test
Expand Down
34 changes: 18 additions & 16 deletions java/src/test/java/cucumber/runtime/java/JavaSnippetTest.java
Expand Up @@ -3,22 +3,24 @@
import cucumber.runtime.snippets.FunctionNameGenerator; import cucumber.runtime.snippets.FunctionNameGenerator;
import cucumber.runtime.snippets.SnippetGenerator; import cucumber.runtime.snippets.SnippetGenerator;
import cucumber.runtime.snippets.UnderscoreConcatenator; import cucumber.runtime.snippets.UnderscoreConcatenator;
import gherkin.formatter.model.Comment; import gherkin.pickles.Argument;
import gherkin.formatter.model.DataTableRow; import gherkin.pickles.PickleCell;
import gherkin.formatter.model.DocString; import gherkin.pickles.PickleLocation;
import gherkin.formatter.model.Step; import gherkin.pickles.PickleRow;
import gherkin.pickles.PickleStep;
import gherkin.pickles.PickleString;
import gherkin.pickles.PickleTable;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;


import java.util.Collections; import java.util.Collections;
import java.util.List;


import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;


public class JavaSnippetTest { public class JavaSnippetTest {


private static final List<Comment> NO_COMMENTS = Collections.emptyList(); private static final String GIVEN_KEYWORD = "Given";
private final FunctionNameGenerator functionNameGenerator = new FunctionNameGenerator(new UnderscoreConcatenator()); private final FunctionNameGenerator functionNameGenerator = new FunctionNameGenerator(new UnderscoreConcatenator());


@Test @Test
Expand Down Expand Up @@ -129,7 +131,7 @@ public void generatesSnippetWithDocString() {
" // Write code here that turns the phrase above into concrete actions\n" + " // Write code here that turns the phrase above into concrete actions\n" +
" throw new PendingException();\n" + " throw new PendingException();\n" +
"}\n"; "}\n";
assertEquals(expected, snippetForDocString("I have:", new DocString("text/plain", "hello", 1))); assertEquals(expected, snippetForDocString("I have:", new PickleString(null, "hello")));
} }


@Test @Test
Expand All @@ -154,22 +156,22 @@ public void generatesSnippetWithDataTable() {
" // E,K,V must be a scalar (String, Integer, Date, enum etc)\n" + " // E,K,V must be a scalar (String, Integer, Date, enum etc)\n" +
" throw new PendingException();\n" + " throw new PendingException();\n" +
"}\n"; "}\n";
List<DataTableRow> dataTable = asList(new DataTableRow(NO_COMMENTS, asList("col1"), 1)); PickleTable dataTable = new PickleTable(asList(new PickleRow(asList(new PickleCell(null, "col1")))));
assertEquals(expected, snippetForDataTable("I have:", dataTable)); assertEquals(expected, snippetForDataTable("I have:", dataTable));
} }


private String snippetFor(String name) { private String snippetFor(String name) {
Step step = new Step(NO_COMMENTS, "Given ", name, 0, null, null); PickleStep step = new PickleStep(name, Collections.<Argument>emptyList(), Collections.<PickleLocation>emptyList());
return new SnippetGenerator(new JavaSnippet()).getSnippet(step, functionNameGenerator); return new SnippetGenerator(new JavaSnippet()).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
} }


private String snippetForDocString(String name, DocString docString) { private String snippetForDocString(String name, PickleString docString) {
Step step = new Step(NO_COMMENTS, "Given ", name, 0, null, docString); PickleStep step = new PickleStep(name, asList((Argument)docString), Collections.<PickleLocation>emptyList());
return new SnippetGenerator(new JavaSnippet()).getSnippet(step, functionNameGenerator); return new SnippetGenerator(new JavaSnippet()).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
} }


private String snippetForDataTable(String name, List<DataTableRow> dataTable) { private String snippetForDataTable(String name, PickleTable dataTable) {
Step step = new Step(NO_COMMENTS, "Given ", name, 0, dataTable, null); PickleStep step = new PickleStep(name, asList((Argument)dataTable), Collections.<PickleLocation>emptyList());
return new SnippetGenerator(new JavaSnippet()).getSnippet(step, functionNameGenerator); return new SnippetGenerator(new JavaSnippet()).getSnippet(step, GIVEN_KEYWORD, functionNameGenerator);
} }
} }

0 comments on commit 41c169a

Please sign in to comment.