@@ -41,22 +41,26 @@ public final class TestContext implements AutoCloseable {
4141 @ Nullable
4242 private Map <String , Replicator > openRepls ;
4343 @ Nullable
44- private Map <String , DocReplListener > openListeners ;
44+ private Map <String , DocReplListener > openDocListeners ;
4545 @ Nullable
4646 private Map <String , Snapshot > openSnapshots ;
47+ @ Nullable
48+ private Map <String , URLEndpointListener > openEndptListeners ;
4749
4850 TestContext (@ NonNull String client ) { this .client = client ; }
4951
5052 @ Override
5153 public void close () {
5254 // belt
53- openListeners = null ;
55+ openDocListeners = null ;
5456
5557 // ... and suspenders...
5658 openSnapshots = null ;
5759
5860 stopRepls ();
5961
62+ stopEndptListeners ();
63+
6064 closeCollections ();
6165
6266 deleteDbs ();
@@ -82,107 +86,148 @@ public void setDbDir(@NonNull File dbDir) {
8286 @ SuppressFBWarnings ("NP_NULL_ON_SOME_PATH" )
8387 public void addDb (@ NonNull String name , @ NonNull Database db ) {
8488 Log .p (TAG , "Adding database to context: " + name );
85- if (openDbs == null ) { openDbs = new HashMap <>(); }
86- if (openDbs .containsKey (name )) { throw new ClientError ("Attempt to replace an open database" ); }
87- openDbs .put (name , db );
89+ Map <String , Database > dbs = openDbs ;
90+ if (dbs == null ) {
91+ dbs = new HashMap <>();
92+ openDbs = dbs ;
93+ }
94+ if (dbs .containsKey (name )) { throw new ClientError ("Attempt to replace an open database" ); }
95+ dbs .put (name , db );
8896 }
8997
9098 @ Nullable
91- public Database getDb (@ NonNull String name ) { return (openDbs == null ) ? null : openDbs .get (name ); }
92-
93- @ Nullable
94- public Database removeDb (@ NonNull String name ) { return (openDbs == null ) ? null : openDbs .remove (name ); }
95-
96- public void addListener (@ NonNull String id , @ NonNull URLEndpointListener listener ) {
97- // TODO: implement
98- throw new UnsupportedOperationException ("Not implemented" );
99- }
100-
101- public void removeListener (@ NonNull String id ) {
102- // TODO: implement
103- throw new UnsupportedOperationException ("Not implemented" );
99+ public Database getDb (@ NonNull String name ) {
100+ final Map <String , Database > dbs = openDbs ;
101+ return (dbs == null ) ? null : dbs .get (name );
104102 }
105103
106104 @ Nullable
107- public URLEndpointListener getListener (@ NonNull String id ) {
108- // TODO: implement
109- throw new UnsupportedOperationException ( "Not implemented" );
105+ public Database removeDb (@ NonNull String name ) {
106+ final Map < String , Database > dbs = openDbs ;
107+ return ( dbs == null ) ? null : dbs . remove ( name );
110108 }
111109
112110 @ Nullable
113111 public Collection getOpenCollection (@ NonNull String collFqn ) {
114- return (openCollections == null ) ? null : openCollections .get (collFqn );
112+ final Map <String , Collection > collections = openCollections ;
113+ return (collections == null ) ? null : collections .get (collFqn );
115114 }
116115
117116 public void addOpenCollection (@ NonNull Collection collection ) {
118- if (openCollections == null ) { openCollections = new HashMap <>(); }
119- openCollections .put (DatabaseService .getCollectionFQN (collection ), collection );
117+ Map <String , Collection > collections = openCollections ;
118+ if (collections == null ) {
119+ collections = new HashMap <>();
120+ openCollections = collections ;
121+ }
122+ collections .put (DatabaseService .getCollectionFQN (collection ), collection );
120123 }
121124
122125 @ SuppressFBWarnings ("NP_NULL_ON_SOME_PATH" )
123126 public void addRepl (@ NonNull String id , @ NonNull Replicator repl ) {
124- if (openRepls == null ) { openRepls = new HashMap <>(); }
125- if (openRepls .containsKey (id )) { throw new ClientError ("Attempt to replace an existing replicator" ); }
126- openRepls .put (id , repl );
127+ Map <String , Replicator > repls = openRepls ;
128+ if (repls == null ) {
129+ repls = new HashMap <>();
130+ openRepls = repls ;
131+ }
132+ if (repls .containsKey (id )) { throw new ClientError ("Attempt to replace an existing replicator" ); }
133+ repls .put (id , repl );
127134 }
128135
129136 @ Nullable
130- public Replicator getRepl (@ NonNull String name ) { return (openRepls == null ) ? null : openRepls .get (name ); }
137+ public Replicator getRepl (@ NonNull String name ) {
138+ final Map <String , Replicator > repls = openRepls ;
139+ return (repls == null ) ? null : repls .get (name );
140+ }
131141
132142 @ SuppressFBWarnings ("NP_NULL_ON_SOME_PATH" )
133143 public void addDocReplListener (@ NonNull String replId , @ NonNull DocReplListener listener ) {
134- if (openListeners == null ) { openListeners = new HashMap <>(); }
135- if (openListeners .containsKey (replId )) { throw new ClientError ("Attempt to replace an existing doc listener" ); }
136- openListeners .put (replId , listener );
144+ Map <String , DocReplListener > docListeners = openDocListeners ;
145+ if (openDocListeners == null ) {
146+ docListeners = new HashMap <>();
147+ openDocListeners = docListeners ;
148+ }
149+ if (docListeners .containsKey (replId )) { throw new ClientError ("Attempt to replace an existing doc listener" ); }
150+ docListeners .put (replId , listener );
137151 }
138152
139153 @ Nullable
140154 public DocReplListener getDocReplListener (@ NonNull String id ) {
141- return (openListeners == null ) ? null : openListeners .get (id );
155+ final Map <String , DocReplListener > docListeners = openDocListeners ;
156+ return (docListeners == null ) ? null : docListeners .get (id );
142157 }
143158
144159 @ NonNull
145160 public String addSnapshot (@ NonNull Snapshot snapshot ) {
146161 final String snapshotId = UUID .randomUUID ().toString ();
147- if (openSnapshots == null ) { openSnapshots = new HashMap <>(); }
148- openSnapshots .put (snapshotId , snapshot );
162+ Map <String , Snapshot > snapshots = openSnapshots ;
163+ if (openSnapshots == null ) {
164+ snapshots = new HashMap <>();
165+ openSnapshots = snapshots ;
166+ }
167+ snapshots .put (snapshotId , snapshot );
149168 return snapshotId ;
150169 }
151170
152171 @ NonNull
153172 public Snapshot getSnapshot (@ NonNull String id ) {
154- if (openSnapshots != null ) {
155- final Snapshot snapshot = openSnapshots .get (id );
173+ final Map <String , Snapshot > shapshots = openSnapshots ;
174+ if (shapshots != null ) {
175+ final Snapshot snapshot = shapshots .get (id );
156176 if (snapshot != null ) { return snapshot ; }
157177 }
158178 throw new ClientError ("No such snapshot: " + id );
159179 }
160180
181+ public void addEndptListener (@ NonNull String id , @ NonNull URLEndpointListener listener ) {
182+ Map <String , URLEndpointListener > endptListeners = openEndptListeners ;
183+ if (endptListeners == null ) {
184+ endptListeners = new HashMap <>();
185+ openEndptListeners = endptListeners ;
186+ }
187+ if (endptListeners .containsKey (id )) { throw new ClientError ("Attempt to replace an existing listener" ); }
188+ endptListeners .put (id , listener );
189+ }
190+
191+ @ Nullable
192+ public URLEndpointListener getEndptListener (@ NonNull String id ) {
193+ final Map <String , URLEndpointListener > endptListeners = openEndptListeners ;
194+ return (endptListeners == null ) ? null : endptListeners .get (id );
195+ }
196+
161197 private void stopRepls () {
162- final Map <String , Replicator > liveRepls = this . openRepls ;
163- this . openRepls = null ;
198+ final Map <String , Replicator > liveRepls = openRepls ;
199+ openRepls = null ;
164200 if (liveRepls == null ) { return ; }
165201 for (Replicator repl : liveRepls .values ()) {
166202 if (repl != null ) { repl .stop (); }
167203 }
168204 }
169205
206+ private void stopEndptListeners () {
207+ final Map <String , URLEndpointListener > liveEndptListeners = openEndptListeners ;
208+ openEndptListeners = null ;
209+ if (liveEndptListeners == null ) { return ; }
210+ for (URLEndpointListener listener : liveEndptListeners .values ()) {
211+ if (listener != null ) { listener .stop (); }
212+ }
213+ }
214+
170215 private void closeCollections () {
171- final Map <String , Collection > openColls = openCollections ;
172- this . openCollections = null ;
173- if (openColls == null ) { return ; }
174- for (Collection collection : openColls .values ()) { collection .close (); }
216+ final Map <String , Collection > liveCollections = openCollections ;
217+ openCollections = null ;
218+ if (liveCollections == null ) { return ; }
219+ for (Collection collection : liveCollections .values ()) { collection .close (); }
175220 }
176221
177222 private void deleteDbs () {
178- final File dbDir = this . dbDir ;
179- this . dbDir = null ;
223+ final File liveDbDir = dbDir ;
224+ dbDir = null ;
180225
181- final Map <String , Database > openDbs = this . openDbs ;
182- this . openDbs = null ;
226+ final Map <String , Database > liveDbs = openDbs ;
227+ openDbs = null ;
183228
184- if (openDbs != null ) {
185- for (Map .Entry <String , Database > entry : openDbs .entrySet ()) {
229+ if (liveDbs != null ) {
230+ for (Map .Entry <String , Database > entry : liveDbs .entrySet ()) {
186231 final Database db = entry .getValue ();
187232 if (db == null ) {
188233 Log .err (TAG , "Attempt to close non-existent database: " + entry .getKey ());
@@ -192,13 +237,13 @@ private void deleteDbs() {
192237 final String dbName = db .getName ();
193238 try { db .close (); }
194239 catch (CouchbaseLiteException e ) {
195- throw new CblApiFailure ("Failed closing database: " + dbName + " in " + dbDir , e );
240+ throw new CblApiFailure ("Failed closing database: " + dbName + " in " + liveDbDir , e );
196241 }
197242 }
198243 }
199244
200- if ((dbDir != null ) && !new FileUtils ().deleteRecursive (dbDir )) {
201- Log .err (TAG , "Failed deleting db dir on reset: " + dbDir );
245+ if ((liveDbDir != null ) && !new FileUtils ().deleteRecursive (liveDbDir )) {
246+ Log .err (TAG , "Failed deleting db dir on reset: " + liveDbDir );
202247 }
203248 }
204249}
0 commit comments