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;
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 ConfigureNetworkProxyActionTest extends TestCase
39 private static final String EMPTY_STRING = "";
41 private static final String VALIDATION_CONTEXT = "saveNetworkProxy";
44 private static final String PROXY_ID_VALID_INPUT = "abcXYZ0129._-";
46 private static final String PROXY_PROTOCOL_VALID_INPUT = "-abcXYZ0129./:\\";
48 private static final String PROXY_HOST_VALID_INPUT = "abcXYZ0129._/\\~:?!&=-";
50 private static final int PROXY_PORT_VALID_INPUT = 8080;
52 private static final String PROXY_USERNAME_VALID_INPUT = "abcXYZ0129.@/_-\\";
55 private static final String PROXY_ID_INVALID_INPUT = "<> \\/~+[ ]'\"";
57 private static final String PROXY_PROTOCOL_INVALID_INPUT = "<> ~+[ ]'\"";
59 private static final String PROXY_HOST_INVALID_INPUT = "<> ~+[ ]'\"";
61 private static final int PROXY_PORT_INVALID_INPUT = 0;
63 private static final String PROXY_USERNAME_INVALID_INPUT = "<> ~+[ ]'\"";
66 private ConfigureNetworkProxyAction configureNetworkProxyAction;
68 private ActionValidatorManager actionValidatorManager;
75 configureNetworkProxyAction = new ConfigureNetworkProxyAction();
77 DefaultActionValidatorManagerFactory factory = new DefaultActionValidatorManagerFactory();
79 actionValidatorManager = factory.createDefaultActionValidatorManager();
83 public void testStruts2ValidationFrameworkWithNullInputs() throws Exception
86 NetworkProxy networkProxy = createNetworkProxy(null, null, null, null);
87 configureNetworkProxyAction.setProxy(networkProxy);
90 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
93 assertTrue(configureNetworkProxyAction.hasFieldErrors());
95 Map<String, List<String>> fieldErrors = configureNetworkProxyAction.getFieldErrors();
97 // make an expected field error object
98 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
101 List<String> expectedErrorMessages = new ArrayList<String>();
102 expectedErrorMessages.add("You must enter an identifier.");
103 expectedFieldErrors.put("proxy.id", expectedErrorMessages);
105 expectedErrorMessages = new ArrayList<String>();
106 expectedErrorMessages.add("You must enter a protocol.");
107 expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
109 expectedErrorMessages = new ArrayList<String>();
110 expectedErrorMessages.add("You must enter a host.");
111 expectedFieldErrors.put("proxy.host", expectedErrorMessages);
113 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
117 public void testStruts2ValidationFrameworkWithBlankInputs() throws Exception
120 NetworkProxy networkProxy = createNetworkProxy(EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING);
121 configureNetworkProxyAction.setProxy(networkProxy);
124 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
127 assertTrue(configureNetworkProxyAction.hasFieldErrors());
129 Map<String, List<String>> fieldErrors = configureNetworkProxyAction.getFieldErrors();
131 // make an expected field error object
132 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
135 List<String> expectedErrorMessages = new ArrayList<String>();
136 expectedErrorMessages.add("You must enter an identifier.");
137 expectedFieldErrors.put("proxy.id", expectedErrorMessages);
139 expectedErrorMessages = new ArrayList<String>();
140 expectedErrorMessages.add("You must enter a protocol.");
141 expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
143 expectedErrorMessages = new ArrayList<String>();
144 expectedErrorMessages.add("You must enter a host.");
145 expectedFieldErrors.put("proxy.host", expectedErrorMessages);
147 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
151 public void testStruts2ValidationFrameworkWithInvalidInputs() throws Exception
154 NetworkProxy networkProxy = createNetworkProxy( PROXY_ID_INVALID_INPUT, PROXY_HOST_INVALID_INPUT,
155 PROXY_PORT_INVALID_INPUT, PROXY_PROTOCOL_INVALID_INPUT,
156 PROXY_USERNAME_INVALID_INPUT );
157 configureNetworkProxyAction.setProxy(networkProxy);
160 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
163 assertTrue(configureNetworkProxyAction.hasFieldErrors());
165 Map<String, List<String>> fieldErrors = configureNetworkProxyAction.getFieldErrors();
167 // make an expected field error object
168 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
171 List<String> expectedErrorMessages = new ArrayList<String>();
172 expectedErrorMessages.add("Proxy id must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-).");
173 expectedFieldErrors.put("proxy.id", expectedErrorMessages);
175 expectedErrorMessages = new ArrayList<String>();
176 expectedErrorMessages.add("Protocol must only contain alphanumeric characters, forward-slashes(/), back-slashes(\\), dots(.), colons(:), and dashes(-).");
177 expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
179 expectedErrorMessages = new ArrayList<String>();
180 expectedErrorMessages.add("Host must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-).");
181 expectedFieldErrors.put("proxy.host", expectedErrorMessages);
183 expectedErrorMessages = new ArrayList<String>();
184 expectedErrorMessages.add("Port needs to be larger than 1");
185 expectedFieldErrors.put("proxy.port", expectedErrorMessages);
187 expectedErrorMessages = new ArrayList<String>();
188 expectedErrorMessages.add("Username must only contain alphanumeric characters, at's(@), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), and dashes(-).");
189 expectedFieldErrors.put("proxy.username", expectedErrorMessages);
191 ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
195 public void testStruts2ValidationFrameworkWithValidInputs() throws Exception
198 NetworkProxy networkProxy = createNetworkProxy(PROXY_ID_VALID_INPUT, PROXY_HOST_VALID_INPUT, PROXY_PORT_VALID_INPUT, PROXY_PROTOCOL_VALID_INPUT, PROXY_USERNAME_VALID_INPUT);
199 configureNetworkProxyAction.setProxy(networkProxy);
202 actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
205 assertFalse(configureNetworkProxyAction.hasFieldErrors());
208 private NetworkProxy createNetworkProxy(String id, String host, int port, String protocol, String username)
210 NetworkProxy networkProxy = new NetworkProxy();
211 networkProxy.setId( id );
212 networkProxy.setHost( host );
213 networkProxy.setPort( port );
214 networkProxy.setProtocol( protocol );
215 networkProxy.setUsername( username );
220 // for simulating empty/null form purposes; excluding primitive data-typed values
221 private NetworkProxy createNetworkProxy(String id, String host, String protocol, String username)
223 NetworkProxy networkProxy = new NetworkProxy();
224 networkProxy.setId( id );
225 networkProxy.setHost( host );
226 networkProxy.setProtocol( protocol );
227 networkProxy.setUsername( username );