View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2010, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.testutil;
23  
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.FileNotFoundException;
27  import java.io.FileReader;
28  
29  import org.apache.commons.io.FilenameUtils;
30  import org.dbunit.DatabaseEnvironment;
31  
32  /**
33   * @author John Hurst (john.b.hurst@gmail.com)
34   * @since 2.4.8
35   */
36  public class TestUtils
37  {
38      private static String getProfileName() throws Exception
39      {
40          return DatabaseEnvironment.getInstance().getProfile().getActiveProfile();
41      }
42  
43      public static String getFileName(String fileName)
44      {
45          return "src/test/resources/" + fileName;
46      }
47  
48      public static File getFile(String fileName)
49      {
50          return new File(getFileName(fileName));
51      }
52  
53      public static File getFileForDatabaseEnvironment(String originalFileName) throws Exception
54      {
55          String fullPath = FilenameUtils.getFullPath(originalFileName);
56          String baseName = FilenameUtils.getBaseName(originalFileName);
57          String extension = FilenameUtils.getExtension(originalFileName);
58          File profileFile = new File(fullPath + baseName + "-" + getProfileName() + "." + extension);
59          if (profileFile.exists())
60          {
61              return profileFile;
62          }
63          else
64          {
65              return new File(originalFileName);
66          }
67      }
68  
69      public static FileReader getFileReader(String fileName) throws FileNotFoundException
70      {
71          return new FileReader(getFileName(fileName));
72      }
73  
74      public static FileInputStream getFileInputStream(String fileName) throws FileNotFoundException
75      {
76          return new FileInputStream(getFileName(fileName));
77      }
78  
79  }