8000 Initial utPLSQL cli code · Pazus/utPLSQL-cli@c49b8a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit c49b8a1

Browse files
committed
Initial utPLSQL cli code
1 parent aa766cd commit c49b8a1

File tree

8 files changed

+452
-0
lines changed

8 files changed

+452
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# IntelliJ
2+
*.iml
3+
.idea/
4+
5+
# Maven
6+
target/
7+
pom.xml.tag
8+
pom.xml.releaseBackup
9+
pom.xml.versionsBackup
10+
pom.xml.next
11+
release.properties
12+
dependency-reduced-pom.xml
13+
buildNumber.properties
14+
.mvn/timing.properties
15+
16+
# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
17+
!/.mvn/wrapper/maven-wrapper.jar

pom.xml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>io.github.utplsql</groupId>
6+
<artifactId>cli</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>cli</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.beust</groupId>
22+
<artifactId>jcommander</artifactId>
23+
<version>1.60</version>
24+
<scope>compile</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>io.github.utplsql</groupId>
28+
<artifactId>java-api</artifactId>
29+
<version>1.0-SNAPSHOT</version>
30+
<scope>compile</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<version>4.12</version>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.codehaus.mojo</groupId>
44+
<artifactId>appassembler-maven-plugin</artifactId>
45+
<version>1.10</version>
46+
<configuration>
47+
<copyConfigurationDirectory>true</copyConfigurationDirectory>
48+
<configurationDirectory>etc</configurationDirectory>
49+
<repositoryName>lib</repositoryName>
50+
<repositoryLayout>flat</repositoryLayout>
51+
<programs>
52+
<program>
53+
<mainClass>io.github.utplsql.cli.Cli</mainClass>
54+
<id>utplsql</id>
55+
</program>
56+
</programs>
57+
</configuration>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
62+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.github.utplsql.cli;
2+
3+
import com.beust.jcommander.JCommander;
4+
import com.beust.jcommander.Parameter;
5+
import com.beust.jcommander.ParameterException;
6+
7+
public class Cli {
8+
9+
public static final String HELP_CMD = "-h";
10+
public static final String RUN_CMD = "run";
11+
12+
public static void main(String[] args) {
13+
JCommander jc = new JCommander();
14+
// jc.addCommand(HELP_CMD, new HelpCommand());
15+
RunCommand runCmd = new RunCommand();
16+
jc.addCommand(RUN_CMD, runCmd);
17+
18+
try {
19+
jc.parse(args);
20+
boolean hasCmd = jc.getParsedCommand() != null;
21+
22+
if (hasCmd && jc.getParsedCommand().equals(RUN_CMD)) {
23+
runCmd.run();
24+
} else {
25+
jc.usage();
26+
}
27+
} catch (ParameterException e) {
28+
if (jc.getParsedCommand() != null)
29+
jc.usage(jc.getParsedCommand());
30+
else
31+
jc.usage();
32+
} catch (Exception e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
37+
private static class HelpCommand {
38+
39+
@Parameter(names = {HELP_CMD, "--help"}, help = true)
40+
public boolean callHelp;
41+
42+
}
43+
44+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package io.github.utplsql.cli;
2+
3+
import com.beust.jcommander.ParameterException;
4+
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
/**
9+
* Created by Vinicius on 21/04/2017.
10+
*/
11+
public class ConnectionInfo {
12+
13+
/**
14+
* Regex pattern to match following connection strings:
15+
* user/pass@127.0.0.1:1521/sid
16+
* user/pass@127.0.0.1/sid
17+
* user/pass@sid
18+
*/
19+
private static final String CONNSTR_PATTERN =
20+
"^(?<user>[0-9a-z]+)/(?<pass>[0-9a-z]+)" +
21+
"(?:(?:@(?<host>[^:/]+)?(?::(?<port>[0-9]+))?(?:/(?<sid1>[0-9a-z]+))$)|(?:@(?<sid2>[0-9a-z]+)$))";
22+
23+
private static final String DEFAULT_HOST = "127.0.0.1";
24+
private static final int DEFAULT_PORT = 1521;
25+
26+
private String user;
27+
private String password;
28+
private String host;
29+
private int port;
30+
private String sid;
31+
32+
public ConnectionInfo() {
33+
}
34+
35+
public ConnectionInfo parseConnectionString(String connectionString) throws ParameterException {
36+
Pattern p = Pattern.compile(CONNSTR_PATTERN);
37+
Matcher m = p.matcher(connectionString);
38+
39+
if (!m.matches())
40+
throw new ParameterException("Invalid connection string!");
41+
42+
this.setUser(m.group("user"));
43+
this.setPassword(m.group("pass"));
44+
this.setHost(m.group("host") != null ? m.group("host") : "127.0.0.1");
45+
this.setPort(m.group("port") != null ? Integer.parseInt(m.group("port")) : 1521);
46+
this.setSid(m.group("sid1") != null ? m.group("sid1") : m.group("sid2"));
47+
48+
return this;
49+
}
50+
51+
public String getUser() {
52+
return user;
53+
}
54+
55+
public void setUser(String user) {
56+
this.user = user;
57+
}
58+
59+
public String getPassword() {
60+
return password;
61+
}
62+
63+
public void setPassword(String password) {
64+
this.password = password;
65+
}
66+
67+
public String getHost() {
68+
return host;
69+
}
70+
71+
public void setHost(String host) {
72+
this.host = host;
73+
}
74+
75+
public int getPort() {
76+
return port;
77+
}
78+
79+
public void setPort(int port) {
80+
this.port = port;
81+
}
82+
83+
public String getSid() {
84+
return sid;
85+
}
86+
87+
public void setSid(String sid) {
88+
this.sid = sid;
89+
}
90+
91+
public String getConnectionUrl() {
92+
return String.format("jdbc:oracle:thin:@%s:%d:%s", getHost(), getPort(), getSid());
93+
}
94+
95+
@Override
96+
public String toString() {
97+
return String.format("%s@%s:%d/%s", getUser(), getHost(), getPort(), getSid());
98+
}
99+
100+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.utplsql.cli;
2+
3+
import com.beust.jcommander.IStringConverter;
4+
import com.beust.jcommander.ParameterException;
5+
6+
/**
7+
* Created by Vinicius on 21/04/2017.
8+
*/
9+
public class ConnectionStringConverter implements IStringConverter<ConnectionInfo> {
10+
11+
@Override
12+
public ConnectionInfo convert(String s) {
13+
try {
14+
return new ConnectionInfo().parseConnectionString(s);
15+
} catch (ParameterException ignored) {
16+
return null;
17+
}
18+
}
19+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package io.github.utplsql.cli;
2+
3+
import com.beust.jcommander.Parameter;
4+
import com.beust.jcommander.Parameters;
5+
import com.sun.deploy.util.StringUtils;
6+
import io.github.utplsql.api.OutputBuffer;
7+
import io.github.utplsql.api.OutputBufferLines;
8+
import io.github.utplsql.api.TestRunner;
9+
import io.github.utplsql.api.utPLSQL;
10+
import io.github.utplsql.api.types.BaseReporter;
11+
import io.github.utplsql.api.types.DocumentationReporter;
12+
13+
import java.sql.Connection;
14+
import java.sql.SQLException;
15+
import java.util.List;
16+
import java.util.concurrent.ExecutorService;
17+
import java.util.concurrent.Executors;
18+
import java.util.concurrent.TimeUnit;
19+
20+
/**
21+
* Created by vinicius.moreira on 19/04/2017.
22+
*/
23+
@Parameters(separators = "=", commandDescription = "run tests")
24+
public class RunCommand {
25+
26+
@Parameter(
27+
required = true, converter = ConnectionStringConverter.class,
28+
description = "user/pass@[[host][:port]/]sid")
29+
private List<ConnectionInfo> connectionInfoList;
30+
31+
@Parameter(
32+
names = {"-p", "--path"},
33+
description = "run suites/tests by path, format: \n" +
34+
"schema or schema:[suite ...][.test] or schema[.suite ...][.test]")
35+
private List<String> testPaths;
36+
37+
public ConnectionInfo getConnectionInfo() {
38+
return connectionInfoList.get(0);
39+
}
40+
41+
public String getTestPaths() {
42+
if (testPaths != null && testPaths.size() > 1)
43+
throw new RuntimeException("Multiple test paths not supported yet.");
44+
45+
return (testPaths == null) ? null : StringUtils.join(testPaths, ",");
46+
}
47+
48+
public void run() throws Exception {
49+
ConnectionInfo ci = getConnectionInfo();
50+
System.out.println("Running Tests For: " + ci.toString());
51+
52+
utPLSQL.init(ci.getConnectionUrl(), ci.getUser(), ci.getPassword());
53+
54+
String tempTestPaths = getTestPaths();
55+
if (tempTestPaths == null) tempTestPaths = ci.getUser();
56+
57+
final BaseReporter reporter = createDocumentationReporter();
58+
final String testPaths = tempTestPaths;
59+
60+
ExecutorService executorService = Executors.newFixedThreadPool(2);
61+
62+
executorService.submit(() -> {
63+
Connection conn = null;
64+
try {
65+
conn = utPLSQL.getConnection();
66+
new TestRunner().run(conn, testPaths, reporter);
67+
} catch (SQLException e) {
68+
// TODO
69+
e.printStackTrace();
70+
} finally {
71+
if (conn != null)
72+
try { conn.close(); } catch (SQLException ignored) {}
73+
}
74+
});
75+
76+
executorService.submit(() -> {
77+
Connection conn = null;
78+
try {
79+
conn = utPLSQL.getConnection();
80+
OutputBufferLines outputLines;
81+
do {
82+
outputLines = new OutputBuffer(reporter.getReporterId())
83+
.fetchAvailable(conn);
84+
85+
Thread.sleep(500);
86+
87+
if (outputLines.getLines().size() > 0)
88+
System.out.println(outputLines.toString());
89+
} while (!outputLines.isFinished());
90+
} catch (SQLException e) {
91+
// TODO
92+
e.printStackTrace();
93+
} catch (InterruptedException ignored) {
94+
// ignored
95+
} finally {
96+
if (conn != null)
97+
try { conn.close(); } catch (SQLException ignored) {}
98+
}
99+
});
100+
101+
executorService.shutdown();
102+
executorService.awaitTermination(60, TimeUnit.MINUTES);
103+
}
104+
105+
private BaseReporter createDocumentationReporter() throws SQLException {
106+
Connection conn = null;
107+
try {
108+
conn = utPLSQL.getConnection();
109+
BaseReporter reporter = new DocumentationReporter();
110+
reporter.setReporterId(utPLSQL.newSysGuid(conn));
111+
return reporter;
112+
} finally {
113+
if (conn != null)
114+
try { conn.close(); } catch (SQLException ignored) {}
115+
}
116+
}
117+
118+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.github.utplsql.cli;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
/**
7+
* Unit test for simple Cli.
8+
*/
9+
public class CliTest {
10+
11+
@Test
12+
public void dummyTest() {
13+
Assert.assertTrue(true);
14+
}
15+
16+
}

0 commit comments

Comments
 (0)
0