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.Converter;
26  
27  
28  /**
29   * Test Case for the FileConverter class.
30   *
31   * @author James Strachan
32   * @version $Revision: 438363 $ $Date: 2006-08-30 00:48:00 -0400 (Wed, 30 Aug 2006) $
33   */
34  
35  public class FileConverterTestCase extends TestCase {
36  
37      private Converter converter = null;
38  
39      // ------------------------------------------------------------------------
40  
41      public FileConverterTestCase(String name) {
42          super(name);
43      }
44      
45      // ------------------------------------------------------------------------
46  
47      public void setUp() throws Exception {
48          converter = makeConverter();
49      }
50  
51      public static TestSuite suite() {
52          return new TestSuite(FileConverterTestCase.class);        
53      }
54  
55      public void tearDown() throws Exception {
56          converter = null;
57      }
58  
59      // ------------------------------------------------------------------------
60      
61      protected Converter makeConverter() {
62          return new FileConverter();
63      }
64      
65      protected Class getExpectedType() {
66          return File.class;
67      }
68  
69      // ------------------------------------------------------------------------
70  
71      public void testSimpleConversion() throws Exception {
72          String[] message= { 
73              "from String",
74              "from String",
75              "from String"
76          };
77          
78          Object[] input = { 
79              "/tmp",
80              "/tmp/foo.txt",
81              "/tmp/does/not/exist.foo"
82          };
83          
84          File[] expected = { 
85              new File("/tmp"),
86              new File("/tmp/foo.txt"),
87              new File("/tmp/does/not/exist.foo")
88          };
89          
90          for(int i=0;i<expected.length;i++) {
91              assertEquals(message[i] + " to File",expected[i],converter.convert(File.class,input[i]));
92              assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
93          }
94      }
95      
96  }
97