Skip to content

Commit d8da220

Browse files
committed
Add custom file mapping support
1 parent b611f2d commit d8da220

File tree

7 files changed

+352
-11
lines changed

7 files changed

+352
-11
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
*/
66
public final class CustomTypes {
77

8-
// Object names must be upper case.
8+
/* Object names must be upper case. */
9+
10+
public static final String UT_VARCHAR2_LIST = "UT_VARCHAR2_LIST";
11+
912
public static final String UT_REPORTERS = "UT_REPORTERS";
1013
public static final String UT_DOCUMENTATION_REPORTER = "UT_DOCUMENTATION_REPORTER";
1114
public static final String UT_COVERAGE_HTML_REPORTER = "UT_COVERAGE_HTML_REPORTER";
@@ -14,7 +17,12 @@ public final class CustomTypes {
1417
public static final String UT_COVERALLS_REPORTER = "UT_COVERALLS_REPORTER";
1518
public static final String UT_COVERAGE_SONAR_REPORTER = "UT_COVERAGE_SONAR_REPORTER";
1619
public static final String UT_SONAR_TEST_REPORTER = "UT_SONAR_TEST_REPORTER";
17-
public static final String UT_VARCHAR2_LIST = "UT_VARCHAR2_LIST";
20+
21+
public static final String UT_FILE_MAPPING = "UT_FILE_MAPPING";
22+
public static final String UT_FILE_MAPPINGS = "UT_FILE_MAPPINGS";
23+
24+
public static final String UT_KEY_VALUE_PAIR = "UT_KEY_VALUE_PAIR";
25+
public static final String UT_KEY_VALUE_PAIRS = "UT_KEY_VALUE_PAIRS";
1826

1927
private CustomTypes() {}
2028

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.github.utplsql.api;
2+
3+
4+
import oracle.jdbc.OracleConnection;
5+
import oracle.jdbc.OracleTypes;
6+
7+
import java.sql.Array;
8+
import java.sql.CallableStatement;
9+
import java.sql.Connection;
10+
import java.sql.SQLException;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
public final class FileMapper {
16+
17+
private FileMapper() {}
18+
19+
/**
20+
* Call the database api to build the custom file mappings.
21+
*/
22+
public static Array buildFileMappingArray(Connection conn, FileMapperOptions mapperOptions) throws SQLException {
23+
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
24+
25+
Map typeMap = conn.getTypeMap();
26+
typeMap.put(CustomTypes.UT_FILE_MAPPING, FileMapping.class);
27+
typeMap.put(CustomTypes.UT_KEY_VALUE_PAIR, KeyValuePair.class);
28+
conn.setTypeMap(typeMap);
29+
30+
CallableStatement callableStatement = conn.prepareCall(
31+
"BEGIN " +
32+
"? := ut_file_mapper.build_file_mappings(" +
33+
"a_object_owner => ?, " +
34+
"a_file_paths => ?, " +
35+
"a_file_to_object_type_mapping => ?, " +
36+
"a_regex_pattern => ?, " +
37+
"a_object_owner_subexpression => ?, " +
38+
"a_object_name_subexpression => ?, " +
39+
"a_object_type_subexpression => ?); " +
40+
"END;");
41+
42+
int paramIdx = 0;
43+
callableStatement.registerOutParameter(++paramIdx, OracleTypes.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
44+
45+
callableStatement.setString(++paramIdx, mapperOptions.getOwner());
46+
callableStatement.setArray(
47+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, mapperOptions.getFilePaths().toArray()));
48+
callableStatement.setArray(
49+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_KEY_VALUE_PAIRS, mapperOptions.getTypeMappings().toArray()));
50+
callableStatement.setString(++paramIdx, mapperOptions.getRegexPattern());
51+
callableStatement.setInt(++paramIdx, mapperOptions.getOwnerSubExpression());
52+
callableStatement.setInt(++paramIdx, mapperOptions.getNameSubExpression());
53+
callableStatement.setInt(++paramIdx, mapperOptions.getTypeSubExpression());
54+
55+
callableStatement.execute();
56+
return callableStatement.getArray(1);
57+
}
58+
59+
public static List<FileMapping> buildFileMappingList(Connection conn, FileMapperOptions mapperOptions) throws SQLException {
60+
java.sql.Array fileMappings = buildFileMappingArray(conn, mapperOptions);
61+
62+
List<FileMapping> mappingList = new ArrayList<>();
63+
for (Object obj : (Object[]) fileMappings.getArray()) {
64+
mappingList.add((FileMapping) obj);
65+
}
66+
67+
return mappingList;
68+
}
69+
70+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package io.github.utplsql.api;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class FileMapperOptions {
7+
8+
private String owner;
9+
private List<String> filePaths;
10+
private List<KeyValuePair> typeMappings;
11+
private String regexPattern;
12+
private int ownerSubExpression;
13+
private int typeSubExpression;
14+
private int nameSubExpression;
15+
16+
public FileMapperOptions() {
17+
this.filePaths = new ArrayList<>();
18+
this.typeMappings = new ArrayList<>();
19+
}
20+
21+
public String getOwner() {
22+
return owner;
23+
}
24+
25+
public void setOwner(String owner) {
26+
this.owner = owner;
27+
}
28+
29+
public List<String> getFilePaths() {
30+
return filePaths;
31+
}
32+
33+
public void setFilePaths(List<String> filePaths) {
34+
this.filePaths = filePaths;
35+
}
36+
37+
public List<KeyValuePair> getTypeMappings() {
38+
return typeMappings;
39+
}
40+
41+
public void setTypeMappings(List<KeyValuePair> typeMappings) {
42+
this.typeMappings = typeMappings;
43+
}
44+
45+
public String getRegexPattern() {
46+
return regexPattern;
47+
}
48+
49+
public void setRegexPattern(String regexPattern) {
50+
this.regexPattern = regexPattern;
51+
}
52+
53+
public int getOwnerSubExpression() {
54+
return ownerSubExpression;
55+
}
56+
57+
public void setOwnerSubExpression(int ownerSubExpression) {
58+
this.ownerSubExpression = ownerSubExpression;
59+
}
60+
61+
public int getTypeSubExpression() {
62+
return typeSubExpression;
63+
}
64+
65+
public void setTypeSubExpression(int typeSubExpression) {
66+
this.typeSubExpression = typeSubExpression;
67+
}
68+
69+
public int getNameSubExpression() {
70+
return nameSubExpression;
71+
}
72+
73+
public void setNameSubExpression(int nameSubExpression) {
74+
this.nameSubExpression = nameSubExpression;
75+
}
76+
77+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.github.utplsql.api;
2+
3+
import java.sql.SQLData;
4+
import java.sql.SQLException;
5+
import java.sql.SQLInput;
6+
import java.sql.SQLOutput;
7+
8+
/**
9+
* Created by Vinicius on 17/07/2017.
10+
*/
11+
public class FileMapping implements SQLData {
12+
13+
private String fileName;
14+
private String objectOwner;
15+
private String objectName;
16+
private String objectType;
17+
18+
public FileMapping() {}
19+
20+
public String getFileName() {
21+
return fileName;
22+
}
23+
24+
public void setFileName(String fileName) {
25+
this.fileName = fileName;
26+
}
27+
28+
public String getObjectOwner() {
29+
return objectOwner;
30+
}
31+
32+
public void setObjectOwner(String objectOwner) {
33+
this.objectOwner = objectOwner;
34+
}
35+
36+
public String getObjectName() {
37+
return objectName;
38+
}
39+
40+
public void setObjectName(String objectName) {
41+
this.objectName = objectName;
42+
}
43+
44+
public String getObjectType() {
45+
return objectType;
46+
}
47+
48+
public void setObjectType(String objectType) {
49+
this.objectType = objectType;
50+
}
51+
52+
@Override
53+
public String getSQLTypeName() throws SQLException {
54+
return CustomTypes.UT_FILE_MAPPING;
55+
}
56+
57+
@Override
58+
public void readSQL(SQLInput stream, String typeName) throws SQLException {
59+
setFileName(stream.readString());
60+
setObjectOwner(stream.readString());
61+
setObjectName(stream.readString());
62+
setObjectType(stream.readString());
63+
}
64+
65+
@Override
66+
public void writeSQL(SQLOutput stream) throws SQLException {
67+
stream.writeString(getFileName());
68+
stream.writeString(getObjectOwner());
69+
stream.writeString(getObjectName());
70+
stream.writeString(getObjectType());
71+
}
72+
73+
@Override
74+
public String toString() {
75+
return String.format("%s/%s.%s", getObjectType(), getObjectOwner(), getObjectName());
76+
}
77+
78+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.github.utplsql.api;
2+
3+
import java.sql.SQLData;
4+
import java.sql.SQLException;
5+
import java.sql.SQLInput;
6+
import java.sql.SQLOutput;
7+
8+
/**
9+
* Created by Vinicius on 22/07/2017.
10+
*/
11+
public class KeyValuePair implements SQLData {
12+
13+
private String key;
14+
private String value;
15+
16+
public KeyValuePair(String key, String value) {
17+
this.key = key;
18+
this.value = value;
19+
}
20+
21+
public String getKey() {
22+
return key;
23+
}
24+
25+
public void setKey(String key) {
26+
this.key = key;
27+
}
28+
29+
public String getValue() {
30+
return value;
31+
}
32+
33+
public void setValue(String value) {
34+
this.value = value;
35+
}
36+
37+
@Override
38+
public String getSQLTypeName() throws SQLException {
39+
return CustomTypes.UT_KEY_VALUE_PAIR;
40+
}
41+
42+
@Override
43+
public void readSQL(SQLInput stream, String typeName) throws SQLException {
44+
setKey(stream.readString());
45+
setValue(stream.readString());
46+
}
47+
48+
@Override
49+
public void writeSQL(SQLOutput stream) throws SQLException {
50+
stream.writeString(getKey());
51+
stream.writeString(getValue());
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return String.format("%s => %s", getKey(), getValue());
57+
}
58+
59+
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void run(Connection conn) throws SomeTestsFailedException, SQLException {
100100
try {
101101
callableStatement = conn.prepareCall(
102102
"BEGIN " +
103-
"ut_runner.run(" +
103+
"ut_runner.run(" +
104104
"a_paths => ?, " +
105105
"a_reporters => ?, " +
106106
"a_color_console => " + colorConsoleStr + ", " +
@@ -110,49 +110,49 @@ public void run(Connection conn) throws SomeTestsFailedException, SQLException {
110110
"a_include_objects => ?, " +
111111
"a_exclude_objects => ?, " +
112112
"a_fail_on_errors => " + failOnErrors + "); " +
113-
"END;");
113+
"END;");
114114

115115
int paramIdx = 0;
116116

117117
callableStatement.setArray(
118-
++paramIdx, oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.pathList.toArray()));
118+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.pathList.toArray()));
119119

120120
callableStatement.setArray(
121-
++paramIdx, oraConn.createARRAY(CustomTypes.UT_REPORTERS, this.reporterList.toArray()));
121+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_REPORTERS, this.reporterList.toArray()));
122122

123123
if (this.coverageSchemes.isEmpty()) {
124124
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
125125
} else {
126126
callableStatement.setArray(
127-
++paramIdx, oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.coverageSchemes.toArray()));
127+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.coverageSchemes.toArray()));
128128
}
129129

130130
if (this.sourceFiles.isEmpty()) {
131131
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
132132
} else {
133133
callableStatement.setArray(
134-
++paramIdx, oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.sourceFiles.toArray()));
134+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.sourceFiles.toArray()));
135135
}
136136

137137
if (this.testFiles.isEmpty()) {
138138
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
139139
} else {
140140
callableStatement.setArray(
141-
++paramIdx, oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.testFiles.toArray()));
141+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.testFiles.toArray()));
142142
}
143143

144144
if (this.includeObjects.isEmpty()) {
145145
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
146146
} else {
147147
callableStatement.setArray(
148-
++paramIdx, oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.includeObjects.toArray()));
148+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.includeObjects.toArray()));
149149
}
150150

151151
if (this.excludeObjects.isEmpty()) {
152152
callableStatement.setNull(++paramIdx, Types.ARRAY, CustomTypes.UT_VARCHAR2_LIST);
153153
} else {
154154
callableStatement.setArray(
155-
++paramIdx, oraConn.createARRAY(CustomTypes.UT_VARCHAR2_LIST, this.excludeObjects.toArray()));
155+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, this.excludeObjects.toArray()));
156156
}
157157

158158
callableStatement.execute();

0 commit comments

Comments
 (0)