001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.cli2.validation; 018 019import java.net.MalformedURLException; 020import java.net.URL; 021 022import java.util.Arrays; 023import java.util.Iterator; 024import java.util.List; 025 026import junit.framework.TestCase; 027 028import org.apache.commons.cli2.resource.ResourceConstants; 029import org.apache.commons.cli2.resource.ResourceHelper; 030 031public class UrlValidatorTest 032 extends TestCase { 033 private static final ResourceHelper resources = ResourceHelper.getResourceHelper(); 034 035 public void testValidate() 036 throws InvalidArgumentException, MalformedURLException { 037 final Object[] array = new Object[] { "http://www.apache.org/", "file:///etc" }; 038 final List list = Arrays.asList(array); 039 final Validator validator = new UrlValidator(); 040 041 validator.validate(list); 042 043 final Iterator i = list.iterator(); 044 assertEquals(new URL("http://www.apache.org/"), i.next()); 045 assertEquals(new URL("file:///etc"), i.next()); 046 assertFalse(i.hasNext()); 047 } 048 049 public void testMalformedURL() 050 throws InvalidArgumentException, MalformedURLException { 051 final Object[] array = new Object[] { "www.apache.org" }; 052 final List list = Arrays.asList(array); 053 final Validator validator = new UrlValidator(); 054 055 try { 056 validator.validate(list); 057 } catch (InvalidArgumentException e) { 058 assertEquals(resources.getMessage(ResourceConstants.URLVALIDATOR_MALFORMED_URL, 059 new Object[] { "www.apache.org" }), e.getMessage()); 060 } 061 } 062 063 public void testBadProtocol() { 064 { 065 final Object[] array = new Object[] { "http://www.apache.org/", "file:///etc" }; 066 final List list = Arrays.asList(array); 067 final UrlValidator validator = new UrlValidator(); 068 validator.setProtocol("http"); 069 070 assertEquals("incorrect protocol", "http", validator.getProtocol()); 071 072 try { 073 validator.validate(list); 074 fail("Expected InvalidArgumentException"); 075 } catch (InvalidArgumentException e) { 076 assertEquals("file:///etc", e.getMessage()); 077 } 078 } 079 080 { 081 final Object[] array = new Object[] { "http://www.apache.org/", "file:///etc" }; 082 final List list = Arrays.asList(array); 083 final UrlValidator validator = new UrlValidator("http"); 084 085 try { 086 validator.validate(list); 087 fail("Expected InvalidArgumentException"); 088 } catch (InvalidArgumentException e) { 089 assertEquals("file:///etc", e.getMessage()); 090 } 091 } 092 } 093}