Skip to content

Commit

Permalink
populating convertable parameters. Closes caelumgh-347
Browse files Browse the repository at this point in the history
  • Loading branch information
lucascs committed Mar 15, 2011
1 parent 379d2c6 commit 6c094a4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,11 @@ private Object createParameter(Parameter param, Map<String, String[]> requestNam
if (requestNames.isEmpty()) {
return Defaults.defaultValue(param.actualType());
}

if (requestNames.containsKey(param.name)) {
String[] values = requestNames.get(param.name);
try {
return createSimpleParameter(param, values, bundle);
} catch(ConversionError ex) {
errors.add(new ValidationMessage(ex.getMessage(), param.name));
return null;
}
Object root = createRoot(param, requestNames, bundle, errors);
if (root == null) {
return null;
}

OgnlContext context = createOgnlContextFor(param, bundle);
OgnlContext context = createOgnlContextFor(param, root, bundle);
for (Entry<String, String[]> parameter : requestNames.entrySet()) {
String key = parameter.getKey().replaceFirst("^" + param.name + "\\.?", "");
String[] values = parameter.getValue();
Expand All @@ -151,6 +144,25 @@ private Object createParameter(Parameter param, Map<String, String[]> requestNam
return context.getRoot();
}

private Object createRoot(Parameter param, Map<String, String[]> requestNames, ResourceBundle bundle,
List<Message> errors) {
if (requestNames.containsKey(param.name)) {
String[] values = requestNames.get(param.name);
try {
return createSimpleParameter(param, values, bundle);
} catch(ConversionError ex) {
errors.add(new ValidationMessage(ex.getMessage(), param.name));
return null;
}
}

try {
return new GenericNullHandler(removal).instantiate(param.actualType());
} catch (Exception ex) {
throw new InvalidParameterException("unable to instantiate type " + param.type, ex);
}
}

private void setProperty(OgnlContext context, String key, String[] values, List<Message> errors) {
try {
logger.debug("Applying {} with {}",key, values);
Expand Down Expand Up @@ -178,13 +190,9 @@ private void setProperty(OgnlContext context, String key, String[] values, List<
}
}

private OgnlContext createOgnlContextFor(Parameter param, ResourceBundle bundle) {
OgnlContext context;
try {
context = createOgnlContext(new GenericNullHandler(removal).instantiate(param.actualType()));
} catch (Exception ex) {
throw new InvalidParameterException("unable to instantiate type " + param.type, ex);
}
private OgnlContext createOgnlContextFor(Parameter param, Object root, ResourceBundle bundle) {
OgnlContext context = createOgnlContext(root);

context.setTraceEvaluations(true);
context.put("rootType", param.type);
context.put("removal", removal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
Expand All @@ -56,6 +57,8 @@
import br.com.caelum.vraptor.validator.DefaultValidationException;
import br.com.caelum.vraptor.validator.Message;

import com.google.common.collect.ImmutableMap;

public class OgnlParametersProviderTest {

private @Mock Converters converters;
Expand All @@ -74,6 +77,7 @@ public class OgnlParametersProviderTest {
private @Mock HttpSession session;
private ResourceMethod list;
private ResourceMethod listOfObject;
private ResourceMethod abc;
private ResourceMethod string;
private ResourceMethod generic;
private ResourceMethod primitive;
Expand All @@ -97,6 +101,7 @@ public void setup() throws Exception {
array = DefaultResourceMethod.instanceFor(MyResource.class, MyResource.class.getDeclaredMethod("array", Long[].class));
list = DefaultResourceMethod.instanceFor(MyResource.class, MyResource.class.getDeclaredMethod("list", List.class));
listOfObject = DefaultResourceMethod.instanceFor(MyResource.class, MyResource.class.getDeclaredMethod("listOfObject", List.class));
abc = DefaultResourceMethod.instanceFor(MyResource.class, MyResource.class.getDeclaredMethod("abc", ABC.class));
simple = DefaultResourceMethod.instanceFor(MyResource.class, MyResource.class.getDeclaredMethod("simple", Long.class));
string = DefaultResourceMethod.instanceFor(MyResource.class, MyResource.class.getDeclaredMethod("string", String.class));
stringArray = DefaultResourceMethod.instanceFor(MyResource.class, MyResource.class.getDeclaredMethod("stringArray", String[].class));
Expand Down Expand Up @@ -288,6 +293,21 @@ public void returnsZeroForAPrimitiveWhenThereAreNoParameters() throws Exception
Long xyz = getParameters(primitive);
assertThat(xyz, is(0l));
}
@Test
public void continuesToFillObjectIfItIsConvertable() throws Exception {
when(parameters.getParameterNames()).thenReturn(Collections.enumeration(Arrays.asList("abc", "abc.x")));
when(parameters.getParameterMap()).thenReturn(ImmutableMap.of("abc", new String[] {""}, "abc.x", new String[] {"3"}));
when(nameProvider.parameterNamesFor(any(Method.class))).thenReturn(new String[]{"abc"});

when(converters.to(ABC.class)).thenReturn((Converter) new Converter<ABC>() {
public ABC convert(String value, Class<? extends ABC> type, ResourceBundle bundle) {
return new ABC();
}
});

ABC returned = getParameters(abc);
assertThat(returned.x, is(3l));
}

private void thereAreNoParameters() {
when(parameters.getParameterNames()).thenReturn(Collections.enumeration(Collections.<String>emptySet()));
Expand Down Expand Up @@ -325,6 +345,8 @@ void list(List<Long> abc) {
}
void listOfObject(List<ABC> abc) {
}
void abc(ABC abc) {
}
void simple(Long xyz) {
}
void string(String abc) {
Expand Down

0 comments on commit 6c094a4

Please sign in to comment.