1616
1717package com .mongodb .async .rx .client ;
1818
19+ import com .mongodb .CommandFailureException ;
1920import com .mongodb .MongoNamespace ;
2021import org .mongodb .Document ;
2122import rx .Observable ;
2930 */
3031public final class Fixture {
3132 private static MongoClientImpl mongoClient ;
32- private static String defaultDatabaseName ;
33+ private static final String DEFAULT_DATABASE_NAME = "JavaDriverTest" ;
3334
3435 private Fixture () {
3536 }
3637
3738 public static synchronized MongoClient getMongoClient () {
3839 if (mongoClient == null ) {
3940 mongoClient = new MongoClientImpl (com .mongodb .async .client .Fixture .getMongoClient ());
41+ Runtime .getRuntime ().addShutdownHook (new ShutdownHook ());
4042 }
4143 return mongoClient ;
4244 }
4345
44-
4546 public static synchronized String getDefaultDatabaseName () {
46- if (defaultDatabaseName == null ) {
47- defaultDatabaseName = "DriverTest-" + System .nanoTime ();
48- }
49- return defaultDatabaseName ;
47+ return DEFAULT_DATABASE_NAME ;
5048 }
5149
5250 public static MongoDatabase getDefaultDatabase () {
@@ -58,11 +56,36 @@ public static MongoCollection<Document> initializeCollection(final MongoNamespac
5856 return getMongoClient ().getDatabase (namespace .getDatabaseName ()).getCollection (namespace .getCollectionName ());
5957 }
6058
59+ public static void dropDatabase (final String name ) {
60+ com .mongodb .async .client .Fixture .dropDatabase (name );
61+ }
62+
63+ public static void drop (final MongoNamespace namespace ) {
64+ com .mongodb .async .client .Fixture .drop (namespace );
65+ }
66+
6167 public static <T > T get (final Observable <T > observable ) {
6268 return observable .timeout (90 , SECONDS ).toBlocking ().first ();
6369 }
6470
6571 public static <T > List <T > getAsList (final Observable <T > observable ) {
6672 return observable .timeout (90 , SECONDS ).toList ().toBlocking ().first ();
6773 }
74+
75+ static class ShutdownHook extends Thread {
76+ @ Override
77+ public void run () {
78+ if (mongoClient != null ) {
79+ if (mongoClient != null ) {
80+ try {
81+ dropDatabase (getDefaultDatabaseName ());
82+ } catch (CommandFailureException e ) {
83+ // ignore
84+ }
85+ }
86+ mongoClient .close ();
87+ mongoClient = null ;
88+ }
89+ }
90+ }
6891}
0 commit comments