-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from caelum/CustomAndInternalAcceptsValidatio…
…nRule Custom and internal accepts validation rule
- Loading branch information
Showing
4 changed files
with
98 additions
and
22 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...c/main/java/br/com/caelum/vraptor/interceptor/CustomAndInternalAcceptsValidationRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package br.com.caelum.vraptor.interceptor; | ||
|
||
import static br.com.caelum.vraptor.interceptor.CustomAcceptsVerifier.getCustomAcceptsAnnotations; | ||
import static com.google.common.base.Preconditions.checkState; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
|
||
import javax.inject.Inject; | ||
|
||
import net.vidageek.mirror.list.dsl.MirrorList; | ||
import br.com.caelum.vraptor.Accepts; | ||
|
||
public class CustomAndInternalAcceptsValidationRule implements ValidationRule { | ||
|
||
private final StepInvoker invoker; | ||
|
||
/** | ||
* @deprecated CDI eyes only | ||
*/ | ||
protected CustomAndInternalAcceptsValidationRule() { | ||
this(null); | ||
} | ||
|
||
@Inject | ||
public CustomAndInternalAcceptsValidationRule(StepInvoker invoker) { | ||
this.invoker = invoker; | ||
} | ||
|
||
@Override | ||
public void validate(Class<?> originalType, MirrorList<Method> methods) { | ||
|
||
Method accepts = invoker.findMethod(methods, Accepts.class, originalType); | ||
List<Annotation> constraints = getCustomAcceptsAnnotations(originalType); | ||
|
||
checkState(accepts == null || constraints.isEmpty(), "Interceptor " | ||
+ "%s must declare internal accepts or custom, not both.", originalType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...st/java/br/com/caelum/vraptor/interceptor/CustomAndInternalAcceptsValidationRuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package br.com.caelum.vraptor.interceptor; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import net.vidageek.mirror.list.dsl.MirrorList; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import br.com.caelum.vraptor.Accepts; | ||
import br.com.caelum.vraptor.factory.Factories; | ||
import br.com.caelum.vraptor.interceptor.example.NotLogged; | ||
|
||
public class CustomAndInternalAcceptsValidationRuleTest { | ||
|
||
private CustomAndInternalAcceptsValidationRule validationRule; | ||
private StepInvoker stepInvoker; | ||
|
||
@Before | ||
public void setUp() { | ||
stepInvoker = Factories.createStepInvoker(); | ||
validationRule = new CustomAndInternalAcceptsValidationRule(stepInvoker); | ||
} | ||
|
||
@Test(expected = IllegalStateException.class) | ||
public void mustNotUseInternalAcceptsAndCustomAccepts(){ | ||
Class<?> type = InternalAndCustomAcceptsInterceptor.class; | ||
MirrorList<Method> methods = stepInvoker.findAllMethods(type); | ||
validationRule.validate(type, methods); | ||
} | ||
|
||
@Test | ||
public void shouldValidateIfConstainsOnlyInternalAccepts(){ | ||
Class<?> type = InternalAcceptsInterceptor.class; | ||
MirrorList<Method> methods = stepInvoker.findAllMethods(type); | ||
validationRule.validate(type, methods); | ||
} | ||
|
||
@Test | ||
public void shouldValidateIfConstainsOnlyCustomAccepts(){ | ||
Class<?> type = CustomAcceptsInterceptor.class; | ||
MirrorList<Method> methods = stepInvoker.findAllMethods(type); | ||
validationRule.validate(type, methods); | ||
} | ||
|
||
@AcceptsWithAnnotations(NotLogged.class) | ||
public class InternalAndCustomAcceptsInterceptor { | ||
@Accepts public boolean accepts(){ return true; } | ||
} | ||
|
||
public class InternalAcceptsInterceptor { | ||
@Accepts public boolean accepts(){ return true; } | ||
} | ||
|
||
@AcceptsWithAnnotations(NotLogged.class) | ||
public class CustomAcceptsInterceptor { | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
...t/java/br/com/caelum/vraptor/interceptor/example/InternalAndCustomAcceptsInterceptor.java
This file was deleted.
Oops, something went wrong.