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.xwork.ActionSupport;
23 import org.apache.commons.lang.StringUtils;
24 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
26 import java.lang.reflect.Method;
27 import java.util.Collection;
28 import java.util.List;
31 * AbstractWebworkTestCase
33 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
36 public abstract class AbstractWebworkTestCase
37 extends PlexusInSpringTestCase
40 * This is a conveinence method for mimicking how the webwork interceptors
41 * operate on an action, before the request is processed.
43 * Call this before each major request to the action to be sure you mimic the webwork process correctly.
45 protected void preRequest( ActionSupport action )
47 action.clearErrorsAndMessages();
51 * Tests the action to ensure that it has errors.
53 * NOTE: Don't forget to run {@link #preRequest(ActionSupport)} before each request to your action!
55 protected void assertHasErrors( ActionSupport action )
57 assertNotNull( action.getActionErrors() );
58 assertTrue( "Expected an error to occur.", action.getActionErrors().size() > 0 );
62 * Tests the action to ensure that it has messages.
64 * NOTE: Don't forget to run {@link #preRequest(ActionSupport)} before each request to your action!
66 protected void assertHasMessages( ActionSupport action )
68 assertNotNull( action.getActionMessages() );
69 assertTrue( "Expected an message to be set.", action.getActionMessages().size() > 0 );
73 * Tests the action to ensure that it has NO errors.
75 * NOTE: Don't forget to run {@link #preRequest(ActionSupport)} before each request to your action!
77 protected void assertNoErrors( ActionSupport action )
79 List<String> errors = (List<String>) action.getActionErrors();
81 assertNotNull( errors );
82 if ( errors.size() > 0 )
84 StringBuffer msg = new StringBuffer();
85 msg.append( "Should have had no errors. but found the following errors." );
87 for ( String error : errors )
89 msg.append( "\n " ).append( error );
91 fail( msg.toString() );
95 protected void assertRequestStatus( ActionSupport action, String expectedStatus, String methodName )
98 action.clearErrorsAndMessages();
100 Method method = action.getClass().getDeclaredMethod( methodName, (Class[]) null );
101 Object actualStatus = method.invoke( action, (Object[]) null );
102 assertTrue( "return should be of type String", actualStatus instanceof String );
104 if ( !StringUtils.equals( expectedStatus, (String) actualStatus ) )
106 StringBuffer msg = new StringBuffer();
107 msg.append( "Unexpected status returned from method <" );
108 msg.append( methodName ).append( "> on action <" );
109 String clazzname = action.getClass().getName();
110 msg.append( clazzname.substring( clazzname.lastIndexOf( '.' ) ) );
111 msg.append( ">: expected:<" ).append( expectedStatus ).append( "> but was:<" );
112 msg.append( (String) actualStatus ).append( ">. (see attached action messages and errors below)" );
114 for ( String message : (Collection<String>) action.getActionMessages() )
116 msg.append( "\n [MESSAGE]: " ).append( message );
119 for ( String error : (Collection<String>) action.getActionErrors() )
121 msg.append( "\n [ERROR]: " ).append( error );
124 fail( msg.toString() );