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.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   * Test Case for the FileConverter class.
31   *
32   * @version $Id$
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       * Tries a conversion to an unsupported target type.
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             // expected result
107         }
108     }
109 }
110