001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.controller;
016:
017: import static org.testng.Assert.assertEquals;
018:
019: import java.util.Collection;
020:
021: import org.apache.struts.action.Action;
022: import org.strecks.action.navigable.NavigableController;
023: import org.strecks.controller.impl.AbstractControllerAction;
024: import org.strecks.controller.impl.ActionWithNonInterface;
025: import org.strecks.controller.impl.ActionWithNonStrutsControllerAction;
026: import org.strecks.controller.impl.ReadOnlyAction;
027: import org.strecks.controller.impl.ReadOnlyControllerAction;
028: import org.strecks.controller.impl.TestAction;
029: import org.strecks.exceptions.ApplicationConfigurationException;
030: import org.strecks.navigate.NavigationHolder;
031: import org.strecks.navigate.internal.impl.ActionWithNavigate;
032: import org.strecks.navigate.internal.impl.ActionWithNoNavigate;
033: import org.testng.Assert;
034: import org.testng.annotations.BeforeMethod;
035: import org.testng.annotations.Test;
036:
037: /**
038: * @author Phil Zoio
039: */
040: public class TestActionCreator {
041:
042: private ActionCreatorImpl actionCreator;
043:
044: @BeforeMethod
045: public void setUp() {
046: actionCreator = new ActionCreatorImpl();
047: }
048:
049: /**
050: * Coarse grained test on happy path. Creates controller action and actionBean which satisfy mutual depependency
051: * requirements
052: */
053: @Test
054: public void testActionCreate() throws Exception {
055:
056: Action createAction = actionCreator
057: .createAction(TestAction.class);
058:
059: assert createAction instanceof ControllerAction;
060:
061: ControllerAction controllerAction = (ControllerAction) createAction;
062: Object newActionBean = controllerAction.getBeanSource()
063: .createBean(null);
064:
065: assert newActionBean instanceof TestAction;
066:
067: }
068:
069: @Test
070: public void testCreateControllerAction() throws Exception {
071:
072: Action createAction = actionCreator
073: .createControllerAction(TestAction.class);
074:
075: assert createAction instanceof ControllerAction;
076:
077: ControllerAction controllerAction = (ControllerAction) createAction;
078: Object newActionBean = controllerAction.getBeanSource()
079: .createBean(null);
080:
081: assert newActionBean instanceof TestAction;
082:
083: assert createAction instanceof BaseControllerAction;
084: BaseControllerAction bc = (BaseControllerAction) createAction;
085: Assert.assertNotNull(bc.getInitMethod());
086:
087: }
088:
089: @Test
090: public void testGetActionInterfaceType() throws Exception {
091: Class actionInterfaceType = actionCreator
092: .getActionInterfaceType(ReadOnlyControllerAction.class,
093: null);
094: assertEquals(actionInterfaceType, ReadOnlyAction.class);
095: }
096:
097: @Test(expectedExceptions=ApplicationConfigurationException.class)
098: public void noActionInterfaceAnnotation() {
099: actionCreator.getActionInterfaceType(String.class,
100: TestAction.class);
101: }
102:
103: @Test(expectedExceptions=ApplicationConfigurationException.class)
104: public void actionInterfaceNotInterface() {
105: actionCreator.getActionInterfaceType(
106: ActionWithNonInterface.class, TestAction.class);
107: }
108:
109: @Test
110: public void instantiateControllerAction() {
111: Object instantiateControllerAction = actionCreator
112: .instantiateControllerAction(
113: ReadOnlyControllerAction.class,
114: TestAction.class);
115: // assert instantiateControllerAction instanceof ReadOnlyControllerAction;
116: System.out.println(instantiateControllerAction);
117: }
118:
119: @Test(expectedExceptions=ApplicationConfigurationException.class)
120: public void abstractControllerAction() {
121: actionCreator.instantiateControllerAction(
122: AbstractControllerAction.class, TestAction.class);
123: }
124:
125: @Test(expectedExceptions=ApplicationConfigurationException.class)
126: public void privateControllerAction() throws Exception {
127: actionCreator
128: .instantiateControllerAction(
129: Class
130: .forName("org.strecks.controller.impl.PrivateControllerAction"),
131: TestAction.class);
132: }
133:
134: @Test
135: public void testIsAssignable() {
136: actionCreator.checkIsAssignable(TestAction.class,
137: ReadOnlyAction.class, ReadOnlyControllerAction.class);
138: }
139:
140: @Test(expectedExceptions=ApplicationConfigurationException.class)
141: public void nonImplementingInterface() throws Exception {
142: actionCreator.checkIsAssignable(TestAction.class,
143: Collection.class, ReadOnlyControllerAction.class);
144: }
145:
146: @Test
147: public void testGetControllerClass() {
148: Class controllerClass = actionCreator
149: .getControllerClass(TestAction.class);
150: assertEquals(controllerClass, ReadOnlyControllerAction.class);
151: }
152:
153: @Test
154: public void testControllerNotStrutsAction() {
155: try {
156: actionCreator.getControllerClass(String.class);
157: } catch (ApplicationConfigurationException e) {
158: assertEquals(
159: e.getMessage(),
160: "java.lang.String is not a Struts Action subclass and does not have a org.strecks.controller.annotation.Controller annotation");
161: }
162: }
163:
164: @Test
165: public void testActionWithControllerNotStrutsAction() {
166: try {
167: actionCreator
168: .getControllerClass(ActionWithNonStrutsControllerAction.class);
169: } catch (ApplicationConfigurationException e) {
170: assertEquals(
171: e.getMessage(),
172: "org.strecks.controller.impl.ActionWithNonStrutsControllerAction is has a org.strecks.controller.annotation.Controller annotation "
173: + "which points to the class org.strecks.controller.impl.NonStrutsActionControllerAction which is not a Struts Action subclass");
174: }
175: }
176:
177: /**
178: * Tests creation with navigate mechanism
179: */
180: @Test
181: public void testCreateWithNavigate() throws Exception {
182:
183: NavigableControllerAction createAction = (NavigableControllerAction) actionCreator
184: .createAction(ActionWithNavigate.class);
185: assert null != createAction.getNavigationHolder();
186:
187: }
188:
189: /**
190: * Tests creation with navigate mechanism
191: */
192: @Test
193: public void testCreateWithNoNavigate() throws Exception {
194:
195: ControllerAction createAction = (ControllerAction) actionCreator
196: .createAction(ActionWithNoNavigate.class);
197: assert !(createAction instanceof NavigableControllerAction);
198:
199: }
200:
201: /**
202: * Tests reading the annotation extension handling mechanism
203: */
204: @Test
205: public void testReadControllerClassAnnotations() throws Exception {
206:
207: NavigableController controller = new NavigableController();
208: actionCreator.readControllerClassAnnotations(
209: ActionWithNavigate.class, controller);
210:
211: NavigationHolder navigationHolder = controller
212: .getNavigationHolder();
213: assert navigationHolder != null;
214:
215: }
216:
217: /**
218: * Tests reading the annotation extension handling mechanism
219: */
220: @Test
221: public void testNoAnnotations() throws Exception {
222:
223: try {
224: NavigableController controller = new NavigableController();
225: actionCreator.readControllerClassAnnotations(
226: ActionWithNoNavigate.class, controller);
227: } catch (ApplicationConfigurationException e) {
228: assertEquals(
229: e.getMessage(),
230: "No annotation using the @NavigationInfo annotation found. This is needed to determine the navigation for the action bean class org.strecks.navigate.internal.impl.ActionWithNoNavigate");
231: }
232:
233: }
234:
235: }
|