/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.navigate.internal;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import org.strecks.controller.impl.TestAction;
import org.strecks.exceptions.ApplicationConfigurationException;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.strecks.navigate.NavigationHandler;
import org.strecks.navigate.NavigationHolder;
import org.strecks.navigate.factory.DefaultNavigationHandlerFactory;
import org.strecks.navigate.factory.IdentityNavigationHandlerFactory;
import org.strecks.navigate.handler.PageNavigationHandler;
import org.strecks.navigate.internal.impl.ActionWithIncompatibleTypes;
import org.strecks.navigate.internal.impl.ActionWithNavigate;
import org.strecks.navigate.internal.impl.ActionWithNavigateNotGetter1;
import org.strecks.navigate.internal.impl.ActionWithNavigateNotGetter2;
import org.strecks.navigate.internal.impl.ActionWithNoNavigate;
import org.strecks.navigate.internal.impl.ActionWithParticularFactory;
import org.strecks.navigate.internal.impl.ActionWithParticularHandler;
import org.strecks.navigate.internal.impl.ActionWithTwoNavigates;
import org.strecks.navigate.internal.impl.ParticularHandlerFactory;
import org.strecks.navigate.internal.impl.ParticularNavigationHandler;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestNavigationReader
{
private BeanNavigationReader navigationReader;
@BeforeMethod
public void setUp()
{
navigationReader = new BeanNavigationReader();
}
@Test
public void testReadNavigate()
{
NavigationHandlerInfo info = navigationReader.readNavigationHandlerFactory(ActionWithNavigate.class);
assert info.getFactory() instanceof DefaultNavigationHandlerFactory;
assertEquals(info.getMethod().getName(), "nextPage");
}
@Test
public void testNullNavigate()
{
NavigationHandlerInfo info = navigationReader.readNavigationHandlerFactory(TestAction.class);
assert null == info;
}
@Test
public void testActionWithTwoNavigates()
{
try
{
navigationReader.readNavigationHandlerFactory(ActionWithTwoNavigates.class);
fail();
}
catch (ApplicationConfigurationException e)
{
assertEquals(
e.getMessage(),
"org.strecks.navigate.internal.impl.ActionWithTwoNavigates violates rule that only one method containing an annotation which uses the @NavigationInfo annotation is permitted");
}
}
@Test
public void testNavigateNotGetter1()
{
try
{
navigationReader.readNavigationHandlerFactory(ActionWithNavigateNotGetter1.class);
fail();
}
catch (ApplicationRuntimeException e)
{
assertEquals(
e.getMessage(),
"Method nextPage1 in class class org.strecks.navigate.internal.impl.ActionWithNavigateNotGetter1 must have 0 parameter(s)");
}
}
@Test
public void testNavigateNotGetter2()
{
try
{
navigationReader.readNavigationHandlerFactory(ActionWithNavigateNotGetter2.class);
fail();
}
catch (ApplicationConfigurationException e)
{
assertEquals(
e.getMessage(),
"org.strecks.navigate.internal.impl.ActionWithNavigateNotGetter2 contains an annotation which uses the @NavigationInfo annotation but returns void");
}
}
@Test
public void testGetNavigationHandler() throws SecurityException, NoSuchMethodException
{
NavigationHandlerInfo info = new NavigationHandlerInfo(new DefaultNavigationHandlerFactory(),
ActionWithNavigate.class.getMethod("nextPage"));
NavigationHandler navigationHandler = navigationReader.getNavigationHandler(ActionWithNavigate.class, info);
assert navigationHandler instanceof PageNavigationHandler;
}
@Test
public void testGetParticularNavigationHandler() throws SecurityException, NoSuchMethodException
{
NavigationHandlerInfo info = navigationReader.readNavigationHandlerFactory(ActionWithParticularHandler.class);
assert info.getFactory() instanceof IdentityNavigationHandlerFactory;
assert info.getFactory().getNavigationHandler(null, null) instanceof ParticularNavigationHandler;
}
@Test
public void testGetParticularNavigationFactory() throws SecurityException, NoSuchMethodException
{
NavigationHandlerInfo info = navigationReader.readNavigationHandlerFactory(ActionWithParticularFactory.class);
assert info.getFactory() instanceof ParticularHandlerFactory;
assert info.getFactory().getNavigationHandler(null, null) instanceof ParticularNavigationHandler;
}
@Test
public void testReadNavigation()
{
assert navigationReader.readAnnotations(ActionWithParticularHandler.class);
NavigationHolder navigation = navigationReader.getNavigationHolder();
assert navigation.getFactory() instanceof IdentityNavigationHandlerFactory;
assert navigation.getHandler() instanceof ParticularNavigationHandler;
assert navigation.getMethod().getName().contains("nextAction");
}
@Test
public void testNoNavigation()
{
try
{
navigationReader.readAnnotations(ActionWithNoNavigate.class);
}
catch (ApplicationConfigurationException e)
{
assertEquals(
e.getMessage(),
"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");
}
}
@Test
public void testIncomaptibleTypes()
{
try
{
navigationReader.readAnnotations(ActionWithIncompatibleTypes.class);
fail();
}
catch (ApplicationConfigurationException e)
{
assertEquals(
e.getMessage(),
"The return type of nextAction() in org.strecks.navigate.internal.impl.ActionWithIncompatibleTypes "
+ "is not compatible with the parameterized generic type interface org.strecks.page.Page "
+ "of the navigation handler org.strecks.navigate.internal.impl.PageNavigationHandler");
}
}
}