1818import com .mongodb .client .result .DeleteResult ;
1919import com .mongodb .client .result .UpdateResult ;
2020import io .opentelemetry .instrumentation .mongo .testing .AbstractMongoClientTest ;
21+ import io .opentelemetry .instrumentation .testing .internal .AutoCleanupExtension ;
2122import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
2223import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
2324import java .util .ArrayList ;
2425import org .bson .BsonDocument ;
2526import org .bson .BsonString ;
2627import org .bson .Document ;
27- import org .junit .jupiter .api .AfterAll ;
2828import org .junit .jupiter .api .BeforeAll ;
2929import org .junit .jupiter .api .extension .RegisterExtension ;
3030
@@ -33,18 +33,14 @@ class MongoClientTest extends AbstractMongoClientTest<MongoCollection<Document>>
3333 @ RegisterExtension
3434 static final InstrumentationExtension testing = AgentInstrumentationExtension .create ();
3535
36+ @ RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension .create ();
37+
3638 private MongoClient client ;
3739
3840 @ BeforeAll
3941 void setup () {
4042 client = MongoClients .create ("mongodb://" + host + ":" + port );
41- }
42-
43- @ AfterAll
44- void cleanup () {
45- if (client != null ) {
46- client .close ();
47- }
43+ cleanup .deferAfterAll (client );
4844 }
4945
5046 @ Override
@@ -53,41 +49,44 @@ protected InstrumentationExtension testing() {
5349 }
5450
5551 @ Override
56- public void createCollection (String dbName , String collectionName ) {
52+ protected void createCollection (String dbName , String collectionName ) {
5753 MongoDatabase db = client .getDatabase (dbName );
5854 db .createCollection (collectionName );
5955 }
6056
6157 @ Override
62- public void createCollectionNoDescription (String dbName , String collectionName ) {
63- MongoDatabase db = MongoClients .create ("mongodb://" + host + ":" + port ).getDatabase (dbName );
64- db .createCollection (collectionName );
58+ protected void createCollectionNoDescription (String dbName , String collectionName ) {
59+ try (MongoClient mongoClient = MongoClients .create ("mongodb://" + host + ":" + port )) {
60+ mongoClient .getDatabase (dbName ).createCollection (collectionName );
61+ }
6562 }
6663
6764 @ Override
68- public void createCollectionWithAlreadyBuiltClientOptions (String dbName , String collectionName ) {
65+ protected void createCollectionWithAlreadyBuiltClientOptions (
66+ String dbName , String collectionName ) {
6967 abort ("not tested on 4.0" );
7068 }
7169
7270 @ Override
73- public void createCollectionCallingBuildTwice (String dbName , String collectionName ) {
71+ protected void createCollectionCallingBuildTwice (String dbName , String collectionName ) {
7472 MongoClientSettings .Builder settings =
7573 MongoClientSettings .builder ()
7674 .applyToClusterSettings (
7775 builder -> builder .hosts (singletonList (new ServerAddress (host , port ))));
7876 settings .build ();
79- MongoDatabase db = MongoClients .create (settings .build ()).getDatabase (dbName );
80- db .createCollection (collectionName );
77+ try (MongoClient mongoClient = MongoClients .create (settings .build ())) {
78+ mongoClient .getDatabase (dbName ).createCollection (collectionName );
79+ }
8180 }
8281
8382 @ Override
84- public long getCollection (String dbName , String collectionName ) {
83+ protected long getCollection (String dbName , String collectionName ) {
8584 MongoDatabase db = client .getDatabase (dbName );
8685 return db .getCollection (collectionName ).estimatedDocumentCount ();
8786 }
8887
8988 @ Override
90- public MongoCollection <Document > setupInsert (String dbName , String collectionName ) {
89+ protected MongoCollection <Document > setupInsert (String dbName , String collectionName ) {
9190 MongoCollection <Document > collection =
9291 testing ()
9392 .runWithSpan (
@@ -102,13 +101,13 @@ public MongoCollection<Document> setupInsert(String dbName, String collectionNam
102101 }
103102
104103 @ Override
105- public long insert (MongoCollection <Document > collection ) {
104+ protected long insert (MongoCollection <Document > collection ) {
106105 collection .insertOne (new Document ("password" , "SECRET" ));
107106 return collection .estimatedDocumentCount ();
108107 }
109108
110109 @ Override
111- public MongoCollection <Document > setupUpdate (String dbName , String collectionName ) {
110+ protected MongoCollection <Document > setupUpdate (String dbName , String collectionName ) {
112111 MongoCollection <Document > collection =
113112 testing ()
114113 .runWithSpan (
@@ -125,7 +124,7 @@ public MongoCollection<Document> setupUpdate(String dbName, String collectionNam
125124 }
126125
127126 @ Override
128- public long update (MongoCollection <Document > collection ) {
127+ protected long update (MongoCollection <Document > collection ) {
129128 UpdateResult result =
130129 collection .updateOne (
131130 new BsonDocument ("password" , new BsonString ("OLDPW" )),
@@ -135,7 +134,7 @@ public long update(MongoCollection<Document> collection) {
135134 }
136135
137136 @ Override
138- public MongoCollection <Document > setupDelete (String dbName , String collectionName ) {
137+ protected MongoCollection <Document > setupDelete (String dbName , String collectionName ) {
139138 MongoCollection <Document > collection =
140139 testing ()
141140 .runWithSpan (
@@ -152,15 +151,15 @@ public MongoCollection<Document> setupDelete(String dbName, String collectionNam
152151 }
153152
154153 @ Override
155- public long delete (MongoCollection <Document > collection ) {
154+ protected long delete (MongoCollection <Document > collection ) {
156155 DeleteResult result =
157156 collection .deleteOne (new BsonDocument ("password" , new BsonString ("SECRET" )));
158157 collection .estimatedDocumentCount ();
159158 return result .getDeletedCount ();
160159 }
161160
162161 @ Override
163- public MongoCollection <Document > setupGetMore (String dbName , String collectionName ) {
162+ protected MongoCollection <Document > setupGetMore (String dbName , String collectionName ) {
164163 MongoCollection <Document > collection =
165164 testing ()
166165 .runWithSpan (
@@ -178,7 +177,7 @@ public MongoCollection<Document> setupGetMore(String dbName, String collectionNa
178177 }
179178
180179 @ Override
181- public void getMore (MongoCollection <Document > collection ) {
180+ protected void getMore (MongoCollection <Document > collection ) {
182181 collection
183182 .find ()
184183 .filter (new Document ("_id" , new Document ("$gte" , 0 )))
@@ -187,7 +186,7 @@ public void getMore(MongoCollection<Document> collection) {
187186 }
188187
189188 @ Override
190- public void error (String dbName , String collectionName ) {
189+ protected void error (String dbName , String collectionName ) {
191190 MongoCollection <Document > collection =
192191 testing ()
193192 .runWithSpan (
0 commit comments