View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli2.validation;
18  
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  
22  import java.util.Arrays;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.commons.cli2.resource.ResourceConstants;
29  import org.apache.commons.cli2.resource.ResourceHelper;
30  
31  public class UrlValidatorTest
32      extends TestCase {
33      private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
34  
35      public void testValidate()
36          throws InvalidArgumentException, MalformedURLException {
37          final Object[] array = new Object[] { "http://www.apache.org/", "file:///etc" };
38          final List list = Arrays.asList(array);
39          final Validator validator = new UrlValidator();
40  
41          validator.validate(list);
42  
43          final Iterator i = list.iterator();
44          assertEquals(new URL("http://www.apache.org/"), i.next());
45          assertEquals(new URL("file:///etc"), i.next());
46          assertFalse(i.hasNext());
47      }
48  
49      public void testMalformedURL()
50          throws InvalidArgumentException, MalformedURLException {
51          final Object[] array = new Object[] { "www.apache.org" };
52          final List list = Arrays.asList(array);
53          final Validator validator = new UrlValidator();
54  
55          try {
56              validator.validate(list);
57          } catch (InvalidArgumentException e) {
58              assertEquals(resources.getMessage(ResourceConstants.URLVALIDATOR_MALFORMED_URL,
59                                                new Object[] { "www.apache.org" }), e.getMessage());
60          }
61      }
62  
63      public void testBadProtocol() {
64          {
65              final Object[] array = new Object[] { "http://www.apache.org/", "file:///etc" };
66              final List list = Arrays.asList(array);
67              final UrlValidator validator = new UrlValidator();
68              validator.setProtocol("http");
69  
70              assertEquals("incorrect protocol", "http", validator.getProtocol());
71  
72              try {
73                  validator.validate(list);
74                  fail("Expected InvalidArgumentException");
75              } catch (InvalidArgumentException e) {
76                  assertEquals("file:///etc", e.getMessage());
77              }
78          }
79  
80          {
81              final Object[] array = new Object[] { "http://www.apache.org/", "file:///etc" };
82              final List list = Arrays.asList(array);
83              final UrlValidator validator = new UrlValidator("http");
84  
85              try {
86                  validator.validate(list);
87                  fail("Expected InvalidArgumentException");
88              } catch (InvalidArgumentException e) {
89                  assertEquals("file:///etc", e.getMessage());
90              }
91          }
92      }
93  }