DatabaseConfigFactory { @Bean public DatabaseConfig createDatabaseConfig(@Value("${ehour.database}") String databaseType, @Value("${ehour.database.driver:}") String driver, @Value("${ehour.database.url:}") String url, @Value("${ehour.database.username:}") String username, @Value("${ehour.database.password:}") String password) throws URISyntaxException { Database database = Database.valueOf(databaseType.toUpperCase()); String sanitizedDriver = StringUtils.isBlank(driver) ? database.defaultDriver : driver; String sanitizedUrl = null; String sanitizedUsername = null; String sanitizedPassword = null; if (database != Database.DERBY) { if (StringUtils.isBlank(username) && StringUtils.isBlank(password)) { URI dbUri = new URI(url); String userInfo = dbUri.getUserInfo(); if (StringUtils.isNotBlank(userInfo)) { String[] splitted = userInfo.split(":"); if (splitted.length == 2) { sanitizedUsername = splitted[0]; sanitizedPassword = splitted[1]; } } String query = (StringUtils.isNotBlank(dbUri.getQuery())) ? "?" + dbUri.getQuery() : ""; String dbUrl = String.format("jdbc:%s: LOGGER.info("Only a DB URL was provided, stripped of username and password and using url " + dbUrl); sanitizedUrl = dbUrl; } else { sanitizedUrl = url; sanitizedUsername = username; sanitizedPassword = password; } verifyUsernamePassword(sanitizedUsername, sanitizedPassword); verifyItem("ehour.database.url", sanitizedUrl); } return new DatabaseConfig(database, sanitizedDriver, sanitizedUrl, sanitizedUsername, sanitizedPassword); }  @Bean DatabaseConfig createDatabaseConfig(@Value("${ehour.database}") String databaseType,
                                               @Value("${ehour.database.driver:}") String driver,
                                               @Value("${ehour.database.url:}") String url,
                                               @Value("${ehour.database.username:}") String username,
                                               @Value("${ehour.database.password:}") String password);  }
@Test public void should_extract_credentials_from_url() throws URISyntaxException { DatabaseConfigFactory factory = new DatabaseConfigFactory(); DatabaseConfig config = factory.createDatabaseConfig("mysql", null, "mysql: null, null); assertEquals("user", config.username); assertEquals("pass", config.password); assertEquals("jdbc:mysql: }
@Test public void should_append_query_parameters() throws URISyntaxException { DatabaseConfigFactory factory = new DatabaseConfigFactory(); DatabaseConfig config = factory.createDatabaseConfig("mysql", null, "mysql: null, null); assertEquals("jdbc:mysql: }
@Test(expected = ConfigurationException.class) public void should_fail_without_credentials() throws URISyntaxException { DatabaseConfigFactory factory = new DatabaseConfigFactory(); factory.createDatabaseConfig("mysql", null, "mysql: null, null); fail("Should have thrown ConfigurationException"); }
@Test public void should_use_default_driver_when_none_supplied() throws URISyntaxException { DatabaseConfigFactory factory = new DatabaseConfigFactory(); DatabaseConfig config = factory.createDatabaseConfig("mysql", null, "mysql: null, null); assertEquals(Database.MYSQL.defaultDriver, config.driver); assertEquals("pass", config.password); }