Skip to content

Commit

Permalink
Merge pull request #154 from utPLSQL/feature/new_cli_library
Browse files Browse the repository at this point in the history
Feature/new cli library
  • Loading branch information
pesse committed Jun 26, 2019
2 parents 2587f24 + 10bef7b commit 7725326
Show file tree
Hide file tree
Showing 51 changed files with 1,811 additions and 981 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,20 @@ ALTER SESSION SET NLS_TERRITORY='AMERICA';
```

## Usage
Currently, utPLSQL-cli supports the following commands:
Currently, utPLSQL-cli supports the following sub-commands:
- run
- info
- reporters
- help

To get more info about a command, use
```
utplsql <sub-command> -h
```
Example:
```
utplsql run -h
```

#### \<ConnectionURL>

Expand Down Expand Up @@ -145,13 +155,13 @@ utplsql run "my/Username"/"myP@ssword"@connectstring
-t=timeInMinutes - Sets the timeout in minutes after which the cli will abort.
(--timeout) Default 60
-dbout - Enables DBMS_OUTPUT in the TestRunner-Session
(--dbms_output) Default: false
-D - Enables DBMS_OUTPUT in the TestRunner-Session
(--dbms_output) Default: false
-random - Enables random order of test executions
-r - Enables random order of test executions
(--random-test-order) Default: false
-seed - Sets the seed to use for random test execution order. If set, it sets -random to true
-seed - Sets the seed to use for random test execution order. If set, it sets -random to true
(--random-test-order-seed)
```

Expand Down
11 changes: 5 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.72</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
Expand All @@ -61,6 +55,11 @@
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.0.0-beta-1b</version>
</dependency>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
Expand Down
63 changes: 37 additions & 26 deletions src/main/java/org/utplsql/cli/Cli.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,62 @@
package org.utplsql.cli;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import picocli.CommandLine;

import java.util.List;

public class Cli {

static final int DEFAULT_ERROR_CODE = 1;

static final String HELP_CMD = "-h";

public static void main(String[] args) {

int exitCode = runWithExitCode(args);
int exitCode = runPicocliWithExitCode(args);

System.exit(exitCode);
}

static int runWithExitCode( String[] args ) {
static int runPicocliWithExitCode(String[] args) {

LoggerConfiguration.configure(LoggerConfiguration.ConfigLevel.NONE);
LocaleInitializer.initLocale();

JCommander jc = new JCommander();
jc.setProgramName("utplsql");

CommandProvider cmdProvider = new CommandProvider(jc);

cmdProvider.commands().forEach(cmd -> jc.addCommand(cmd.getCommand(), cmd));
CommandLine commandLine = new CommandLine(UtplsqlPicocliCommand.class);
commandLine.setTrimQuotes(true);

int exitCode = DEFAULT_ERROR_CODE;

if ( args.length >= 1 && args[0].equals("-h") ) // Help?
{
exitCode = 0;
jc.usage();
}
else {
try {
jc.parse(args);

exitCode = cmdProvider.getCommand(jc.getParsedCommand()).run();
try {

List<CommandLine> parsedLines = commandLine.parse(args);

boolean commandWasRun = false;
for (CommandLine parsedLine : parsedLines) {
if (parsedLine.isUsageHelpRequested()) {
parsedLine.usage(System.out);
return 0;
} else if (parsedLine.isVersionHelpRequested()) {
parsedLine.printVersionHelp(System.out);
return 0;
}

Object command = parsedLine.getCommand();
if (command instanceof ICommand) {
exitCode = ((ICommand) command).run();
commandWasRun = true;
break;
}
}

} catch (ParameterException e) {
exitCode = new HelpCommand(jc, e.getMessage()).run();
} catch (Exception e) {
e.printStackTrace();
if (!commandWasRun) {
commandLine.usage(System.out);
}
} catch (CommandLine.ParameterException e) {
System.err.println(e.getMessage());
if (!CommandLine.UnmatchedArgumentException.printSuggestions(e, System.err)) {
e.getCommandLine().usage(System.err);
}
} catch (Exception e) {
e.printStackTrace();
}

return exitCode;
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/org/utplsql/cli/CliVersionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;

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

static {
try {
try ( InputStream in = JavaApiVersionInfo.class.getClassLoader().getResourceAsStream("utplsql-cli.version");
BufferedReader reader = new BufferedReader(new InputStreamReader(in)) ) {
try (InputStream in = JavaApiVersionInfo.class.getClassLoader().getResourceAsStream("utplsql-cli.version");
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
MAVEN_PROJECT_VERSION = reader.readLine();
}
}
catch ( IOException e ) {
} catch (IOException e) {
System.out.println("WARNING: Could not get Version information!");
}
}

public static String getVersion() { return MAVEN_PROJECT_VERSION; }
public static String getInfo() { return MAVEN_PROJECT_NAME + " " + getVersion(); }
public static String getVersion() {
return MAVEN_PROJECT_VERSION;
}

public static String getInfo() {
return MAVEN_PROJECT_NAME + " " + getVersion();
}


}
40 changes: 0 additions & 40 deletions src/main/java/org/utplsql/cli/CommandProvider.java

This file was deleted.

16 changes: 8 additions & 8 deletions src/main/java/org/utplsql/cli/ConnectionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ public class ConnectionConfig {
private final String password;
private final String connect;

public ConnectionConfig( String connectString ) {
public ConnectionConfig(String connectString) {
Matcher m = Pattern.compile("^(\".+\"|[^/]+)/(\".+\"|[^@]+)@(.*)$").matcher(connectString);
if ( m.find() ) {
if (m.find()) {
user = stripEnclosingQuotes(m.group(1));
password = stripEnclosingQuotes(m.group(2));
connect = m.group(3);
}
else
} else {
throw new IllegalArgumentException("Not a valid connectString: '" + connectString + "'");
}
}

private String stripEnclosingQuotes( String value ) {
if ( value.length() > 1
private String stripEnclosingQuotes(String value) {
if (value.length() > 1
&& value.startsWith("\"")
&& value.endsWith("\"")) {
return value.substring(1, value.length()-1);
return value.substring(1, value.length() - 1);
} else {
return value;
}
Expand All @@ -49,6 +49,6 @@ public String getConnectString() {
public boolean isSysDba() {
return user != null &&
(user.toLowerCase().endsWith(" as sysdba")
|| user.toLowerCase().endsWith(" as sysoper"));
|| user.toLowerCase().endsWith(" as sysoper"));
}
}
25 changes: 0 additions & 25 deletions src/main/java/org/utplsql/cli/ConnectionInfo.java

This file was deleted.

13 changes: 6 additions & 7 deletions src/main/java/org/utplsql/cli/DataSourceProvider.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.utplsql.cli;

import com.zaxxer.hikari.HikariDataSource;
import org.utplsql.cli.datasource.TestedDataSourceProvider;

import javax.sql.DataSource;
import java.io.File;
import java.sql.SQLException;

/** Helper class to give you a ready-to-use datasource
/**
* Helper class to give you a ready-to-use datasource
*
* @author pesse
*/
Expand All @@ -21,19 +21,18 @@ public class DataSourceProvider {
}
}

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

requireOjdbc();

ConnectionConfig config = new ConnectionConfig(info.getConnectionString());
ConnectionConfig config = new ConnectionConfig(connectString);
warnIfSysDba(config);

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

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

private static void warnIfSysDba(ConnectionConfig config) {
if ( config.isSysDba() ) {
if (config.isSysDba()) {
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!");
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/utplsql/cli/FileWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public List<String> getFileList(File baseDir, String inspectPath) {
public List<String> getFileList(File baseDir, String inspectPath, boolean relative) {
File inspectDir = new File(baseDir, inspectPath);

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

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

if (directoryFiles == null)
if (directoryFiles == null) {
return;
}

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

if (relative)
if (relative) {
absolutePath = absolutePath.substring(baseDir.getAbsolutePath().length() + 1);
}

fileList.add(absolutePath);
} else {
Expand Down
Loading

0 comments on commit 7725326

Please sign in to comment.