1 package org.apache.archiva.web.action.admin.legacy;
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 junit.framework.TestCase;
24 import org.apache.archiva.admin.model.beans.LegacyArtifactPath;
25 import org.apache.archiva.web.validator.utils.ValidatorUtil;
26 import org.apache.archiva.web.action.admin.repositories.DefaultActionValidatorManagerFactory;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
33 public class AddLegacyArtifactPathActionTest
36 private static final String EMPTY_STRING = "";
39 private static final String LEGACY_ARTIFACT_PATH_PATH_VALID_INPUT = "-abcXYZ0129._/\\";
41 private static final String GROUP_ID_VALID_INPUT = "abcXYZ0129._-";
43 private static final String ARTIFACT_ID_VALID_INPUT = "abcXYZ0129._-";
45 private static final String VERSION_VALID_INPUT = "abcXYZ0129._-";
47 private static final String CLASSIFIER_VALID_INPUT = "abcXYZ0129._-";
49 private static final String TYPE_VALID_INPUT = "abcXYZ0129._-";
52 private static final String LEGACY_ARTIFACT_PATH_PATH_INVALID_INPUT = "<> ~+[ ]'\"";
54 private static final String GROUP_ID_INVALID_INPUT = "<> \\/~+[ ]'\"";
56 private static final String ARTIFACT_ID_INVALID_INPUT = "<> \\/~+[ ]'\"";
58 private static final String VERSION_INVALID_INPUT = "<> \\/~+[ ]'\"";
60 private static final String CLASSIFIER_INVALID_INPUT = "<> \\/~+[ ]'\"";
62 private static final String TYPE_INVALID_INPUT = "<> \\/~+[ ]'\"";
65 private AddLegacyArtifactPathAction addLegacyArtifactPathAction;
67 private ActionValidatorManager actionValidatorManager;
73 addLegacyArtifactPathAction = new AddLegacyArtifactPathAction();
75 DefaultActionValidatorManagerFactory factory = new DefaultActionValidatorManagerFactory();
77 actionValidatorManager = factory.createDefaultActionValidatorManager();
80 public void testStruts2ValidationFrameworkWithNullInputs()
84 LegacyArtifactPath legacyArtifactPath = createLegacyArtifactPath( null );
85 populateAddLegacyArtifactPathActionFields( addLegacyArtifactPathAction, legacyArtifactPath, null, null, null,
89 actionValidatorManager.validate( addLegacyArtifactPathAction, EMPTY_STRING );
92 assertTrue( addLegacyArtifactPathAction.hasFieldErrors() );
94 Map<String, List<String>> fieldErrors = addLegacyArtifactPathAction.getFieldErrors();
96 // make an expected field error object
97 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
100 List<String> expectedErrorMessages = new ArrayList<String>();
101 expectedErrorMessages.add( "You must enter a legacy path." );
102 expectedFieldErrors.put( "legacyArtifactPath.path", expectedErrorMessages );
104 expectedErrorMessages = new ArrayList<String>();
105 expectedErrorMessages.add( "You must enter a groupId." );
106 expectedFieldErrors.put( "groupId", expectedErrorMessages );
108 expectedErrorMessages = new ArrayList<String>();
109 expectedErrorMessages.add( "You must enter an artifactId." );
110 expectedFieldErrors.put( "artifactId", expectedErrorMessages );
112 expectedErrorMessages = new ArrayList<String>();
113 expectedErrorMessages.add( "You must enter a version." );
114 expectedFieldErrors.put( "version", expectedErrorMessages );
116 expectedErrorMessages = new ArrayList<String>();
117 expectedErrorMessages.add( "You must enter a type." );
118 expectedFieldErrors.put( "type", expectedErrorMessages );
120 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
123 public void testStruts2ValidationFrameworkWithBlankInputs()
127 LegacyArtifactPath legacyArtifactPath = createLegacyArtifactPath( EMPTY_STRING );
128 populateAddLegacyArtifactPathActionFields( addLegacyArtifactPathAction, legacyArtifactPath, EMPTY_STRING,
129 EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING );
132 actionValidatorManager.validate( addLegacyArtifactPathAction, EMPTY_STRING );
135 assertTrue( addLegacyArtifactPathAction.hasFieldErrors() );
137 Map<String, List<String>> fieldErrors = addLegacyArtifactPathAction.getFieldErrors();
139 // make an expected field error object
140 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
143 List<String> expectedErrorMessages = new ArrayList<String>();
144 expectedErrorMessages.add( "You must enter a legacy path." );
145 expectedFieldErrors.put( "legacyArtifactPath.path", expectedErrorMessages );
147 expectedErrorMessages = new ArrayList<String>();
148 expectedErrorMessages.add( "You must enter a groupId." );
149 expectedFieldErrors.put( "groupId", expectedErrorMessages );
151 expectedErrorMessages = new ArrayList<String>();
152 expectedErrorMessages.add( "You must enter an artifactId." );
153 expectedFieldErrors.put( "artifactId", expectedErrorMessages );
155 expectedErrorMessages = new ArrayList<String>();
156 expectedErrorMessages.add( "You must enter a version." );
157 expectedFieldErrors.put( "version", expectedErrorMessages );
159 expectedErrorMessages = new ArrayList<String>();
160 expectedErrorMessages.add( "You must enter a type." );
161 expectedFieldErrors.put( "type", expectedErrorMessages );
163 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
166 public void testStruts2ValidationFrameworkWithInvalidInputs()
170 LegacyArtifactPath legacyArtifactPath = createLegacyArtifactPath( LEGACY_ARTIFACT_PATH_PATH_INVALID_INPUT );
171 populateAddLegacyArtifactPathActionFields( addLegacyArtifactPathAction, legacyArtifactPath,
172 GROUP_ID_INVALID_INPUT, ARTIFACT_ID_INVALID_INPUT,
173 VERSION_INVALID_INPUT, CLASSIFIER_INVALID_INPUT,
174 TYPE_INVALID_INPUT );
177 actionValidatorManager.validate( addLegacyArtifactPathAction, EMPTY_STRING );
180 assertTrue( addLegacyArtifactPathAction.hasFieldErrors() );
182 Map<String, List<String>> fieldErrors = addLegacyArtifactPathAction.getFieldErrors();
184 // make an expected field error object
185 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
188 List<String> expectedErrorMessages = new ArrayList<String>();
189 expectedErrorMessages.add(
190 "Legacy path must only contain alphanumeric characters, forward-slashes(/), back-slashes(\\), underscores(_), dots(.), and dashes(-)." );
191 expectedFieldErrors.put( "legacyArtifactPath.path", expectedErrorMessages );
193 expectedErrorMessages = new ArrayList<String>();
194 expectedErrorMessages.add(
195 "Group id must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
196 expectedFieldErrors.put( "groupId", expectedErrorMessages );
198 expectedErrorMessages = new ArrayList<String>();
199 expectedErrorMessages.add(
200 "Artifact id must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
201 expectedFieldErrors.put( "artifactId", expectedErrorMessages );
203 expectedErrorMessages = new ArrayList<String>();
204 expectedErrorMessages.add(
205 "Version must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
206 expectedFieldErrors.put( "version", expectedErrorMessages );
208 expectedErrorMessages = new ArrayList<String>();
209 expectedErrorMessages.add(
210 "Classifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
211 expectedFieldErrors.put( "classifier", expectedErrorMessages );
213 expectedErrorMessages = new ArrayList<String>();
214 expectedErrorMessages.add(
215 "Type must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
216 expectedFieldErrors.put( "type", expectedErrorMessages );
218 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
221 public void testStruts2ValidationFrameworkWithValidInputs()
225 LegacyArtifactPath legacyArtifactPath = createLegacyArtifactPath( LEGACY_ARTIFACT_PATH_PATH_VALID_INPUT );
226 populateAddLegacyArtifactPathActionFields( addLegacyArtifactPathAction, legacyArtifactPath,
227 GROUP_ID_VALID_INPUT, ARTIFACT_ID_VALID_INPUT, VERSION_VALID_INPUT,
228 CLASSIFIER_VALID_INPUT, TYPE_VALID_INPUT );
231 actionValidatorManager.validate( addLegacyArtifactPathAction, EMPTY_STRING );
234 assertFalse( addLegacyArtifactPathAction.hasFieldErrors() );
237 private LegacyArtifactPath createLegacyArtifactPath( String path )
239 LegacyArtifactPath legacyArtifactPath = new LegacyArtifactPath();
240 legacyArtifactPath.setPath( path );
241 return legacyArtifactPath;
244 private void populateAddLegacyArtifactPathActionFields( AddLegacyArtifactPathAction addLegacyArtifactPathAction,
245 LegacyArtifactPath legacyArtifactPath, String groupId,
246 String artifactId, String version, String classifier,
249 addLegacyArtifactPathAction.setLegacyArtifactPath( legacyArtifactPath );
250 addLegacyArtifactPathAction.setGroupId( groupId );
251 addLegacyArtifactPathAction.setArtifactId( artifactId );
252 addLegacyArtifactPathAction.setVersion( version );
253 addLegacyArtifactPathAction.setClassifier( classifier );
254 addLegacyArtifactPathAction.setType( type );