Skip to content

Commit 723babe

Browse files
committed
Changes on the api and tests
1 parent 82176c9 commit 723babe

File tree

13 files changed

+242
-236
lines changed

13 files changed

+242
-236
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ env:
1313
- DOCKER_CFG=$HOME/.docker
1414
- DOCKER_REPO="viniciusam/oracledb"
1515
- MAVEN_HOME=/usr/local/maven
16+
- DB_USER=app
17+
- DB_PASS=app
1618
matrix:
1719
- ORACLE_VERSION="11g-xe-r2" CONNECTION_STR="127.0.0.1:1521/XE" DOCKER_OPTIONS="--shm-size=1g"
1820

@@ -26,6 +28,7 @@ install:
2628
- bash .travis/maven_cfg.sh
2729
- bash .travis/start_db.sh
2830
- bash .travis/install_utplsql.sh
31+
- bash .travis/create_app_user.sh
2932

3033
script:
3134
- mvn test -B

.travis/create_app_user.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -ev
3+
4+
sqlplus -S -L sys/oracle@//$CONNECTION_STR AS SYSDBA <<EOF
5+
create user $DB_USER identified by $DB_PASS
6+
quota unlimited on USERS
7+
default tablespace USERS;
8+
grant create session,
9+
create procedure,
10+
create type,
11+
create table,
12+
create sequence,
13+
create view
14+
to $DB_USER;
15+
grant select any dictionary to $DB_USER;
16+
exit;
17+
EOF
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.github.utplsql.api;
2+
3+
import oracle.jdbc.OracleTypes;
4+
5+
import java.sql.CallableStatement;
6+
import java.sql.Connection;
7+
import java.sql.SQLException;
8+
9+
/**
10+
* Created by Vinicius Avellar on 12/04/2017.
11+
*/
12+
public final class DBHelper {
13+
14+
public static String newSysGuid(Connection conn) throws SQLException {
15+
CallableStatement callableStatement = null;
16+
try {
17+
callableStatement = conn.prepareCall("BEGIN :id := sys_guid(); END;");
18+
callableStatement.registerOutParameter(":id", OracleTypes.RAW);
19+
callableStatement.executeUpdate();
20+
return callableStatement.getString(":id");
21+
} finally {
22+
if (callableStatement != null)
23+
callableStatement.close();
24+
}
25+
}
26+
27+
}
Lines changed: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,103 @@
11
package io.github.utplsql.api;
22

3+
import io.github.utplsql.api.types.BaseReporter;
34
import oracle.jdbc.OracleTypes;
45

5-
import java.sql.CallableStatement;
6-
import java.sql.Connection;
7-
import java.sql.ResultSet;
8-
import java.sql.SQLException;
6+
import java.io.PrintStream;
7+
import java.sql.*;
8+
import java.util.ArrayList;
9+
import java.util.List;
910

1011
/**
11-
* Created by Vinicius on 13/04/2017.
12+
* Fetches the lines produced by a reporter.
1213
*/
1314
public class OutputBuffer {
1415

15-
private String reporterId;
16+
private BaseReporter reporter;
1617

17-
public OutputBuffer(String reporterId) {
18-
this.reporterId = reporterId;
18+
/**
19+
* Creates a new OutputBuffer.
20+
* @param reporter the reporter to be used
21+
*/
22+
public OutputBuffer(BaseReporter reporter) {
23+
this.reporter = reporter;
1924
}
2025

21-
public String getReporterId() {
22-
return reporterId;
26+
/**
27+
* Returns the reporter used by this buffer.
28+
* @return the reporter instance
29+
*/
30+
public BaseReporter getReporter() {
31+
return reporter;
2332
}
2433

25-
public void setReporterId(String reporterId) {
26-
this.reporterId = reporterId;
34+
/**
35+
* Print the lines as soon as they are produced and write to a PrintStream.
36+
* @param conn DB connection
37+
* @param ps the PrintStream to be used, e.g: System.out
38+
* @throws SQLException any sql errors
39+
*/
40+
public void printAvailable(Connection conn, PrintStream ps) throws SQLException {
41+
List<PrintStream> printStreams = new ArrayList<>(1);
42+
printStreams.add(ps);
43+
printAvailable(conn, printStreams);
2744
}
2845

29-
public OutputBufferLines fetchAvailable(Connection conn) throws SQLException {
30-
CallableStatement callableStatement = null;
46+
/**
47+
* Print the lines as soon as they are produced and write to a list of PrintStreams.
48+
* @param conn DB connection
49+
* @param printStreams the PrintStream list to be used, e.g: System.out, new PrintStream(new FileOutputStream)
50+
* @throws SQLException any sql errors
51+
*/
52+
public void printAvailable(Connection conn, List<PrintStream> printStreams) throws SQLException {
53+
fetchAvailable(conn, s -> {
54+
for (PrintStream ps : printStreams)
55+
ps.println(s);
56+
});
57+
}
58+
59+
/**
60+
* Print the lines as soon as they are produced and call the callback passing the new line.
61+
* @param conn DB connection
62+
* @param cb the callback to be called
63+
* @throws SQLException any sql errors
64+
*/
65+
public void fetchAvailable(Connection conn, Callback cb) throws SQLException {
66+
PreparedStatement preparedStatement = null;
3167
ResultSet resultSet = null;
3268
try {
33-
callableStatement = conn.prepareCall("BEGIN ? := ut_output_buffer.get_available_lines(?); END;");
69+
preparedStatement = conn.prepareCall("SELECT * FROM table(ut_output_buffer.get_lines(?))");
70+
preparedStatement.setString(1, getReporter().getReporterId());
71+
resultSet = preparedStatement.executeQuery();
3472

35-
callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
36-
callableStatement.setString(2, getReporterId());
37-
callableStatement.execute();
38-
39-
resultSet = (ResultSet) callableStatement.getObject(1);
40-
41-
OutputBufferLines outputLines = new OutputBufferLines();
42-
while (resultSet.next()) {
43-
outputLines.add(resultSet.getString("text"));
44-
if (resultSet.getInt("is_finished") == 1)
45-
outputLines.setFinished(true);
46-
}
47-
return outputLines;
73+
while (resultSet.next())
74+
cb.onLineFetched(resultSet.getString(1));
4875
} finally {
4976
if (resultSet != null)
5077
resultSet.close();
51-
if (callableStatement != null)
52-
callableStatement.close();
78+
if (preparedStatement != null)
79+
preparedStatement.close();
5380
}
5481
}
5582

56-
public OutputBufferLines fetchAll(Connection conn) throws SQLException {
83+
/**
84+
* Get all lines from output buffer and return it as a list of strings.
85+
* @param conn DB connection
86+
* @return the lines
87+
* @throws SQLException any sql errors
88+
*/
89+
public List<String> fetchAll(Connection conn) throws SQLException {
5790
CallableStatement callableStatement = null;
5891
ResultSet resultSet = null;
5992
try {
6093
callableStatement = conn.prepareCall("BEGIN ? := ut_output_buffer.get_lines_cursor(?); END;");
61-
6294
callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
63-
callableStatement.setString(2, getReporterId());
95+
callableStatement.setString(2, getReporter().getReporterId());
6496
callableStatement.execute();
6597

6698
resultSet = (ResultSet) callableStatement.getObject(1);
6799

68-
OutputBufferLines outputLines = new OutputBufferLines();
69-
outputLines.setFinished(true);
100+
List<String> outputLines = new ArrayList<>();
70101
while (resultSet.next()) {
71102
outputLines.add(resultSet.getString("text"));
72103
}
@@ -79,4 +110,11 @@ public OutputBufferLines fetchAll(Connection conn) throws SQLException {
79110
}
80111
}
81112

113+
/**
114+
* Callback to be called when a new line is available from the output buffer.
115+
*/
116+
public interface Callback {
117+
void onLineFetched(String s);
118+
}
119+
82120
}

src/main/java/io/github/utplsql/api/OutputBufferLines.java

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

src/main/java/io/github/utplsql/api/TestRunner.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,11 @@ public class TestRunner {
1313

1414
public TestRunner() {}
1515

16-
public void run(Connection conn) throws SQLException {
17-
CallableStatement callableStatement = null;
18-
try {
19-
callableStatement = conn.prepareCall("BEGIN ut_runner.run(); END;");
20-
callableStatement.execute();
21-
} finally {
22-
if (callableStatement != null)
23-
callableStatement.close();
24-
}
25-
}
26-
2716
public void run(Connection conn, String path, BaseReporter reporter) throws SQLException {
28-
if (reporter.getReporterId() == null || reporter.getReporterId().isEmpty()) {
29-
reporter.setReporterId(utPLSQL.newSysGuid(conn));
30-
}
31-
17+
validateReporter(conn, reporter);
3218
CallableStatement callableStatement = null;
3319
try {
34-
callableStatement = conn.prepareCall("BEGIN ut_runner.run(:path, :reporter); END;");
20+
callableStatement = conn.prepareCall("BEGIN ut_runner.run(a_path => :path, a_reporter => :reporter); END;");
3521
callableStatement.setString(":path", path);
3622
callableStatement.setObject(":reporter", reporter);
3723
callableStatement.execute();
@@ -41,4 +27,15 @@ public void run(Connection conn, String path, BaseReporter reporter) throws SQLE
4127
}
4228
}
4329

30+
/**
31+
* Check if the reporter was initialized, if not call reporter.init.
32+
* @param conn the database connection
33+
* @param reporter the reporter
34+
* @throws SQLException any sql exception
35+
*/
36+
private void validateReporter(Connection conn, BaseReporter reporter) throws SQLException {
37+
if (reporter.getReporterId() == null || reporter.getReporterId().isEmpty())
38+
reporter.init(conn);
39+
}
40+
4441
}

src/main/java/io/github/utplsql/api/types/BaseReporter.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.utplsql.api.types;
22

3+
import io.github.utplsql.api.DBHelper;
4+
35
import java.sql.*;
46
import java.util.Calendar;
57

@@ -12,31 +14,35 @@ public abstract class BaseReporter implements SQLData {
1214
private String reporterId;
1315
private java.sql.Date startDate;
1416

15-
public BaseReporter() {
16-
this.startDate = new java.sql.Date(Calendar.getInstance().getTimeInMillis());
17+
public BaseReporter() {}
18+
19+
public BaseReporter init(Connection conn) throws SQLException {
20+
setStartDate(new java.sql.Date(Calendar.getInstance().getTimeInMillis()));
21+
setReporterId(DBHelper.newSysGuid(conn));
22+
return this;
1723
}
1824

1925
public String getSelfType() {
2026
return this.selfType;
2127
}
2228

23-
public void setSelfType(String selfType) {
29+
private void setSelfType(String selfType) {
2430
this.selfType = selfType;
2531
}
2632

2733
public String getReporterId() {
2834
return this.reporterId;
2935
}
3036

31-
public void setReporterId(String reporterId) {
37+
private void setReporterId(String reporterId) {
3238
this.reporterId = reporterId;
3339
}
3440

3541
public java.sql.Date getStartDate() {
3642
return this.startDate;
3743
}
3844

39-
public void setStartDate(java.sql.Date startDate) {
45+
private void setStartDate(java.sql.Date startDate) {
4046
this.startDate = startDate;
4147
}
4248

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.utplsql.api.types;
2+
3+
import java.sql.SQLException;
4+
5+
/**
6+
* Created by Vinicius on 13/04/2017.
7+
*/
8+
public class CoverageHTMLReporter extends BaseReporter {
9+
10+
@Override
11+
public String getSQLTypeName() throws SQLException {
12+
return CustomTypes.UT_COVERAGE_HTML_REPORTER.getName();
13+
}
14+
15+
}

src/main/java/io/github/utplsql/api/types/CustomTypes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
/**
44
* Created by Vinicius on 13/04/2017.
5-
* utPLSQL custom data types.
5+
* DBHelper custom data types.
66
*/
77
public enum CustomTypes {
88
// Object names must be upper case.
99
UT_DOCUMENTATION_REPORTER("UT_DOCUMENTATION_REPORTER"),
10+
UT_COVERAGE_HTML_REPORTER("UT_COVERAGE_HTML_REPORTER"),
1011
UT_VARCHAF2_LIST("UT_VARCHAR2_LIST");
1112

1213
private String typeName;

0 commit comments

Comments
 (0)