1 package org.apache.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 junit.framework.TestCase;
24 import org.apache.archiva.admin.model.beans.NetworkProxy;
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 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 NetworkProxy networkProxy = createNetworkProxy(null, null, null, null);
81 configureNetworkProxyAction.setProxy(networkProxy);
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 NetworkProxy networkProxy = createNetworkProxy(EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING);
114 configureNetworkProxyAction.setProxy(networkProxy);
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 NetworkProxy networkProxy = createNetworkProxy( PROXY_ID_INVALID_INPUT, PROXY_HOST_INVALID_INPUT,
147 PROXY_PORT_INVALID_INPUT, PROXY_PROTOCOL_INVALID_INPUT,
148 PROXY_USERNAME_INVALID_INPUT );
149 configureNetworkProxyAction.setProxy(networkProxy);
152 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
155 assertTrue(configureNetworkProxyAction.hasFieldErrors());
157 Map<String, List<String>> fieldErrors = configureNetworkProxyAction.getFieldErrors();
159 // make an expected field error object
160 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
163 List<String> expectedErrorMessages = new ArrayList<String>();
164 expectedErrorMessages.add("Proxy id must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-).");
165 expectedFieldErrors.put("proxy.id", expectedErrorMessages);
167 expectedErrorMessages = new ArrayList<String>();
168 expectedErrorMessages.add("Protocol must only contain alphanumeric characters, forward-slashes(/), back-slashes(\\), dots(.), colons(:), and dashes(-).");
169 expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
171 expectedErrorMessages = new ArrayList<String>();
172 expectedErrorMessages.add("Host must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-).");
173 expectedFieldErrors.put("proxy.host", expectedErrorMessages);
175 expectedErrorMessages = new ArrayList<String>();
176 expectedErrorMessages.add("Port needs to be larger than 1");
177 expectedFieldErrors.put("proxy.port", expectedErrorMessages);
179 expectedErrorMessages = new ArrayList<String>();
180 expectedErrorMessages.add("Username must only contain alphanumeric characters, at's(@), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), and dashes(-).");
181 expectedFieldErrors.put("proxy.username", expectedErrorMessages);
183 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
186 public void testStruts2ValidationFrameworkWithValidInputs() throws Exception
189 NetworkProxy networkProxy = createNetworkProxy(PROXY_ID_VALID_INPUT, PROXY_HOST_VALID_INPUT, PROXY_PORT_VALID_INPUT, PROXY_PROTOCOL_VALID_INPUT, PROXY_USERNAME_VALID_INPUT);
190 configureNetworkProxyAction.setProxy(networkProxy);
193 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
196 assertFalse(configureNetworkProxyAction.hasFieldErrors());
199 private NetworkProxy createNetworkProxy(String id, String host, int port, String protocol, String username)
201 NetworkProxy networkProxy = new NetworkProxy();
202 networkProxy.setId( id );
203 networkProxy.setHost( host );
204 networkProxy.setPort( port );
205 networkProxy.setProtocol( protocol );
206 networkProxy.setUsername( username );
211 // for simulating empty/null form purposes; excluding primitive data-typed values
212 private NetworkProxy createNetworkProxy(String id, String host, String protocol, String username)
214 NetworkProxy networkProxy = new NetworkProxy();
215 networkProxy.setId( id );
216 networkProxy.setHost( host );
217 networkProxy.setProtocol( protocol );
218 networkProxy.setUsername( username );