Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilherme Silveira committed May 1, 2009
1 parent c321c8f commit 937c6e7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
/***
*
* Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. 2. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution. 3. Neither the name of the
* copyright holders nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package br.com.caelum.vraptor.vraptor2;

import java.lang.reflect.InvocationTargetException;
Expand All @@ -13,18 +40,16 @@
public class ExecuteAndViewInterceptor implements Interceptor {

private final MethodInfo info;
private final MethodInfo parameters;

public ExecuteAndViewInterceptor(MethodInfo info, MethodInfo parameters) {
public ExecuteAndViewInterceptor(MethodInfo info) {
this.info = info;
this.parameters = parameters;
}

public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance)
throws InterceptionException {
try {
Method reflectionMethod = method.getMethod();
Object[] parameters = this.parameters.getParameters();
Object[] parameters = this.info.getParameters();
Object result = reflectionMethod.invoke(resourceInstance, parameters);
if (Info.isOldComponent(method.getResource())) {
if (result == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
/***
*
* Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. 2. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution. 3. Neither the name of the
* copyright holders nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package br.com.caelum.vraptor.vraptor2;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -19,7 +46,6 @@
import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.http.ParametersProvider;
import br.com.caelum.vraptor.interceptor.DogAlike;
import br.com.caelum.vraptor.resource.DefaultResource;
import br.com.caelum.vraptor.resource.DefaultResourceMethod;
Expand All @@ -28,31 +54,27 @@
public class ExecuteAndViewInterceptorTest {

private Mockery mockery;
private ParametersProvider provider;
private MethodInfo info;
private InterceptorStack stack;
private MethodInfo parameters;

@Before
public void setup() throws NoSuchMethodException {
this.mockery = new Mockery();
this.provider = mockery.mock(ParametersProvider.class);
this.info = mockery.mock(MethodInfo.class);
this.stack = mockery.mock(InterceptorStack.class);
this.parameters = mockery.mock(MethodInfo.class);
}

@Test
public void shouldInvokeTheMethodAndNotProceedWithInterceptorStack() throws SecurityException,
NoSuchMethodException, IOException, InterceptionException {
ExecuteAndViewInterceptor interceptor = new ExecuteAndViewInterceptor(info, parameters);
ExecuteAndViewInterceptor interceptor = new ExecuteAndViewInterceptor(info);
final ResourceMethod method = new DefaultResourceMethod(new DefaultResource(DogAlike.class), DogAlike.class.getMethod("bark"));
final DogAlike auau = mockery.mock(DogAlike.class);
mockery.checking(new Expectations() {
{
one(auau).bark();
one(stack).next(method, auau);
one(parameters).getParameters(); will(returnValue(new Object[]{}));
one(info).getParameters(); will(returnValue(new Object[]{}));
}
});
interceptor.intercept(stack, method, auau);
Expand All @@ -62,15 +84,15 @@ public void shouldInvokeTheMethodAndNotProceedWithInterceptorStack() throws Secu
@Test
public void shouldThrowMethodExceptionIfThereIsAnInvocationException() throws IOException, SecurityException,
NoSuchMethodException {
ExecuteAndViewInterceptor interceptor = new ExecuteAndViewInterceptor(info, parameters);
ExecuteAndViewInterceptor interceptor = new ExecuteAndViewInterceptor(info);
ResourceMethod method = new DefaultResourceMethod(new DefaultResource(DogAlike.class), DogAlike.class.getMethod("bark"));
final DogAlike auau = mockery.mock(DogAlike.class);
final RuntimeException exception = new RuntimeException();
mockery.checking(new Expectations() {
{
one(auau).bark();
will(throwException(exception));
one(parameters).getParameters(); will(returnValue(new Object[]{}));
one(info).getParameters(); will(returnValue(new Object[]{}));
}
});
try {
Expand All @@ -84,14 +106,14 @@ public void shouldThrowMethodExceptionIfThereIsAnInvocationException() throws IO

@Test
public void shouldUseTheProvidedArguments() throws SecurityException, NoSuchMethodException, InterceptionException, IOException {
ExecuteAndViewInterceptor interceptor = new ExecuteAndViewInterceptor(info, parameters);
ExecuteAndViewInterceptor interceptor = new ExecuteAndViewInterceptor(info);
final ResourceMethod method = new DefaultResourceMethod(new DefaultResource(DogAlike.class), DogAlike.class.getMethod("bark", int.class));
final DogAlike auau = mockery.mock(DogAlike.class);
mockery.checking(new Expectations() {
{
one(auau).bark(3);
one(stack).next(method, auau);
one(parameters).getParameters(); will(returnValue(new Object[]{3}));
one(info).getParameters(); will(returnValue(new Object[]{3}));
}
});
interceptor.intercept(stack, method, auau);
Expand All @@ -106,14 +128,14 @@ interface OldDog {

@Test
public void shouldForwardIfUsingAnOldComponent() throws SecurityException, NoSuchMethodException, InterceptionException, IOException, ServletException {
ExecuteAndViewInterceptor interceptor = new ExecuteAndViewInterceptor(info, parameters);
ExecuteAndViewInterceptor interceptor = new ExecuteAndViewInterceptor(info);
final ResourceMethod method = new DefaultResourceMethod(new DefaultResource(OldDog.class), OldDog.class.getMethod("bark"));
final OldDog auau = mockery.mock(OldDog.class);
mockery.checking(new Expectations() {
{
one(auau).bark();
one(stack).next(method, auau);
one(parameters).getParameters(); will(returnValue(new Object[]{}));
one(info).getParameters(); will(returnValue(new Object[]{}));
}
});
interceptor.intercept(stack, method, auau);
Expand All @@ -123,14 +145,14 @@ public void shouldForwardIfUsingAnOldComponent() throws SecurityException, NoSuc

@Test
public void shouldForwardWithResultIfUsingAnOldComponent() throws SecurityException, NoSuchMethodException, InterceptionException, IOException, ServletException {
ExecuteAndViewInterceptor interceptor = new ExecuteAndViewInterceptor(info, parameters);
ExecuteAndViewInterceptor interceptor = new ExecuteAndViewInterceptor(info);
final ResourceMethod method = new DefaultResourceMethod(new DefaultResource(OldDog.class), OldDog.class.getMethod("barkResponse"));
final OldDog auau = mockery.mock(OldDog.class);
mockery.checking(new Expectations() {
{
one(auau).barkResponse(); will(returnValue("response"));
one(stack).next(method, auau);
one(parameters).getParameters(); will(returnValue(new Object[]{}));
one(info).getParameters(); will(returnValue(new Object[]{}));
}
});
interceptor.intercept(stack, method, auau);
Expand Down

0 comments on commit 937c6e7

Please sign in to comment.