Skip to content

Commit 69dbdee

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1038 - Assert Mongo instances cleaned up properly after test runs.
Add JUnit rule and RunListener taking care of clean up task. Original pull request: #221.
1 parent dedb9f3 commit 69dbdee

File tree

4 files changed

+345
-12
lines changed

4 files changed

+345
-12
lines changed

spring-data-mongodb/pom.xml

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33

44
<modelVersion>4.0.0</modelVersion>
5-
5+
66
<artifactId>spring-data-mongodb</artifactId>
77

88
<name>Spring Data MongoDB - Core</name>
@@ -21,7 +21,7 @@
2121
</properties>
2222

2323
<dependencies>
24-
24+
2525
<!-- Spring -->
2626
<dependency>
2727
<groupId>org.springframework</groupId>
@@ -77,7 +77,7 @@
7777
<version>1.0</version>
7878
<optional>true</optional>
7979
</dependency>
80-
80+
8181
<!-- CDI -->
8282
<dependency>
8383
<groupId>javax.enterprise</groupId>
@@ -86,21 +86,21 @@
8686
<scope>provided</scope>
8787
<optional>true</optional>
8888
</dependency>
89-
89+
9090
<dependency>
9191
<groupId>javax.el</groupId>
9292
<artifactId>el-api</artifactId>
9393
<version>${cdi}</version>
9494
<scope>test</scope>
9595
</dependency>
96-
96+
9797
<dependency>
9898
<groupId>org.apache.openwebbeans.test</groupId>
9999
<artifactId>cditest-owb</artifactId>
100100
<version>${webbeans}</version>
101101
<scope>test</scope>
102102
</dependency>
103-
103+
104104
<dependency>
105105
<groupId>javax.servlet</groupId>
106106
<artifactId>servlet-api</artifactId>
@@ -115,7 +115,7 @@
115115
<version>${validation}</version>
116116
<optional>true</optional>
117117
</dependency>
118-
118+
119119
<dependency>
120120
<groupId>org.objenesis</groupId>
121121
<artifactId>objenesis</artifactId>
@@ -129,23 +129,23 @@
129129
<version>4.2.0.Final</version>
130130
<scope>test</scope>
131131
</dependency>
132-
132+
133133
<dependency>
134134
<groupId>joda-time</groupId>
135135
<artifactId>joda-time</artifactId>
136136
<version>${jodatime}</version>
137137
<scope>test</scope>
138138
</dependency>
139-
139+
140140
<dependency>
141141
<groupId>org.slf4j</groupId>
142142
<artifactId>jul-to-slf4j</artifactId>
143143
<version>${slf4j}</version>
144144
<scope>test</scope>
145145
</dependency>
146-
146+
147147
</dependencies>
148-
148+
149149
<build>
150150
<plugins>
151151

@@ -189,9 +189,14 @@
189189
<systemPropertyVariables>
190190
<java.util.logging.config.file>src/test/resources/logging.properties</java.util.logging.config.file>
191191
</systemPropertyVariables>
192+
<properties>
193+
<property>
194+
<name>listener</name>
195+
<value>org.springframework.data.mongodb.test.util.CleanMongoDBJunitRunListener</value>
196+
</property>
197+
</properties>
192198
</configuration>
193199
</plugin>
194200
</plugins>
195201
</build>
196-
197202
</project>
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.test.util;
17+
18+
import java.net.UnknownHostException;
19+
import java.util.Arrays;
20+
import java.util.Collection;
21+
import java.util.HashSet;
22+
import java.util.Set;
23+
24+
import org.junit.rules.TestRule;
25+
import org.junit.runner.Description;
26+
import org.junit.runners.model.Statement;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
import org.springframework.util.CollectionUtils;
30+
31+
import com.mongodb.DB;
32+
import com.mongodb.MongoClient;
33+
34+
/**
35+
* @author Christoph Strobl
36+
*/
37+
public class CleanMongoDB implements TestRule {
38+
39+
private static final Logger LOGGER = LoggerFactory.getLogger(CleanMongoDB.class);
40+
41+
public enum Types {
42+
DATABASE, COLLECTION, INDEX;
43+
}
44+
45+
private Set<String> preserveDatabases = new HashSet<String>() {
46+
47+
private static final long serialVersionUID = -8698807376808700046L;
48+
49+
{
50+
add("admin");
51+
add("local");
52+
}
53+
};
54+
55+
private Set<String> dbNames = new HashSet<String>();
56+
private Set<String> collectionNames = new HashSet<String>();
57+
private Set<Types> types = new HashSet<CleanMongoDB.Types>();
58+
private MongoClient client;
59+
60+
public CleanMongoDB() {
61+
this(null);
62+
}
63+
64+
public CleanMongoDB(String host, int port) throws UnknownHostException {
65+
this(new MongoClient(host, port));
66+
}
67+
68+
public CleanMongoDB(MongoClient client) {
69+
this.client = client;
70+
}
71+
72+
public static CleanMongoDB everything() {
73+
74+
CleanMongoDB cleanMongoDB = new CleanMongoDB();
75+
cleanMongoDB.clean(Types.DATABASE);
76+
return cleanMongoDB;
77+
}
78+
79+
public static CleanMongoDB databases(String... dbNames) {
80+
81+
CleanMongoDB cleanMongoDB = new CleanMongoDB();
82+
cleanMongoDB.clean(Types.DATABASE);
83+
cleanMongoDB.collectionNames.addAll(Arrays.asList(dbNames));
84+
return cleanMongoDB;
85+
}
86+
87+
public static CleanMongoDB indexes() {
88+
89+
CleanMongoDB cleanMongoDB = new CleanMongoDB();
90+
cleanMongoDB.clean(Types.INDEX);
91+
return cleanMongoDB;
92+
}
93+
94+
public CleanMongoDB clean(Types... types) {
95+
96+
this.types.addAll(Arrays.asList(types));
97+
return this;
98+
}
99+
100+
public Statement apply() {
101+
return apply(null, null);
102+
}
103+
104+
public Statement apply(Statement base, Description description) {
105+
return new MongoCleanStatement(base);
106+
}
107+
108+
private class MongoCleanStatement extends Statement {
109+
110+
private final Statement base;
111+
112+
public MongoCleanStatement(Statement base) {
113+
this.base = base;
114+
}
115+
116+
@Override
117+
public void evaluate() throws Throwable {
118+
119+
if (base != null) {
120+
base.evaluate();
121+
}
122+
123+
boolean isInternal = false;
124+
if (client == null) {
125+
client = new MongoClient();
126+
isInternal = true;
127+
}
128+
129+
Collection<String> dbNamesToUse = dbNames;
130+
if (dbNamesToUse.isEmpty()) {
131+
dbNamesToUse = client.getDatabaseNames();
132+
}
133+
134+
for (String dbName : dbNamesToUse) {
135+
136+
if (preserveDatabases.contains(dbName.toLowerCase())) {
137+
continue;
138+
}
139+
140+
if (types.contains(Types.DATABASE)) {
141+
client.dropDatabase(dbName);
142+
LOGGER.debug("Dropping DB '{}'. ", dbName);
143+
}
144+
145+
if (types.contains(Types.COLLECTION)) {
146+
147+
DB db = client.getDB(dbName);
148+
Collection<String> collectionsToUse = initCollectionNames(db);
149+
for (String collectionName : collectionsToUse) {
150+
if (db.collectionExists(collectionName)) {
151+
db.getCollectionFromString(collectionName).drop();
152+
LOGGER.debug("Dropping collection '{}' for DB '{}'. ", collectionName, dbName);
153+
}
154+
}
155+
}
156+
157+
if (types.contains(Types.INDEX)) {
158+
159+
DB db = client.getDB(dbName);
160+
Collection<String> collectionsToUse = initCollectionNames(db);
161+
for (String collectionName : collectionsToUse) {
162+
if (db.collectionExists(collectionName)) {
163+
db.getCollectionFromString(collectionName).dropIndexes();
164+
LOGGER.debug("Dropping indexes in collection '{}' for DB '{}'. ", collectionName, dbName);
165+
}
166+
}
167+
}
168+
}
169+
170+
if (isInternal) {
171+
client.close();
172+
client = null;
173+
}
174+
}
175+
176+
private Collection<String> initCollectionNames(DB db) {
177+
178+
Collection<String> collectionsToUse = collectionNames;
179+
if (CollectionUtils.isEmpty(collectionsToUse)) {
180+
collectionsToUse = db.getCollectionNames();
181+
}
182+
return collectionsToUse;
183+
}
184+
}
185+
186+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.test.util;
17+
18+
import org.junit.runner.Result;
19+
import org.junit.runner.notification.RunListener;
20+
import org.springframework.data.mongodb.test.util.CleanMongoDB.Types;
21+
22+
/**
23+
* @author Christoph Strobl
24+
*/
25+
public class CleanMongoDBJunitRunListener extends RunListener {
26+
27+
@Override
28+
public void testRunFinished(Result result) throws Exception {
29+
super.testRunFinished(result);
30+
try {
31+
new CleanMongoDB().clean(Types.INDEX).apply().evaluate();
32+
} catch (Throwable e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
37+
}

0 commit comments

Comments
 (0)