Skip to content

Feature/test failure indicator #13

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 4 commits into from
Jul 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ env:
- CACHE_DIR=$HOME/.cache
- MAVEN_HOME=/usr/local/maven
- MAVEN_CFG=$HOME/.m2
- API_DB_URL="127.0.0.1:1521:XE"
- API_DB_USER=api
- API_DB_PASS=api
- DB_URL="127.0.0.1:1521:XE"
- DB_USER=app
- DB_PASS=app
matrix:
- ORACLE_VERSION="11g-xe-r2" DOCKER_OPTIONS="--shm-size=1g"

Expand All @@ -31,6 +31,7 @@ install:
- bash .travis/maven_cfg.sh
- bash .travis/start_db.sh
- bash .travis/install_utplsql.sh
- bash .travis/install_demo_project.sh

script:
- mvn test -B
44 changes: 44 additions & 0 deletions .travis/install_demo_project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
set -ev
cd $(dirname $(readlink -f $0))

PROJECT_FILE="utPLSQL-demo-project"
git clone -b develop --single-branch https://github.com/utPLSQL/utPLSQL-demo-project.git

cat > demo_project.sh.tmp <<EOF
sqlplus -S -L sys/oracle@//127.0.0.1:1521/xe AS SYSDBA <<SQL
create user ${DB_USER} identified by ${DB_PASS} quota unlimited on USERS default tablespace USERS;
grant create session, create procedure, create type, create table, create sequence, create view to ${DB_USER};
grant select any dictionary to ${DB_USER};
exit
SQL

cd ${PROJECT_FILE}
sqlplus -S -L ${DB_USER}/${DB_PASS}@//127.0.0.1:1521/xe <<SQL
whenever sqlerror exit failure rollback
whenever oserror exit failure rollback

@source/award_bonus/employees_test.sql
@source/award_bonus/award_bonus.prc

@source/between_string/betwnstr.fnc

@source/remove_rooms_by_name/rooms.sql
@source/remove_rooms_by_name/remove_rooms_by_name.prc

@test/award_bonus/test_award_bonus.pks
@test/award_bonus/test_award_bonus.pkb

@test/between_string/test_betwnstr.pks
@test/between_string/test_betwnstr.pkb

@test/remove_rooms_by_name/test_remove_rooms_by_name.pks
@test/remove_rooms_by_name/test_remove_rooms_by_name.pkb

exit
SQL
EOF

docker cp ./$PROJECT_FILE $ORACLE_VERSION:/$PROJECT_FILE
docker cp ./demo_project.sh.tmp $ORACLE_VERSION:/demo_project.sh
docker exec $ORACLE_VERSION bash demo_project.sh
7 changes: 4 additions & 3 deletions .travis/install_utplsql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ git clone -b develop --single-branch https://github.com/utPLSQL/utPLSQL.git

# Create a temporary install script.
cat > install.sh.tmp <<EOF
# tar -xzf $UTPLSQL_FILE.tar.gz && rm $UTPLSQL_FILE.tar.gz
cd /$UTPLSQL_FILE/source
sqlplus -S -L sys/oracle@//127.0.0.1:1521/xe AS SYSDBA @install_headless.sql
# tar -xzf ${UTPLSQL_FILE}.tar.gz && rm ${UTPLSQL_FILE}.tar.gz
cd ${UTPLSQL_FILE}/source
sqlplus -S -L sys/oracle@//127.0.0.1:1521/xe AS SYSDBA @install_headless.sql ut3 ut3 users
EOF

# Copy utPLSQL files to the container and install it.
Expand All @@ -28,6 +28,7 @@ docker cp ./create_api_user.sh $ORACLE_VERSION:/create_api_user.sh

# Remove temporary files.
# rm $UTPLSQL_FILE.tar.gz
rm -rf $UTPLSQL_FILE
rm install.sh.tmp

# Execute the utPLSQL installation inside the container.
Expand Down
28 changes: 24 additions & 4 deletions src/main/java/io/github/utplsql/api/TestRunner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.utplsql.api;

import io.github.utplsql.api.exception.SomeTestsFailedException;
import io.github.utplsql.api.reporter.DocumentationReporter;
import io.github.utplsql.api.reporter.Reporter;
import oracle.jdbc.OracleConnection;
Expand All @@ -21,6 +22,7 @@ public class TestRunner {
private List<String> testFiles = new ArrayList<>();
private List<String> includeObjects = new ArrayList<>();
private List<String> excludeObjects = new ArrayList<>();
private boolean failOnErrors = false;

public TestRunner addPath(String path) {
this.pathList.add(path);
Expand Down Expand Up @@ -72,7 +74,12 @@ public TestRunner excludeObject(String obj) {
return this;
}

public void run(Connection conn) throws SQLException {
public TestRunner failOnErrors(boolean failOnErrors) {
this.failOnErrors = failOnErrors;
return this;
}

public void run(Connection conn) throws SomeTestsFailedException, SQLException {
for (Reporter r : this.reporterList)
validateReporter(conn, r);

Expand All @@ -86,16 +93,23 @@ public void run(Connection conn) throws SQLException {

// Workaround because Oracle JDBC doesn't support passing boolean to stored procedures.
String colorConsoleStr = Boolean.toString(this.colorConsole);
String failOnErrors = Boolean.toString(this.failOnErrors);

OracleConnection oraConn = conn.unwrap(OracleConnection.class);
CallableStatement callableStatement = null;
try {
callableStatement = conn.prepareCall(
"BEGIN " +
"ut_runner.run(" +
"a_paths => ?, a_reporters => ?, a_color_console => " + colorConsoleStr + ", " +
"a_coverage_schemes => ?, a_source_files => ?, a_test_files => ?, " +
"a_include_objects => ?, a_exclude_objects => ?); " +
"a_paths => ?, " +
"a_reporters => ?, " +
"a_color_console => " + colorConsoleStr + ", " +
"a_coverage_schemes => ?, " +
"a_source_files => ?, " +
"a_test_files => ?, " +
"a_include_objects => ?, " +
"a_exclude_objects => ?, " +
"a_fail_on_errors => " + failOnErrors + "); " +
"END;");

int paramIdx = 0;
Expand Down Expand Up @@ -142,6 +156,12 @@ public void run(Connection conn) throws SQLException {
}

callableStatement.execute();
} catch (SQLException e) {
if (e.getErrorCode() == SomeTestsFailedException.ERROR_CODE) {
throw new SomeTestsFailedException(e.getMessage(), e);
} else {
throw e;
}
} finally {
if (callableStatement != null)
callableStatement.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.utplsql.api.exception;

import java.sql.SQLException;

/**
* Custom exception class to indicate if some tests failed.
*/
public class SomeTestsFailedException extends SQLException {

public static final int ERROR_CODE = 20213;

public SomeTestsFailedException(String reason, Throwable cause) {
super(reason, cause);
}

}
17 changes: 16 additions & 1 deletion src/test/java/io/github/utplsql/api/TestRunnerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.utplsql.api;

import io.github.utplsql.api.exception.SomeTestsFailedException;
import io.github.utplsql.api.reporter.*;
import io.github.utplsql.api.rules.DatabaseRule;
import org.junit.Assert;
Expand Down Expand Up @@ -32,7 +33,6 @@ public void runWithManyReporters() {
try {
Connection conn = db.newConnection();
new TestRunner()
.addPath("ut3")
.addPath(db.getUser())
.addReporter(new DocumentationReporter().init(conn))
.addReporter(new CoverageHTMLReporter().init(conn))
Expand All @@ -47,4 +47,19 @@ public void runWithManyReporters() {
}
}

@Test
public void failOnErrors() {
try {
Connection conn = db.newConnection();
new TestRunner()
.failOnErrors(true)
.run(conn);
Assert.fail();
} catch (SomeTestsFailedException ignored) {
System.out.println("Expected exception object thrown.");
} catch (SQLException e) {
Assert.fail("Wrong exception object thrown.");
}
}

}
6 changes: 3 additions & 3 deletions src/test/java/io/github/utplsql/api/rules/DatabaseRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class DatabaseRule extends ExternalResource {
private static String sPass;

static {
sUrl = System.getenv("API_DB_URL") != null ? System.getenv("API_DB_URL") : "127.0.0.1:1521:XE";
sUser = System.getenv("API_DB_USER") != null ? System.getenv("API_DB_USER") : "app";
sPass = System.getenv("API_DB_PASS") != null ? System.getenv("API_DB_PASS") : "app";
sUrl = System.getenv("DB_URL") != null ? System.getenv("DB_URL") : "192.168.99.100:1521:XE";
sUser = System.getenv("DB_USER") != null ? System.getenv("DB_USER") : "app";
sPass = System.getenv("DB_PASS") != null ? System.getenv("DB_PASS") : "app";
}

private List<Connection> connectionList;
Expand Down