Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Api development startup
  • Loading branch information
viniciusam committed Apr 13, 2017
commit 8ca4248650aba9a08ece6785519a7352104be553
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
Expand All @@ -28,6 +30,24 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>net.orfjackal.retrolambda</groupId>
<artifactId>retrolambda-maven-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<goals>
<goal>process-main</goal>
<goal>process-test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>maven.oracle.com</id>
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/io/github/utplsql/App.java

This file was deleted.

44 changes: 44 additions & 0 deletions src/main/java/io/github/utplsql/OutputBuffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.utplsql;

import oracle.jdbc.OracleTypes;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Vinicius on 13/04/2017.
*/
public final class OutputBuffer {

private OutputBuffer() {}

public static List<String> getAllLines(String reporterId) throws SQLException {
CallableStatement callableStatement = null;
ResultSet resultSet = null;
try {
callableStatement = UTPLSQL.getConnection()
.prepareCall("BEGIN :lines := ut_output_buffer.get_lines_cursor(:reporter_id); END;");

callableStatement.registerOutParameter(":lines", OracleTypes.CURSOR);
callableStatement.setString(":reporter_id", reporterId);
callableStatement.execute();

resultSet = (ResultSet) callableStatement.getObject(":lines");

List<String> outputLines = new ArrayList<>();
while (resultSet.next()) {
outputLines.add(resultSet.getString(0));
}
return outputLines;
} finally {
if (resultSet != null)
resultSet.close();
if (callableStatement != null)
callableStatement.close();
}
}

}
45 changes: 45 additions & 0 deletions src/main/java/io/github/utplsql/TestRunner.java
Original file line number Diff line number Diff line change
@@ -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 final class TestRunner {

private TestRunner() {}

public static void run() throws SQLException {
CallableStatement callableStatement = null;
try {
callableStatement = UTPLSQL.getConnection()
.prepareCall("BEGIN ut.run(); END;");
callableStatement.execute();
} finally {
if (callableStatement != null)
callableStatement.close();
}
}

public static 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.run(:path, :reporter); END;");
callableStatement.setString(":path", path);
callableStatement.setObject(":reporter", reporter);
callableStatement.execute();
} finally {
if (callableStatement != null)
callableStatement.close();
}
}

}
59 changes: 59 additions & 0 deletions src/main/java/io/github/utplsql/UTPLSQL.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
60 changes: 60 additions & 0 deletions src/main/java/io/github/utplsql/types/BaseReporter.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
22 changes: 22 additions & 0 deletions src/main/java/io/github/utplsql/types/CustomTypes.java
Original file line number Diff line number Diff line change
@@ -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_VARCHAF2_LIST");

private String typeName;

CustomTypes(String typeName) {
this.typeName = typeName;
}

public String getName() {
return this.typeName;
}

}
15 changes: 15 additions & 0 deletions src/main/java/io/github/utplsql/types/DocumentationReporter.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
35 changes: 0 additions & 35 deletions src/test/java/io/github/utplsql/AppTest.java

This file was deleted.

43 changes: 43 additions & 0 deletions src/test/java/io/github/utplsql/OutputBufferTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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.Connection;
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();

@Test
public void getLinesFromOutputBuffer() {
try {
Connection conn = UTPLSQL.getConnection();
conn.setAutoCommit(false);

BaseReporter reporter = new DocumentationReporter();
reporter.setReporterId(UTPLSQL.newSysGuid());
TestRunner.run("", reporter);

List<String> outputLines = OutputBuffer.getAllLines(reporter.getReporterId());
for (int i = 0; i < outputLines.size(); i++) {
System.out.println(outputLines.get(0));
}

conn.commit();
} catch (SQLException e) {
Assert.fail(e.getMessage());
}
}

}
Loading