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