Skip to content

Commit 5a628a8

Browse files
committed
Changes on FileMapper API
1 parent 507f675 commit 5a628a8

File tree

4 files changed

+88
-89
lines changed

4 files changed

+88
-89
lines changed

src/main/java/io/github/utplsql/api/FileMapper.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
import oracle.jdbc.OracleConnection;
55
import oracle.jdbc.OracleTypes;
66

7-
import java.sql.Array;
8-
import java.sql.CallableStatement;
9-
import java.sql.Connection;
10-
import java.sql.SQLException;
7+
import java.sql.*;
118
import java.util.ArrayList;
129
import java.util.List;
1310
import java.util.Map;
@@ -20,7 +17,7 @@ private FileMapper() {}
2017
* Call the database api to build the custom file mappings.
2118
*/
2219
public static Array buildFileMappingArray(
23-
Connection conn, List<String> filePaths, FileMapperOptions mapperOptions) throws SQLException {
20+
Connection conn, FileMapperOptions mapperOptions) throws SQLException {
2421
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
2522

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

46-
callableStatement.setString(++paramIdx, mapperOptions.getObjectOwner());
47-
callableStatement.setArray(
48-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, filePaths.toArray()));
43+
if (mapperOptions.getRegexPattern() == null) {
44+
callableStatement.setNull(++paramIdx, Types.VARCHAR);
45+
} else {
46+
callableStatement.setString(++paramIdx, mapperOptions.getObjectOwner());
47+
}
48+
4949
callableStatement.setArray(
50-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_KEY_VALUE_PAIRS, mapperOptions.getTypeMappings().toArray()));
51-
callableStatement.setString(++paramIdx, mapperOptions.getRegexPattern());
52-
callableStatement.setInt(++paramIdx, mapperOptions.getOwnerSubExpression());
53-
callableStatement.setInt(++paramIdx, mapperOptions.getNameSubExpression());
54-
callableStatement.setInt(++paramIdx, mapperOptions.getTypeSubExpression());
50+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, mapperOptions.getFilePaths().toArray()));
51+
52+
if (mapperOptions.getTypeMappings() == null) {
53+
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_KEY_VALUE_PAIR);
54+
} else {
55+
callableStatement.setArray(
56+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_KEY_VALUE_PAIRS, mapperOptions.getTypeMappings().toArray()));
57+
}
58+
59+
if (mapperOptions.getRegexPattern() == null) {
60+
callableStatement.setNull(++paramIdx, Types.VARCHAR);
61+
} else {
62+
callableStatement.setString(++paramIdx, mapperOptions.getRegexPattern());
63+
}
64+
65+
if (mapperOptions.getOwnerSubExpression() == null) {
66+
callableStatement.setNull(++paramIdx, Types.INTEGER);
67+
} else {
68+
callableStatement.setInt(++paramIdx, mapperOptions.getOwnerSubExpression());
69+
}
70+
71+
if (mapperOptions.getNameSubExpression() == null) {
72+
callableStatement.setNull(++paramIdx, Types.INTEGER);
73+
} else {
74+
callableStatement.setInt(++paramIdx, mapperOptions.getNameSubExpression());
75+
}
76+
77+
if (mapperOptions.getTypeSubExpression() == null) {
78+
callableStatement.setNull(++paramIdx, Types.INTEGER);
79+
} else {
80+
callableStatement.setInt(++paramIdx, mapperOptions.getTypeSubExpression());
81+
}
5582

5683
callableStatement.execute();
5784
return callableStatement.getArray(1);
5885
}
5986

6087
public static List<FileMapping> buildFileMappingList(
61-
Connection conn, List<String> filePaths, FileMapperOptions mapperOptions) throws SQLException {
62-
java.sql.Array fileMappings = buildFileMappingArray(conn, filePaths, mapperOptions);
88+
Connection conn, FileMapperOptions mapperOptions) throws SQLException {
89+
java.sql.Array fileMappings = buildFileMappingArray(conn, mapperOptions);
6390

6491
List<FileMapping> mappingList = new ArrayList<>();
6592
for (Object obj : (Object[]) fileMappings.getArray()) {

src/main/java/io/github/utplsql/api/FileMapperOptions.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
package io.github.utplsql.api;
22

3-
import java.util.ArrayList;
43
import java.util.List;
54

65
public class FileMapperOptions {
76

7+
private List<String> filePaths;
88
private String objectOwner;
99
private List<KeyValuePair> typeMappings;
1010
private String regexPattern;
11-
private int ownerSubExpression;
12-
private int typeSubExpression;
13-
private int nameSubExpression;
11+
private Integer ownerSubExpression;
12+
private Integer typeSubExpression;
13+
private Integer nameSubExpression;
1414

15-
public FileMapperOptions() {
16-
this.typeMappings = new ArrayList<>();
15+
public FileMapperOptions(List<String> filePaths) {
16+
this.setFilePaths(filePaths);
17+
}
18+
19+
public List<String> getFilePaths() {
20+
return filePaths;
21+
}
22+
23+
public void setFilePaths(List<String> filePaths) {
24+
this.filePaths = filePaths;
1725
}
1826

1927
public String getObjectOwner() {
@@ -40,27 +48,27 @@ public void setRegexPattern(String regexPattern) {
4048
this.regexPattern = regexPattern;
4149
}
4250

43-
public int getOwnerSubExpression() {
51+
public Integer getOwnerSubExpression() {
4452
return ownerSubExpression;
4553
}
4654

47-
public void setOwnerSubExpression(int ownerSubExpression) {
55+
public void setOwnerSubExpression(Integer ownerSubExpression) {
4856
this.ownerSubExpression = ownerSubExpression;
4957
}
5058

51-
public int getTypeSubExpression() {
59+
public Integer getTypeSubExpression() {
5260
return typeSubExpression;
5361
}
5462

55-
public void setTypeSubExpression(int typeSubExpression) {
63+
public void setTypeSubExpression(Integer typeSubExpression) {
5664
this.typeSubExpression = typeSubExpression;
5765
}
5866

59-
public int getNameSubExpression() {
67+
public Integer getNameSubExpression() {
6068
return nameSubExpression;
6169
}
6270

63-
public void setNameSubExpression(int nameSubExpression) {
71+
public void setNameSubExpression(Integer nameSubExpression) {
6472
this.nameSubExpression = nameSubExpression;
6573
}
6674

src/main/java/io/github/utplsql/api/TestRunner.java

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ public TestRunner addCoverageScheme(String coverageScheme) {
5959
return this;
6060
}
6161

62-
public TestRunner withSourceFiles(List<String> sourceFiles) {
63-
if (sourceFiles != null) this.sourceFiles.addAll(sourceFiles);
64-
return this;
65-
}
66-
67-
public TestRunner withTestFiles(List<String> testFiles) {
68-
if (testFiles != null) this.testFiles.addAll(testFiles);
69-
return this;
70-
}
71-
7262
public TestRunner includeObject(String obj) {
7363
this.includeObjects.add(obj);
7464
return this;
@@ -110,29 +100,21 @@ public void run(Connection conn) throws SomeTestsFailedException, SQLException {
110100
String colorConsoleStr = Boolean.toString(this.colorConsole);
111101
String failOnErrors = Boolean.toString(this.failOnErrors);
112102

113-
String sourceFilesParam = "a_source_files";
114-
String testFilesParam = "a_test_files";
115-
116-
if (this.sourceMappingOptions != null || this.testMappingOptions != null) {
117-
sourceFilesParam = "a_source_file_mappings";
118-
testFilesParam = "a_test_file_mappings";
119-
}
120-
121103
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
122104
CallableStatement callableStatement = null;
123105
try {
124106
callableStatement = conn.prepareCall(
125107
"BEGIN " +
126108
"ut_runner.run(" +
127-
"a_paths => ?, " +
128-
"a_reporters => ?, " +
129-
"a_color_console => " + colorConsoleStr + ", " +
130-
"a_coverage_schemes => ?, " +
131-
sourceFilesParam + " => ?, " +
132-
testFilesParam + " => ?, " +
133-
"a_include_objects => ?, " +
134-
"a_exclude_objects => ?, " +
135-
"a_fail_on_errors => " + failOnErrors + "); " +
109+
"a_paths => ?, " +
110+
"a_reporters => ?, " +
111+
"a_color_console => " + colorConsoleStr + ", " +
112+
"a_coverage_schemes => ?, " +
113+
"a_source_file_mappings => ?, " +
114+
"a_test_file_mappings => ?, " +
115+
"a_include_objects => ?, " +
116+
"a_exclude_objects => ?, " +
117+
"a_fail_on_errors => " + failOnErrors + "); " +
136118
"END;");
137119

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

153-
if (this.sourceMappingOptions != null || this.testMappingOptions != null) {
154-
if (this.sourceMappingOptions != null) {
155-
List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(
156-
conn, this.sourceFiles, this.sourceMappingOptions);
157-
158-
callableStatement.setArray(
159-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray()));
160-
} else {
161-
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
162-
}
163-
164-
if (this.testMappingOptions != null) {
165-
List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(
166-
conn, this.testFiles, this.testMappingOptions);
167-
168-
callableStatement.setArray(
169-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray()));
170-
} else {
171-
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
172-
}
135+
if (this.sourceMappingOptions == null) {
136+
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
137+
} else {
138+
List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(conn, this.sourceMappingOptions);
139+
140+
callableStatement.setArray(
141+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray()));
142+
}
143+
144+
if (this.testMappingOptions == null) {
145+
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
173146
} else {
174-
if (this.sourceFiles.isEmpty()) {
175-
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
176-
} else {
177-
callableStatement.setArray(
178-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.sourceFiles.toArray()));
179-
}
180-
181-
if (this.testFiles.isEmpty()) {
182-
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
183-
} else {
184-
callableStatement.setArray(
185-
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.testFiles.toArray()));
186-
}
147+
List<FileMapping> sourceMappings = FileMapper.buildFileMappingList(conn, this.testMappingOptions);
148+
149+
callableStatement.setArray(
150+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_FILE_MAPPINGS, sourceMappings.toArray()));
187151
}
188152

189153
if (this.includeObjects.isEmpty()) {

src/test/java/io/github/utplsql/api/FileMapperTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public void testFileMapper() throws SQLException {
2424
"sources/app/procedures/award_bonus.sql",
2525
"sources/app/functions/betwnstr.sql");
2626

27-
FileMapperOptions mapperOptions = new FileMapperOptions();
27+
FileMapperOptions mapperOptions = new FileMapperOptions(filePaths);
2828
mapperOptions.setObjectOwner("APP");
2929
mapperOptions.setTypeMappings(typeMappings);
3030
mapperOptions.setRegexPattern("\\w+[\\\\\\/](\\w+)[\\\\\\/](\\w+)[\\\\\\/](\\w+)[.](\\w{3})");
3131
mapperOptions.setOwnerSubExpression(1);
3232
mapperOptions.setTypeSubExpression(2);
3333
mapperOptions.setNameSubExpression(3);
3434

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

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

0 commit comments

Comments
 (0)