Skip to content

Commit 091dbea

Browse files
author
Samuel Nitsche
committed
Merge branch 'develop' of https://github.com/utPLSQL/utPLSQL-cli into feature/version-check
2 parents d46bc3f + 194525b commit 091dbea

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
@@ -21,14 +21,11 @@ The latest CLI is always compatible with all database frameworks of the same maj
2121
For example CLI-3.0.4 is compatible with database framework 3.0.0-3.0.4 but not with database framework 2.x.
2222

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

2626
```
27-
user - Username to connect as.
28-
password - Password of the user.
29-
host - Server address, defaults to 127.0.0.1.
30-
port - Server port, defaults to 1521.
31-
db - Database to connect to.
27+
<ConnectionURL> - <user>/<password>@//<host>[:<port>]/<service> OR <user>/<password>@<TNSName> OR <user>/<password>@<host>:<port>:<SID>
28+
To connect using TNS, you need to have the ORACLE_HOME environment variable set.
3229
-p=suite_path(s) - A suite path or a comma separated list of suite paths for unit test to be executed.
3330
The path(s) can be in one of the following formats:
3431
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
@@ -30,9 +30,10 @@
3030
public class RunCommand {
3131

3232
@Parameter(
33-
required = true, converter = ConnectionStringConverter.class,
33+
required = true,
34+
converter = ConnectionInfo.ConnectionStringConverter.class,
3435
arity = 1,
35-
description = "user/pass@[[host][:port]/]db")
36+
description = "<user>/<password>@//<host>[:<port>]/<service> OR <user>/<password>@<TNSName> OR <user>/<password>@<host>:<port>:<SID>")
3637
private List<ConnectionInfo> connectionInfoList = new ArrayList<>();
3738

3839
@Parameter(
@@ -115,8 +116,6 @@ public int run() throws Exception {
115116
testMappingOptions[0] = getMapperOptions(this.testPathParams, testFiles);
116117
}
117118

118-
if (testPaths.isEmpty()) testPaths.add(ci.getUser());
119-
120119
// Do the reporters initialization, so we can use the id to run and gather results.
121120
try (Connection conn = ci.getConnection()) {
122121
for (ReporterOptions ro : reporterOptionsList) {

0 commit comments

Comments
 (0)