|
| 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 | +} |
0 commit comments