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.io.File;
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 FileConverterTestCase extends TestCase {
36
37 private Converter converter = null;
38
39
40
41 public FileConverterTestCase(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(FileConverterTestCase.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 FileConverter();
65 }
66
67 protected Class<?> getExpectedType() {
68 return File.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 };
79
80 final Object[] input = {
81 "/tmp",
82 "/tmp/foo.txt",
83 "/tmp/does/not/exist.foo"
84 };
85
86 final File[] expected = {
87 new File("/tmp"),
88 new File("/tmp/foo.txt"),
89 new File("/tmp/does/not/exist.foo")
90 };
91
92 for(int i=0;i<expected.length;i++) {
93 assertEquals(message[i] + " to File",expected[i],converter.convert(File.class,input[i]));
94 assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
95 }
96 }
97
98
99
100
101 public void testUnsupportedTargetType() {
102 try {
103 converter.convert(Integer.class, "/tmp");
104 fail("Could convert to unsupported type!");
105 } catch (final ConversionException cex) {
106
107 }
108 }
109 }
110