Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes caelum/vraptor#581. Only instantiate objects with same type #355

Merged
merged 3 commits into from
Feb 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public Object instantiate(Target<?> target, Parameters params) {

@Override
public boolean isAbleToInstantiate(Target<?> target) {
return request.getAttribute(target.getName()) != null;
Object value = request.getAttribute(target.getName());
return value != null && target.getClassType().isInstance(value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
package br.com.caelum.vraptor.http.iogi;

import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage;
import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
Expand All @@ -43,18 +45,22 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.junit.Test;

import br.com.caelum.iogi.parameters.Parameter;
import br.com.caelum.iogi.parameters.Parameters;
import br.com.caelum.iogi.reflection.Target;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.http.ParametersProvider;
import br.com.caelum.vraptor.http.ParametersProviderTest;
import br.com.caelum.vraptor.validator.Message;

public class IogiParametersProviderTest extends ParametersProviderTest {

Expand Down Expand Up @@ -166,11 +172,29 @@ public void isCapableOfDealingWithSetsOfObjects() throws Exception {
assertThat(abc.iterator().next().getX(), is(1l));
}

@Test
public void shouldInjectOnlyAttributesWithSameType() throws Exception {
Result result = mock(Result.class);
when(request.getAttribute("result")).thenReturn(result);
when(request.getParameterMap()).thenReturn(singletonMap("result", new String[] { "buggy" }));

ControllerMethod method = DefaultControllerMethod.instanceFor(OtherResource.class,
OtherResource.class.getDeclaredMethod("logic", String.class));

List<Message> errors = new ArrayList<Message>();
Object[] out = provider.getParametersFor(method, errors);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the test because I think that we need to check if IOGI got the right parameter too. @lucascs right?


assertThat(out[0], is(not((Object) result)));
assertThat(out[0], is((Object) "buggy"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the casting is necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because assertThat expects the same type of out[], not subtypes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, my mistake =) :shipit:

}

//----------

class OtherResource {
void logic(NeedsMyResource param) {
}
void logic(String result) {
}
}

static class NeedsMyResource {
Expand Down