22
22
import java .sql .Connection ;
23
23
import java .util .ArrayList ;
24
24
import java .util .Arrays ;
25
+ import java .util .HashMap ;
25
26
import java .util .List ;
27
+ import java .util .Map ;
28
+ import java .util .Map .Entry ;
29
+ import java .util .Optional ;
26
30
import java .util .logging .Logger ;
27
31
28
32
import org .utplsql .sqldev .dal .RealtimeReporterDao ;
@@ -53,6 +57,7 @@ public CodeCoverageReporter(final List<String> pathList, final List<String> incl
53
57
final String connectionName ) {
54
58
this .pathList = pathList ;
55
59
this .includeObjectList = includeObjectList ;
60
+ setDefaultSchema ();
56
61
setConnection (connectionName );
57
62
}
58
63
@@ -62,6 +67,7 @@ public CodeCoverageReporter(final List<String> pathList, final List<String> incl
62
67
this .pathList = pathList ;
63
68
this .includeObjectList = includeObjectList ;
64
69
this .conn = conn ;
70
+ setDefaultSchema ();
65
71
}
66
72
67
73
private void setConnection (final String connectionName ) {
@@ -75,6 +81,31 @@ private void setConnection(final String connectionName) {
75
81
this .conn = DatabaseTools .getConnection (connectionName );
76
82
}
77
83
}
84
+
85
+ private void setDefaultSchema () {
86
+ if (includeObjectList != null && !includeObjectList .isEmpty ()) {
87
+ // use the owner with the most hits in includeObjectList
88
+ HashMap <String , Integer > owners = new HashMap <>();
89
+ for (String entry : includeObjectList ) {
90
+ String [] obj = entry .toUpperCase ().split ("\\ ." );
91
+ if (obj .length == 2 ) {
92
+ // only if objectOwner and objectName are available
93
+ Integer count = owners .get (obj [0 ]);
94
+ if (count == null ) {
95
+ count = 1 ;
96
+ } else {
97
+ count ++;
98
+ }
99
+ owners .put (obj [0 ], count );
100
+ }
101
+ }
102
+ Optional <Entry <String , Integer >> top = owners .entrySet ().stream ()
103
+ .sorted (Map .Entry .<String , Integer >comparingByValue ().reversed ()).findFirst ();
104
+ if (top .isPresent ()) {
105
+ schemas = top .get ().getKey ();
106
+ }
107
+ }
108
+ }
78
109
79
110
private ArrayList <String > toStringList (final String s ) {
80
111
final ArrayList <String > list = new ArrayList <>();
@@ -92,8 +123,14 @@ private void run() {
92
123
logger .fine (() -> "Running code coverage reporter for " + pathList + "..." );
93
124
try {
94
125
final RealtimeReporterDao dao = new RealtimeReporterDao (conn );
95
- final PreferenceModel preferences = PreferenceModel .getInstance (Preferences .getPreferences ());
96
- if (preferences .isUseRealtimeReporter () && dao .isSupported ()) {
126
+ PreferenceModel preferences ;
127
+ try {
128
+ preferences = PreferenceModel .getInstance (Preferences .getPreferences ());
129
+ } catch (NoClassDefFoundError error ) {
130
+ // not running in SQL Developer (in tests)
131
+ preferences = PreferenceModel .getInstance (null );
132
+ }
133
+ if (preferences .isUseRealtimeReporter () && dao .isSupported () && connectionName != null ) {
97
134
runCodeCoverageWithRealtimeReporter ();
98
135
} else {
99
136
runCodeCoverageStandalone ();
@@ -181,6 +218,10 @@ public void setSchemas(final String schemas) {
181
218
this .schemas = schemas ;
182
219
}
183
220
221
+ public String getSchemas () {
222
+ return schemas ;
223
+ }
224
+
184
225
public void setIncludeObjects (final String includeObjects ) {
185
226
this .includeObjects = includeObjects ;
186
227
}
0 commit comments