1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32
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
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
126 }
127 }
128 }
129