1 package org.apache.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;
29 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
30 import org.junit.runner.RunWith;
32 * AbstractWebworkTestCase
36 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
37 public abstract class AbstractWebworkTestCase
38 extends StrutsSpringTestCase
43 protected String[] getContextLocations()
45 return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
49 * This is a conveinence method for mimicking how the webwork interceptors
50 * operate on an action, before the request is processed.
52 * Call this before each major request to the action to be sure you mimic the webwork process correctly.
54 protected void preRequest( ActionSupport action )
56 action.clearErrorsAndMessages();
60 * Tests the action to ensure that it has errors.
62 * NOTE: Don't forget to run {@link #preRequest(ActionSupport)} before each request to your action!
64 protected void assertHasErrors( ActionSupport action )
66 assertNotNull( action.getActionErrors() );
67 assertTrue( "Expected an error to occur.", action.getActionErrors().size() > 0 );
71 * Tests the action to ensure that it has messages.
73 * NOTE: Don't forget to run {@link #preRequest(ActionSupport)} before each request to your action!
75 protected void assertHasMessages( ActionSupport action )
77 assertNotNull( action.getActionMessages() );
78 assertTrue( "Expected an message to be set.", action.getActionMessages().size() > 0 );
82 * Tests the action to ensure that it has NO errors.
84 * NOTE: Don't forget to run {@link #preRequest(ActionSupport)} before each request to your action!
86 @SuppressWarnings("unchecked")
87 protected void assertNoErrors( ActionSupport action )
89 List<String> errors = (List<String>) action.getActionErrors();
91 assertNotNull( errors );
92 if ( errors.size() > 0 )
94 StringBuilder msg = new StringBuilder();
95 msg.append( "Should have had no errors. but found the following errors." );
97 for ( String error : errors )
99 msg.append( "\n " ).append( error );
101 fail( msg.toString() );
105 @SuppressWarnings("unchecked")
106 protected void assertRequestStatus( ActionSupport action, String expectedStatus, String methodName )
109 action.clearErrorsAndMessages();
111 Method method = action.getClass().getDeclaredMethod( methodName, (Class[]) null );
112 Object actualStatus = method.invoke( action, (Object[]) null );
113 assertTrue( "return should be of type String", actualStatus instanceof String );
115 if ( !StringUtils.equals( expectedStatus, (String) actualStatus ) )
117 StringBuilder msg = new StringBuilder();
118 msg.append( "Unexpected status returned from method <" );
119 msg.append( methodName ).append( "> on action <" );
120 String clazzname = action.getClass().getName();
121 msg.append( clazzname.substring( clazzname.lastIndexOf( '.' ) ) );
122 msg.append( ">: expected:<" ).append( expectedStatus ).append( "> but was:<" );
123 msg.append( (String) actualStatus ).append( ">. (see attached action messages and errors below)" );
125 for ( String message : (Collection<String>) action.getActionMessages() )
127 msg.append( "\n [MESSAGE]: " ).append( message );
130 for ( String error : (Collection<String>) action.getActionErrors() )
132 msg.append( "\n [ERROR]: " ).append( error );
135 fail( msg.toString() );