001: package org.vraptor.reflection;
002:
003: import java.lang.reflect.Field;
004: import java.util.ArrayList;
005: import java.util.Arrays;
006: import java.util.List;
007:
008: import org.vraptor.AbstractTest;
009: import org.vraptor.annotations.Parameter;
010: import org.vraptor.component.FieldAnnotation;
011: import org.vraptor.converter.ConversionException;
012: import org.vraptor.converter.ConverterManager;
013: import org.vraptor.converter.SimpleConverterManager;
014: import org.vraptor.converter.basic.StringConverter;
015: import org.vraptor.introspector.FieldParameter;
016:
017: public class JPathExecutorTest extends AbstractTest {
018:
019: private static final String VALUE = "TEST";
020:
021: private static final String OTHER_VALUE = "TEST_2";
022:
023: private JPathExecutor getExecutor(Object obj) {
024: ConverterManager manager = new SimpleConverterManager();
025: manager.register(new StringConverter());
026: return new JPathExecutor(manager, createLogicRequest(),
027: new Object[0], obj);
028: }
029:
030: public void testSimpleNoCreateSetter() throws SettingException,
031: ConversionException {
032: ParentClass object = new ParentClass();
033: getExecutor(object).set(path("mainChild.name"), VALUE,
034: new String[] { VALUE }, getField(object, "mainChild"));
035: assertEquals(VALUE, object.getMainChild().getName());
036: }
037:
038: /**
039: * Tests a setter with create=true
040: *
041: * @throws SettingException
042: */
043: public void testSimpleCreateSetter() throws SettingException,
044: ConversionException {
045: ParentClass object = new ParentClass();
046: getExecutor(object).set(path("lazyChild.name"), VALUE,
047: new String[] { VALUE }, getField(object, "lazyChild"));
048: assertEquals(VALUE, object.lazyChild.getName());
049: }
050:
051: /**
052: * Test array position with no instantiation
053: *
054: * @throws SettingException
055: * setter exception
056: */
057: public void testArrayNoCreateSetter() throws SettingException,
058: ConversionException {
059: ParentClass object = new ParentClass();
060: getExecutor(object).set(path("childArray[1].name"), VALUE,
061: new String[] { VALUE }, getField(object, "childArray"));
062: assertEquals(VALUE, object.childArray[1].getName());
063: }
064:
065: public void testArrayInvalidIndexSetter()
066: throws ConversionException {
067: ParentClass object = new ParentClass();
068: try {
069: getExecutor(object).set(path("childArray[3].name"), VALUE,
070: new String[] { VALUE },
071: getField(object, "childArray"));
072: fail();
073: } catch (SettingException e) {
074: // ok
075: }
076: }
077:
078: public void testTooFastArrayCreateSetter()
079: throws ConversionException {
080: ParentClass object = new ParentClass();
081: try {
082: getExecutor(object).set(path("lazyChildArray[1].name"),
083: VALUE, new String[] { VALUE },
084: getField(object, "lazyChildArray"));
085: fail();
086: } catch (SettingException e) {
087: // ok
088: }
089: }
090:
091: public void testArrayCreateSetter() throws SettingException,
092: ConversionException {
093: ParentClass object = new ParentClass();
094: getExecutor(object).set(path("lazyChildArray[0].name"), VALUE,
095: new String[] { VALUE },
096: getField(object, "lazyChildArray"));
097: assertEquals(VALUE, object.lazyChildArray[0].getName());
098: }
099:
100: public void testSequentialArrayCreateSetter()
101: throws SettingException, ConversionException {
102: ParentClass object = new ParentClass();
103: getExecutor(object).set(path("lazyChildArray[0].name"), VALUE,
104: new String[] { VALUE },
105: getField(object, "lazyChildArray"));
106: getExecutor(object).set(path("lazyChildArray[1].name"), VALUE,
107: new String[] { VALUE },
108: getField(object, "lazyChildArray"));
109: assertEquals(VALUE, object.lazyChildArray[1].getName());
110: }
111:
112: public void testListNoCreateSetter() throws SettingException,
113: ConversionException {
114: ParentClass object = new ParentClass();
115: getExecutor(object).set(path("childList[1].name"), VALUE,
116: new String[] { VALUE }, getField(object, "childList"));
117: assertEquals(VALUE, object.childList.get(1).getName());
118: }
119:
120: public void testListInvalidIndexSetter() throws ConversionException {
121: ParentClass object = new ParentClass();
122: try {
123: getExecutor(object).set(path("childList[3].name"), VALUE,
124: new String[] { VALUE },
125: getField(object, "childList"));
126: fail();
127: } catch (SettingException e) {
128: // ok
129: }
130: }
131:
132: public void testListCreate() throws SettingException,
133: ConversionException {
134: ParentClass object = new ParentClass();
135: getExecutor(object).set(path("lazyChildList[0].name"), VALUE,
136: new String[] { VALUE },
137: getField(object, "lazyChildList"));
138: assertEquals(VALUE, object.lazyChildList.get(0).getName());
139: }
140:
141: public void testMissedParameterListCreate()
142: throws ConversionException {
143: ParentClass object = new ParentClass();
144: try {
145: getExecutor(object).set(path("lazyChildList[1].name"),
146: VALUE, new String[] { VALUE },
147: getField(object, "lazyChildList"));
148: } catch (SettingException e) {
149: }
150: }
151:
152: public void testSetNoCreateSetter() throws SettingException,
153: ConversionException {
154: ParentClass object = new ParentClass();
155: getExecutor(object).set(path("childSet[0].name"), VALUE,
156: new String[] { VALUE }, getField(object, "childSet"));
157: List<ChildClass> list = new ArrayList<ChildClass>(
158: object.childSet);
159: assertEquals(VALUE, list.get(0).getName());
160: }
161:
162: public void testMissedParameterSetCreate()
163: throws ConversionException {
164: ParentClass object = new ParentClass();
165: try {
166: getExecutor(object).set(path("lazyChildSet[1].name"),
167: VALUE, new String[] { VALUE },
168: getField(object, "lazyChildSet"));
169: fail();
170: } catch (SettingException e) {
171: }
172: }
173:
174: public void testSetInvalidIndexSetter() throws ConversionException {
175: ParentClass object = new ParentClass();
176: try {
177: getExecutor(object).set(path("childSet[3].name"), VALUE,
178: new String[] { VALUE },
179: getField(object, "childSet"));
180: fail();
181: } catch (SettingException e) {
182: // ok
183: }
184: }
185:
186: public void testSetCreateSetter() throws SettingException,
187: ConversionException {
188: ParentClass object = new ParentClass();
189: getExecutor(object).set(path("lazyChildSet[0].name"), VALUE,
190: new String[] { VALUE },
191: getField(object, "lazyChildSet"));
192: List<ChildClass> list = new ArrayList<ChildClass>(
193: object.lazyChildSet);
194: assertEquals(VALUE, list.get(0).getName());
195: }
196:
197: public void testSetCreateSetterToVerifySetBeforeAddingToTheCollection()
198: throws SettingException, ConversionException {
199: ParentClass object = new ParentClass();
200: // failing! Here we are adding two different objects to the Set
201: // TODO: wrong test, must add the SAME object twice to the Set!
202: getExecutor(object).set(path("lazyChildSet[0].name"), VALUE,
203: new String[] { VALUE },
204: getField(object, "lazyChildSet"));
205: getExecutor(object).set(path("lazyChildSet[1].name"), VALUE,
206: new String[] { VALUE },
207: getField(object, "lazyChildSet"));
208: // assertEquals(1, object.lazyChildSet.size());
209: }
210:
211: public void testSetCreateSetterToVerifySetBeforeAddingToTheCollectionDifferentObjects()
212: throws SettingException, ConversionException {
213: ParentClass object = new ParentClass();
214: // failing!
215: getExecutor(object).set(path("lazyChildSet[0].name"), VALUE,
216: new String[] { VALUE },
217: getField(object, "lazyChildSet"));
218: getExecutor(object).set(path("lazyChildSet[1].name"), VALUE,
219: new String[] { OTHER_VALUE },
220: getField(object, "lazyChildSet"));
221: assertEquals(2, object.lazyChildSet.size());
222: }
223:
224: public void testSequentialSetCreateSetter()
225: throws SettingException, ConversionException {
226: ParentClass object = new ParentClass();
227: getExecutor(object).set(path("lazyChildSet[0].name"), VALUE,
228: new String[] { VALUE },
229: getField(object, "lazyChildSet"));
230: getExecutor(object).set(path("lazyChildSet[1].name"), VALUE,
231: new String[] { VALUE },
232: getField(object, "lazyChildSet"));
233: getExecutor(object).set(path("lazyChildSet[2].name"),
234: VALUE + "2", new String[] { VALUE + "2" },
235: getField(object, "lazyChildSet"));
236: List<ChildClass> list = new ArrayList<ChildClass>(
237: object.lazyChildSet);
238: assertEquals(VALUE + "2", list.get(2).getName());
239: }
240:
241: public void testMissingParameterSetCreateSetter()
242: throws ConversionException {
243: ParentClass object = new ParentClass();
244: try {
245: getExecutor(object).set(path("lazyChildSet[3].name"),
246: VALUE, new String[] { VALUE },
247: getField(object, "lazyChildSet"));
248: fail();
249: } catch (SettingException e) {
250: // ok
251: }
252: }
253:
254: public void testSimpleStringArraySet() throws SettingException,
255: ConversionException {
256: ParentClass object = new ParentClass();
257: getExecutor(object).set(path("values"), VALUE,
258: new String[] { VALUE, VALUE },
259: getField(object, "values"));
260: assertTrue(Arrays.deepEquals(object.values, new String[] {
261: VALUE, VALUE }));
262: }
263:
264: public void testInternalStringArraySet() throws SettingException,
265: ConversionException {
266: ParentClass object = new ParentClass();
267: getExecutor(object).set(path("child.values"), VALUE,
268: new String[] { VALUE, VALUE },
269: getField(object, "child"));
270: assertTrue(Arrays.deepEquals(new String[] { VALUE, VALUE },
271: object.child.getValues()));
272: }
273:
274: public void testInternalNoCreateArraySet() throws SettingException,
275: ConversionException {
276: ParentClass object = new ParentClass();
277: getExecutor(object).set(path("mother.childs[1].name"), VALUE,
278: new String[] { VALUE }, getField(object, "mother"));
279: assertEquals(VALUE, object.mother.getChilds()[1].getName());
280: }
281:
282: public void testInternalCreateArraySet() throws SettingException,
283: ConversionException {
284: ParentClass object = new ParentClass();
285: getExecutor(object).set(path("lazyMother.childs[0].name"),
286: VALUE, new String[] { VALUE },
287: getField(object, "lazyMother"));
288: assertEquals(VALUE, object.lazyMother.getChilds()[0].getName());
289: }
290:
291: public void testCorrectlySetsListFirstPositionIndexTest()
292: throws SettingException, ConversionException {
293: class IntegerListTest {
294: @Parameter(create=true)
295: List<Integer> list = new ArrayList<Integer>();
296: }
297: IntegerListTest t = new IntegerListTest();
298: getExecutor(t).set(path("list[0]"), "2", new String[] { "2" },
299: getField(t, "list"));
300: assertEquals(1, t.list.size());
301: }
302:
303: public void testArrayIndex() throws SettingException,
304: ConversionException {
305: class ListTest {
306: @Parameter(create=true)
307: double[] val;
308: }
309: ListTest t = new ListTest();
310: getExecutor(t).set(path("val[0]"), "2", new String[] { "2" },
311: getField(t, "val"));
312: assertEquals(1, t.val.length);
313: }
314:
315: public void testSequencialArrayIndex() throws SettingException,
316: ConversionException {
317: class PolTest {
318: @Parameter(create=true)
319: double[] c;
320: }
321: PolTest t = new PolTest();
322: getExecutor(t).set(path("c[0]"), "2", new String[] { "2" },
323: getField(t, "c"));
324: getExecutor(t).set(path("c[1]"), "-7", new String[] { "-7" },
325: getField(t, "c"));
326: getExecutor(t).set(path("c[2]"), "-7", new String[] { "-7" },
327: getField(t, "c"));
328: getExecutor(t).set(path("c[3]"), "2", new String[] { "2" },
329: getField(t, "c"));
330: assertEquals(t.c[0], 2.0);
331: assertEquals(t.c[1], -7.0);
332: assertEquals(t.c[2], -7.0);
333: assertEquals(t.c[3], 2.0);
334: }
335:
336: private FieldParameter getField(Object obj, String str) {
337: Field[] fields = obj.getClass().getDeclaredFields();
338: for (Field field : fields) {
339: if (field.getName().equals(str)) {
340: field.setAccessible(true);
341: return new FieldParameter(
342: new FieldAnnotation<Parameter>(field
343: .getAnnotation(Parameter.class), field));
344: }
345: }
346: throw new RuntimeException("Unable to find field");
347: }
348:
349: private String[] path(String pathString) {
350: return pathString.split("[\\.\\[\\]]+");
351: }
352:
353: public static class ParameterTest {
354: @Parameter
355: private User user = new User();
356: }
357:
358: public static class User {
359: private String name;
360:
361: public String getName() {
362: return name;
363: }
364:
365: public void setName(String name) {
366: this .name = name;
367: }
368: }
369:
370: public void testUsesDepthParameter() throws SettingException,
371: ConversionException {
372: ParameterTest object = new ParameterTest();
373: getExecutor(object).set(path("user.name"), "guilherme",
374: new String[] { "guilherme" }, getField(object, "user"));
375: assertEquals("guilherme", object.user.name);
376: }
377:
378: }
|