2222import java .sql .Connection ;
2323import java .util .ArrayList ;
2424import java .util .Arrays ;
25+ import java .util .HashMap ;
2526import java .util .List ;
27+ import java .util .Map ;
28+ import java .util .Map .Entry ;
29+ import java .util .Optional ;
2630import java .util .logging .Logger ;
2731
2832import org .utplsql .sqldev .dal .RealtimeReporterDao ;
@@ -53,6 +57,7 @@ public CodeCoverageReporter(final List<String> pathList, final List<String> incl
5357 final String connectionName ) {
5458 this .pathList = pathList ;
5559 this .includeObjectList = includeObjectList ;
60+ setDefaultSchema ();
5661 setConnection (connectionName );
5762 }
5863
@@ -62,6 +67,7 @@ public CodeCoverageReporter(final List<String> pathList, final List<String> incl
6267 this .pathList = pathList ;
6368 this .includeObjectList = includeObjectList ;
6469 this .conn = conn ;
70+ setDefaultSchema ();
6571 }
6672
6773 private void setConnection (final String connectionName ) {
@@ -75,6 +81,31 @@ private void setConnection(final String connectionName) {
7581 this .conn = DatabaseTools .getConnection (connectionName );
7682 }
7783 }
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+ }
78109
79110 private ArrayList <String > toStringList (final String s ) {
80111 final ArrayList <String > list = new ArrayList <>();
@@ -92,8 +123,14 @@ private void run() {
92123 logger .fine (() -> "Running code coverage reporter for " + pathList + "..." );
93124 try {
94125 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 ) {
97134 runCodeCoverageWithRealtimeReporter ();
98135 } else {
99136 runCodeCoverageStandalone ();
@@ -181,6 +218,10 @@ public void setSchemas(final String schemas) {
181218 this .schemas = schemas ;
182219 }
183220
221+ public String getSchemas () {
222+ return schemas ;
223+ }
224+
184225 public void setIncludeObjects (final String includeObjects ) {
185226 this .includeObjects = includeObjects ;
186227 }
0 commit comments