001: package org.vraptor.component;
002:
003: import java.lang.reflect.Method;
004: import java.util.Date;
005: import java.util.List;
006:
007: import org.vraptor.AbstractTest;
008: import org.vraptor.annotations.Logic;
009: import org.vraptor.annotations.Parameter;
010:
011: public class DefaultParameterInfoProviderTest extends AbstractTest {
012:
013: private DefaultParameterInfoProvider provider;
014:
015: protected void setUp() throws Exception {
016: super .setUp();
017: provider = new DefaultParameterInfoProvider();
018: }
019:
020: public void testReadsMoreThanOneParameter()
021: throws InvalidComponentException {
022: List<MethodParameter> params = provider.provideFor(loadMethod(
023: Params.class, "twoParams"));
024: assertEquals(2, params.size());
025: }
026:
027: public void testReadsNoParameter() throws InvalidComponentException {
028: List<MethodParameter> params = provider.provideFor(loadMethod(
029: Params.class, "noParams"));
030: assertEquals(0, params.size());
031: }
032:
033: public void testReadsATypeParam() throws InvalidComponentException {
034: List<MethodParameter> params = provider.provideFor(loadMethod(
035: Params.class, "twoParams"));
036: assertEquals("date", params.get(0).getKey());
037: }
038:
039: public void testReadsAPrimitiveParam()
040: throws InvalidComponentException {
041: List<MethodParameter> params = provider.provideFor(loadMethod(
042: Params.class, "primitiveParam"));
043: assertEquals("int", params.get(0).getKey());
044: }
045:
046: private Method loadMethod(Class<?> type, String methodName)
047: throws InvalidComponentException {
048: for (Method method : type.getMethods()) {
049: if (method.getName().equals(methodName)) {
050: return method;
051: }
052: }
053: fail("Method " + methodName + " not found.");
054: return null;
055: }
056:
057: public void testAnnotatedParam() throws InvalidComponentException {
058: List<MethodParameter> params = provider.provideFor(loadMethod(
059: Params.class, "annotatedParam"));
060: assertEquals("_date_", params.get(0).getKey());
061: }
062:
063: public void testAnnotatedLogicWithParameterNames()
064: throws InvalidComponentException {
065: List<MethodParameter> params = provider.provideFor(loadMethod(
066: Params.class, "annotatedWithLogic"));
067: assertEquals("_date_", params.get(0).getKey());
068: assertEquals("_other_date_", params.get(1).getKey());
069: }
070:
071: public void testAnnotatedLogicWithParameterNames2()
072: throws InvalidComponentException {
073: List<MethodParameter> params = provider.provideFor(loadMethod(
074: Params.class, "annotatedWithLogicAndParameter"));
075: assertEquals("_date_", params.get(0).getKey());
076: assertEquals("_other_date_", params.get(1).getKey());
077: }
078:
079: public void testAnnotatedLogicWithParameterNamesButWrongNumberOfNames()
080: throws InvalidComponentException {
081: try {
082: provider.provideFor(loadMethod(Params.class,
083: "annotatedWithLogicButWrongNumberOfNames"));
084: fail();
085: } catch (IllegalArgumentException e) {
086:
087: }
088: }
089:
090: public static class Params {
091:
092: public void noParams() {
093: }
094:
095: public void primitiveParam(int p) {
096: }
097:
098: public void typeParam(Date d) {
099: }
100:
101: public void twoParams(Date d, String p) {
102: }
103:
104: // parameter and useless annotation
105: public void annotatedParam(@Parameter(key="_date_")
106: @Deprecated
107: Date d) {
108:
109: }
110:
111: @Logic(parameters={"_date_","_other_date_"})
112: public void annotatedWithLogic(Date d, Date d2) {
113:
114: }
115:
116: @Logic(parameters={"_date_","_other_date_"})
117: public void annotatedWithLogicAndParameter(Date d, @Parameter
118: Date d2) {
119:
120: }
121:
122: @Logic(parameters={"_date_","_other_date_"})
123: public void annotatedWithLogicAndParameterOverriding(Date d,
124: @Parameter(key="x")
125: Date d2) {
126:
127: }
128:
129: @Logic(parameters={"_date_","_other_date_","_non_existant_"})
130: public void annotatedWithLogicButWrongNumberOfNames(Date d,
131: Date d2) {
132:
133: }
134: }
135:
136: }
|