001: package org.vraptor.component;
002:
003: import java.util.ArrayList;
004: import java.util.Map;
005:
006: import org.jmock.Mock;
007: import org.vraptor.AbstractTest;
008: import org.vraptor.LogicException;
009: import org.vraptor.annotations.Logic;
010: import org.vraptor.validator.ValidationErrors;
011:
012: public class DefaultLogicMethodFactoryTest extends AbstractTest {
013:
014: private DefaultLogicMethodFactory factory;
015: private Mock provider;
016:
017: protected void setUp() throws Exception {
018: super .setUp();
019: provider = mock(ParameterInfoProvider.class);
020: this .factory = new DefaultLogicMethodFactory(registry
021: .getValidationErrorsFactory(),
022: (ParameterInfoProvider) provider.proxy());
023: }
024:
025: public static class Door {
026: int i;
027:
028: public void open() {
029: i = 1;
030: }
031:
032: public void close() {
033: i = 3;
034: }
035:
036: public void validateClose(ValidationErrors errors) {
037: i = 2;
038: }
039:
040: public void close(int val) {
041: i = val;
042: }
043:
044: public void validateClose(ValidationErrors errors, int val) {
045: i = -val;
046: }
047:
048: }
049:
050: public void testFindsMethodWithValidationMethod()
051: throws SecurityException, NoSuchMethodException,
052: LogicException, InvalidComponentException {
053: provider.expects(once()).method("provideFor").will(
054: returnValue(new ArrayList<MethodParameter>()));
055: DefaultLogicMethod method = factory.create("open", Door.class,
056: Door.class.getMethod("open"));
057: Door door = new Door();
058: method.validate(door, null, null, new Door[] {});
059: assertEquals(0, door.i);
060: }
061:
062: public void testFindsMethodWithValidationMethodReceivingParameters()
063: throws SecurityException, NoSuchMethodException,
064: LogicException, InvalidComponentException {
065: provider.expects(once()).method("provideFor").will(
066: returnValue(new ArrayList<MethodParameter>()));
067: DefaultLogicMethod method = factory.create("close", Door.class,
068: Door.class
069: .getMethod("close", new Class[] { int.class }));
070: Door door = new Door();
071: method.validate(door, null, null, new Object[] { 999 });
072: assertEquals(-999, door.i);
073: }
074:
075: public void testFindsMethodWithoutValidationMethod()
076: throws SecurityException, NoSuchMethodException,
077: LogicException, InvalidComponentException {
078: provider.expects(once()).method("provideFor").will(
079: returnValue(new ArrayList<MethodParameter>()));
080: DefaultLogicMethod method = factory.create("close", Door.class,
081: Door.class.getMethod("close"));
082: Door door = new Door();
083: method.validate(door, null, null, new Door[] {});
084: assertEquals(2, door.i);
085: }
086:
087: public void testTriesToLoadAnInvalidStaticLogic()
088: throws InvalidComponentException {
089: assertEquals(0, factory.loadLogics(InvalidStaticLogic.class)
090: .size());
091: }
092:
093: public void testIgnoresNonPublicLogic()
094: throws InvalidComponentException {
095: assertEquals(0, factory.loadLogics(NonPublicLogic.class).size());
096: }
097:
098: public void testAcceptsMethodsWithArguments()
099: throws InvalidComponentException {
100: provider.expects(once()).method("provideFor").will(
101: returnValue(new ArrayList<MethodParameter>()));
102: assertEquals(1, factory.loadLogics(LogicWithArgs.class).size());
103: }
104:
105: public static class LogicWithArgs {
106: public void close(int i) {
107: }
108: }
109:
110: public static class InvalidStaticLogic {
111: public static void close() {
112: }
113: }
114:
115: public static class NonPublicLogic {
116: protected void close() {
117: }
118: }
119:
120: public static class LogicWithDots {
121: @Logic("my.name")
122: public void execute() {
123:
124: }
125: }
126:
127: public static class RenamedLogic {
128: @Logic("_new_name_")
129: public void execute() {
130:
131: }
132: }
133:
134: public static class NotRenamedLogic {
135: @Logic(parameters="a")
136: public void execute(int a) {
137:
138: }
139: }
140:
141: public void testRejectsLogicsWithDots() throws SecurityException,
142: NoSuchMethodException {
143: try {
144: factory.create("a.b", LogicWithDots.class,
145: LogicWithDots.class.getMethod("execute"));
146: fail("Should reject logic name with dot.");
147: } catch (InvalidComponentException e) {
148: // ok
149: }
150: }
151:
152: public void testAnnotatedLogicRenaming()
153: throws InvalidComponentException {
154: provider.expects(once()).method("provideFor").will(
155: returnValue(new ArrayList<MethodParameter>()));
156: Map<String, DefaultLogicMethod> map = factory
157: .loadLogics(RenamedLogic.class);
158: assertEquals(1, map.size());
159: assertTrue(map.containsKey("_new_name_"));
160: assertTrue(!map.containsKey("execute"));
161: }
162:
163: public void testAnnotatedLogicNotRenamed()
164: throws InvalidComponentException {
165: provider.expects(once()).method("provideFor").will(
166: returnValue(new ArrayList<MethodParameter>()));
167: Map<String, DefaultLogicMethod> map = factory
168: .loadLogics(NotRenamedLogic.class);
169: assertEquals(1, map.size());
170: assertTrue(map.containsKey("execute"));
171: }
172:
173: }
|