29
29
import org .elasticsearch .action .count .CountResponse ;
30
30
import org .elasticsearch .river .mongodb .RiverMongoDBTestAbstract ;
31
31
import org .testng .Assert ;
32
- import org .testng .annotations .AfterClass ;
33
- import org .testng .annotations .BeforeClass ;
32
+ import org .testng .annotations .AfterMethod ;
33
+ import org .testng .annotations .BeforeMethod ;
34
34
import org .testng .annotations .Test ;
35
35
36
36
import com .mongodb .DB ;
@@ -56,7 +56,8 @@ protected RiverMongoDropCollectionTest(String river, String database, String col
56
56
super (river , database , collection , index );
57
57
}
58
58
59
- @ BeforeClass
59
+ // @BeforeClass
60
+ @ BeforeMethod
60
61
public void createDatabase () {
61
62
logger .debug ("createDatabase {}" , getDatabase ());
62
63
try {
@@ -73,11 +74,12 @@ public void createDatabase() {
73
74
}
74
75
}
75
76
76
- @ AfterClass
77
+ // @AfterClass
78
+ @ AfterMethod
77
79
public void cleanUp () {
78
80
super .deleteRiver ();
79
81
logger .info ("Drop database " + mongoDB .getName ());
80
- mongoDB .dropDatabase ();
82
+ // mongoDB.dropDatabase();
81
83
}
82
84
83
85
@ Test
@@ -112,6 +114,8 @@ public void testDropCollection() throws Throwable {
112
114
logger .error ("testDropCollection failed." , t );
113
115
t .printStackTrace ();
114
116
throw t ;
117
+ } finally {
118
+ mongoDB .dropDatabase ();
115
119
}
116
120
}
117
121
@@ -158,6 +162,52 @@ public void testDropCollectionIssue79() throws Throwable {
158
162
logger .error ("testDropCollectionIssue79 failed." , t );
159
163
t .printStackTrace ();
160
164
throw t ;
165
+ } finally {
166
+ mongoDB .dropDatabase ();
161
167
}
162
168
}
169
+
170
+ @ Test
171
+ public void testDropDatabaseIssue133 () throws Throwable {
172
+ logger .debug ("Start testDropDatabaseIssue133" );
173
+ try {
174
+ String mongoDocument = copyToStringFromClasspath (TEST_SIMPLE_MONGODB_DOCUMENT_JSON );
175
+ DBObject dbObject = (DBObject ) JSON .parse (mongoDocument );
176
+ mongoCollection .insert (dbObject );
177
+ Thread .sleep (wait );
178
+
179
+ assertThat (getNode ().client ().admin ().indices ().exists (new IndicesExistsRequest (getIndex ())).actionGet ().isExists (),
180
+ equalTo (true ));
181
+ assertThat (getNode ().client ().admin ().indices ().prepareTypesExists (getIndex ()).setTypes (getDatabase ()).execute ().actionGet ()
182
+ .isExists (), equalTo (true ));
183
+ long countRequest = getNode ().client ().count (countRequest (getIndex ())).actionGet ().getCount ();
184
+ mongoDB .dropDatabase ();
185
+ Thread .sleep (wait );
186
+ assertThat (databaseExists (database ), equalTo (false ));
187
+ Thread .sleep (wait );
188
+ refreshIndex ();
189
+
190
+ if (!dropCollectionOption ) {
191
+ countRequest = getNode ().client ().count (countRequest (getIndex ())).actionGet ().getCount ();
192
+ assertThat (countRequest , greaterThan (0L ));
193
+ } else {
194
+ countRequest = getNode ().client ().count (countRequest (getIndex ())).actionGet ().getCount ();
195
+ assertThat (countRequest , equalTo (0L ));
196
+ }
197
+ } catch (Throwable t ) {
198
+ logger .error ("testDropDatabaseIssue133 failed." , t );
199
+ t .printStackTrace ();
200
+ throw t ;
201
+ } finally {
202
+ }
203
+ }
204
+
205
+ private boolean databaseExists (String name ) {
206
+ for (String databaseName :mongo .getDatabaseNames ()) {
207
+ if (databaseName .equals (name )) {
208
+ return true ;
209
+ }
210
+ }
211
+ return false ;
212
+ }
163
213
}
0 commit comments