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

Skip to content

Commit 7725326

Browse files
authored
Merge pull request #154 from utPLSQL/feature/new_cli_library
Feature/new cli library
2 parents 2587f24 + 10bef7b commit 7725326

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1811
-981
lines changed

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,20 @@ ALTER SESSION SET NLS_TERRITORY='AMERICA';
5555
```
5656

5757
## Usage
58-
Currently, utPLSQL-cli supports the following commands:
58+
Currently, utPLSQL-cli supports the following sub-commands:
5959
- run
6060
- info
6161
- reporters
62+
- help
63+
64+
To get more info about a command, use
65+
```
66+
utplsql <sub-command> -h
67+
```
68+
Example:
69+
```
70+
utplsql run -h
71+
```
6272

6373
#### \<ConnectionURL>
6474

@@ -145,13 +155,13 @@ utplsql run "my/Username"/"myP@ssword"@connectstring
145155
-t=timeInMinutes - Sets the timeout in minutes after which the cli will abort.
146156
(--timeout) Default 60
147157
148-
-dbout - Enables DBMS_OUTPUT in the TestRunner-Session
149-
(--dbms_output) Default: false
158+
-D - Enables DBMS_OUTPUT in the TestRunner-Session
159+
(--dbms_output) Default: false
150160
151-
-random - Enables random order of test executions
161+
-r - Enables random order of test executions
152162
(--random-test-order) Default: false
153163
154-
-seed - Sets the seed to use for random test execution order. If set, it sets -random to true
164+
-seed - Sets the seed to use for random test execution order. If set, it sets -random to true
155165
(--random-test-order-seed)
156166
```
157167

pom.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@
3939
</exclusion>
4040
</exclusions>
4141
</dependency>
42-
<dependency>
43-
<groupId>com.beust</groupId>
44-
<artifactId>jcommander</artifactId>
45-
<version>1.72</version>
46-
<scope>compile</scope>
47-
</dependency>
4842
<dependency>
4943
<groupId>com.zaxxer</groupId>
5044
<artifactId>HikariCP</artifactId>
@@ -61,6 +55,11 @@
6155
<artifactId>logback-classic</artifactId>
6256
<version>1.2.3</version>
6357
</dependency>
58+
<dependency>
59+
<groupId>info.picocli</groupId>
60+
<artifactId>picocli</artifactId>
61+
<version>4.0.0-beta-1b</version>
62+
</dependency>
6463
<dependency>
6564
<groupId>com.oracle.jdbc</groupId>
6665
<artifactId>ojdbc8</artifactId>

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

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,62 @@
11
package org.utplsql.cli;
22

3-
import com.beust.jcommander.JCommander;
4-
import com.beust.jcommander.ParameterException;
3+
import picocli.CommandLine;
4+
5+
import java.util.List;
56

67
public class Cli {
78

89
static final int DEFAULT_ERROR_CODE = 1;
910

10-
static final String HELP_CMD = "-h";
11-
1211
public static void main(String[] args) {
1312

14-
int exitCode = runWithExitCode(args);
13+
int exitCode = runPicocliWithExitCode(args);
1514

1615
System.exit(exitCode);
1716
}
1817

19-
static int runWithExitCode( String[] args ) {
18+
static int runPicocliWithExitCode(String[] args) {
2019

2120
LoggerConfiguration.configure(LoggerConfiguration.ConfigLevel.NONE);
2221
LocaleInitializer.initLocale();
2322

24-
JCommander jc = new JCommander();
25-
jc.setProgramName("utplsql");
26-
27-
CommandProvider cmdProvider = new CommandProvider(jc);
28-
29-
cmdProvider.commands().forEach(cmd -> jc.addCommand(cmd.getCommand(), cmd));
23+
CommandLine commandLine = new CommandLine(UtplsqlPicocliCommand.class);
24+
commandLine.setTrimQuotes(true);
3025

3126
int exitCode = DEFAULT_ERROR_CODE;
3227

33-
if ( args.length >= 1 && args[0].equals("-h") ) // Help?
34-
{
35-
exitCode = 0;
36-
jc.usage();
37-
}
38-
else {
39-
try {
40-
jc.parse(args);
41-
42-
exitCode = cmdProvider.getCommand(jc.getParsedCommand()).run();
28+
try {
29+
30+
List<CommandLine> parsedLines = commandLine.parse(args);
31+
32+
boolean commandWasRun = false;
33+
for (CommandLine parsedLine : parsedLines) {
34+
if (parsedLine.isUsageHelpRequested()) {
35+
parsedLine.usage(System.out);
36+
return 0;
37+
} else if (parsedLine.isVersionHelpRequested()) {
38+
parsedLine.printVersionHelp(System.out);
39+
return 0;
40+
}
41+
42+
Object command = parsedLine.getCommand();
43+
if (command instanceof ICommand) {
44+
exitCode = ((ICommand) command).run();
45+
commandWasRun = true;
46+
break;
47+
}
48+
}
4349

44-
} catch (ParameterException e) {
45-
exitCode = new HelpCommand(jc, e.getMessage()).run();
46-
} catch (Exception e) {
47-
e.printStackTrace();
50+
if (!commandWasRun) {
51+
commandLine.usage(System.out);
52+
}
53+
} catch (CommandLine.ParameterException e) {
54+
System.err.println(e.getMessage());
55+
if (!CommandLine.UnmatchedArgumentException.printSuggestions(e, System.err)) {
56+
e.getCommandLine().usage(System.err);
4857
}
58+
} catch (Exception e) {
59+
e.printStackTrace();
4960
}
5061

5162
return exitCode;

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import java.io.InputStream;
88
import java.io.InputStreamReader;
99

10-
/** This class is getting updated automatically by the build process.
10+
/**
11+
* This class is getting updated automatically by the build process.
1112
* Please do not update its constants manually cause they will be overwritten.
1213
*
1314
* @author pesse
@@ -19,18 +20,22 @@ public class CliVersionInfo {
1920

2021
static {
2122
try {
22-
try ( InputStream in = JavaApiVersionInfo.class.getClassLoader().getResourceAsStream("utplsql-cli.version");
23-
BufferedReader reader = new BufferedReader(new InputStreamReader(in)) ) {
23+
try (InputStream in = JavaApiVersionInfo.class.getClassLoader().getResourceAsStream("utplsql-cli.version");
24+
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
2425
MAVEN_PROJECT_VERSION = reader.readLine();
2526
}
26-
}
27-
catch ( IOException e ) {
27+
} catch (IOException e) {
2828
System.out.println("WARNING: Could not get Version information!");
2929
}
3030
}
3131

32-
public static String getVersion() { return MAVEN_PROJECT_VERSION; }
33-
public static String getInfo() { return MAVEN_PROJECT_NAME + " " + getVersion(); }
32+
public static String getVersion() {
33+
return MAVEN_PROJECT_VERSION;
34+
}
35+
36+
public static String getInfo() {
37+
return MAVEN_PROJECT_NAME + " " + getVersion();
38+
}
3439

3540

3641
}

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

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

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ public class ConnectionConfig {
99
private final String password;
1010
private final String connect;
1111

12-
public ConnectionConfig( String connectString ) {
12+
public ConnectionConfig(String connectString) {
1313
Matcher m = Pattern.compile("^(\".+\"|[^/]+)/(\".+\"|[^@]+)@(.*)$").matcher(connectString);
14-
if ( m.find() ) {
14+
if (m.find()) {
1515
user = stripEnclosingQuotes(m.group(1));
1616
password = stripEnclosingQuotes(m.group(2));
1717
connect = m.group(3);
18-
}
19-
else
18+
} else {
2019
throw new IllegalArgumentException("Not a valid connectString: '" + connectString + "'");
20+
}
2121
}
2222

23-
private String stripEnclosingQuotes( String value ) {
24-
if ( value.length() > 1
23+
private String stripEnclosingQuotes(String value) {
24+
if (value.length() > 1
2525
&& value.startsWith("\"")
2626
&& value.endsWith("\"")) {
27-
return value.substring(1, value.length()-1);
27+
return value.substring(1, value.length() - 1);
2828
} else {
2929
return value;
3030
}
@@ -49,6 +49,6 @@ public String getConnectString() {
4949
public boolean isSysDba() {
5050
return user != null &&
5151
(user.toLowerCase().endsWith(" as sysdba")
52-
|| user.toLowerCase().endsWith(" as sysoper"));
52+
|| user.toLowerCase().endsWith(" as sysoper"));
5353
}
5454
}

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

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

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.utplsql.cli;
22

3-
import com.zaxxer.hikari.HikariDataSource;
43
import org.utplsql.cli.datasource.TestedDataSourceProvider;
54

65
import javax.sql.DataSource;
76
import java.io.File;
87
import java.sql.SQLException;
98

10-
/** Helper class to give you a ready-to-use datasource
9+
/**
10+
* Helper class to give you a ready-to-use datasource
1111
*
1212
* @author pesse
1313
*/
@@ -21,19 +21,18 @@ public class DataSourceProvider {
2121
}
2222
}
2323

24-
public static DataSource getDataSource(ConnectionInfo info, int maxConnections ) throws SQLException {
24+
public static DataSource getDataSource(String connectString, int maxConnections) throws SQLException {
2525

2626
requireOjdbc();
2727

28-
ConnectionConfig config = new ConnectionConfig(info.getConnectionString());
28+
ConnectionConfig config = new ConnectionConfig(connectString);
2929
warnIfSysDba(config);
3030

3131
return new TestedDataSourceProvider(config, maxConnections).getDataSource();
3232
}
3333

3434
private static void requireOjdbc() {
35-
if ( !OracleLibraryChecker.checkOjdbcExists() )
36-
{
35+
if (!OracleLibraryChecker.checkOjdbcExists()) {
3736
System.out.println("Could not find Oracle JDBC driver in classpath. Please download the jar from Oracle website" +
3837
" and copy it to the 'lib' folder of your utPLSQL-cli installation.");
3938
System.out.println("Download from http://www.oracle.com/technetwork/database/features/jdbc/jdbc-ucp-122-3110062.html");
@@ -43,7 +42,7 @@ private static void requireOjdbc() {
4342
}
4443

4544
private static void warnIfSysDba(ConnectionConfig config) {
46-
if ( config.isSysDba() ) {
45+
if (config.isSysDba()) {
4746
System.out.println("WARNING: You are connecting to the database as SYSDBA or SYSOPER, which is NOT RECOMMENDED and can put your database at risk!");
4847
}
4948
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ public List<String> getFileList(File baseDir, String inspectPath) {
1616
public List<String> getFileList(File baseDir, String inspectPath, boolean relative) {
1717
File inspectDir = new File(baseDir, inspectPath);
1818

19-
if (!inspectDir.isDirectory())
19+
if (!inspectDir.isDirectory()) {
2020
throw new IllegalArgumentException(inspectPath + " is not a directory.");
21+
}
2122

2223
List<String> fileList = new ArrayList<>();
2324
listDirFiles(baseDir, inspectDir, fileList, relative);
@@ -28,15 +29,17 @@ public List<String> getFileList(File baseDir, String inspectPath, boolean relati
2829
private void listDirFiles(File baseDir, File directory, List<String> fileList, boolean relative) {
2930
File[] directoryFiles = directory.listFiles();
3031

31-
if (directoryFiles == null)
32+
if (directoryFiles == null) {
3233
return;
34+
}
3335

3436
for (File file : directoryFiles) {
3537
if (file.isFile()) {
3638
String absolutePath = file.getAbsolutePath();
3739

38-
if (relative)
40+
if (relative) {
3941
absolutePath = absolutePath.substring(baseDir.getAbsolutePath().length() + 1);
42+
}
4043

4144
fileList.add(absolutePath);
4245
} else {

0 commit comments

Comments
 (0)