01: package org.vraptor.introspector;
02:
03: import java.lang.reflect.Field;
04:
05: import junit.framework.TestCase;
06:
07: import org.vraptor.LogicRequest;
08: import org.vraptor.annotations.Conversion;
09: import org.vraptor.component.FieldAnnotation;
10: import org.vraptor.converter.ConversionException;
11: import org.vraptor.converter.Converter;
12:
13: /**
14: * Tests for the read parameter class.
15: *
16: * @author Guilherme Silveira
17: */
18: public class FieldParameterTest extends TestCase {
19:
20: public class OverridenStringConverter implements Converter {
21:
22: public Object convert(String value, Class<?> type,
23: LogicRequest context) throws ConversionException {
24: return String.format("{%s}", value);
25: }
26:
27: public Class<?>[] getSupportedTypes() {
28: return new Class[] { String.class };
29: }
30:
31: }
32:
33: @org.vraptor.annotations.Parameter
34: @SuppressWarnings("unused")
35: private String test1;
36:
37: @org.vraptor.annotations.Parameter(key="test",create=true)
38: @Conversion(OverridenStringConverter.class)
39: @SuppressWarnings("unused")
40: private String test2;
41:
42: private FieldParameter param = new FieldParameter(getField("test1"));
43:
44: private FieldParameter param2 = new FieldParameter(
45: getField("test2"));
46:
47: public void testKey() {
48: assertEquals("test1", param.getKey());
49: assertEquals("test", param2.getKey());
50: }
51:
52: private FieldAnnotation<org.vraptor.annotations.Parameter> getField(
53: String name) {
54: Field f = null;
55: try {
56: f = FieldParameterTest.class.getDeclaredField(name);
57: } catch (SecurityException e) {
58: e.printStackTrace();
59: } catch (NoSuchFieldException e) {
60: e.printStackTrace();
61: }
62: return new FieldAnnotation<org.vraptor.annotations.Parameter>(
63: f
64: .getAnnotation(org.vraptor.annotations.Parameter.class),
65: f);
66: }
67:
68: public void testShouldInstantiateInternal() {
69: assertFalse(param.mightCreate());
70: }
71:
72: public void testShouldntInstantiateInternal() {
73: assertTrue(param2.mightCreate());
74: }
75:
76: public void testNoOverridenConverter() {
77: assertNull(param.getOverridenConverter());
78: }
79:
80: public void testOverridenConverter() {
81: assertEquals(OverridenStringConverter.class, param2
82: .getOverridenConverter());
83: }
84:
85: }
|