Skip to content

Commit 194525b

Browse files
authored
Merge pull request #42 from utPLSQL/feature/remove_connection_parser
Feature/remove connection parser
2 parents 088b130 + 5e67253 commit 194525b

File tree

5 files changed

+145
-260
lines changed

5 files changed

+145
-260
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ You can download development versions on [Bintray](https://bintray.com/viniciusa
1717
* When using reporters for Sonar or Coveralls client needs to be invoked from project's root directory.
1818

1919
## Usage
20-
utplsql run user/pass@[[host][:port]/]db [-p=(ut_path|ut_paths)] [-f=format [-o=output_file] [-s] ...]
20+
utplsql run \<ConnectionURL\> [-p=(ut_path|ut_paths)] [-f=format [-o=output_file] [-s] ...]
2121

2222
```
23-
user - Username to connect as.
24-
password - Password of the user.
25-
host - Server address, defaults to 127.0.0.1.
26-
port - Server port, defaults to 1521.
27-
db - Database to connect to.
23+
<ConnectionURL> - <user>/<password>@//<host>[:<port>]/<service> OR <user>/<password>@<TNSName> OR <user>/<password>@<host>:<port>:<SID>
24+
To connect using TNS, you need to have the ORACLE_HOME environment variable set.
2825
-p=suite_path(s) - A suite path or a comma separated list of suite paths for unit test to be executed.
2926
The path(s) can be in one of the following formats:
3027
schema[.package[.procedure]]
Lines changed: 25 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,45 @@
11
package org.utplsql.cli;
22

3-
import com.beust.jcommander.ParameterException;
3+
import com.beust.jcommander.IStringConverter;
4+
import oracle.ucp.jdbc.PoolDataSource;
5+
import oracle.ucp.jdbc.PoolDataSourceFactory;
46

7+
import java.io.File;
58
import java.sql.Connection;
6-
import java.sql.DriverManager;
79
import java.sql.SQLException;
810

9-
/**
10-
* Created by Vinicius on 21/04/2017.
11-
*/
1211
public class ConnectionInfo {
1312

14-
private static final String DEFAULT_HOST = "127.0.0.1";
15-
private static final int DEFAULT_PORT = 1521;
16-
17-
private String user;
18-
private String password;
19-
private String host;
20-
private int port;
21-
private String database;
22-
23-
public ConnectionInfo() {}
24-
25-
public Connection getConnection() throws SQLException {
26-
return DriverManager.getConnection(getConnectionUrl(), getUser(), getPassword());
27-
}
28-
29-
public ConnectionInfo parseConnectionString(String connectionString)
30-
throws ParameterException, IllegalArgumentException {
31-
32-
if (connectionString == null || connectionString.isEmpty())
33-
throw invalidConnectionString();
34-
35-
int i = connectionString.lastIndexOf("@");
36-
if (i == -1)
37-
throw invalidConnectionString();
38-
39-
String credentials = connectionString.substring(0, i);
40-
String host = connectionString.substring(i+1);
41-
parseCredentials(credentials);
42-
parseHost(host);
43-
44-
return this;
45-
}
46-
47-
private void parseCredentials(String str) throws ParameterException, IllegalArgumentException {
48-
int barIdx = str.indexOf("/");
49-
50-
if (barIdx == -1 || barIdx == 0 || barIdx == str.length() - 1)
51-
throw invalidConnectionString();
52-
53-
this.setUser(str.substring(0, barIdx));
54-
this.setPassword(str.substring(barIdx+1));
55-
}
56-
57-
private void parseHost(String str) throws ParameterException, IllegalArgumentException {
58-
if (str == null || str.isEmpty())
59-
throw invalidConnectionString();
60-
61-
int colonIdx = str.indexOf(":");
62-
int barIdx = str.indexOf("/");
63-
64-
if ((colonIdx != -1 && barIdx == -1) || barIdx == 0) // @host:port or // @/db
65-
throw invalidConnectionString();
66-
67-
if (colonIdx != -1) { // @host:port/db
68-
setHost(str.substring(0, colonIdx));
69-
setPort(Integer.parseInt(str.substring(colonIdx + 1, barIdx)));
70-
setDatabase(str.substring(barIdx + 1));
71-
}
72-
else
73-
if (barIdx != -1) { // @host/db
74-
setHost(str.substring(0, barIdx));
75-
setPort(DEFAULT_PORT);
76-
setDatabase(str.substring(barIdx + 1));
77-
}
78-
else { // @db
79-
setHost(DEFAULT_HOST);
80-
setPort(DEFAULT_PORT);
81-
setDatabase(str);
13+
static {
14+
String oracleHome = System.getenv("ORACLE_HOME");
15+
if (oracleHome != null) {
16+
System.setProperty("oracle.net.tns_admin",
17+
String.join(File.separator, oracleHome, "NETWORK", "ADMIN"));
8218
}
8319
}
8420

85-
private ParameterException invalidConnectionString() {
86-
return new ParameterException("Invalid connection string.");
87-
}
88-
89-
public String getUser() {
90-
return user;
91-
}
92-
93-
private void setUser(String user) {
94-
this.user = user;
95-
}
96-
97-
public String getPassword() {
98-
return password;
99-
}
100-
101-
private void setPassword(String password) {
102-
this.password = password;
103-
}
104-
105-
public String getHost() {
106-
return host;
107-
}
21+
private PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
10822

109-
private void setHost(String host) {
110-
this.host = host;
111-
}
112-
113-
public int getPort() {
114-
return port;
115-
}
116-
117-
private void setPort(int port) {
118-
this.port = port;
119-
}
120-
121-
public String getDatabase() {
122-
return database;
23+
public ConnectionInfo(String connectionInfo) {
24+
try {
25+
this.pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
26+
this.pds.setURL("jdbc:oracle:thin:" + connectionInfo);
27+
this.pds.setInitialPoolSize(2);
28+
} catch (SQLException e) {
29+
e.printStackTrace();
30+
}
12331
}
12432

125-
private void setDatabase(String database) {
126-
this.database = database;
33+
public Connection getConnection() throws SQLException {
34+
return pds.getConnection();
12735
}
12836

129-
public String getConnectionUrl() {
130-
return String.format("jdbc:oracle:thin:@//%s:%d/%s", getHost(), getPort(), getDatabase());
131-
}
37+
public static class ConnectionStringConverter implements IStringConverter<ConnectionInfo> {
13238

133-
@Override
134-
public String toString() {
135-
return String.format("%s@%s:%d/%s", getUser(), getHost(), getPort(), getDatabase());
39+
@Override
40+
public ConnectionInfo convert(String s) {
41+
return new ConnectionInfo(s);
42+
}
13643
}
13744

13845
}

src/main/java/org/utplsql/cli/ConnectionStringConverter.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/main/java/org/utplsql/cli/RunCommand.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
public class RunCommand {
2727

2828
@Parameter(
29-
required = true, converter = ConnectionStringConverter.class,
29+
required = true,
30+
converter = ConnectionInfo.ConnectionStringConverter.class,
3031
arity = 1,
31-
description = "user/pass@[[host][:port]/]db")
32+
description = "<user>/<password>@//<host>[:<port>]/<service> OR <user>/<password>@<TNSName> OR <user>/<password>@<host>:<port>:<SID>")
3233
private List<ConnectionInfo> connectionInfoList = new ArrayList<>();
3334

3435
@Parameter(
@@ -102,8 +103,6 @@ public int run() throws Exception {
102103
testMappingOptions[0] = getMapperOptions(this.testPathParams, testFiles);
103104
}
104105

105-
if (testPaths.isEmpty()) testPaths.add(ci.getUser());
106-
107106
// Do the reporters initialization, so we can use the id to run and gather results.
108107
try (Connection conn = ci.getConnection()) {
109108
for (ReporterOptions ro : reporterOptionsList) {

0 commit comments

Comments
 (0)