Skip to content

Changes on FileMapper API #15

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 1 commit into from
Jul 24, 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
57 changes: 42 additions & 15 deletions src/main/java/io/github/utplsql/api/FileMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleTypes;

import java.sql.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -20,7 +17,7 @@ private FileMapper() {}
* Call the database api to build the custom file mappings.
*/
public static Array buildFileMappingArray(
Connection conn, List<String> filePaths, FileMapperOptions mapperOptions) throws SQLException {
Connection conn, FileMapperOptions mapperOptions) throws SQLException {
OracleConnection oraConn = conn.unwrap(OracleConnection.class);

Map typeMap = conn.getTypeMap();
Expand All @@ -43,23 +40,53 @@ public static Array buildFileMappingArray(
int paramIdx = 0;
callableStatement.registerOutParameter(++paramIdx, OracleTypes.ARRAY, CustomTypes.UT_FILE_MAPPINGS);

callableStatement.setString(++paramIdx, mapperOptions.getObjectOwner());
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, filePaths.toArray()));
if (mapperOptions.getRegexPattern() == null) {
callableStatement.setNull(++paramIdx, Types.VARCHAR);
} else {
callableStatement.setString(++paramIdx, mapperOptions.getObjectOwner());
}

callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_KEY_VALUE_PAIRS, mapperOptions.getTypeMappings().toArray()));
callableStatement.setString(++paramIdx, mapperOptions.getRegexPattern());
callableStatement.setInt(++paramIdx, mapperOptions.getOwnerSubExpression());
callableStatement.setInt(++paramIdx, mapperOptions.getNameSubExpression());
callableStatement.setInt(++paramIdx, mapperOptions.getTypeSubExpression());
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, mapperOptions.getFilePaths().toArray()));

if (mapperOptions.getTypeMappings() == null) {
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_KEY_VALUE_PAIR);
} else {
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_KEY_VALUE_PAIRS, mapperOptions.getTypeMappings().toArray()));
}

if (mapperOptions.getRegexPattern() == null) {
callableStatement.setNull(++paramIdx, Types.VARCHAR);
} else {
callableStatement.setString(++paramIdx, mapperOptions.getRegexPattern());
}

if (mapperOptions.getOwnerSubExpression() == null) {
callableStatement.setNull(++paramIdx, Types.INTEGER);
} else {
callableStatement.setInt(++paramIdx, mapperOptions.getOwnerSubExpression());
}

if (mapperOptions.getNameSubExpression() == null) {
callableStatement.setNull(++paramIdx, Types.INTEGER);
} else {
callableStatement.setInt(++paramIdx, mapperOptions.getNameSubExpression());
}

if (mapperOptions.getTypeSubExpression() == null) {
callableStatement.setNull(++paramIdx, Types.INTEGER);
} else {
callableStatement.setInt(++paramIdx, mapperOptions.getTypeSubExpression());
}

callableStatement.execute();
return callableStatement.getArray(1);
}

public static List<FileMapping> buildFileMappingList(
Connection conn, List<String> filePaths, FileMapperOptions mapperOptions) throws SQLException {
java.sql.Array fileMappings = buildFileMappingArray(conn, filePaths, mapperOptions);
Connection conn, FileMapperOptions mapperOptions) throws SQLException {
java.sql.Array fileMappings = buildFileMappingArray(conn, mapperOptions);

List<FileMapping> mappingList = new ArrayList<>();
for (Object obj : (Object[]) fileMappings.getArray()) {
Expand Down
32 changes: 20 additions & 12 deletions src/main/java/io/github/utplsql/api/FileMapperOptions.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package io.github.utplsql.api;

import java.util.ArrayList;
import java.util.List;

public class FileMapperOptions {

private List<String> filePaths;
private String objectOwner;
private List<KeyValuePair> typeMappings;
private String regexPattern;
private int ownerSubExpression;
private int typeSubExpression;
private int nameSubExpression;
private Integer ownerSubExpression;
private Integer typeSubExpression;
private Integer nameSubExpression;

public FileMapperOptions() {
this.typeMappings = new ArrayList<>();
public FileMapperOptions(List<String> filePaths) {
this.setFilePaths(filePaths);
}

public List<String> getFilePaths() {
return filePaths;
}

public void setFilePaths(List<String> filePaths) {
this.filePaths = filePaths;
}

public String getObjectOwner() {
Expand All @@ -40,27 +48,27 @@ public void setRegexPattern(String regexPattern) {
this.regexPattern = regexPattern;
}

public int getOwnerSubExpression() {
public Integer getOwnerSubExpression() {
return ownerSubExpression;
}

public void setOwnerSubExpression(int ownerSubExpression) {
public void setOwnerSubExpression(Integer ownerSubExpression) {
this.ownerSubExpression = ownerSubExpression;
}

public int getTypeSubExpression() {
public Integer getTypeSubExpression() {
return typeSubExpression;
}

public void setTypeSubExpression(int typeSubExpression) {
public void setTypeSubExpression(Integer typeSubExpression) {
this.typeSubExpression = typeSubExpression;
}

public int getNameSubExpression() {
public Integer getNameSubExpression() {
return nameSubExpression;
}

public void setNameSubExpression(int nameSubExpression) {
public void setNameSubExpression(Integer nameSubExpression) {
this.nameSubExpression = nameSubExpression;
}

Expand Down
84 changes: 24 additions & 60 deletions src/main/java/io/github/utplsql/api/TestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,6 @@ public TestRunner addCoverageScheme(String coverageScheme) {
return this;
}

public TestRunner withSourceFiles(List<String> sourceFiles) {
if (sourceFiles != null) this.sourceFiles.addAll(sourceFiles);
return this;
}

public TestRunner withTestFiles(List<String> testFiles) {
if (testFiles != null) this.testFiles.addAll(testFiles);
return this;
}

public TestRunner includeObject(String obj) {
this.includeObjects.add(obj);
return this;
Expand Down Expand Up @@ -110,29 +100,21 @@ public void run(Connection conn) throws SomeTestsFailedException, SQLException {
String colorConsoleStr = Boolean.toString(this.colorConsole);
String failOnErrors = Boolean.toString(this.failOnErrors);

String sourceFilesParam = "a_source_files";
String testFilesParam = "a_test_files";

if (this.sourceMappingOptions != null || this.testMappingOptions != null) {
sourceFilesParam = "a_source_file_mappings";
testFilesParam = "a_test_file_mappings";
}

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 => ?, " +
sourceFilesParam + " => ?, " +
testFilesParam + " => ?, " +
"a_include_objects => ?, " +
"a_exclude_objects => ?, " +
"a_fail_on_errors => " + failOnErrors + "); " +
"a_paths => ?, " +
"a_reporters => ?, " +
"a_color_console => " + colorConsoleStr + ", " +
"a_coverage_schemes => ?, " +
"a_source_file_mappings => ?, " +
"a_test_file_mappings => ?, " +
"a_include_objects => ?, " +
"a_exclude_objects => ?, " +
"a_fail_on_errors => " + failOnErrors + "); " +
"END;");

int paramIdx = 0;
Expand All @@ -150,40 +132,22 @@ public void run(Connection conn) throws SomeTestsFailedException, SQLException {
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.coverageSchemes.toArray()));
}

if (this.sourceMappingOptions != null || this.testMappingOptions != null) {
if (this.sourceMappingOptions != null) {
List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(
conn, this.sourceFiles, this.sourceMappingOptions);

callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray()));
} else {
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
}

if (this.testMappingOptions != null) {
List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(
conn, this.testFiles, this.testMappingOptions);

callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray()));
} else {
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
}
if (this.sourceMappingOptions == null) {
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
} else {
List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(conn, this.sourceMappingOptions);

callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray()));
}

if (this.testMappingOptions == null) {
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
} else {
if (this.sourceFiles.isEmpty()) {
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
} else {
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.sourceFiles.toArray()));
}

if (this.testFiles.isEmpty()) {
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
} else {
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.testFiles.toArray()));
}
List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(conn, this.testMappingOptions);

callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray()));
}

if (this.includeObjects.isEmpty()) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/io/github/utplsql/api/FileMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public void testFileMapper() throws SQLException {
"sources/app/procedures/award_bonus.sql",
"sources/app/functions/betwnstr.sql");

FileMapperOptions mapperOptions = new FileMapperOptions();
FileMapperOptions mapperOptions = new FileMapperOptions(filePaths);
mapperOptions.setObjectOwner("APP");
mapperOptions.setTypeMappings(typeMappings);
mapperOptions.setRegexPattern("\\w+[\\\\\\/](\\w+)[\\\\\\/](\\w+)[\\\\\\/](\\w+)[.](\\w{3})");
mapperOptions.setOwnerSubExpression(1);
mapperOptions.setTypeSubExpression(2);
mapperOptions.setNameSubExpression(3);

List<FileMapping> fileMappings = FileMapper.buildFileMappingList(db.newConnection(), filePaths, mapperOptions);
List<FileMapping> fileMappings = FileMapper.buildFileMappingList(db.newConnection(), mapperOptions);

if (fileMappings.size() != 2)
Assert.fail("Wrong mapping list size.");
Expand Down