diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d0c11c --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# IntelliJ +*.iml +.idea/ + +# Maven +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties + +# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored) +!/.mvn/wrapper/maven-wrapper.jar diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..661f6b8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,33 @@ +sudo: required +language: java + +services: + - docker + +jdk: +# - oraclejdk7 + - oraclejdk8 + +env: + global: + - DOCKER_CFG=$HOME/.docker + - DOCKER_REPO="viniciusam/oracledb" + - MAVEN_HOME=/usr/local/maven + - UTPLSQL_VERSION="v3.0.0-beta" + - UTPLSQL_FILE="utPLSQLv3.0.0.562-beta" + matrix: + - ORACLE_VERSION="11g-xe-r2" CONNECTION_STR="127.0.0.1:1521/XE" DOCKER_OPTIONS="--shm-size=1g" + +cache: + directories: + - $DOCKER_CFG + - $HOME/.m2 + - $MAVEN_HOME/lib/ext # Used to cache wagon-http lib. + +install: + - bash .travis/maven_cfg.sh + - bash .travis/start_db.sh + - bash .travis/install_utplsql.sh + +script: + - mvn test -B diff --git a/.travis/install_utplsql.sh b/.travis/install_utplsql.sh new file mode 100644 index 0000000..6e7b5f3 --- /dev/null +++ b/.travis/install_utplsql.sh @@ -0,0 +1,24 @@ +#!/bin/bash +set -ev + +# Download the specified version of utPLSQL. +curl -L -O "https://github.com/utPLSQL/utPLSQL/releases/download/$UTPLSQL_VERSION/$UTPLSQL_FILE.tar.gz" + +# Create a temporary install script. +cat > install.sh.tmp < + + + + + + + + maven.oracle.com + ###USERNAME### + ###PASSWORD### + + + ANY + ANY + OAM 11g + + + + + + http.protocol.allow-circular-redirects + %b,true + + + + + + + + + diff --git a/.travis/settings.xml b/.travis/settings.xml new file mode 100644 index 0000000..731ef52 --- /dev/null +++ b/.travis/settings.xml @@ -0,0 +1,51 @@ + + + + + + + + + maven.oracle.com + ${env.ORACLE_OTN_USER} + ${env.ORACLE_OTN_PASSWORD} + + + ANY + ANY + OAM 11g + + + + + + http.protocol.allow-circular-redirects + %b,true + + + + + + + + + diff --git a/.travis/start_db.sh b/.travis/start_db.sh new file mode 100644 index 0000000..2367d64 --- /dev/null +++ b/.travis/start_db.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -ev + +# If docker credentials are not cached, do the login. +if [ ! -f $DOCKER_CFG/config.json ]; then + docker login -u "$DOCKER_USER" -p "$DOCKER_PASSWORD" +else + echo "Using docker login from cache..." +fi + +# Pull the specified db version from docker hub. +docker pull $DOCKER_REPO:$ORACLE_VERSION +docker run -d --name $ORACLE_VERSION $DOCKER_OPTIONS -p 1521:1521 $DOCKER_REPO:$ORACLE_VERSION +docker logs -f $ORACLE_VERSION | grep -m 1 "DATABASE IS READY TO USE!" --line-buffered diff --git a/README.md b/README.md new file mode 100644 index 0000000..f288b78 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ + +##### Configuring the Oracle Maven Repository ##### +http://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9010 +https://blogs.oracle.com/dev2dev/entry/how_to_get_oracle_jdbc#pom + +##### IntelliJ IDEA Maven Module ##### +https://www.jetbrains.com/help/idea/2017.1/maven.html diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a300d87 --- /dev/null +++ b/pom.xml @@ -0,0 +1,72 @@ + + 4.0.0 + + io.github.utplsql + java-api + 1.0 + jar + + utPLSQL-java-api + https://github.com/utPLSQL/utPLSQL-java-api + + + UTF-8 + 1.8 + 1.8 + + + + + com.oracle.jdbc + ojdbc7 + 12.1.0.2 + + + junit + junit + 4.12 + test + + + + + + + + + + + + + + + + + + + + + + + + maven.oracle.com + + true + + + false + + https://maven.oracle.com + default + + + + + + maven.oracle.com + https://maven.oracle.com + + + + diff --git a/src/main/java/io/github/utplsql/OutputBuffer.java b/src/main/java/io/github/utplsql/OutputBuffer.java new file mode 100644 index 0000000..1a2878b --- /dev/null +++ b/src/main/java/io/github/utplsql/OutputBuffer.java @@ -0,0 +1,80 @@ +package io.github.utplsql; + +import oracle.jdbc.OracleTypes; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Vinicius on 13/04/2017. + */ +public class OutputBuffer { + + private String reporterId; + + public OutputBuffer(String reporterId) { + this.reporterId = reporterId; + } + + public String getReporterId() { + return reporterId; + } + + public void setReporterId(String reporterId) { + this.reporterId = reporterId; + } + + public List getLines() throws SQLException { + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + try { + preparedStatement = UTPLSQL.getConnection() + .prepareStatement("SELECT * FROM TABLE(ut_output_buffer.get_lines(?, 1))"); + + preparedStatement.setString(1, getReporterId()); + resultSet = preparedStatement.executeQuery(); + + List outputLines = new ArrayList<>(); + while (resultSet.next()) { + outputLines.add(resultSet.getString(1)); + } + return outputLines; + } finally { + if (resultSet != null) + resultSet.close(); + if (preparedStatement != null) + preparedStatement.close(); + } + } + + public List getAllLines() throws SQLException { + CallableStatement callableStatement = null; + ResultSet resultSet = null; + try { + callableStatement = UTPLSQL.getConnection() + .prepareCall("BEGIN ? := ut_output_buffer.get_lines_cursor(?); END;"); + + callableStatement.registerOutParameter(1, OracleTypes.CURSOR); + callableStatement.setString(2, getReporterId()); + callableStatement.execute(); + + resultSet = (ResultSet) callableStatement.getObject(1); + + List outputLines = new ArrayList<>(); + while (resultSet.next()) { + outputLines.add(resultSet.getString("text")); + } + return outputLines; + } finally { + if (resultSet != null) + resultSet.close(); + if (callableStatement != null) + callableStatement.close(); + } + } + +} diff --git a/src/main/java/io/github/utplsql/TestRunner.java b/src/main/java/io/github/utplsql/TestRunner.java new file mode 100644 index 0000000..21d1601 --- /dev/null +++ b/src/main/java/io/github/utplsql/TestRunner.java @@ -0,0 +1,45 @@ +package io.github.utplsql; + +import io.github.utplsql.types.BaseReporter; + +import java.sql.CallableStatement; +import java.sql.SQLException; + +/** + * Created by Vinicius Avellar on 12/04/2017. + */ +public class TestRunner { + + public TestRunner() {} + + public void run() throws SQLException { + CallableStatement callableStatement = null; + try { + callableStatement = UTPLSQL.getConnection() + .prepareCall("BEGIN ut_runner.run(); END;"); + callableStatement.execute(); + } finally { + if (callableStatement != null) + callableStatement.close(); + } + } + + public void run(String path, BaseReporter reporter) throws SQLException { + if (reporter.getReporterId() == null || reporter.getReporterId().isEmpty()) { + reporter.setReporterId(UTPLSQL.newSysGuid()); + } + + CallableStatement callableStatement = null; + try { + callableStatement = UTPLSQL.getConnection() + .prepareCall("BEGIN ut_runner.run(:path, :reporter); END;"); + callableStatement.setString(":path", path); + callableStatement.setObject(":reporter", reporter); + callableStatement.execute(); + } finally { + if (callableStatement != null) + callableStatement.close(); + } + } + +} diff --git a/src/main/java/io/github/utplsql/UTPLSQL.java b/src/main/java/io/github/utplsql/UTPLSQL.java new file mode 100644 index 0000000..89a8155 --- /dev/null +++ b/src/main/java/io/github/utplsql/UTPLSQL.java @@ -0,0 +1,59 @@ +package io.github.utplsql; + +import io.github.utplsql.types.DocumentationReporter; +import io.github.utplsql.types.CustomTypes; +import oracle.jdbc.OracleTypes; + +import java.sql.*; +import java.util.Map; + +/** + * Created by Vinicius Avellar on 12/04/2017. + */ +public final class UTPLSQL { + + private static Connection sConnection; + + private UTPLSQL() {} + + public static void init(String url, String user, String password) throws SQLException { + UTPLSQL.init(DriverManager.getConnection(url, user, password)); + } + + public static void init(Connection conn) throws SQLException { + sConnection = conn; + createTypeMap(); + } + + public static void close() { + if (sConnection != null) + try { sConnection.close(); } catch (SQLException ignored) {} + } + + protected static Connection getConnection() { + if (sConnection == null) + throw new RuntimeException("Connection not initialized."); + + return sConnection; + } + + public static String newSysGuid() throws SQLException { + CallableStatement callableStatement = null; + try { + callableStatement = sConnection.prepareCall("BEGIN :id := sys_guid(); END;"); + callableStatement.registerOutParameter(":id", OracleTypes.RAW); + callableStatement.executeUpdate(); + return callableStatement.getString(":id"); + } finally { + if (callableStatement != null) + callableStatement.close(); + } + } + + private static void createTypeMap() throws SQLException { + Map typeMap = sConnection.getTypeMap(); + typeMap.put(CustomTypes.UT_DOCUMENTATION_REPORTER.getName(), DocumentationReporter.class); + sConnection.setTypeMap(typeMap); + } + +} diff --git a/src/main/java/io/github/utplsql/types/BaseReporter.java b/src/main/java/io/github/utplsql/types/BaseReporter.java new file mode 100644 index 0000000..d2c1265 --- /dev/null +++ b/src/main/java/io/github/utplsql/types/BaseReporter.java @@ -0,0 +1,60 @@ +package io.github.utplsql.types; + +import java.sql.SQLData; +import java.sql.SQLException; +import java.sql.SQLInput; +import java.sql.SQLOutput; +import java.util.Calendar; + +/** + * Created by Vinicius on 13/04/2017. + */ +public abstract class BaseReporter implements SQLData { + + private String selfType; + private String reporterId; + private java.sql.Date startDate; + + public BaseReporter() { + startDate = new java.sql.Date(Calendar.getInstance().getTimeInMillis()); + } + + public String getSelfType() { + return this.selfType; + } + + public void setSelfType(String selfType) { + this.selfType = selfType; + } + + public String getReporterId() { + return this.reporterId; + } + + public void setReporterId(String reporterId) { + this.reporterId = reporterId; + } + + public java.sql.Date getStartDate() { + return this.startDate; + } + + public void setStartDate(java.sql.Date startDate) { + this.startDate = startDate; + } + + @Override + public void readSQL(SQLInput stream, String typeName) throws SQLException { + setSelfType(stream.readString()); + setReporterId(stream.readString()); + setStartDate(stream.readDate()); + } + + @Override + public void writeSQL(SQLOutput stream) throws SQLException { + stream.writeString(getSelfType()); + stream.writeString(getReporterId()); + stream.writeDate(getStartDate()); + } + +} diff --git a/src/main/java/io/github/utplsql/types/CustomTypes.java b/src/main/java/io/github/utplsql/types/CustomTypes.java new file mode 100644 index 0000000..22398dd --- /dev/null +++ b/src/main/java/io/github/utplsql/types/CustomTypes.java @@ -0,0 +1,22 @@ +package io.github.utplsql.types; + +/** + * Created by Vinicius on 13/04/2017. + * utPLSQL custom data types. + */ +public enum CustomTypes { + // Object names must be upper case. + UT_DOCUMENTATION_REPORTER("UT_DOCUMENTATION_REPORTER"), + UT_VARCHAF2_LIST("UT_VARCHAR2_LIST"); + + private String typeName; + + CustomTypes(String typeName) { + this.typeName = typeName; + } + + public String getName() { + return this.typeName; + } + +} diff --git a/src/main/java/io/github/utplsql/types/DocumentationReporter.java b/src/main/java/io/github/utplsql/types/DocumentationReporter.java new file mode 100644 index 0000000..72e1873 --- /dev/null +++ b/src/main/java/io/github/utplsql/types/DocumentationReporter.java @@ -0,0 +1,15 @@ +package io.github.utplsql.types; + +import java.sql.SQLException; + +/** + * Created by Vinicius on 13/04/2017. + */ +public class DocumentationReporter extends BaseReporter { + + @Override + public String getSQLTypeName() throws SQLException { + return CustomTypes.UT_DOCUMENTATION_REPORTER.getName(); + } + +} diff --git a/src/test/java/io/github/utplsql/OutputBufferTest.java b/src/test/java/io/github/utplsql/OutputBufferTest.java new file mode 100644 index 0000000..1343f0e --- /dev/null +++ b/src/test/java/io/github/utplsql/OutputBufferTest.java @@ -0,0 +1,71 @@ +package io.github.utplsql; + +import io.github.utplsql.rules.DatabaseRule; +import io.github.utplsql.types.BaseReporter; +import io.github.utplsql.types.DocumentationReporter; +import org.junit.Assert; +import org.junit.ClassRule; +import org.junit.Test; + +import java.sql.SQLException; +import java.util.List; + +/** + * Created by Vinicius on 13/04/2017. + */ +public class OutputBufferTest { + + @ClassRule + public static final DatabaseRule sInitialization = new DatabaseRule(); + + private BaseReporter currentReporter = null; + + public BaseReporter createReporter() throws SQLException { + BaseReporter reporter = new DocumentationReporter(); + reporter.setReporterId(UTPLSQL.newSysGuid()); + System.out.println("Reporter ID: " + reporter.getReporterId()); + return reporter; + } + + @Test + public void getLinesFromOutputBuffer() { + try { + final BaseReporter reporter = createReporter(); +// new TestRunner().run("", reporter); + new Thread(() -> { + try { + new TestRunner().run("", reporter); + } catch (SQLException e) { + e.printStackTrace(); + } + }).start(); + + List outputLines = new OutputBuffer(reporter.getReporterId()) + .getLines(); + + for (int i = 0; i < outputLines.size(); i++) { + System.out.println(outputLines.get(i)); + } + } catch (SQLException e) { + Assert.fail(e.getMessage()); + } + } + + @Test + public void getAllLinesFromOutputBuffer() { + try { + final BaseReporter reporter = createReporter(); + new TestRunner().run("", reporter); + + List outputLines = new OutputBuffer(reporter.getReporterId()) + .getAllLines(); + + for (int i = 0; i < outputLines.size(); i++) { + System.out.println(outputLines.get(i)); + } + } catch (SQLException e) { + Assert.fail(e.getMessage()); + } + } + +} diff --git a/src/test/java/io/github/utplsql/TestRunnerTest.java b/src/test/java/io/github/utplsql/TestRunnerTest.java new file mode 100644 index 0000000..fc63229 --- /dev/null +++ b/src/test/java/io/github/utplsql/TestRunnerTest.java @@ -0,0 +1,40 @@ +package io.github.utplsql; + +import io.github.utplsql.rules.DatabaseRule; +import io.github.utplsql.types.BaseReporter; +import io.github.utplsql.types.DocumentationReporter; +import org.junit.Assert; +import org.junit.ClassRule; +import org.junit.Test; + +import java.sql.SQLException; + +/** + * Created by Vinicius on 13/04/2017. + */ +public class TestRunnerTest { + + @ClassRule + public static final DatabaseRule sInitialization = new DatabaseRule(); + + @Test + public void runWithoutParams() { + try { + new TestRunner().run(); + } catch (SQLException e) { + Assert.fail(e.getMessage()); + } + } + + @Test + public void runWithDocumentationReporter() { + try { + BaseReporter reporter = new DocumentationReporter(); + new TestRunner().run("", reporter); + Assert.assertNotNull(reporter.getReporterId()); + } catch (SQLException e) { + Assert.fail(e.getMessage()); + } + } + +} diff --git a/src/test/java/io/github/utplsql/rules/DatabaseRule.java b/src/test/java/io/github/utplsql/rules/DatabaseRule.java new file mode 100644 index 0000000..71863a8 --- /dev/null +++ b/src/test/java/io/github/utplsql/rules/DatabaseRule.java @@ -0,0 +1,22 @@ +package io.github.utplsql.rules; + +import io.github.utplsql.UTPLSQL; +import org.junit.rules.ExternalResource; + +/** + * Created by Vinicius on 13/04/2017. + */ +public class DatabaseRule extends ExternalResource { + + @Override + protected void before() throws Throwable { + // TODO: Options file. + UTPLSQL.init("jdbc:oracle:thin:@127.0.0.1:1521:xe", "ut3", "ut3"); + } + + @Override + protected void after() { + UTPLSQL.close(); + } + +}