PageRenderTime 30ms CodeModel.GetById 22ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/fixtures-maven-plugin/src/main/java/net/fixtures/plugin/FixturesHelper.java

https://bitbucket.org/emont01/maven-fixtures
Java | 60 lines | 37 code | 9 blank | 14 comment | 2 complexity | b6cd178b8577d076d8262b4af4516adc MD5 | raw file
 1/*
 2 * To change this template, choose Tools | Templates
 3 * and open the template in the editor.
 4 */
 5package net.fixtures.plugin;
 6
 7import java.io.File;
 8import java.io.FileInputStream;
 9import java.io.FileNotFoundException;
10import java.io.InputStream;
11import java.util.Map;
12import org.apache.commons.lang.StringUtils;
13import org.yaml.snakeyaml.Yaml;
14
15/**
16 *
17 * @author eivar
18 */
19class FixturesHelper {
20
21  /**
22   * database connection fields
23   */
24  final static String URL = "url", USER_NAME = "username", PASSWORD = "password";
25  /**
26   * path constants
27   */
28  public final static String PATH_SEPARATOR = System.getProperty("file.separator");
29  public final static String BASE_DIR = System.getProperty("basedir");
30  private final static String RESOURCES_PATH = buildPath(BASE_DIR, "src", "main", "resources");
31
32  static Map<String, Map<String, Object>> readDatabaseConfig(String configFile) {
33    try {
34      configFile = configFile == null ? buildPath(RESOURCES_PATH, "database.yml") : configFile;
35      return readYamlFile(configFile);
36    } catch (FileNotFoundException ex) {
37      throw new RuntimeException("Imposible to load database.yml config", ex);
38    }
39  }
40
41  static Map<String, Map<String, Object>> readYamlFile(String configFile) throws FileNotFoundException {
42    return readYamlFile(new File(configFile));
43  }
44
45  static Map<String, Map<String, Object>> readYamlFile(File configFile) throws FileNotFoundException {
46    return readYamlFile(new FileInputStream(configFile));
47  }
48
49  static Map<String, Map<String, Object>> readYamlFile(InputStream configFile) {
50    return (Map<String, Map<String, Object>>) new Yaml().load(configFile);
51  }
52
53  static String buildPath(Object... elments) {
54    return StringUtils.join(elments, PATH_SEPARATOR);
55  }
56
57  static String convertToString(Object value) {
58    return value == null ? "" : value.toString();
59  }
60}