diff --git a/src/main/java/io/github/utplsql/api/FileMapper.java b/src/main/java/io/github/utplsql/api/FileMapper.java index d5c1c16..661e9df 100644 --- a/src/main/java/io/github/utplsql/api/FileMapper.java +++ b/src/main/java/io/github/utplsql/api/FileMapper.java @@ -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; @@ -20,7 +17,7 @@ private FileMapper() {} * Call the database api to build the custom file mappings. */ public static Array buildFileMappingArray( - Connection conn, List filePaths, FileMapperOptions mapperOptions) throws SQLException { + Connection conn, FileMapperOptions mapperOptions) throws SQLException { OracleConnection oraConn = conn.unwrap(OracleConnection.class); Map typeMap = conn.getTypeMap(); @@ -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 buildFileMappingList( - Connection conn, List 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 mappingList = new ArrayList<>(); for (Object obj : (Object[]) fileMappings.getArray()) { diff --git a/src/main/java/io/github/utplsql/api/FileMapperOptions.java b/src/main/java/io/github/utplsql/api/FileMapperOptions.java index cee3ecf..723210d 100644 --- a/src/main/java/io/github/utplsql/api/FileMapperOptions.java +++ b/src/main/java/io/github/utplsql/api/FileMapperOptions.java @@ -1,19 +1,27 @@ package io.github.utplsql.api; -import java.util.ArrayList; import java.util.List; public class FileMapperOptions { + private List filePaths; private String objectOwner; private List 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 filePaths) { + this.setFilePaths(filePaths); + } + + public List getFilePaths() { + return filePaths; + } + + public void setFilePaths(List filePaths) { + this.filePaths = filePaths; } public String getObjectOwner() { @@ -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; } diff --git a/src/main/java/io/github/utplsql/api/TestRunner.java b/src/main/java/io/github/utplsql/api/TestRunner.java index 7183a77..d4d766b 100644 --- a/src/main/java/io/github/utplsql/api/TestRunner.java +++ b/src/main/java/io/github/utplsql/api/TestRunner.java @@ -59,16 +59,6 @@ public TestRunner addCoverageScheme(String coverageScheme) { return this; } - public TestRunner withSourceFiles(List sourceFiles) { - if (sourceFiles != null) this.sourceFiles.addAll(sourceFiles); - return this; - } - - public TestRunner withTestFiles(List testFiles) { - if (testFiles != null) this.testFiles.addAll(testFiles); - return this; - } - public TestRunner includeObject(String obj) { this.includeObjects.add(obj); return this; @@ -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; @@ -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 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 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 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 sourceMappings = FileMapper.buildFileMappingList(conn, this.testMappingOptions); + + callableStatement.setArray( + ++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray())); } if (this.includeObjects.isEmpty()) { diff --git a/src/test/java/io/github/utplsql/api/FileMapperTest.java b/src/test/java/io/github/utplsql/api/FileMapperTest.java index d96631e..c040fb8 100644 --- a/src/test/java/io/github/utplsql/api/FileMapperTest.java +++ b/src/test/java/io/github/utplsql/api/FileMapperTest.java @@ -24,7 +24,7 @@ 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})"); @@ -32,7 +32,7 @@ public void testFileMapper() throws SQLException { mapperOptions.setTypeSubExpression(2); mapperOptions.setNameSubExpression(3); - List fileMappings = FileMapper.buildFileMappingList(db.newConnection(), filePaths, mapperOptions); + List fileMappings = FileMapper.buildFileMappingList(db.newConnection(), mapperOptions); if (fileMappings.size() != 2) Assert.fail("Wrong mapping list size.");