1 package org.apache.maven.archiva.web.action.admin.networkproxies;
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.NetworkProxyConfiguration;
30 import org.apache.maven.archiva.web.action.admin.repositories.DefaultActionValidatorManagerFactory;
31 import org.apache.archiva.web.validator.utils.ValidatorUtil;
33 public class ConfigureNetworkProxyActionTest extends TestCase
35 private static final String EMPTY_STRING = "";
37 private static final String VALIDATION_CONTEXT = "saveNetworkProxy";
40 private static final String PROXY_ID_VALID_INPUT = "abcXYZ0129._-";
42 private static final String PROXY_PROTOCOL_VALID_INPUT = "-abcXYZ0129./:\\";
44 private static final String PROXY_HOST_VALID_INPUT = "abcXYZ0129._/\\~:?!&=-";
46 private static final int PROXY_PORT_VALID_INPUT = 8080;
48 private static final String PROXY_USERNAME_VALID_INPUT = "abcXYZ0129.@/_-\\";
51 private static final String PROXY_ID_INVALID_INPUT = "<> \\/~+[ ]'\"";
53 private static final String PROXY_PROTOCOL_INVALID_INPUT = "<> ~+[ ]'\"";
55 private static final String PROXY_HOST_INVALID_INPUT = "<> ~+[ ]'\"";
57 private static final int PROXY_PORT_INVALID_INPUT = 0;
59 private static final String PROXY_USERNAME_INVALID_INPUT = "<> ~+[ ]'\"";
62 private ConfigureNetworkProxyAction configureNetworkProxyAction;
64 private ActionValidatorManager actionValidatorManager;
70 configureNetworkProxyAction = new ConfigureNetworkProxyAction();
72 DefaultActionValidatorManagerFactory factory = new DefaultActionValidatorManagerFactory();
74 actionValidatorManager = factory.createDefaultActionValidatorManager();
77 public void testStruts2ValidationFrameworkWithNullInputs() throws Exception
80 NetworkProxyConfiguration networkProxyConfiguration = createNetworkProxyConfiguration(null, null, null, null);
81 configureNetworkProxyAction.setProxy(networkProxyConfiguration);
84 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
87 assertTrue(configureNetworkProxyAction.hasFieldErrors());
89 Map<String, List<String>> fieldErrors = configureNetworkProxyAction.getFieldErrors();
91 // make an expected field error object
92 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
95 List<String> expectedErrorMessages = new ArrayList<String>();
96 expectedErrorMessages.add("You must enter an identifier.");
97 expectedFieldErrors.put("proxy.id", expectedErrorMessages);
99 expectedErrorMessages = new ArrayList<String>();
100 expectedErrorMessages.add("You must enter a protocol.");
101 expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
103 expectedErrorMessages = new ArrayList<String>();
104 expectedErrorMessages.add("You must enter a host.");
105 expectedFieldErrors.put("proxy.host", expectedErrorMessages);
107 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
110 public void testStruts2ValidationFrameworkWithBlankInputs() throws Exception
113 NetworkProxyConfiguration networkProxyConfiguration = createNetworkProxyConfiguration(EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING);
114 configureNetworkProxyAction.setProxy(networkProxyConfiguration);
117 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
120 assertTrue(configureNetworkProxyAction.hasFieldErrors());
122 Map<String, List<String>> fieldErrors = configureNetworkProxyAction.getFieldErrors();
124 // make an expected field error object
125 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
128 List<String> expectedErrorMessages = new ArrayList<String>();
129 expectedErrorMessages.add("You must enter an identifier.");
130 expectedFieldErrors.put("proxy.id", expectedErrorMessages);
132 expectedErrorMessages = new ArrayList<String>();
133 expectedErrorMessages.add("You must enter a protocol.");
134 expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
136 expectedErrorMessages = new ArrayList<String>();
137 expectedErrorMessages.add("You must enter a host.");
138 expectedFieldErrors.put("proxy.host", expectedErrorMessages);
140 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
143 public void testStruts2ValidationFrameworkWithInvalidInputs() throws Exception
146 NetworkProxyConfiguration networkProxyConfiguration = createNetworkProxyConfiguration(PROXY_ID_INVALID_INPUT, PROXY_HOST_INVALID_INPUT, PROXY_PORT_INVALID_INPUT, PROXY_PROTOCOL_INVALID_INPUT, PROXY_USERNAME_INVALID_INPUT);
147 configureNetworkProxyAction.setProxy(networkProxyConfiguration);
150 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
153 assertTrue(configureNetworkProxyAction.hasFieldErrors());
155 Map<String, List<String>> fieldErrors = configureNetworkProxyAction.getFieldErrors();
157 // make an expected field error object
158 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
161 List<String> expectedErrorMessages = new ArrayList<String>();
162 expectedErrorMessages.add("Proxy id must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-).");
163 expectedFieldErrors.put("proxy.id", expectedErrorMessages);
165 expectedErrorMessages = new ArrayList<String>();
166 expectedErrorMessages.add("Protocol must only contain alphanumeric characters, forward-slashes(/), back-slashes(\\), dots(.), colons(:), and dashes(-).");
167 expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
169 expectedErrorMessages = new ArrayList<String>();
170 expectedErrorMessages.add("Host must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-).");
171 expectedFieldErrors.put("proxy.host", expectedErrorMessages);
173 expectedErrorMessages = new ArrayList<String>();
174 expectedErrorMessages.add("Port needs to be larger than 1");
175 expectedFieldErrors.put("proxy.port", expectedErrorMessages);
177 expectedErrorMessages = new ArrayList<String>();
178 expectedErrorMessages.add("Username must only contain alphanumeric characters, at's(@), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), and dashes(-).");
179 expectedFieldErrors.put("proxy.username", expectedErrorMessages);
181 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
184 public void testStruts2ValidationFrameworkWithValidInputs() throws Exception
187 NetworkProxyConfiguration networkProxyConfiguration = createNetworkProxyConfiguration(PROXY_ID_VALID_INPUT, PROXY_HOST_VALID_INPUT, PROXY_PORT_VALID_INPUT, PROXY_PROTOCOL_VALID_INPUT, PROXY_USERNAME_VALID_INPUT);
188 configureNetworkProxyAction.setProxy(networkProxyConfiguration);
191 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
194 assertFalse(configureNetworkProxyAction.hasFieldErrors());
197 private NetworkProxyConfiguration createNetworkProxyConfiguration(String id, String host, int port, String protocol, String username)
199 NetworkProxyConfiguration networkProxyConfiguration = new NetworkProxyConfiguration();
200 networkProxyConfiguration.setId(id);
201 networkProxyConfiguration.setHost(host);
202 networkProxyConfiguration.setPort(port);
203 networkProxyConfiguration.setProtocol(protocol);
204 networkProxyConfiguration.setUsername(username);
205 return networkProxyConfiguration;
209 // for simulating empty/null form purposes; excluding primitive data-typed values
210 private NetworkProxyConfiguration createNetworkProxyConfiguration(String id, String host, String protocol, String username)
212 NetworkProxyConfiguration networkProxyConfiguration = new NetworkProxyConfiguration();
213 networkProxyConfiguration.setId(id);
214 networkProxyConfiguration.setHost(host);
215 networkProxyConfiguration.setProtocol(protocol);
216 networkProxyConfiguration.setUsername(username);
217 return networkProxyConfiguration;