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.codehaus.plexus.spring.PlexusInSpringTestCase;
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 PlexusInSpringTestCase
39 * This is a conveinence method for mimicking how the webwork interceptors
40 * operate on an action, before the request is processed.
42 * Call this before each major request to the action to be sure you mimic the webwork process correctly.
44 protected void preRequest( ActionSupport action )
46 action.clearErrorsAndMessages();
50 * Tests the action to ensure that it has errors.
52 * NOTE: Don't forget to run {@link #preRequest(ActionSupport)} before each request to your action!
54 protected void assertHasErrors( ActionSupport action )
56 assertNotNull( action.getActionErrors() );
57 assertTrue( "Expected an error to occur.", action.getActionErrors().size() > 0 );
61 * Tests the action to ensure that it has messages.
63 * NOTE: Don't forget to run {@link #preRequest(ActionSupport)} before each request to your action!
65 protected void assertHasMessages( ActionSupport action )
67 assertNotNull( action.getActionMessages() );
68 assertTrue( "Expected an message to be set.", action.getActionMessages().size() > 0 );
72 * Tests the action to ensure that it has NO errors.
74 * NOTE: Don't forget to run {@link #preRequest(ActionSupport)} before each request to your action!
76 protected void assertNoErrors( ActionSupport action )
78 List<String> errors = (List<String>) action.getActionErrors();
80 assertNotNull( errors );
81 if ( errors.size() > 0 )
83 StringBuffer msg = new StringBuffer();
84 msg.append( "Should have had no errors. but found the following errors." );
86 for ( String error : errors )
88 msg.append( "\n " ).append( error );
90 fail( msg.toString() );
94 protected void assertRequestStatus( ActionSupport action, String expectedStatus, String methodName )
97 action.clearErrorsAndMessages();
99 Method method = action.getClass().getDeclaredMethod( methodName, (Class[]) null );
100 Object actualStatus = method.invoke( action, (Object[]) null );
101 assertTrue( "return should be of type String", actualStatus instanceof String );
103 if ( !StringUtils.equals( expectedStatus, (String) actualStatus ) )
105 StringBuffer msg = new StringBuffer();
106 msg.append( "Unexpected status returned from method <" );
107 msg.append( methodName ).append( "> on action <" );
108 String clazzname = action.getClass().getName();
109 msg.append( clazzname.substring( clazzname.lastIndexOf( '.' ) ) );
110 msg.append( ">: expected:<" ).append( expectedStatus ).append( "> but was:<" );
111 msg.append( (String) actualStatus ).append( ">. (see attached action messages and errors below)" );
113 for ( String message : (Collection<String>) action.getActionMessages() )
115 msg.append( "\n [MESSAGE]: " ).append( message );
118 for ( String error : (Collection<String>) action.getActionErrors() )
120 msg.append( "\n [ERROR]: " ).append( error );
123 fail( msg.toString() );