/*
* 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.controller;
import static org.testng.Assert.assertEquals;
import java.util.Collection;
import org.apache.struts.action.Action;
import org.strecks.action.navigable.NavigableController;
import org.strecks.controller.impl.AbstractControllerAction;
import org.strecks.controller.impl.ActionWithNonInterface;
import org.strecks.controller.impl.ActionWithNonStrutsControllerAction;
import org.strecks.controller.impl.ReadOnlyAction;
import org.strecks.controller.impl.ReadOnlyControllerAction;
import org.strecks.controller.impl.TestAction;
import org.strecks.exceptions.ApplicationConfigurationException;
import org.strecks.navigate.NavigationHolder;
import org.strecks.navigate.internal.impl.ActionWithNavigate;
import org.strecks.navigate.internal.impl.ActionWithNoNavigate;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestActionCreator
{
private ActionCreatorImpl actionCreator;
@BeforeMethod
public void setUp()
{
actionCreator = new ActionCreatorImpl();
}
/**
* Coarse grained test on happy path. Creates controller action and actionBean which satisfy mutual depependency
* requirements
*/
@Test
public void testActionCreate() throws Exception
{
Action createAction = actionCreator.createAction(TestAction.class);
assert createAction instanceof ControllerAction;
ControllerAction controllerAction = (ControllerAction) createAction;
Object newActionBean = controllerAction.getBeanSource().createBean(null);
assert newActionBean instanceof TestAction;
}
@Test
public void testCreateControllerAction() throws Exception
{
Action createAction = actionCreator.createControllerAction(TestAction.class);
assert createAction instanceof ControllerAction;
ControllerAction controllerAction = (ControllerAction) createAction;
Object newActionBean = controllerAction.getBeanSource().createBean(null);
assert newActionBean instanceof TestAction;
assert createAction instanceof BaseControllerAction;
BaseControllerAction bc = (BaseControllerAction) createAction;
Assert.assertNotNull(bc.getInitMethod());
}
@Test
public void testGetActionInterfaceType() throws Exception
{
Class actionInterfaceType = actionCreator.getActionInterfaceType(ReadOnlyControllerAction.class, null);
assertEquals(actionInterfaceType, ReadOnlyAction.class);
}
@Test(expectedExceptions=ApplicationConfigurationException.class)
public void noActionInterfaceAnnotation()
{
actionCreator.getActionInterfaceType(String.class, TestAction.class);
}
@Test(expectedExceptions=ApplicationConfigurationException.class)
public void actionInterfaceNotInterface()
{
actionCreator.getActionInterfaceType(ActionWithNonInterface.class, TestAction.class);
}
@Test
public void instantiateControllerAction()
{
Object instantiateControllerAction = actionCreator.instantiateControllerAction(ReadOnlyControllerAction.class,
TestAction.class);
// assert instantiateControllerAction instanceof ReadOnlyControllerAction;
System.out.println(instantiateControllerAction);
}
@Test(expectedExceptions=ApplicationConfigurationException.class)
public void abstractControllerAction()
{
actionCreator.instantiateControllerAction(AbstractControllerAction.class, TestAction.class);
}
@Test(expectedExceptions=ApplicationConfigurationException.class)
public void privateControllerAction() throws Exception
{
actionCreator.instantiateControllerAction(Class
.forName("org.strecks.controller.impl.PrivateControllerAction"), TestAction.class);
}
@Test
public void testIsAssignable()
{
actionCreator.checkIsAssignable(TestAction.class, ReadOnlyAction.class, ReadOnlyControllerAction.class);
}
@Test(expectedExceptions=ApplicationConfigurationException.class)
public void nonImplementingInterface() throws Exception
{
actionCreator.checkIsAssignable(TestAction.class, Collection.class, ReadOnlyControllerAction.class);
}
@Test
public void testGetControllerClass()
{
Class controllerClass = actionCreator.getControllerClass(TestAction.class);
assertEquals(controllerClass, ReadOnlyControllerAction.class);
}
@Test
public void testControllerNotStrutsAction()
{
try
{
actionCreator.getControllerClass(String.class);
}
catch (ApplicationConfigurationException e)
{
assertEquals(
e.getMessage(),
"java.lang.String is not a Struts Action subclass and does not have a org.strecks.controller.annotation.Controller annotation");
}
}
@Test
public void testActionWithControllerNotStrutsAction()
{
try
{
actionCreator.getControllerClass(ActionWithNonStrutsControllerAction.class);
}
catch (ApplicationConfigurationException e)
{
assertEquals(
e.getMessage(),
"org.strecks.controller.impl.ActionWithNonStrutsControllerAction is has a org.strecks.controller.annotation.Controller annotation "
+ "which points to the class org.strecks.controller.impl.NonStrutsActionControllerAction which is not a Struts Action subclass");
}
}
/**
* Tests creation with navigate mechanism
*/
@Test
public void testCreateWithNavigate() throws Exception
{
NavigableControllerAction createAction = (NavigableControllerAction) actionCreator
.createAction(ActionWithNavigate.class);
assert null != createAction.getNavigationHolder();
}
/**
* Tests creation with navigate mechanism
*/
@Test
public void testCreateWithNoNavigate() throws Exception
{
ControllerAction createAction = (ControllerAction) actionCreator.createAction(ActionWithNoNavigate.class);
assert !(createAction instanceof NavigableControllerAction);
}
/**
* Tests reading the annotation extension handling mechanism
*/
@Test
public void testReadControllerClassAnnotations() throws Exception
{
NavigableController controller = new NavigableController();
actionCreator.readControllerClassAnnotations(ActionWithNavigate.class, controller);
NavigationHolder navigationHolder = controller.getNavigationHolder();
assert navigationHolder != null;
}
/**
* Tests reading the annotation extension handling mechanism
*/
@Test
public void testNoAnnotations() throws Exception
{
try
{
NavigableController controller = new NavigableController();
actionCreator.readControllerClassAnnotations(ActionWithNoNavigate.class, controller);
}
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");
}
}
}