Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix invalid connection strings test
  • Loading branch information
viniciusam committed Jun 18, 2017
commit 6f303ed9ebbebebc48f89176b00a678222fc9263
15 changes: 7 additions & 8 deletions src/main/java/io/github/utplsql/cli/ConnectionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ConnectionInfo parseConnectionString(String connectionString)
private void parseCredentials(String str) throws ParameterException, IllegalArgumentException {
int barIdx = str.indexOf("/");

if (barIdx == -1 || str.length() == 1)
if (barIdx == -1 || barIdx == 0 || barIdx == str.length() - 1)
throw invalidConnectionString();

this.setUser(str.substring(0, barIdx));
Expand All @@ -61,22 +61,21 @@ private void parseHost(String str) throws ParameterException, IllegalArgumentExc
int colonIdx = str.indexOf(":");
int barIdx = str.indexOf("/");

if (colonIdx != -1 && barIdx != -1) {
if ((colonIdx != -1 && barIdx == -1) || barIdx == 0) // @host:port or // @/db
throw invalidConnectionString();

if (colonIdx != -1) { // @host:port/db
setHost(str.substring(0, colonIdx));
setPort(Integer.parseInt(str.substring(colonIdx + 1, barIdx)));
setDatabase(str.substring(barIdx + 1));
}
else
if (colonIdx == -1 && barIdx != -1) {
if (barIdx != -1) { // @host/db
setHost(str.substring(0, barIdx));
setPort(DEFAULT_PORT);
setDatabase(str.substring(barIdx + 1));
}
else
if (colonIdx != -1) {
throw invalidConnectionString();
}
else {
else { // @db
setHost(DEFAULT_HOST);
setPort(DEFAULT_PORT);
setDatabase(str);
Expand Down
66 changes: 58 additions & 8 deletions src/test/java/io/github/utplsql/cli/ConnectionInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ConnectionInfoTest {
*/

@Test
public void connectionStr_Full() {
public void valid_Full() {
try {
ConnectionInfo ci = new ConnectionInfo()
.parseConnectionString("my_user/p@[email protected]:3000/db_1.acme.com");
Expand All @@ -34,7 +34,7 @@ public void connectionStr_Full() {
}

@Test
public void connectionStr_WithoutPort() {
public void valid_WithoutPort() {
try {
ConnectionInfo ci = new ConnectionInfo()
.parseConnectionString("my_user/p@[email protected]/db_1.acme.com");
Expand All @@ -51,7 +51,7 @@ public void connectionStr_WithoutPort() {
}

@Test
public void connectionStr_WithoutHostAndPort() {
public void valid_WithoutHostAndPort() {
try {
ConnectionInfo ci = new ConnectionInfo()
.parseConnectionString("my_user/p@[email protected]/db_1.acme.com");
Expand All @@ -68,14 +68,64 @@ public void connectionStr_WithoutHostAndPort() {
}

@Test
public void connectionStr_Invalid() {
public void invalid_WithoutDatabase_1() {
try {
new ConnectionInfo().parseConnectionString("user/pass@");
Assert.fail();
} catch (ParameterException ignored) {}
}

@Test
public void invalid_WithoutDatabase_2() {
try {
new ConnectionInfo().parseConnectionString("user/pass");
new ConnectionInfo().parseConnectionString("user/@");
new ConnectionInfo().parseConnectionString("/pass@");
new ConnectionInfo().parseConnectionString("/@");
new ConnectionInfo().parseConnectionString("@");
Assert.fail();
} catch (ParameterException ignored) {}
}

@Test
public void invalid_WithoutDatabase_3() {
try {
new ConnectionInfo().parseConnectionString("user/pass@localhost:1521");
Assert.fail();
} catch (ParameterException ignored) {}
}

@Test
public void invalid_WithoutHost() {
try {
new ConnectionInfo().parseConnectionString("user/pass@/db");
Assert.fail();
} catch (ParameterException ignored) {}
}

@Test
public void invalid_WithoutPassword() {
try {
new ConnectionInfo().parseConnectionString("user/@db");
Assert.fail();
} catch (ParameterException ignored) {}
}

@Test
public void invalid_WithoutUsername() {
try {
new ConnectionInfo().parseConnectionString("/pass@db");
Assert.fail();
} catch (ParameterException ignored) {}
}

@Test
public void invalid_WithoutUserPassDb_1() {
try {
new ConnectionInfo().parseConnectionString("/@db");
Assert.fail();
} catch (ParameterException ignored) {}
}

@Test
public void invalid_WithoutUserPass() {
try {
new ConnectionInfo().parseConnectionString("@db");
Assert.fail();
} catch (ParameterException ignored) {}
Expand Down