001: package org.vraptor.component;
002:
003: import java.util.ArrayList;
004:
005: import org.vraptor.AbstractTest;
006: import org.vraptor.LogicException;
007: import org.vraptor.annotations.Logic;
008: import org.vraptor.annotations.Viewless;
009: import org.vraptor.validator.UnstableValidationException;
010:
011: public class DefaultLogicMethodTest extends AbstractTest {
012:
013: public void testGetsItsName() throws SecurityException,
014: NoSuchMethodException, LogicException {
015: LogicMethod method = new DefaultLogicMethod(registry
016: .getValidationErrorsFactory(), "string",
017: LogicTest.class.getMethod("execute"), null,
018: new ArrayList<MethodParameter>());
019: assertEquals("string", method.getName());
020: }
021:
022: public void testReturnsAnOkString() throws SecurityException,
023: NoSuchMethodException, LogicException {
024: LogicTest test = new LogicTest();
025: LogicMethod method = new DefaultLogicMethod(registry
026: .getValidationErrorsFactory(), "string",
027: LogicTest.class.getMethod("execute"), null,
028: new ArrayList<MethodParameter>());
029: String value = method.execute(test, createLogicRequest(),
030: new Object[0]);
031: assertEquals("ok", value);
032: }
033:
034: public void testReturnsVoidMeansOk() throws SecurityException,
035: NoSuchMethodException, LogicException {
036: LogicTest test = new LogicTest();
037: LogicMethod method = new DefaultLogicMethod(registry
038: .getValidationErrorsFactory(), "void", LogicTest.class
039: .getMethod("executeVoid"), null,
040: new ArrayList<MethodParameter>());
041: Object value = method.execute(test, createLogicRequest(),
042: new Object[0]);
043: assertEquals("ok", value);
044: }
045:
046: public void testFindsAViewlessMethod() throws SecurityException,
047: NoSuchMethodException, LogicException {
048: LogicMethod method = new DefaultLogicMethod(registry
049: .getValidationErrorsFactory(), "void", LogicTest.class
050: .getMethod("viewless"), null,
051: new ArrayList<MethodParameter>());
052: assertFalse(method.shouldRedirect());
053: }
054:
055: public void testFindsAMethodWithViewRedirection()
056: throws SecurityException, NoSuchMethodException,
057: LogicException {
058: LogicMethod method = new DefaultLogicMethod(registry
059: .getValidationErrorsFactory(), "void", LogicTest.class
060: .getMethod("executeVoid"), null,
061: new ArrayList<MethodParameter>());
062: assertTrue(method.shouldRedirect());
063: }
064:
065: public void testException() throws SecurityException,
066: NoSuchMethodException, LogicException {
067: LogicTest test = new LogicTest();
068: LogicMethod method = new DefaultLogicMethod(registry
069: .getValidationErrorsFactory(), "problem",
070: LogicTest.class.getMethod("executeProblem"), null,
071: new ArrayList<MethodParameter>());
072: try {
073: method.execute(test, createLogicRequest(), new Object[0]);
074: fail();
075: } catch (LogicException e) {
076: // ok
077: }
078: }
079:
080: public void testExecutesValidationWithException()
081: throws SecurityException, NoSuchMethodException,
082: LogicException {
083: LogicTest test = new LogicTest();
084: DefaultLogicMethod method = new DefaultLogicMethod(registry
085: .getValidationErrorsFactory(), "withValidation",
086: LogicTest.class.getMethod("withValidation"),
087: LogicTest.class.getMethod("validateWithValidation"),
088: new ArrayList<MethodParameter>());
089: try {
090: method.validate(test, createLogicRequest(), null,
091: new Object[] {});
092: fail();
093: } catch (UnstableValidationException e) {
094: // ok
095: }
096: }
097:
098: public void testExecutesMethodWithException() throws Throwable {
099: LogicTest test = new LogicTest();
100: LogicMethod method = new DefaultLogicMethod(registry
101: .getValidationErrorsFactory(), "problem",
102: LogicTest.class.getMethod("executeProblem"), null,
103: new ArrayList<MethodParameter>());
104: try {
105: method.execute(test, createLogicRequest(), new Object[0]);
106: fail();
107: } catch (LogicException e) {
108: assertEquals(e.getCause().getClass(),
109: ArithmeticException.class);
110: }
111: }
112:
113: public class LogicTest {
114: @Logic("string")
115: public String execute() {
116: return "ok";
117: }
118:
119: @Viewless
120: public void viewless() {
121: }
122:
123: @Logic("void")
124: public void executeVoid() {
125: }
126:
127: @Logic("problem")
128: public void executeProblem() {
129: @SuppressWarnings("unused")
130: int i = 1 / 0;
131: }
132:
133: public void withValidation() {
134:
135: }
136:
137: public void validateWithValidation() {
138: throw new RuntimeException();
139: }
140: }
141:
142: }
|