Skip to content

Feature/new cli library #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6717057
Failing test to catch and reproduce #143
pesse Jun 4, 2019
038981f
Merge branch 'develop' into feature/new_cli_library
pesse Jun 7, 2019
b21f651
Unit-Test to simulate passing all parameters that should be known
pesse Jun 7, 2019
ce849fc
Initial implementation of Picocli usage
pesse Jun 11, 2019
5520562
Some more tests around FileMapping
pesse Jun 12, 2019
f9c4cf7
New RunAction which does the logic based on RunCommandConfig
pesse Jun 12, 2019
d8cbce2
RunAction tested and functional so far
pesse Jun 12, 2019
3b5e168
Include RunAction into RunCommand
pesse Jun 12, 2019
60997d7
New entry point for Picocli
pesse Jun 12, 2019
d13ae5b
Extract functionality to interface
pesse Jun 12, 2019
5e86b01
Adapt tests to IRunCommand and implement necessary functionality
pesse Jun 12, 2019
00f3f5f
Change VersionCommand to Picocli
pesse Jun 12, 2019
2ce4e91
Change ReportersCommand to Picocli
pesse Jun 13, 2019
b88d5c5
Fix arity for mapping options and prevent NPE
pesse Jun 13, 2019
c80dc8c
Refactor RunCommandTest to cover new approach
pesse Jun 13, 2019
8eec2cb
Implement rules for printToScreen Reporters: Only one can go to screen
pesse Jun 13, 2019
ded39d3
Implemented new Picocli help
pesse Jun 13, 2019
6922e52
Removed some now unnecessary stuff
pesse Jun 13, 2019
5aa4c1d
We don't need the old RunCommand anymore, time to get rid of it
pesse Jun 13, 2019
60fcfaf
Improve help and help-tests
pesse Jun 13, 2019
a3608b5
Refactor: Remove unnecessary logic
pesse Jun 13, 2019
09bf357
Refactor: extract ReporterConfig -> ReporterOption conversion
pesse Jun 13, 2019
1b65b51
Refactor: getCommand() no longer needed
pesse Jun 13, 2019
77fd752
Refactor: Include only left check-functionality
pesse Jun 13, 2019
e443e3c
Refactor: Cleanup ReporterManager a bit
pesse Jun 13, 2019
0bdca7d
Get rid of JCommander dependency
pesse Jun 13, 2019
ae0e9c4
Move ConnectionString description to main Utplsql-Command
pesse Jun 18, 2019
f47c0de
Fix Typo
pesse Jun 18, 2019
bb18ea1
Some Refactorings and make things final
pesse Jun 18, 2019
1a984ff
Merge branch 'develop' into feature/new_cli_library
pesse Jun 18, 2019
81e0953
Reformatting
pesse Jun 18, 2019
a8acbb9
Add `-h` option to all commands and cover it with tests
pesse Jun 18, 2019
10bef7b
Adjust documentation to reflect parameter changes
pesse Jun 18, 2019
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
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