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.IOException;
26  import java.io.InputStream;
27  import java.io.Reader;
28  import java.nio.charset.StandardCharsets;
29  import java.nio.file.Files;
30  import java.nio.file.Path;
31  import java.nio.file.Paths;
32  
33  import org.dbunit.DatabaseEnvironment;
34  
35  /**
36   * @author John Hurst (john.b.hurst@gmail.com)
37   * @since 2.4.8
38   */
39  public class TestUtils
40  {
41      private static String getProfileName() throws Exception
42      {
43          return DatabaseEnvironment.getInstance().getProfile()
44                  .getActiveProfile();
45      }
46  
47      public static String getFileName(String fileName)
48      {
49          return "src/test/resources/" + fileName;
50      }
51  
52      public static File getFile(String fileName)
53      {
54          final Path path = Paths.get(getFileName(fileName));
55          return path.toFile();
56      }
57  
58      public static File getFileForDatabaseEnvironment(String originalFileName)
59              throws Exception
60      {
61          String profilePath =
62                  originalFileName.replace(".", "-" + getProfileName() + ".");
63          File profileFile = Paths.get(profilePath).toFile();
64          if (profileFile.exists())
65          {
66              return profileFile;
67          } else
68          {
69              final Path path = Paths.get(originalFileName);
70              return path.toFile();
71          }
72      }
73  
74      public static Reader getFileReader(String fileName) throws IOException
75      {
76          final Path path = Paths.get(getFileName(fileName));
77          return Files.newBufferedReader(path, StandardCharsets.UTF_8);
78      }
79  
80      public static InputStream getFileInputStream(String fileName)
81              throws IOException
82      {
83          final Path path = Paths.get(getFileName(fileName));
84          return Files.newInputStream(path);
85      }
86  
87  }