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  
18  package org.apache.commons.beanutils.converters;
19  
20  import junit.framework.TestSuite;
21  import junit.framework.TestCase;
22  
23  import org.apache.commons.beanutils.Converter;
24  
25  import java.net.URL;
26  
27  
28  /***
29   * Test Case for the URLConverter class.
30   *
31   * @version $Revision: 471628 $ $Date: 2006-11-06 04:06:45 +0000 (Mon, 06 Nov 2006) $
32   */
33  
34  public class URLConverterTestCase extends TestCase {
35  
36      private Converter converter = null;
37  
38      // ------------------------------------------------------------------------
39  
40      public URLConverterTestCase(String name) {
41          super(name);
42      }
43      
44      // ------------------------------------------------------------------------
45  
46      public void setUp() throws Exception {
47          converter = makeConverter();
48      }
49  
50      public static TestSuite suite() {
51          return new TestSuite(URLConverterTestCase.class);        
52      }
53  
54      public void tearDown() throws Exception {
55          converter = null;
56      }
57  
58      // ------------------------------------------------------------------------
59      
60      protected Converter makeConverter() {
61          return new URLConverter();
62      }
63      
64      protected Class getExpectedType() {
65          return URL.class;
66      }
67  
68      // ------------------------------------------------------------------------
69  
70      public void testSimpleConversion() throws Exception {
71          String[] message= { 
72              "from String",
73              "from String",
74              "from String",
75              "from String",
76              "from String",
77              "from String",
78              "from String",
79              "from String",
80          };
81          
82          Object[] input = { 
83              "http://www.apache.org",
84              "http://www.apache.org/",
85              "ftp://cvs.apache.org",
86              "file://project.xml",
87              "http://208.185.179.12",
88              "http://www.apache.org:9999/test/thing",
89              "http://user:admin@www.apache.org:50/one/two.three",
90              "http://notreal.apache.org",
91          };
92          
93          URL[] expected = { 
94              new URL("http://www.apache.org"),
95              new URL("http://www.apache.org/"),
96              new URL("ftp://cvs.apache.org"),
97              new URL("file://project.xml"),
98              new URL("http://208.185.179.12"),
99              new URL("http://www.apache.org:9999/test/thing"),
100             new URL("http://user:admin@www.apache.org:50/one/two.three"),
101             new URL("http://notreal.apache.org")
102         };
103         
104         for(int i=0;i<expected.length;i++) {
105             assertEquals(message[i] + " to URL",expected[i],converter.convert(URL.class,input[i]));
106             assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
107         }
108         
109         for(int i=0;i<expected.length;i++) {
110             assertEquals(input[i] + " to String", input[i], converter.convert(String.class, expected[i]));
111         }
112     }
113     
114 }
115