Skip to content

Commit

Permalink
Merge pull request #240 from caelum/CustomAndInternalAcceptsValidatio…
Browse files Browse the repository at this point in the history
…nRule

Custom and internal accepts validation rule
  • Loading branch information
Turini committed Oct 29, 2013
2 parents 9b129aa + 69b54fe commit 0b44a5f
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 22 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import br.com.caelum.vraptor.VRaptorException;
import br.com.caelum.vraptor.controller.ControllerInstance;
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.core.InterceptorStack;
Expand All @@ -29,7 +28,6 @@
import br.com.caelum.vraptor.interceptor.example.AlwaysAcceptsAspectInterceptor;
import br.com.caelum.vraptor.interceptor.example.ExampleOfSimpleStackInterceptor;
import br.com.caelum.vraptor.interceptor.example.InterceptorWithCustomizedAccepts;
import br.com.caelum.vraptor.interceptor.example.InternalAndCustomAcceptsInterceptor;
import br.com.caelum.vraptor.interceptor.example.MethodLevelAcceptsController;
import br.com.caelum.vraptor.interceptor.example.WithoutAroundInterceptor;
import br.com.caelum.vraptor.ioc.Container;
Expand Down Expand Up @@ -178,12 +176,6 @@ public void shouldNotInvokeIfDoesNotHaveAround() throws Exception {
verify(simpleInterceptorStack).next();
}

@Test(expected = VRaptorException.class)
public void mustNotUseInternalAcceptsAndCustomAccepts(){
InternalAndCustomAcceptsInterceptor interceptor = new InternalAndCustomAcceptsInterceptor();
newAspectStyleInterceptorHandler(InternalAndCustomAcceptsInterceptor.class, interceptor);
}

@Test
public void shouldAcceptCustomizedAccepts() throws Exception {

Expand Down
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 {
}
}

This file was deleted.

0 comments on commit 0b44a5f

Please sign in to comment.