From 6d6458bd6c69249e34b94f5c8ce7f0b1a7759fb6 Mon Sep 17 00:00:00 2001 From: Chris Rimes Date: Thu, 24 Apr 2014 21:28:06 +0100 Subject: [PATCH 1/2] Cache the StepMatcher on the StepCandidate. They're not that big (~1k each) and recreating and recompiling the regex pattern every time is insanely expensive. --- .../org/jbehave/eclipse/editor/step/StepCandidate.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/step/StepCandidate.java b/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/step/StepCandidate.java index c9dfea6..2d48ff2 100644 --- a/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/step/StepCandidate.java +++ b/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/step/StepCandidate.java @@ -22,6 +22,7 @@ public class StepCandidate { public final Integer priority; private ParametrizedStep parametrizedStep; private StepPatternParser stepParser; + private StepMatcher matcher; public StepCandidate(LocalizedStepSupport localizedSupport, String parameterPrefix, IMethod method, @@ -73,7 +74,7 @@ public String typeWord() { } public boolean matches(String stepWithoutKeyword) { - return matcher(stepType, stepPattern).matches(stepWithoutKeyword); + return matcher().matches(stepWithoutKeyword); } public String toString() { @@ -97,8 +98,11 @@ public String toString() { return builder.toString(); } - private StepMatcher matcher(StepType stepType, String stepPattern) { - return stepParser.parseStep(stepType, stepPattern); + private StepMatcher matcher() { + if (matcher == null) { + matcher = stepParser.parseStep(stepType, stepPattern); + } + return matcher; } } \ No newline at end of file From 956e0c8be21c55bf0d59f3fcadfbc30d7f67df16 Mon Sep 17 00:00:00 2001 From: rimesc Date: Mon, 26 Jan 2015 21:43:32 +0000 Subject: [PATCH 2/2] Naively handle the case where the priority isn't a literal integer (it might be a reference to a constant or an expression) by setting the priority to null. --- .../step/MethodToStepCandidateReducer.java | 13 ++++++- .../MethodToStepCandidateReducerTest.java | 34 +++++++++++++++---- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/step/MethodToStepCandidateReducer.java b/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/step/MethodToStepCandidateReducer.java index b3eb46d..ae65ac8 100644 --- a/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/step/MethodToStepCandidateReducer.java +++ b/org.jbehave.eclipse/src/org/jbehave/eclipse/editor/step/MethodToStepCandidateReducer.java @@ -82,7 +82,7 @@ public void reduce(final IMethod method, if (basicStep) { String stepPattern = getValue(annotationAttributes, "value"); - priority = getValue(annotationAttributes, "priority"); + priority = getPriority(annotationAttributes); patterns = extractPatternVariants(patterns, stepPattern); } else if (Aliases.class.getName().equals(fullQualifiedName)) { @@ -137,6 +137,17 @@ private List extractPatternVariants(List patterns, return patterns; } + private Integer getPriority(IMemberValuePair[] annotationAttributes) { + try { + return getValue(annotationAttributes, "priority"); + } + catch (ClassCastException ex) { + // The priority was a constant or expression. + // TODO it should be possible to get the value of a constant, at least + return null; + } + } + @SuppressWarnings("unchecked") public static T getValue(IMemberValuePair[] memberValuePairs, String key) { for (IMemberValuePair kv : memberValuePairs) { diff --git a/org.jbehave.eclipse/test/org/jbehave/eclipse/editor/step/MethodToStepCandidateReducerTest.java b/org.jbehave.eclipse/test/org/jbehave/eclipse/editor/step/MethodToStepCandidateReducerTest.java index 9c14d3d..1b809e9 100644 --- a/org.jbehave.eclipse/test/org/jbehave/eclipse/editor/step/MethodToStepCandidateReducerTest.java +++ b/org.jbehave.eclipse/test/org/jbehave/eclipse/editor/step/MethodToStepCandidateReducerTest.java @@ -15,6 +15,8 @@ import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; +import static java.util.Arrays.asList; + @RunWith(MockitoJUnitRunner.class) public class MethodToStepCandidateReducerTest { @@ -82,6 +84,17 @@ public void testListenerInformedWithPriority_WhenProvided() "a priority", 1); } + @Test + public void testListenerNotInformedWithPriority_WhenNotRecognized() + throws JavaModelException { + givenAnnotation("Given", "a priority", "PRIORITY"); + + whenTheMethodWasProcessed(); + + thenListenerShouldHaveBeenInformedOnlyWith(StepType.GIVEN, + "a priority", null); + } + @Test public void testListenerInformedWithFirst_WhenTwoAnnotations() throws JavaModelException { @@ -263,19 +276,23 @@ public void givenNoAnnotation() { } public void givenAnnotation(String elementName, String value) { - givenAnnotation(elementName, value, null); + final List attributes = new ArrayList(); + + attributes.add(getMemberValuePair("value", value)); + + givenAnnotation(elementName, getMemberValuePair("value", value)); } public void givenAnnotation(String elementName, String value, - Integer priority) { + Object priority) { final List attributes = new ArrayList(); attributes.add(getMemberValuePair("value", value)); - if (priority != null) { - attributes.add(getMemberValuePair("priority", priority)); - } + attributes.add(getMemberValuePair("priority", priority)); - givenAnnotation(elementName, attributes); + givenAnnotation(elementName, + getMemberValuePair("value", value), + getMemberValuePair("priority", priority)); } public void givenAliases(String[] values) { @@ -285,6 +302,11 @@ public void givenAliases(String[] values) { givenAnnotation("Aliases", attributes); } + private void givenAnnotation(String elementName, + IMemberValuePair... attributes) { + givenAnnotation(elementName, asList(attributes)); + } + private void givenAnnotation(String elementName, List attributes) { final IAnnotation annotation = Mockito.mock(IAnnotation.class);