Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HerokuDatasourceParser
package ar.edu.undav.ejemplo.config;
/**
* Juan Lagostena on 30/12/16
* .
*/
public class HerokuDatasourceParser {
public static final String SEPARATOR = ":";
public static final String SLASH = "/";
private String url;
private String username;
private String password;
public void parseCompleteURL(String url) {
String[] urlFragments = url.split(SEPARATOR);
this.username = urlFragments[2].substring(2);
String[] passwordAndHostFragments = urlFragments[3].split("@");
String host = passwordAndHostFragments[1];
String[] portAndSchemaFragments = urlFragments[4].split(SLASH);
this.password = passwordAndHostFragments[0];
String port = portAndSchemaFragments[0];
String schema = portAndSchemaFragments[1];
StringBuffer buffer = new StringBuffer();
this.url = buffer.append(urlFragments[0]).append(SEPARATOR).append(urlFragments[1]).append(SEPARATOR)
.append(SLASH).append(SLASH).append(host).append(SEPARATOR).append(port).
append("/").append(schema).toString();
}
public String getUrl() {
return url;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
package ar.edu.undav.ejemplo.config;
import org.junit.Assert;
import org.junit.Test;
/**
* Juan Lagostena on 30/12/16
* .
*/
public class HerokuDatasourceParserTest {
@Test
public void parseCompleteURL() throws Exception {
String username = "pepe";
String password = "bfw8wr0oss0uw3k1";
String host = "mcldisu5rtyu29wf.cbetxkdyhwsb.us-east-1.rds.amazonaws.com";
int port = 3306;
String schema = "lyti5ouskt6frrrr";
String driver = "mariadb";
String completeURL = "jdbc:" + driver + "://" + username + ":" + password + "@" + host + ":" + port + "/" + schema;
HerokuDatasourceParser parser = new HerokuDatasourceParser();
parser.parseCompleteURL(completeURL);
String jdbcUrl = "jdbc:" + driver + "://" + host + ":" + port + "/" + schema;
Assert.assertEquals(username, parser.getUsername());
Assert.assertEquals(password, parser.getPassword());
Assert.assertEquals(jdbcUrl, parser.getUrl());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.