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;
32 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
37 public class AddLegacyArtifactPathActionTest
40 private static final String EMPTY_STRING = "";
43 private static final String LEGACY_ARTIFACT_PATH_PATH_VALID_INPUT = "-abcXYZ0129._/\\";
45 private static final String GROUP_ID_VALID_INPUT = "abcXYZ0129._-";
47 private static final String ARTIFACT_ID_VALID_INPUT = "abcXYZ0129._-";
49 private static final String VERSION_VALID_INPUT = "abcXYZ0129._-";
51 private static final String CLASSIFIER_VALID_INPUT = "abcXYZ0129._-";
53 private static final String TYPE_VALID_INPUT = "abcXYZ0129._-";
56 private static final String LEGACY_ARTIFACT_PATH_PATH_INVALID_INPUT = "<> ~+[ ]'\"";
58 private static final String GROUP_ID_INVALID_INPUT = "<> \\/~+[ ]'\"";
60 private static final String ARTIFACT_ID_INVALID_INPUT = "<> \\/~+[ ]'\"";
62 private static final String VERSION_INVALID_INPUT = "<> \\/~+[ ]'\"";
64 private static final String CLASSIFIER_INVALID_INPUT = "<> \\/~+[ ]'\"";
66 private static final String TYPE_INVALID_INPUT = "<> \\/~+[ ]'\"";
69 private AddLegacyArtifactPathAction addLegacyArtifactPathAction;
71 private ActionValidatorManager actionValidatorManager;
78 addLegacyArtifactPathAction = new AddLegacyArtifactPathAction();
80 DefaultActionValidatorManagerFactory factory = new DefaultActionValidatorManagerFactory();
82 actionValidatorManager = factory.createDefaultActionValidatorManager();
86 public void testStruts2ValidationFrameworkWithNullInputs()
90 LegacyArtifactPath legacyArtifactPath = createLegacyArtifactPath( null );
91 populateAddLegacyArtifactPathActionFields( addLegacyArtifactPathAction, legacyArtifactPath, null, null, null,
95 actionValidatorManager.validate( addLegacyArtifactPathAction, EMPTY_STRING );
98 assertTrue( addLegacyArtifactPathAction.hasFieldErrors() );
100 Map<String, List<String>> fieldErrors = addLegacyArtifactPathAction.getFieldErrors();
102 // make an expected field error object
103 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
106 List<String> expectedErrorMessages = new ArrayList<String>();
107 expectedErrorMessages.add( "You must enter a legacy path." );
108 expectedFieldErrors.put( "legacyArtifactPath.path", expectedErrorMessages );
110 expectedErrorMessages = new ArrayList<String>();
111 expectedErrorMessages.add( "You must enter a groupId." );
112 expectedFieldErrors.put( "groupId", expectedErrorMessages );
114 expectedErrorMessages = new ArrayList<String>();
115 expectedErrorMessages.add( "You must enter an artifactId." );
116 expectedFieldErrors.put( "artifactId", expectedErrorMessages );
118 expectedErrorMessages = new ArrayList<String>();
119 expectedErrorMessages.add( "You must enter a version." );
120 expectedFieldErrors.put( "version", expectedErrorMessages );
122 expectedErrorMessages = new ArrayList<String>();
123 expectedErrorMessages.add( "You must enter a type." );
124 expectedFieldErrors.put( "type", expectedErrorMessages );
126 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
130 public void testStruts2ValidationFrameworkWithBlankInputs()
134 LegacyArtifactPath legacyArtifactPath = createLegacyArtifactPath( EMPTY_STRING );
135 populateAddLegacyArtifactPathActionFields( addLegacyArtifactPathAction, legacyArtifactPath, EMPTY_STRING,
136 EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING );
139 actionValidatorManager.validate( addLegacyArtifactPathAction, EMPTY_STRING );
142 assertTrue( addLegacyArtifactPathAction.hasFieldErrors() );
144 Map<String, List<String>> fieldErrors = addLegacyArtifactPathAction.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 legacy path." );
152 expectedFieldErrors.put( "legacyArtifactPath.path", expectedErrorMessages );
154 expectedErrorMessages = new ArrayList<String>();
155 expectedErrorMessages.add( "You must enter a groupId." );
156 expectedFieldErrors.put( "groupId", expectedErrorMessages );
158 expectedErrorMessages = new ArrayList<String>();
159 expectedErrorMessages.add( "You must enter an artifactId." );
160 expectedFieldErrors.put( "artifactId", expectedErrorMessages );
162 expectedErrorMessages = new ArrayList<String>();
163 expectedErrorMessages.add( "You must enter a version." );
164 expectedFieldErrors.put( "version", expectedErrorMessages );
166 expectedErrorMessages = new ArrayList<String>();
167 expectedErrorMessages.add( "You must enter a type." );
168 expectedFieldErrors.put( "type", expectedErrorMessages );
170 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
174 public void testStruts2ValidationFrameworkWithInvalidInputs()
178 LegacyArtifactPath legacyArtifactPath = createLegacyArtifactPath( LEGACY_ARTIFACT_PATH_PATH_INVALID_INPUT );
179 populateAddLegacyArtifactPathActionFields( addLegacyArtifactPathAction, legacyArtifactPath,
180 GROUP_ID_INVALID_INPUT, ARTIFACT_ID_INVALID_INPUT,
181 VERSION_INVALID_INPUT, CLASSIFIER_INVALID_INPUT,
182 TYPE_INVALID_INPUT );
185 actionValidatorManager.validate( addLegacyArtifactPathAction, EMPTY_STRING );
188 assertTrue( addLegacyArtifactPathAction.hasFieldErrors() );
190 Map<String, List<String>> fieldErrors = addLegacyArtifactPathAction.getFieldErrors();
192 // make an expected field error object
193 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
196 List<String> expectedErrorMessages = new ArrayList<String>();
197 expectedErrorMessages.add(
198 "Legacy path must only contain alphanumeric characters, forward-slashes(/), back-slashes(\\), underscores(_), dots(.), and dashes(-)." );
199 expectedFieldErrors.put( "legacyArtifactPath.path", expectedErrorMessages );
201 expectedErrorMessages = new ArrayList<String>();
202 expectedErrorMessages.add(
203 "Group id must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
204 expectedFieldErrors.put( "groupId", expectedErrorMessages );
206 expectedErrorMessages = new ArrayList<String>();
207 expectedErrorMessages.add(
208 "Artifact id must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
209 expectedFieldErrors.put( "artifactId", expectedErrorMessages );
211 expectedErrorMessages = new ArrayList<String>();
212 expectedErrorMessages.add(
213 "Version must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
214 expectedFieldErrors.put( "version", expectedErrorMessages );
216 expectedErrorMessages = new ArrayList<String>();
217 expectedErrorMessages.add(
218 "Classifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
219 expectedFieldErrors.put( "classifier", expectedErrorMessages );
221 expectedErrorMessages = new ArrayList<String>();
222 expectedErrorMessages.add(
223 "Type must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
224 expectedFieldErrors.put( "type", expectedErrorMessages );
226 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
230 public void testStruts2ValidationFrameworkWithValidInputs()
234 LegacyArtifactPath legacyArtifactPath = createLegacyArtifactPath( LEGACY_ARTIFACT_PATH_PATH_VALID_INPUT );
235 populateAddLegacyArtifactPathActionFields( addLegacyArtifactPathAction, legacyArtifactPath,
236 GROUP_ID_VALID_INPUT, ARTIFACT_ID_VALID_INPUT, VERSION_VALID_INPUT,
237 CLASSIFIER_VALID_INPUT, TYPE_VALID_INPUT );
240 actionValidatorManager.validate( addLegacyArtifactPathAction, EMPTY_STRING );
243 assertFalse( addLegacyArtifactPathAction.hasFieldErrors() );
246 private LegacyArtifactPath createLegacyArtifactPath( String path )
248 LegacyArtifactPath legacyArtifactPath = new LegacyArtifactPath();
249 legacyArtifactPath.setPath( path );
250 return legacyArtifactPath;
253 private void populateAddLegacyArtifactPathActionFields( AddLegacyArtifactPathAction addLegacyArtifactPathAction,
254 LegacyArtifactPath legacyArtifactPath, String groupId,
255 String artifactId, String version, String classifier,
258 addLegacyArtifactPathAction.setLegacyArtifactPath( legacyArtifactPath );
259 addLegacyArtifactPathAction.setGroupId( groupId );
260 addLegacyArtifactPathAction.setArtifactId( artifactId );
261 addLegacyArtifactPathAction.setVersion( version );
262 addLegacyArtifactPathAction.setClassifier( classifier );
263 addLegacyArtifactPathAction.setType( type );