/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. */
  5. package net.fixtures.plugin;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.InputStream;
  10. import java.util.Map;
  11. import org.apache.commons.lang.StringUtils;
  12. import org.yaml.snakeyaml.Yaml;
  13. /**
  14. *
  15. * @author eivar
  16. */
  17. class FixturesHelper {
  18. /**
  19. * database connection fields
  20. */
  21. final static String URL = "url", USER_NAME = "username", PASSWORD = "password";
  22. /**
  23. * path constants
  24. */
  25. public final static String PATH_SEPARATOR = System.getProperty("file.separator");
  26. public final static String BASE_DIR = System.getProperty("basedir");
  27. private final static String RESOURCES_PATH = buildPath(BASE_DIR, "src", "main", "resources");
  28. static Map<String, Map<String, Object>> readDatabaseConfig(String configFile) {
  29. try {
  30. configFile = configFile == null ? buildPath(RESOURCES_PATH, "database.yml") : configFile;
  31. return readYamlFile(configFile);
  32. } catch (FileNotFoundException ex) {
  33. throw new RuntimeException("Imposible to load database.yml config", ex);
  34. }
  35. }
  36. static Map<String, Map<String, Object>> readYamlFile(String configFile) throws FileNotFoundException {
  37. return readYamlFile(new File(configFile));
  38. }
  39. static Map<String, Map<String, Object>> readYamlFile(File configFile) throws FileNotFoundException {
  40. return readYamlFile(new FileInputStream(configFile));
  41. }
  42. static Map<String, Map<String, Object>> readYamlFile(InputStream configFile) {
  43. return (Map<String, Map<String, Object>>) new Yaml().load(configFile);
  44. }
  45. static String buildPath(Object... elments) {
  46. return StringUtils.join(elments, PATH_SEPARATOR);
  47. }
  48. static String convertToString(Object value) {
  49. return value == null ? "" : value.toString();
  50. }
  51. }