1 package org.apache.maven.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.maven.archiva.configuration.OrganisationInformation;
24 import org.apache.maven.archiva.web.action.admin.repositories.DefaultActionValidatorManagerFactory;
25 import org.apache.maven.archiva.web.validator.utils.ValidatorUtil;
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 assertEquals( "LOGO1", orginfo.getLogoLocation() );
93 assertEquals( "NAME1", orginfo.getName() );
94 assertEquals( "URL1", orginfo.getUrl() );
97 public void testStruts2ValidationFrameworkWithNullInputs()
101 action = getAction();
102 populateOrganisationValues( action, null, null, null );
105 actionValidatorManager.validate( action, EMPTY_STRING );
108 assertTrue( action.hasFieldErrors() );
110 Map<String, List<String>> fieldErrors = action.getFieldErrors();
112 // make an expected field error object
113 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
116 List<String> expectedErrorMessages = new ArrayList<String>();
117 expectedErrorMessages.add( "You must enter a name" );
118 expectedFieldErrors.put( "organisationName", expectedErrorMessages );
120 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
123 public void testStruts2ValidationFrameworkWithBlankInputs()
127 action = getAction();
128 populateOrganisationValues( action, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING );
131 actionValidatorManager.validate( action, EMPTY_STRING );
134 assertTrue( action.hasFieldErrors() );
136 Map<String, List<String>> fieldErrors = action.getFieldErrors();
138 // make an expected field error object
139 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
142 List<String> expectedErrorMessages = new ArrayList<String>();
143 expectedErrorMessages.add( "You must enter a name" );
144 expectedFieldErrors.put( "organisationName", expectedErrorMessages );
146 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
149 public void testStruts2ValidationFrameworkWithInvalidInputs()
153 action = getAction();
154 populateOrganisationValues( action, ORGANISATION_NAME_INVALID_INPUT, ORGANISATION_URL_INVALID_INPUT,
155 ORGANISATION_LOGO_INVALID_INPUT );
158 actionValidatorManager.validate( action, EMPTY_STRING );
161 assertTrue( action.hasFieldErrors() );
163 Map<String, List<String>> fieldErrors = action.getFieldErrors();
165 // make an expected field error object
166 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
169 List<String> expectedErrorMessages = new ArrayList<String>();
170 expectedErrorMessages.add(
171 "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(-)." );
172 expectedFieldErrors.put( "organisationName", expectedErrorMessages );
174 expectedErrorMessages = new ArrayList<String>();
175 expectedErrorMessages.add( "You must enter a URL" );
176 expectedFieldErrors.put( "organisationUrl", expectedErrorMessages );
178 expectedErrorMessages = new ArrayList<String>();
179 expectedErrorMessages.add( "You must enter a URL" );
180 expectedFieldErrors.put( "organisationLogo", expectedErrorMessages );
182 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
185 public void testStruts2ValidationFrameworkWithValidInputs()
189 action = getAction();
190 populateOrganisationValues( action, ORGANISATION_NAME_VALID_INPUT, ORGANISATION_URL_VALID_INPUT,
191 ORGANISATION_LOGO_VALID_INPUT );
194 actionValidatorManager.validate( action, EMPTY_STRING );
197 assertFalse( action.hasFieldErrors() );
200 private void populateOrganisationValues( AbstractAppearanceAction abstractAppearanceAction, String name, String url,
203 abstractAppearanceAction.setOrganisationName( name );
204 abstractAppearanceAction.setOrganisationUrl( url );
205 abstractAppearanceAction.setOrganisationLogo( logo );
209 protected AbstractAppearanceAction getAction()
211 //return (EditOrganisationInfoAction) lookup( Action.class.getName(), "editOrganisationInfo" );
212 return (EditOrganisationInfoAction) getActionProxy( "/admin/editOrganisationInfo" ).getAction();