1 package org.apache.archiva.web.action.admin.appearance;
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.validator.ActionValidatorManager;
23 import org.apache.archiva.web.validator.utils.ValidatorUtil;
24 import org.apache.archiva.configuration.OrganisationInformation;
25 import org.apache.archiva.web.action.admin.repositories.DefaultActionValidatorManagerFactory;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
31 import org.junit.Before;
32 import org.junit.Test;
36 public class EditOrganizationInfoActionTest
37 extends AbstractOrganizationInfoActionTest
39 private static final String EMPTY_STRING = "";
42 private static final String ORGANISATION_NAME_VALID_INPUT = "abcXYZ0129. _/\\~ :?!&=-";
44 private static final String ORGANISATION_URL_VALID_INPUT = "file://home/user/abcXYZ0129._/\\~:?!&=-<> ~+[ ]'\"";
46 private static final String ORGANISATION_LOGO_VALID_INPUT = "file://home/user/abcXYZ0129._/\\~:?!&=-<> ~+[ ]'\"";
49 private static final String ORGANISATION_NAME_INVALID_INPUT = "<>~+[ ]'\"";
51 private static final String ORGANISATION_URL_INVALID_INPUT = "/home/user/abcXYZ0129._/\\~:?!&=-<> ~+[ ]'\"";
53 private static final String ORGANISATION_LOGO_INVALID_INPUT = "/home/user/abcXYZ0129._/\\~:?!&=-<> ~+[ ]'\"";
56 private ActionValidatorManager actionValidatorManager;
65 DefaultActionValidatorManagerFactory factory = new DefaultActionValidatorManagerFactory();
67 actionValidatorManager = factory.createDefaultActionValidatorManager();
71 public void testOrganisationInfoSaves()
74 config.setOrganisationInfo( new OrganisationInformation() );
75 OrganisationInformation orginfo = config.getOrganisationInfo();
76 orginfo.setLogoLocation( "LOGO" );
77 orginfo.setName( "NAME" );
78 orginfo.setUrl( "URL" );
80 configuration.save( config );
86 assertEquals( "LOGO", action.getOrganisationLogo() );
87 assertEquals( "NAME", action.getOrganisationName() );
88 assertEquals( "URL", action.getOrganisationUrl() );
90 action.setOrganisationLogo( "LOGO1" );
91 action.setOrganisationName( "NAME1" );
92 action.setOrganisationUrl( "URL1" );
96 orginfo = config.getOrganisationInfo();
98 assertEquals( "LOGO1", orginfo.getLogoLocation() );
99 assertEquals( "NAME1", orginfo.getName() );
100 assertEquals( "URL1", orginfo.getUrl() );
104 public void testStruts2ValidationFrameworkWithNullInputs()
108 action = getAction();
109 populateOrganisationValues( action, null, null, null );
112 actionValidatorManager.validate( action, EMPTY_STRING );
115 assertTrue( action.hasFieldErrors() );
117 Map<String, List<String>> fieldErrors = action.getFieldErrors();
119 // make an expected field error object
120 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
123 List<String> expectedErrorMessages = new ArrayList<String>();
124 expectedErrorMessages.add( "You must enter a name" );
125 expectedFieldErrors.put( "organisationName", expectedErrorMessages );
127 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
131 public void testStruts2ValidationFrameworkWithBlankInputs()
135 action = getAction();
136 populateOrganisationValues( action, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING );
139 actionValidatorManager.validate( action, EMPTY_STRING );
142 assertTrue( action.hasFieldErrors() );
144 Map<String, List<String>> fieldErrors = action.getFieldErrors();
146 // make an expected field error object
147 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
150 List<String> expectedErrorMessages = new ArrayList<String>();
151 expectedErrorMessages.add( "You must enter a name" );
152 expectedFieldErrors.put( "organisationName", expectedErrorMessages );
154 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
158 public void testStruts2ValidationFrameworkWithInvalidInputs()
162 action = getAction();
163 populateOrganisationValues( action, ORGANISATION_NAME_INVALID_INPUT, ORGANISATION_URL_INVALID_INPUT,
164 ORGANISATION_LOGO_INVALID_INPUT );
167 actionValidatorManager.validate( action, EMPTY_STRING );
170 assertTrue( action.hasFieldErrors() );
172 Map<String, List<String>> fieldErrors = action.getFieldErrors();
174 // make an expected field error object
175 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
178 List<String> expectedErrorMessages = new ArrayList<String>();
179 expectedErrorMessages.add(
180 "Organisation name must only contain alphanumeric characters, white-spaces(' '), equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-)." );
181 expectedFieldErrors.put( "organisationName", expectedErrorMessages );
183 expectedErrorMessages = new ArrayList<String>();
184 expectedErrorMessages.add( "You must enter a URL." );
185 expectedFieldErrors.put( "organisationUrl", expectedErrorMessages );
187 expectedErrorMessages = new ArrayList<String>();
188 expectedErrorMessages.add( "You must enter a URL for your logo." );
189 expectedFieldErrors.put( "organisationLogo", expectedErrorMessages );
191 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
195 public void testStruts2ValidationFrameworkWithValidInputs()
199 action = getAction();
200 populateOrganisationValues( action, ORGANISATION_NAME_VALID_INPUT, ORGANISATION_URL_VALID_INPUT,
201 ORGANISATION_LOGO_VALID_INPUT );
204 actionValidatorManager.validate( action, EMPTY_STRING );
207 assertFalse( action.hasFieldErrors() );
210 private void populateOrganisationValues( AbstractAppearanceAction abstractAppearanceAction, String name, String url,
213 abstractAppearanceAction.setOrganisationName( name );
214 abstractAppearanceAction.setOrganisationUrl( url );
215 abstractAppearanceAction.setOrganisationLogo( logo );
219 protected AbstractAppearanceAction getAction()
221 return (EditOrganisationInfoAction) getActionProxy( "/admin/editAppearance.action" ).getAction();