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;
34 public class EditOrganizationInfoActionTest
35 extends AbstractOrganizationInfoActionTest
37 private static final String EMPTY_STRING = "";
40 private static final String ORGANISATION_NAME_VALID_INPUT = "abcXYZ0129. _/\\~ :?!&=-";
42 private static final String ORGANISATION_URL_VALID_INPUT = "file://home/user/abcXYZ0129._/\\~:?!&=-<> ~+[ ]'\"";
44 private static final String ORGANISATION_LOGO_VALID_INPUT = "file://home/user/abcXYZ0129._/\\~:?!&=-<> ~+[ ]'\"";
47 private static final String ORGANISATION_NAME_INVALID_INPUT = "<>~+[ ]'\"";
49 private static final String ORGANISATION_URL_INVALID_INPUT = "/home/user/abcXYZ0129._/\\~:?!&=-<> ~+[ ]'\"";
51 private static final String ORGANISATION_LOGO_INVALID_INPUT = "/home/user/abcXYZ0129._/\\~:?!&=-<> ~+[ ]'\"";
54 private ActionValidatorManager actionValidatorManager;
62 DefaultActionValidatorManagerFactory factory = new DefaultActionValidatorManagerFactory();
64 actionValidatorManager = factory.createDefaultActionValidatorManager();
67 public void testOrganisationInfoSaves()
70 config.setOrganisationInfo( new OrganisationInformation() );
71 OrganisationInformation orginfo = config.getOrganisationInfo();
72 orginfo.setLogoLocation( "LOGO" );
73 orginfo.setName( "NAME" );
74 orginfo.setUrl( "URL" );
76 configuration.save( config );
82 assertEquals( "LOGO", action.getOrganisationLogo() );
83 assertEquals( "NAME", action.getOrganisationName() );
84 assertEquals( "URL", action.getOrganisationUrl() );
86 action.setOrganisationLogo( "LOGO1" );
87 action.setOrganisationName( "NAME1" );
88 action.setOrganisationUrl( "URL1" );
92 orginfo = config.getOrganisationInfo();
94 assertEquals( "LOGO1", orginfo.getLogoLocation() );
95 assertEquals( "NAME1", orginfo.getName() );
96 assertEquals( "URL1", orginfo.getUrl() );
99 public void testStruts2ValidationFrameworkWithNullInputs()
103 action = getAction();
104 populateOrganisationValues( action, null, null, null );
107 actionValidatorManager.validate( action, EMPTY_STRING );
110 assertTrue( action.hasFieldErrors() );
112 Map<String, List<String>> fieldErrors = action.getFieldErrors();
114 // make an expected field error object
115 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
118 List<String> expectedErrorMessages = new ArrayList<String>();
119 expectedErrorMessages.add( "You must enter a name" );
120 expectedFieldErrors.put( "organisationName", expectedErrorMessages );
122 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
125 public void testStruts2ValidationFrameworkWithBlankInputs()
129 action = getAction();
130 populateOrganisationValues( action, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING );
133 actionValidatorManager.validate( action, EMPTY_STRING );
136 assertTrue( action.hasFieldErrors() );
138 Map<String, List<String>> fieldErrors = action.getFieldErrors();
140 // make an expected field error object
141 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
144 List<String> expectedErrorMessages = new ArrayList<String>();
145 expectedErrorMessages.add( "You must enter a name" );
146 expectedFieldErrors.put( "organisationName", expectedErrorMessages );
148 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
151 public void testStruts2ValidationFrameworkWithInvalidInputs()
155 action = getAction();
156 populateOrganisationValues( action, ORGANISATION_NAME_INVALID_INPUT, ORGANISATION_URL_INVALID_INPUT,
157 ORGANISATION_LOGO_INVALID_INPUT );
160 actionValidatorManager.validate( action, EMPTY_STRING );
163 assertTrue( action.hasFieldErrors() );
165 Map<String, List<String>> fieldErrors = action.getFieldErrors();
167 // make an expected field error object
168 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
171 List<String> expectedErrorMessages = new ArrayList<String>();
172 expectedErrorMessages.add(
173 "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(-)." );
174 expectedFieldErrors.put( "organisationName", expectedErrorMessages );
176 expectedErrorMessages = new ArrayList<String>();
177 expectedErrorMessages.add( "You must enter a URL." );
178 expectedFieldErrors.put( "organisationUrl", expectedErrorMessages );
180 expectedErrorMessages = new ArrayList<String>();
181 expectedErrorMessages.add( "You must enter a URL for your logo." );
182 expectedFieldErrors.put( "organisationLogo", expectedErrorMessages );
184 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
187 public void testStruts2ValidationFrameworkWithValidInputs()
191 action = getAction();
192 populateOrganisationValues( action, ORGANISATION_NAME_VALID_INPUT, ORGANISATION_URL_VALID_INPUT,
193 ORGANISATION_LOGO_VALID_INPUT );
196 actionValidatorManager.validate( action, EMPTY_STRING );
199 assertFalse( action.hasFieldErrors() );
202 private void populateOrganisationValues( AbstractAppearanceAction abstractAppearanceAction, String name, String url,
205 abstractAppearanceAction.setOrganisationName( name );
206 abstractAppearanceAction.setOrganisationUrl( url );
207 abstractAppearanceAction.setOrganisationLogo( logo );
211 protected AbstractAppearanceAction getAction()
213 return (EditOrganisationInfoAction) getActionProxy( "/admin/editAppearance.action" ).getAction();