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.archiva.admin.repository.networkproxy.NetworkProxy;
30 import org.apache.maven.archiva.configuration.NetworkProxyConfiguration;
31 import org.apache.maven.archiva.web.action.admin.repositories.DefaultActionValidatorManagerFactory;
32 import org.apache.archiva.web.validator.utils.ValidatorUtil;
34 public class ConfigureNetworkProxyActionTest extends TestCase
36 private static final String EMPTY_STRING = "";
38 private static final String VALIDATION_CONTEXT = "saveNetworkProxy";
41 private static final String PROXY_ID_VALID_INPUT = "abcXYZ0129._-";
43 private static final String PROXY_PROTOCOL_VALID_INPUT = "-abcXYZ0129./:\\";
45 private static final String PROXY_HOST_VALID_INPUT = "abcXYZ0129._/\\~:?!&=-";
47 private static final int PROXY_PORT_VALID_INPUT = 8080;
49 private static final String PROXY_USERNAME_VALID_INPUT = "abcXYZ0129.@/_-\\";
52 private static final String PROXY_ID_INVALID_INPUT = "<> \\/~+[ ]'\"";
54 private static final String PROXY_PROTOCOL_INVALID_INPUT = "<> ~+[ ]'\"";
56 private static final String PROXY_HOST_INVALID_INPUT = "<> ~+[ ]'\"";
58 private static final int PROXY_PORT_INVALID_INPUT = 0;
60 private static final String PROXY_USERNAME_INVALID_INPUT = "<> ~+[ ]'\"";
63 private ConfigureNetworkProxyAction configureNetworkProxyAction;
65 private ActionValidatorManager actionValidatorManager;
71 configureNetworkProxyAction = new ConfigureNetworkProxyAction();
73 DefaultActionValidatorManagerFactory factory = new DefaultActionValidatorManagerFactory();
75 actionValidatorManager = factory.createDefaultActionValidatorManager();
78 public void testStruts2ValidationFrameworkWithNullInputs() throws Exception
81 NetworkProxy networkProxy = createNetworkProxy(null, null, null, null);
82 configureNetworkProxyAction.setProxy(networkProxy);
85 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
88 assertTrue(configureNetworkProxyAction.hasFieldErrors());
90 Map<String, List<String>> fieldErrors = configureNetworkProxyAction.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 an identifier.");
98 expectedFieldErrors.put("proxy.id", expectedErrorMessages);
100 expectedErrorMessages = new ArrayList<String>();
101 expectedErrorMessages.add("You must enter a protocol.");
102 expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
104 expectedErrorMessages = new ArrayList<String>();
105 expectedErrorMessages.add("You must enter a host.");
106 expectedFieldErrors.put("proxy.host", expectedErrorMessages);
108 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
111 public void testStruts2ValidationFrameworkWithBlankInputs() throws Exception
114 NetworkProxy networkProxy = createNetworkProxy(EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING);
115 configureNetworkProxyAction.setProxy(networkProxy);
118 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
121 assertTrue(configureNetworkProxyAction.hasFieldErrors());
123 Map<String, List<String>> fieldErrors = configureNetworkProxyAction.getFieldErrors();
125 // make an expected field error object
126 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
129 List<String> expectedErrorMessages = new ArrayList<String>();
130 expectedErrorMessages.add("You must enter an identifier.");
131 expectedFieldErrors.put("proxy.id", expectedErrorMessages);
133 expectedErrorMessages = new ArrayList<String>();
134 expectedErrorMessages.add("You must enter a protocol.");
135 expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
137 expectedErrorMessages = new ArrayList<String>();
138 expectedErrorMessages.add("You must enter a host.");
139 expectedFieldErrors.put("proxy.host", expectedErrorMessages);
141 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
144 public void testStruts2ValidationFrameworkWithInvalidInputs() throws Exception
147 NetworkProxy networkProxy = createNetworkProxy( PROXY_ID_INVALID_INPUT, PROXY_HOST_INVALID_INPUT,
148 PROXY_PORT_INVALID_INPUT, PROXY_PROTOCOL_INVALID_INPUT,
149 PROXY_USERNAME_INVALID_INPUT );
150 configureNetworkProxyAction.setProxy(networkProxy);
153 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
156 assertTrue(configureNetworkProxyAction.hasFieldErrors());
158 Map<String, List<String>> fieldErrors = configureNetworkProxyAction.getFieldErrors();
160 // make an expected field error object
161 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
164 List<String> expectedErrorMessages = new ArrayList<String>();
165 expectedErrorMessages.add("Proxy id must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-).");
166 expectedFieldErrors.put("proxy.id", expectedErrorMessages);
168 expectedErrorMessages = new ArrayList<String>();
169 expectedErrorMessages.add("Protocol must only contain alphanumeric characters, forward-slashes(/), back-slashes(\\), dots(.), colons(:), and dashes(-).");
170 expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
172 expectedErrorMessages = new ArrayList<String>();
173 expectedErrorMessages.add("Host must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-).");
174 expectedFieldErrors.put("proxy.host", expectedErrorMessages);
176 expectedErrorMessages = new ArrayList<String>();
177 expectedErrorMessages.add("Port needs to be larger than 1");
178 expectedFieldErrors.put("proxy.port", expectedErrorMessages);
180 expectedErrorMessages = new ArrayList<String>();
181 expectedErrorMessages.add("Username must only contain alphanumeric characters, at's(@), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), and dashes(-).");
182 expectedFieldErrors.put("proxy.username", expectedErrorMessages);
184 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
187 public void testStruts2ValidationFrameworkWithValidInputs() throws Exception
190 NetworkProxy networkProxy = createNetworkProxy(PROXY_ID_VALID_INPUT, PROXY_HOST_VALID_INPUT, PROXY_PORT_VALID_INPUT, PROXY_PROTOCOL_VALID_INPUT, PROXY_USERNAME_VALID_INPUT);
191 configureNetworkProxyAction.setProxy(networkProxy);
194 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
197 assertFalse(configureNetworkProxyAction.hasFieldErrors());
200 private NetworkProxy createNetworkProxy(String id, String host, int port, String protocol, String username)
202 NetworkProxy networkProxy = new NetworkProxy();
203 networkProxy.setId( id );
204 networkProxy.setHost( host );
205 networkProxy.setPort( port );
206 networkProxy.setProtocol( protocol );
207 networkProxy.setUsername( username );
212 // for simulating empty/null form purposes; excluding primitive data-typed values
213 private NetworkProxy createNetworkProxy(String id, String host, String protocol, String username)
215 NetworkProxy networkProxy = new NetworkProxy();
216 networkProxy.setId( id );
217 networkProxy.setHost( host );
218 networkProxy.setProtocol( protocol );
219 networkProxy.setUsername( username );