1 package org.apache.maven.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 java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
28 import junit.framework.TestCase;
29 import org.apache.maven.archiva.configuration.LegacyArtifactPath;
30 import org.apache.maven.archiva.web.action.admin.repositories.DefaultActionValidatorManagerFactory;
31 import org.apache.archiva.web.validator.utils.ValidatorUtil;
33 public class AddLegacyArtifactPathActionTest extends TestCase
35 private static final String EMPTY_STRING = "";
38 private static final String LEGACY_ARTIFACT_PATH_PATH_VALID_INPUT = "-abcXYZ0129._/\\";
40 private static final String GROUP_ID_VALID_INPUT = "abcXYZ0129._-";
42 private static final String ARTIFACT_ID_VALID_INPUT = "abcXYZ0129._-";
44 private static final String VERSION_VALID_INPUT = "abcXYZ0129._-";
46 private static final String CLASSIFIER_VALID_INPUT = "abcXYZ0129._-";
48 private static final String TYPE_VALID_INPUT = "abcXYZ0129._-";
51 private static final String LEGACY_ARTIFACT_PATH_PATH_INVALID_INPUT = "<> ~+[ ]'\"";
53 private static final String GROUP_ID_INVALID_INPUT = "<> \\/~+[ ]'\"";
55 private static final String ARTIFACT_ID_INVALID_INPUT = "<> \\/~+[ ]'\"";
57 private static final String VERSION_INVALID_INPUT = "<> \\/~+[ ]'\"";
59 private static final String CLASSIFIER_INVALID_INPUT = "<> \\/~+[ ]'\"";
61 private static final String TYPE_INVALID_INPUT = "<> \\/~+[ ]'\"";
64 private AddLegacyArtifactPathAction addLegacyArtifactPathAction;
66 private ActionValidatorManager actionValidatorManager;
69 public void setUp() throws Exception
71 addLegacyArtifactPathAction = new AddLegacyArtifactPathAction();
73 DefaultActionValidatorManagerFactory factory = new DefaultActionValidatorManagerFactory();
75 actionValidatorManager = factory.createDefaultActionValidatorManager();
78 public void testStruts2ValidationFrameworkWithNullInputs() throws Exception
81 LegacyArtifactPath legacyArtifactPath = createLegacyArtifactPath(null);
82 populateAddLegacyArtifactPathActionFields(addLegacyArtifactPathAction, legacyArtifactPath, null, null, null, null, null);
85 actionValidatorManager.validate(addLegacyArtifactPathAction, EMPTY_STRING);
88 assertTrue(addLegacyArtifactPathAction.hasFieldErrors());
90 Map<String, List<String>> fieldErrors = addLegacyArtifactPathAction.getFieldErrors();
92 // make an expected field error object
93 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
96 List<String> expectedErrorMessages = new ArrayList<String>();
97 expectedErrorMessages.add("You must enter a legacy path.");
98 expectedFieldErrors.put("legacyArtifactPath.path", expectedErrorMessages);
100 expectedErrorMessages = new ArrayList<String>();
101 expectedErrorMessages.add("You must enter a groupId.");
102 expectedFieldErrors.put("groupId", expectedErrorMessages);
104 expectedErrorMessages = new ArrayList<String>();
105 expectedErrorMessages.add("You must enter an artifactId.");
106 expectedFieldErrors.put("artifactId", expectedErrorMessages);
108 expectedErrorMessages = new ArrayList<String>();
109 expectedErrorMessages.add("You must enter a version.");
110 expectedFieldErrors.put("version", expectedErrorMessages);
112 expectedErrorMessages = new ArrayList<String>();
113 expectedErrorMessages.add("You must enter a type.");
114 expectedFieldErrors.put("type", expectedErrorMessages);
116 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
119 public void testStruts2ValidationFrameworkWithBlankInputs() throws Exception
122 LegacyArtifactPath legacyArtifactPath = createLegacyArtifactPath(EMPTY_STRING);
123 populateAddLegacyArtifactPathActionFields(addLegacyArtifactPathAction, legacyArtifactPath, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING);
126 actionValidatorManager.validate(addLegacyArtifactPathAction, EMPTY_STRING);
129 assertTrue(addLegacyArtifactPathAction.hasFieldErrors());
131 Map<String, List<String>> fieldErrors = addLegacyArtifactPathAction.getFieldErrors();
133 // make an expected field error object
134 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
137 List<String> expectedErrorMessages = new ArrayList<String>();
138 expectedErrorMessages.add("You must enter a legacy path.");
139 expectedFieldErrors.put("legacyArtifactPath.path", expectedErrorMessages);
141 expectedErrorMessages = new ArrayList<String>();
142 expectedErrorMessages.add("You must enter a groupId.");
143 expectedFieldErrors.put("groupId", expectedErrorMessages);
145 expectedErrorMessages = new ArrayList<String>();
146 expectedErrorMessages.add("You must enter an artifactId.");
147 expectedFieldErrors.put("artifactId", expectedErrorMessages);
149 expectedErrorMessages = new ArrayList<String>();
150 expectedErrorMessages.add("You must enter a version.");
151 expectedFieldErrors.put("version", expectedErrorMessages);
153 expectedErrorMessages = new ArrayList<String>();
154 expectedErrorMessages.add("You must enter a type.");
155 expectedFieldErrors.put("type", expectedErrorMessages);
157 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
160 public void testStruts2ValidationFrameworkWithInvalidInputs() throws Exception
163 LegacyArtifactPath legacyArtifactPath = createLegacyArtifactPath(LEGACY_ARTIFACT_PATH_PATH_INVALID_INPUT);
164 populateAddLegacyArtifactPathActionFields(addLegacyArtifactPathAction, legacyArtifactPath, GROUP_ID_INVALID_INPUT, ARTIFACT_ID_INVALID_INPUT, VERSION_INVALID_INPUT, CLASSIFIER_INVALID_INPUT, TYPE_INVALID_INPUT);
167 actionValidatorManager.validate(addLegacyArtifactPathAction, EMPTY_STRING);
170 assertTrue(addLegacyArtifactPathAction.hasFieldErrors());
172 Map<String, List<String>> fieldErrors = addLegacyArtifactPathAction.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("Legacy path must only contain alphanumeric characters, forward-slashes(/), back-slashes(\\), underscores(_), dots(.), and dashes(-).");
180 expectedFieldErrors.put("legacyArtifactPath.path", expectedErrorMessages);
182 expectedErrorMessages = new ArrayList<String>();
183 expectedErrorMessages.add("Group id must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-).");
184 expectedFieldErrors.put("groupId", expectedErrorMessages);
186 expectedErrorMessages = new ArrayList<String>();
187 expectedErrorMessages.add("Artifact id must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-).");
188 expectedFieldErrors.put("artifactId", expectedErrorMessages);
190 expectedErrorMessages = new ArrayList<String>();
191 expectedErrorMessages.add("Version must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-).");
192 expectedFieldErrors.put("version", expectedErrorMessages);
194 expectedErrorMessages = new ArrayList<String>();
195 expectedErrorMessages.add("Classifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-).");
196 expectedFieldErrors.put("classifier", expectedErrorMessages);
198 expectedErrorMessages = new ArrayList<String>();
199 expectedErrorMessages.add("Type must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-).");
200 expectedFieldErrors.put("type", expectedErrorMessages);
202 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
205 public void testStruts2ValidationFrameworkWithValidInputs() throws Exception
208 LegacyArtifactPath legacyArtifactPath = createLegacyArtifactPath(LEGACY_ARTIFACT_PATH_PATH_VALID_INPUT);
209 populateAddLegacyArtifactPathActionFields(addLegacyArtifactPathAction, legacyArtifactPath, GROUP_ID_VALID_INPUT, ARTIFACT_ID_VALID_INPUT, VERSION_VALID_INPUT, CLASSIFIER_VALID_INPUT, TYPE_VALID_INPUT);
212 actionValidatorManager.validate(addLegacyArtifactPathAction, EMPTY_STRING);
215 assertFalse(addLegacyArtifactPathAction.hasFieldErrors());
218 private LegacyArtifactPath createLegacyArtifactPath(String path)
220 LegacyArtifactPath legacyArtifactPath = new LegacyArtifactPath();
221 legacyArtifactPath.setPath(path);
222 return legacyArtifactPath;
225 private void populateAddLegacyArtifactPathActionFields(AddLegacyArtifactPathAction addLegacyArtifactPathAction, LegacyArtifactPath legacyArtifactPath, String groupId, String artifactId, String version, String classifier, String type)
227 addLegacyArtifactPathAction.setLegacyArtifactPath(legacyArtifactPath);
228 addLegacyArtifactPathAction.setGroupId(groupId);
229 addLegacyArtifactPathAction.setArtifactId(artifactId);
230 addLegacyArtifactPathAction.setVersion(version);
231 addLegacyArtifactPathAction.setClassifier(classifier);
232 addLegacyArtifactPathAction.setType(type);