Skip to content

Commit c64b958

Browse files
committed
Simplify ClientAcceptanceTest so that it no longer relies on dropping a database.
1 parent c634e76 commit c64b958

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

driver/src/test/acceptance/com/mongodb/acceptancetest/core/ClientAcceptanceTest.java

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,21 @@
1616

1717
package com.mongodb.acceptancetest.core;
1818

19-
import com.mongodb.MongoClient;
2019
import com.mongodb.client.DatabaseTestCase;
2120
import com.mongodb.client.MongoDatabase;
2221
import org.bson.Document;
22+
import org.hamcrest.BaseMatcher;
23+
import org.hamcrest.Description;
2324
import org.junit.Test;
2425

2526
import java.util.ArrayList;
2627
import java.util.List;
2728

28-
import static com.mongodb.Fixture.getMongoClient;
2929
import static org.hamcrest.CoreMatchers.hasItem;
3030
import static org.hamcrest.CoreMatchers.hasItems;
3131
import static org.hamcrest.CoreMatchers.not;
32-
import static org.hamcrest.Matchers.greaterThan;
3332
import static org.hamcrest.core.Is.is;
3433
import static org.junit.Assert.assertThat;
35-
import static org.junit.Assert.assertTrue;
3634

3735
/**
3836
* Documents the basic functionality available for the MongoClient via the Java driver.
@@ -48,45 +46,18 @@ public void shouldListDatabaseNamesFromDatabase() {
4846
}
4947

5048
@Test
51-
public void shouldListDatabasesFromDatabase() {
52-
database.drop();
53-
54-
List<Document> databases = client.listDatabases().into(new ArrayList<Document>());
55-
int size = databases.size();
56-
57-
database.createCollection(getCollectionName());
58-
databases = client.listDatabases().into(new ArrayList<Document>());
59-
assertThat(databases.size(), is(greaterThan(size)));
60-
}
61-
62-
63-
@Test
64-
public void shouldListDatabasesNamesFromDatabase() {
65-
database.drop();
66-
67-
List<String> databases = client.listDatabaseNames().into(new ArrayList<String>());
68-
int size = databases.size();
69-
70-
database.createCollection(getCollectionName());
71-
databases = client.listDatabaseNames().into(new ArrayList<String>());
72-
assertThat(databases.size(), is(greaterThan(size)));
73-
assertTrue(databases.contains(getDatabaseName()));
74-
}
75-
76-
@Test
77-
public void shouldBeAbleToListAllTheDatabasesAvailable() {
78-
MongoClient mongoClient = getMongoClient();
79-
MongoDatabase firstDatabase = mongoClient.getDatabase("FirstNewDatabase");
80-
MongoDatabase secondDatabase = mongoClient.getDatabase("SecondNewDatabase");
81-
MongoDatabase otherDatabase = mongoClient.getDatabase("DatabaseThatDoesNotExistYet");
49+
public void shouldBeAbleToListAllTheDatabaseNamesAvailable() {
50+
MongoDatabase firstDatabase = client.getDatabase("FirstNewDatabase");
51+
MongoDatabase secondDatabase = client.getDatabase("SecondNewDatabase");
52+
MongoDatabase otherDatabase = client.getDatabase("DatabaseThatDoesNotExistYet");
8253

8354
try {
8455
// given
8556
firstDatabase.getCollection("coll").insertOne(new Document("aDoc", "to force database creation"));
8657
secondDatabase.getCollection("coll").insertOne(new Document("aDoc", "to force database creation"));
8758

8859
//when
89-
List<String> databaseNames = mongoClient.listDatabaseNames().into(new ArrayList<String>());
60+
List<String> databaseNames = client.listDatabaseNames().into(new ArrayList<String>());
9061

9162
//then
9263
assertThat(databaseNames, hasItems(firstDatabase.getName(), secondDatabase.getName()));
@@ -97,4 +68,38 @@ public void shouldBeAbleToListAllTheDatabasesAvailable() {
9768
secondDatabase.drop();
9869
}
9970
}
71+
72+
@Test
73+
public void shouldListDatabase() {
74+
List<Document> databases = client.listDatabases().into(new ArrayList<Document>());
75+
76+
database.createCollection(getCollectionName());
77+
databases = client.listDatabases().into(new ArrayList<Document>());
78+
assertThat(databases, new DatabaseNameMatcher(getDatabaseName()));
79+
}
80+
81+
private static final class DatabaseNameMatcher extends BaseMatcher<List<Document>> {
82+
83+
private final String name;
84+
85+
DatabaseNameMatcher(final String name) {
86+
this.name = name;
87+
}
88+
89+
@Override
90+
public boolean matches(final Object item) {
91+
List<Document> databases = (List<Document>) item;
92+
for (Document cur : databases) {
93+
if (cur.get("name").equals(name)) {
94+
return true;
95+
}
96+
}
97+
return false;
98+
}
99+
100+
@Override
101+
public void describeTo(final Description description) {
102+
description.appendText("Document containing a name of " + name);
103+
}
104+
}
100105
}

0 commit comments

Comments
 (0)