Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit304c8bc

Browse files
Merge pull request#105 from utPLSQL/feature/issue-102-code-coverage
#102 Run tests with code coverage
2 parents584cc33 +64cbad0 commit304c8bc

File tree

11 files changed

+394
-69
lines changed

11 files changed

+394
-69
lines changed

‎sqldev/src/main/java/org/utplsql/sqldev/coverage/CodeCoverageReporter.java

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,28 @@
2222
importjava.sql.Connection;
2323
importjava.util.ArrayList;
2424
importjava.util.Arrays;
25+
importjava.util.HashMap;
2526
importjava.util.List;
27+
importjava.util.Map;
2628
importjava.util.logging.Logger;
29+
importjava.util.stream.Collectors;
2730

31+
importorg.utplsql.sqldev.dal.RealtimeReporterDao;
2832
importorg.utplsql.sqldev.dal.UtplsqlDao;
2933
importorg.utplsql.sqldev.exception.GenericDatabaseAccessException;
3034
importorg.utplsql.sqldev.exception.GenericRuntimeException;
3135
importorg.utplsql.sqldev.model.DatabaseTools;
3236
importorg.utplsql.sqldev.model.FileTools;
37+
importorg.utplsql.sqldev.model.preference.PreferenceModel;
38+
importorg.utplsql.sqldev.runner.UtplsqlRunner;
3339
importorg.utplsql.sqldev.ui.coverage.CodeCoverageReporterDialog;
3440

41+
importoracle.ide.config.Preferences;
42+
3543
publicclassCodeCoverageReporter {
3644
privatestaticfinalLoggerlogger =Logger.getLogger(CodeCoverageReporter.class.getName());
3745

46+
privateStringconnectionName;
3847
privateConnectionconn;
3948
privateList<String>pathList;
4049
privateList<String>includeObjectList;
@@ -47,14 +56,17 @@ public CodeCoverageReporter(final List<String> pathList, final List<String> incl
4756
finalStringconnectionName) {
4857
this.pathList =pathList;
4958
this.includeObjectList =includeObjectList;
59+
setDefaultSchema();
5060
setConnection(connectionName);
5161
}
5262

63+
// constructor for testing purposes only
5364
publicCodeCoverageReporter(finalList<String>pathList,finalList<String>includeObjectList,
5465
finalConnectionconn) {
5566
this.pathList =pathList;
5667
this.includeObjectList =includeObjectList;
5768
this.conn =conn;
69+
setDefaultSchema();
5870
}
5971

6072
privatevoidsetConnection(finalStringconnectionName) {
@@ -64,7 +76,32 @@ private void setConnection(final String connectionName) {
6476
thrownewNullPointerException();
6577
}else {
6678
// must be closed manually
67-
conn =DatabaseTools.cloneConnection(connectionName);
79+
this.connectionName =connectionName;
80+
this.conn =DatabaseTools.getConnection(connectionName);
81+
}
82+
}
83+
84+
privatevoidsetDefaultSchema() {
85+
if (includeObjectList !=null && !includeObjectList.isEmpty()) {
86+
// use the owner with the most hits in includeObjectList
87+
HashMap<String,Integer>owners =newHashMap<>();
88+
for (Stringentry :includeObjectList) {
89+
String[]obj =entry.toUpperCase().split("\\.");
90+
if (obj.length ==2) {
91+
// only if objectOwner and objectName are available
92+
Integercount =owners.get(obj[0]);
93+
if (count ==null) {
94+
count =1;
95+
}else {
96+
count++;
97+
}
98+
owners.put(obj[0],count);
99+
}
100+
}
101+
List<String>sortedOwners =owners.entrySet().stream()
102+
.sorted(Map.Entry.<String,Integer>comparingByValue().reversed()).map(Map.Entry::getKey)
103+
.collect(Collectors.toList());
104+
schemas =String.join(", ",sortedOwners);
68105
}
69106
}
70107

@@ -83,12 +120,57 @@ private ArrayList<String> toStringList(final String s) {
83120
privatevoidrun() {
84121
logger.fine(() ->"Running code coverage reporter for " +pathList +"...");
85122
try {
86-
finalUtplsqlDaodal =newUtplsqlDao(conn);
87-
finalStringcontent =dal.htmlCodeCoverage(pathList,toStringList(schemas),
123+
finalRealtimeReporterDaodao =newRealtimeReporterDao(conn);
124+
PreferenceModelpreferences;
125+
try {
126+
preferences =PreferenceModel.getInstance(Preferences.getPreferences());
127+
}catch (NoClassDefFoundErrorerror) {
128+
// not running in SQL Developer (in tests)
129+
preferences =PreferenceModel.getInstance(null);
130+
}
131+
if (preferences.isUseRealtimeReporter() &&dao.isSupported() &&connectionName !=null) {
132+
runCodeCoverageWithRealtimeReporter();
133+
}else {
134+
runCodeCoverageStandalone();
135+
}
136+
}finally {
137+
if (frame !=null) {
138+
frame.exit();
139+
}
140+
}
141+
}
142+
143+
privatevoidrunCodeCoverageWithRealtimeReporter() {
144+
finalUtplsqlRunnerrunner =newUtplsqlRunner(pathList,toStringList(schemas),toStringList(includeObjects),
145+
toStringList(excludeObjects),connectionName);
146+
runner.runTestAsync();
147+
}
148+
149+
privatevoidrunCodeCoverageStandalone() {
150+
ConnectioncoverageConn =null;
151+
try {
152+
coverageConn =conn !=null ?conn :DatabaseTools.cloneConnection(connectionName);
153+
finalUtplsqlDaodao =newUtplsqlDao(coverageConn);
154+
finalStringhtml =dao.htmlCodeCoverage(pathList,toStringList(schemas),
88155
toStringList(includeObjects),toStringList(excludeObjects));
156+
openInBrowser(html);
157+
}finally {
158+
try {
159+
if (coverageConn !=null &&conn ==null) {
160+
// close only if connection has been cloned
161+
DatabaseTools.closeConnection(coverageConn);
162+
}
163+
}catch (GenericDatabaseAccessExceptione) {
164+
// ignore
165+
}
166+
}
167+
}
168+
169+
publicstaticvoidopenInBrowser(Stringhtml) {
170+
try {
89171
finalFilefile =File.createTempFile("utplsql_",".html");
90172
logger.fine(() ->"Writing result to " +file +"...");
91-
FileTools.writeFile(file.toPath(),Arrays.asList(content.split(System.lineSeparator())),StandardCharsets.UTF_8);
173+
FileTools.writeFile(file.toPath(),Arrays.asList(html.split(System.lineSeparator())),StandardCharsets.UTF_8);
92174
finalURLurl =file.toURI().toURL();
93175
logger.fine(() ->"Opening " +url.toExternalForm() +" in browser...");
94176
finalDesktopdesktop =Desktop.isDesktopSupported() ?Desktop.getDesktop() :null;
@@ -97,21 +179,12 @@ private void run() {
97179
logger.fine(() ->url.toExternalForm() +" opened in browser.");
98180
}else {
99181
logger.severe(
100-
() ->"Could not launch " +file +"in browser. No default browser defined on this system.");
182+
() ->"Could not launch " +file +"in browser. No default browser defined on this system.");
101183
}
102184
}catch (Exceptione) {
103-
finalStringmsg ="Error whilerunning code coveragefor " +pathList +".";
185+
finalStringmsg ="Error whileopening code coverageHTML report in browser.";
104186
logger.severe(() ->msg);
105187
thrownewGenericRuntimeException(msg,e);
106-
}finally {
107-
try {
108-
DatabaseTools.closeConnection(conn);
109-
}catch (GenericDatabaseAccessExceptione) {
110-
// ignore
111-
}
112-
if (frame !=null) {
113-
frame.exit();
114-
}
115188
}
116189
}
117190

@@ -143,6 +216,10 @@ public void setSchemas(final String schemas) {
143216
this.schemas =schemas;
144217
}
145218

219+
publicStringgetSchemas() {
220+
returnschemas;
221+
}
222+
146223
publicvoidsetIncludeObjects(finalStringincludeObjects) {
147224
this.includeObjects =includeObjects;
148225
}

‎sqldev/src/main/java/org/utplsql/sqldev/dal/RealtimeReporterDao.java

Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class RealtimeReporterDao {
6464
publicRealtimeReporterDao(finalConnectionconn) {
6565
this.conn =conn;
6666
jdbcTemplate =newJdbcTemplate(newSingleConnectionDataSource(conn,true));
67-
jdbcTemplate.setFetchSize(1);
67+
jdbcTemplate.setFetchSize(UtplsqlDao.FETCH_ROWS);
6868
}
6969

7070
publicbooleanisSupported() {
@@ -93,6 +93,47 @@ public void produceReport(final String reporterId, final List<String> pathList)
9393
jdbcTemplate.update(plsql,binds);
9494
}
9595

96+
publicvoidproduceReportWithCoverage(finalStringrealtimeReporterId,finalStringcoverageReporterId,
97+
finalList<String>pathList,finalList<String>schemaList,finalList<String>includeObjectList,
98+
finalList<String>excludeObjectList) {
99+
StringBuildersb =newStringBuilder();
100+
sb.append("DECLARE\n");
101+
sb.append(" l_rt_rep ut_realtime_reporter := ut_realtime_reporter();\n");
102+
sb.append(" l_cov_rep ut_coverage_html_reporter := ut_coverage_html_reporter();\n");
103+
sb.append("BEGIN\n");
104+
sb.append(" l_rt_rep.set_reporter_id(?);\n");
105+
sb.append(" l_rt_rep.output_buffer.init();\n");
106+
sb.append(" l_cov_rep.set_reporter_id(?);\n");
107+
sb.append(" l_cov_rep.output_buffer.init();\n");
108+
sb.append(" sys.dbms_output.enable(NULL);\n");
109+
sb.append(" ut_runner.run(\n");
110+
sb.append(" a_paths => ut_varchar2_list(\n");
111+
sb.append(StringTools.getCSV(pathList,31));
112+
sb.append(" ),\n");
113+
if (schemaList !=null && !schemaList.isEmpty()) {
114+
sb.append(" a_coverage_schemes => ut_varchar2_list(\n");
115+
sb.append(StringTools.getCSV(schemaList,31));
116+
sb.append(" ),\n");
117+
}
118+
if (includeObjectList !=null && !includeObjectList.isEmpty()) {
119+
sb.append(" a_include_objects => ut_varchar2_list(\n");
120+
sb.append(StringTools.getCSV(includeObjectList,31));
121+
sb.append(" ),\n");
122+
}
123+
if (excludeObjectList !=null && !excludeObjectList.isEmpty()) {
124+
sb.append(" a_exclude_objects => ut_varchar2_list(\n");
125+
sb.append(StringTools.getCSV(excludeObjectList,31));
126+
sb.append(" ),\n");
127+
}
128+
sb.append(" a_reporters => ut_reporters(l_rt_rep, l_cov_rep)\n");
129+
sb.append(" );\n");
130+
sb.append(" sys.dbms_output.disable;\n");
131+
sb.append("END;");
132+
finalStringplsql =sb.toString();
133+
finalObject[]binds = {realtimeReporterId,coverageReporterId };
134+
jdbcTemplate.update(plsql,binds);
135+
}
136+
96137
publicvoidconsumeReport(finalStringreporterId,finalRealtimeReporterEventConsumerconsumer) {
97138
StringBuildersb =newStringBuilder();
98139
sb.append("DECLARE\n");
@@ -102,28 +143,63 @@ public void consumeReport(final String reporterId, final RealtimeReporterEventCo
102143
sb.append(" ? := l_reporter.get_lines_cursor();\n");
103144
sb.append("END;");
104145
finalStringplsql =sb.toString();
105-
jdbcTemplate.execute(plsql,newCallableStatementCallback<Void>() {
146+
jdbcTemplate.setFetchSize(1);
147+
try {
148+
jdbcTemplate.execute(plsql,newCallableStatementCallback<Void>() {
149+
@Override
150+
publicVoiddoInCallableStatement(finalCallableStatementcs)throwsSQLException {
151+
cs.setString(1,reporterId);
152+
cs.registerOutParameter(2,OracleTypes.CURSOR);
153+
cs.execute();
154+
finalResultSetrs = (ResultSet)cs.getObject(2);
155+
while (rs.next()) {
156+
finalStringitemType =rs.getString("item_type");
157+
finalClobtextClob =rs.getClob("text");
158+
finalStringtextString =textClob.getSubString(1, ((int)textClob.length()));
159+
finalRealtimeReporterEventevent =convert(itemType,textString);
160+
if (event !=null) {
161+
consumer.process(event);
162+
}
163+
}
164+
rs.close();
165+
returnnull;
166+
}
167+
});
168+
}finally {
169+
jdbcTemplate.setFetchSize(UtplsqlDao.FETCH_ROWS);
170+
}
171+
}
172+
173+
publicStringgetHtmlCoverage(finalStringreporterId) {
174+
StringBuildersb =newStringBuilder();
175+
sb.append("DECLARE\n");
176+
sb.append(" l_reporter ut_coverage_html_reporter := ut_coverage_html_reporter();\n");
177+
sb.append("BEGIN\n");
178+
sb.append(" l_reporter.set_reporter_id(?);\n");
179+
sb.append(" ? := l_reporter.get_lines_cursor();\n");
180+
sb.append("END;");
181+
finalStringplsql =sb.toString();
182+
returnjdbcTemplate.execute(plsql,newCallableStatementCallback<String>() {
106183
@Override
107-
publicVoiddoInCallableStatement(finalCallableStatementcs)throwsSQLException {
184+
publicStringdoInCallableStatement(finalCallableStatementcs)throwsSQLException {
108185
cs.setString(1,reporterId);
109186
cs.registerOutParameter(2,OracleTypes.CURSOR);
110187
cs.execute();
111-
finalResultSetrs = ((ResultSet)cs.getObject(2));
188+
finalStringBuildersb =newStringBuilder();
189+
finalResultSetrs = (ResultSet)cs.getObject(2);
112190
while (rs.next()) {
113-
finalStringitemType =rs.getString("item_type");
114-
finalClobtextClob =rs.getClob("text");
115-
finalStringtextString =textClob.getSubString(1, ((int)textClob.length()));
116-
finalRealtimeReporterEventevent =convert(itemType,textString);
117-
if (event !=null) {
118-
consumer.process(event);
191+
finalStringtext =rs.getString("text");
192+
if (text !=null) {
193+
sb.append(text);
194+
sb.append('\n');
119195
}
120196
}
121197
rs.close();
122-
returnnull;
198+
returnsb.toString();
123199
}
124200
});
125-
}
126-
201+
}
202+
127203
privateRealtimeReporterEventconvert(finalStringitemType,finalStringtext) {
128204
logger.fine(() ->"\n---- " +itemType +" ----\n" +text);
129205
try {

‎sqldev/src/main/java/org/utplsql/sqldev/dal/UtplsqlDao.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class UtplsqlDao {
4242
publicstaticfinalintFIRST_VERSION_WITH_ANNOTATION_API =3001003;
4343
publicstaticfinalintFIRST_VERSION_WITHOUT_INTERNAL_API =3001008;
4444
publicstaticfinalintFIRST_VERSION_WITH_HAS_SUITES_API =3001008;
45+
publicstaticfinalintFETCH_ROWS =100;
4546
privateJdbcTemplatejdbcTemplate;
4647
// cache fields
4748
privateBooleancachedDbaViewAccessible;
@@ -50,6 +51,7 @@ public class UtplsqlDao {
5051

5152
publicUtplsqlDao(finalConnectionconn) {
5253
jdbcTemplate =newJdbcTemplate(newSingleConnectionDataSource(conn,true));
54+
jdbcTemplate.setFetchSize(FETCH_ROWS);
5355
}
5456

5557
/**
@@ -918,17 +920,17 @@ public String htmlCodeCoverage(final List<String> pathList, final List<String> s
918920
sb.append(" a_paths => ut_varchar2_list(\n");
919921
sb.append(StringTools.getCSV(pathList,16));
920922
sb.append(" ),\n");
921-
if (!schemaList.isEmpty()) {
923+
if (schemaList !=null &&!schemaList.isEmpty()) {
922924
sb.append(" a_coverage_schemes => ut_varchar2_list(\n");
923925
sb.append(StringTools.getCSV(schemaList,16));
924926
sb.append(" ),\n");
925927
}
926-
if (!includeObjectList.isEmpty()) {
928+
if (includeObjectList !=null &&!includeObjectList.isEmpty()) {
927929
sb.append(" a_include_objects => ut_varchar2_list(\n");
928930
sb.append(StringTools.getCSV(includeObjectList,16));
929931
sb.append(" ),\n");
930932
}
931-
if (!excludeObjectList.isEmpty()) {
933+
if (excludeObjectList !=null &&excludeObjectList.isEmpty()) {
932934
sb.append(" a_exclude_objects => ut_varchar2_list(\n");
933935
sb.append(StringTools.getCSV(excludeObjectList,16));
934936
sb.append(" ),\n");

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp