1 package org.apache.maven.archiva.web.action;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.xwork2.ActionSupport;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.struts2.StrutsSpringTestCase;
26 import java.lang.reflect.Method;
27 import java.util.Collection;
28 import java.util.List;
31 * AbstractWebworkTestCase
35 public abstract class AbstractWebworkTestCase
36 extends StrutsSpringTestCase
41 protected String[] getContextLocations()
43 return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
47 * This is a conveinence method for mimicking how the webwork interceptors
48 * operate on an action, before the request is processed.
50 * Call this before each major request to the action to be sure you mimic the webwork process correctly.
52 protected void preRequest( ActionSupport action )
54 action.clearErrorsAndMessages();
58 * Tests the action to ensure that it has errors.
60 * NOTE: Don't forget to run {@link #preRequest(ActionSupport)} before each request to your action!
62 protected void assertHasErrors( ActionSupport action )
64 assertNotNull( action.getActionErrors() );
65 assertTrue( "Expected an error to occur.", action.getActionErrors().size() > 0 );
69 * Tests the action to ensure that it has messages.
71 * NOTE: Don't forget to run {@link #preRequest(ActionSupport)} before each request to your action!
73 protected void assertHasMessages( ActionSupport action )
75 assertNotNull( action.getActionMessages() );
76 assertTrue( "Expected an message to be set.", action.getActionMessages().size() > 0 );
80 * Tests the action to ensure that it has NO errors.
82 * NOTE: Don't forget to run {@link #preRequest(ActionSupport)} before each request to your action!
84 @SuppressWarnings("unchecked")
85 protected void assertNoErrors( ActionSupport action )
87 List<String> errors = (List<String>) action.getActionErrors();
89 assertNotNull( errors );
90 if ( errors.size() > 0 )
92 StringBuffer msg = new StringBuffer();
93 msg.append( "Should have had no errors. but found the following errors." );
95 for ( String error : errors )
97 msg.append( "\n " ).append( error );
99 fail( msg.toString() );
103 @SuppressWarnings("unchecked")
104 protected void assertRequestStatus( ActionSupport action, String expectedStatus, String methodName )
107 action.clearErrorsAndMessages();
109 Method method = action.getClass().getDeclaredMethod( methodName, (Class[]) null );
110 Object actualStatus = method.invoke( action, (Object[]) null );
111 assertTrue( "return should be of type String", actualStatus instanceof String );
113 if ( !StringUtils.equals( expectedStatus, (String) actualStatus ) )
115 StringBuffer msg = new StringBuffer();
116 msg.append( "Unexpected status returned from method <" );
117 msg.append( methodName ).append( "> on action <" );
118 String clazzname = action.getClass().getName();
119 msg.append( clazzname.substring( clazzname.lastIndexOf( '.' ) ) );
120 msg.append( ">: expected:<" ).append( expectedStatus ).append( "> but was:<" );
121 msg.append( (String) actualStatus ).append( ">. (see attached action messages and errors below)" );
123 for ( String message : (Collection<String>) action.getActionMessages() )
125 msg.append( "\n [MESSAGE]: " ).append( message );
128 for ( String error : (Collection<String>) action.getActionErrors() )
130 msg.append( "\n [ERROR]: " ).append( error );
133 fail( msg.toString() );