001    /*
002     *  Copyright 2003-2004 The Apache Software Foundation
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.apache.commons.convert1.string;
017    
018    import junit.framework.TestSuite;
019    import junit.framework.TestCase;
020    
021    import org.apache.commons.convert1.Converter;
022    
023    import java.net.URL;
024    
025    
026    /**
027     * Test Case for the URLConverter class.
028     *
029     * @author Henri Yandell
030     * @version $Id: URLConverterTestCase.java 155441 2005-02-26 13:19:22Z dirkv $
031     */
032    
033    public class URLConverterTestCase extends TestCase {
034    
035        private Converter converter = null;
036    
037        // ------------------------------------------------------------------------
038    
039        public URLConverterTestCase(String name) {
040            super(name);
041        }
042        
043        // ------------------------------------------------------------------------
044    
045        public void setUp() throws Exception {
046            converter = makeConverter();
047        }
048    
049        public static TestSuite suite() {
050            return new TestSuite(URLConverterTestCase.class);        
051        }
052    
053        public void tearDown() throws Exception {
054            converter = null;
055        }
056    
057        // ------------------------------------------------------------------------
058        
059        protected Converter makeConverter() {
060            return new URLConverter();
061        }
062        
063        protected Class getExpectedType() {
064            return URL.class;
065        }
066    
067        // ------------------------------------------------------------------------
068    
069        public void testSimpleConversion() throws Exception {
070            String[] message= { 
071                "from String",
072                "from String",
073                "from String",
074                "from String",
075                "from String",
076                "from String",
077                "from String",
078                "from String",
079            };
080            
081            Object[] input = { 
082                "http://www.apache.org",
083                "http://www.apache.org/",
084                "ftp://cvs.apache.org",
085                "file://project.xml",
086                "http://208.185.179.12",
087                "http://www.apache.org:9999/test/thing",
088                "http://user:admin@www.apache.org:50/one/two.three",
089                "http://notreal.apache.org",
090            };
091            
092            URL[] expected = { 
093                new URL("http://www.apache.org"),
094                new URL("http://www.apache.org/"),
095                new URL("ftp://cvs.apache.org"),
096                new URL("file://project.xml"),
097                new URL("http://208.185.179.12"),
098                new URL("http://www.apache.org:9999/test/thing"),
099                new URL("http://user:admin@www.apache.org:50/one/two.three"),
100                new URL("http://notreal.apache.org")
101            };
102            
103            for(int i=0;i<expected.length;i++) {
104                assertEquals(message[i] + " to URL",expected[i],converter.convert(URL.class,input[i]));
105                assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
106            }
107        }
108        
109    }
110