16
16
17
17
package com .mongodb .acceptancetest .core ;
18
18
19
- import com .mongodb .MongoClient ;
20
19
import com .mongodb .client .DatabaseTestCase ;
21
20
import com .mongodb .client .MongoDatabase ;
22
21
import org .bson .Document ;
22
+ import org .hamcrest .BaseMatcher ;
23
+ import org .hamcrest .Description ;
23
24
import org .junit .Test ;
24
25
25
26
import java .util .ArrayList ;
26
27
import java .util .List ;
27
28
28
- import static com .mongodb .Fixture .getMongoClient ;
29
29
import static org .hamcrest .CoreMatchers .hasItem ;
30
30
import static org .hamcrest .CoreMatchers .hasItems ;
31
31
import static org .hamcrest .CoreMatchers .not ;
32
- import static org .hamcrest .Matchers .greaterThan ;
33
32
import static org .hamcrest .core .Is .is ;
34
33
import static org .junit .Assert .assertThat ;
35
- import static org .junit .Assert .assertTrue ;
36
34
37
35
/**
38
36
* Documents the basic functionality available for the MongoClient via the Java driver.
@@ -48,45 +46,18 @@ public void shouldListDatabaseNamesFromDatabase() {
48
46
}
49
47
50
48
@ 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" );
82
53
83
54
try {
84
55
// given
85
56
firstDatabase .getCollection ("coll" ).insertOne (new Document ("aDoc" , "to force database creation" ));
86
57
secondDatabase .getCollection ("coll" ).insertOne (new Document ("aDoc" , "to force database creation" ));
87
58
88
59
//when
89
- List <String > databaseNames = mongoClient .listDatabaseNames ().into (new ArrayList <String >());
60
+ List <String > databaseNames = client .listDatabaseNames ().into (new ArrayList <String >());
90
61
91
62
//then
92
63
assertThat (databaseNames , hasItems (firstDatabase .getName (), secondDatabase .getName ()));
@@ -97,4 +68,38 @@ public void shouldBeAbleToListAllTheDatabasesAvailable() {
97
68
secondDatabase .drop ();
98
69
}
99
70
}
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
+ }
100
105
}
0 commit comments