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.navigate.internal;
016:
017: import static org.testng.Assert.assertEquals;
018: import static org.testng.Assert.fail;
019:
020: import org.strecks.controller.impl.TestAction;
021: import org.strecks.exceptions.ApplicationConfigurationException;
022: import org.strecks.exceptions.ApplicationRuntimeException;
023: import org.strecks.navigate.NavigationHandler;
024: import org.strecks.navigate.NavigationHolder;
025: import org.strecks.navigate.factory.DefaultNavigationHandlerFactory;
026: import org.strecks.navigate.factory.IdentityNavigationHandlerFactory;
027: import org.strecks.navigate.handler.PageNavigationHandler;
028: import org.strecks.navigate.internal.impl.ActionWithIncompatibleTypes;
029: import org.strecks.navigate.internal.impl.ActionWithNavigate;
030: import org.strecks.navigate.internal.impl.ActionWithNavigateNotGetter1;
031: import org.strecks.navigate.internal.impl.ActionWithNavigateNotGetter2;
032: import org.strecks.navigate.internal.impl.ActionWithNoNavigate;
033: import org.strecks.navigate.internal.impl.ActionWithParticularFactory;
034: import org.strecks.navigate.internal.impl.ActionWithParticularHandler;
035: import org.strecks.navigate.internal.impl.ActionWithTwoNavigates;
036: import org.strecks.navigate.internal.impl.ParticularHandlerFactory;
037: import org.strecks.navigate.internal.impl.ParticularNavigationHandler;
038: import org.testng.annotations.BeforeMethod;
039: import org.testng.annotations.Test;
040:
041: /**
042: * @author Phil Zoio
043: */
044: public class TestNavigationReader {
045:
046: private BeanNavigationReader navigationReader;
047:
048: @BeforeMethod
049: public void setUp() {
050: navigationReader = new BeanNavigationReader();
051: }
052:
053: @Test
054: public void testReadNavigate() {
055: NavigationHandlerInfo info = navigationReader
056: .readNavigationHandlerFactory(ActionWithNavigate.class);
057: assert info.getFactory() instanceof DefaultNavigationHandlerFactory;
058: assertEquals(info.getMethod().getName(), "nextPage");
059: }
060:
061: @Test
062: public void testNullNavigate() {
063: NavigationHandlerInfo info = navigationReader
064: .readNavigationHandlerFactory(TestAction.class);
065: assert null == info;
066: }
067:
068: @Test
069: public void testActionWithTwoNavigates() {
070: try {
071: navigationReader
072: .readNavigationHandlerFactory(ActionWithTwoNavigates.class);
073: fail();
074: } catch (ApplicationConfigurationException e) {
075: assertEquals(
076: e.getMessage(),
077: "org.strecks.navigate.internal.impl.ActionWithTwoNavigates violates rule that only one method containing an annotation which uses the @NavigationInfo annotation is permitted");
078: }
079: }
080:
081: @Test
082: public void testNavigateNotGetter1() {
083: try {
084: navigationReader
085: .readNavigationHandlerFactory(ActionWithNavigateNotGetter1.class);
086: fail();
087: } catch (ApplicationRuntimeException e) {
088: assertEquals(
089: e.getMessage(),
090: "Method nextPage1 in class class org.strecks.navigate.internal.impl.ActionWithNavigateNotGetter1 must have 0 parameter(s)");
091: }
092: }
093:
094: @Test
095: public void testNavigateNotGetter2() {
096: try {
097: navigationReader
098: .readNavigationHandlerFactory(ActionWithNavigateNotGetter2.class);
099: fail();
100: } catch (ApplicationConfigurationException e) {
101: assertEquals(
102: e.getMessage(),
103: "org.strecks.navigate.internal.impl.ActionWithNavigateNotGetter2 contains an annotation which uses the @NavigationInfo annotation but returns void");
104: }
105: }
106:
107: @Test
108: public void testGetNavigationHandler() throws SecurityException,
109: NoSuchMethodException {
110: NavigationHandlerInfo info = new NavigationHandlerInfo(
111: new DefaultNavigationHandlerFactory(),
112: ActionWithNavigate.class.getMethod("nextPage"));
113:
114: NavigationHandler navigationHandler = navigationReader
115: .getNavigationHandler(ActionWithNavigate.class, info);
116: assert navigationHandler instanceof PageNavigationHandler;
117: }
118:
119: @Test
120: public void testGetParticularNavigationHandler()
121: throws SecurityException, NoSuchMethodException {
122: NavigationHandlerInfo info = navigationReader
123: .readNavigationHandlerFactory(ActionWithParticularHandler.class);
124: assert info.getFactory() instanceof IdentityNavigationHandlerFactory;
125: assert info.getFactory().getNavigationHandler(null, null) instanceof ParticularNavigationHandler;
126: }
127:
128: @Test
129: public void testGetParticularNavigationFactory()
130: throws SecurityException, NoSuchMethodException {
131: NavigationHandlerInfo info = navigationReader
132: .readNavigationHandlerFactory(ActionWithParticularFactory.class);
133: assert info.getFactory() instanceof ParticularHandlerFactory;
134: assert info.getFactory().getNavigationHandler(null, null) instanceof ParticularNavigationHandler;
135: }
136:
137: @Test
138: public void testReadNavigation() {
139: assert navigationReader
140: .readAnnotations(ActionWithParticularHandler.class);
141: NavigationHolder navigation = navigationReader
142: .getNavigationHolder();
143: assert navigation.getFactory() instanceof IdentityNavigationHandlerFactory;
144: assert navigation.getHandler() instanceof ParticularNavigationHandler;
145: assert navigation.getMethod().getName().contains("nextAction");
146: }
147:
148: @Test
149: public void testNoNavigation() {
150: try {
151: navigationReader
152: .readAnnotations(ActionWithNoNavigate.class);
153: } catch (ApplicationConfigurationException e) {
154: assertEquals(
155: e.getMessage(),
156: "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");
157: }
158: }
159:
160: @Test
161: public void testIncomaptibleTypes() {
162: try {
163:
164: navigationReader
165: .readAnnotations(ActionWithIncompatibleTypes.class);
166: fail();
167:
168: } catch (ApplicationConfigurationException e) {
169: assertEquals(
170: e.getMessage(),
171: "The return type of nextAction() in org.strecks.navigate.internal.impl.ActionWithIncompatibleTypes "
172: + "is not compatible with the parameterized generic type interface org.strecks.page.Page "
173: + "of the navigation handler org.strecks.navigate.internal.impl.PageNavigationHandler");
174: }
175:
176: }
177:
178: }
|