Skip to content

Commit

Permalink
Fix validation issue for non annotated fact parameters
Browse files Browse the repository at this point in the history
Resolves #165
  • Loading branch information
fmbenhassine committed Mar 30, 2019
1 parent 3f6e87b commit df6e0f7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void checkConditionMethod(final Object rule) {
Method conditionMethod = conditionMethods.get(0);

if (!isConditionMethodWellDefined(conditionMethod)) {
throw new IllegalArgumentException(format("Condition method '%s' defined in rule '%s' must be public, may have parameters annotated with @Fact (and/or exactly one parameter of type or extending Facts) and return boolean type.", conditionMethod, rule.getClass().getName()));
throw new IllegalArgumentException(format("Condition method '%s' defined in rule '%s' must be public, must return boolean type and may have parameters annotated with @Fact (and/or exactly one parameter of type Facts or one of its sub-types).", conditionMethod, rule.getClass().getName()));
}
}

Expand All @@ -82,7 +82,7 @@ private void checkActionMethods(final Object rule) {

for (Method actionMethod : actionMethods) {
if (!isActionMethodWellDefined(actionMethod)) {
throw new IllegalArgumentException(format("Action method '%s' defined in rule '%s' must be public, must return void type and may have parameters annotated with @Fact (and/or exactly one parameter of type or extending Facts).", actionMethod, rule.getClass().getName()));
throw new IllegalArgumentException(format("Action method '%s' defined in rule '%s' must be public, must return void type and may have parameters annotated with @Fact (and/or exactly one parameter of type Facts or one of its sub-types).", actionMethod, rule.getClass().getName()));
}
}
}
Expand Down Expand Up @@ -134,13 +134,26 @@ private boolean validParameters(final Method method) {
if(notAnnotatedParameterCount > 1){
return false;
}
Class<?>[] parameterTypes = method.getParameterTypes();
if(parameterTypes.length == 1 && notAnnotatedParameterCount == 1){
return Facts.class.isAssignableFrom(parameterTypes[0]);
if (notAnnotatedParameterCount == 1) {
Class<?>[] parameterTypes = method.getParameterTypes();
int index = getIndexOfParameterOfTypeFacts(method); // TODO use method.getParameters when moving to Java 8
return Facts.class.isAssignableFrom(parameterTypes[index]);
}
return true;
}

private int getIndexOfParameterOfTypeFacts(Method method) {
Class<?>[] parameterTypes = method.getParameterTypes();
int index = 0;
for (Class<?> parameterType : parameterTypes) {
if (Facts.class.isAssignableFrom(parameterType)) {
return index;
}
index++;
}
return 0;
}

private boolean isActionMethodWellDefined(final Method method) {
return Modifier.isPublic(method.getModifiers())
&& method.getReturnType().equals(Void.TYPE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* The MIT License
*
* Copyright (c) 2019, Mahmoud Ben Hassine ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jeasy.rules.annotation;

@Rule
public class AnnotatedRuleWithOneParameterNotAnnotatedWithFactAndNotOfTypeFacts {

@Condition
public boolean when(@Fact("fact1") Object fact1, Object fact2) {
return true;
}

@Action
public void then(@Fact("fact1") Object fact1, Object fact2) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public void conditionMethodMustReturnBooleanType() {
ruleDefinitionValidator.validateRuleDefinition(new AnnotatedRuleWithConditionMethodHavingNonBooleanReturnType());
}

@Test(expected = IllegalArgumentException.class)
public void conditionMethodParametersShouldAllBeAnnotatedWithFactUnlessExactlyOneOfThemIsOfTypeFacts() {
ruleDefinitionValidator.validateRuleDefinition(new AnnotatedRuleWithOneParameterNotAnnotatedWithFactAndNotOfTypeFacts());
}

/*
* Action method tests
*/
Expand Down Expand Up @@ -102,6 +107,11 @@ public void actionMethodMustReturnVoid() {
ruleDefinitionValidator.validateRuleDefinition(new AnnotatedRuleWithActionMethodThatReturnsNonVoidType());
}

@Test(expected = IllegalArgumentException.class)
public void actionMethodParametersShouldAllBeAnnotatedWithFactUnlessExactlyOneOfThemIsOfTypeFacts() {
ruleDefinitionValidator.validateRuleDefinition(new AnnotatedRuleWithOneParameterNotAnnotatedWithFactAndNotOfTypeFacts());
}

/*
* Priority method tests
*/
Expand Down

0 comments on commit df6e0f7

Please sign in to comment.