Author: mreutegg Date: Wed Oct 28 09:51:11 2015 New Revision: 1710972 URL: http://svn.apache.org/viewvc?rev=1710972&view=rev Log: OAK-3562: Tests do not close MongoConnection
Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoConnectionFactory.java (with props) Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/migration/DocumentToExternalMigrationTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractDocumentStoreTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMongoConnectionTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMultiDocumentStoreTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterJoinTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentDiscoveryLiteServiceTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentStoreFixture.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoDocumentStoreTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoUtils.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/RandomizedClusterTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/SimpleTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/MongoBlobStoreTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudGetLengthTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudReadTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudWriteTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/MongoCloudBlobGCTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreGetLengthTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreReadTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreWriteTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/MongoDataStoreBlobGCTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/impl/DocumentMKConcurrentAddTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheConsistencyIT.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ClusterConflictTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CollisionMarkerTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/JournalIT.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/QueryHintTest.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/RetryReadIT.java Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/migration/DocumentToExternalMigrationTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/migration/DocumentToExternalMigrationTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/migration/DocumentToExternalMigrationTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/migration/DocumentToExternalMigrationTest.java Wed Oct 28 09:51:11 2015 @@ -24,6 +24,7 @@ import java.io.IOException; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; +import org.apache.jackrabbit.oak.plugins.document.MongoConnectionFactory; import org.apache.jackrabbit.oak.plugins.document.MongoUtils; import org.apache.jackrabbit.oak.plugins.document.mongo.MongoBlobStore; import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; @@ -32,14 +33,18 @@ import org.apache.jackrabbit.oak.spi.blo import org.apache.jackrabbit.oak.spi.state.NodeStore; import org.junit.Assume; import org.junit.BeforeClass; +import org.junit.Rule; public class DocumentToExternalMigrationTest extends AbstractMigratorTest { - private DocumentNodeStore nodeStore;; + @Rule + public MongoConnectionFactory connectionFactory = new MongoConnectionFactory(); + + private DocumentNodeStore nodeStore; @Override protected NodeStore createNodeStore(BlobStore blobStore, File repository) throws IOException { - MongoConnection connection = MongoUtils.getConnection(); + MongoConnection connection = connectionFactory.getConnection(); Assume.assumeNotNull(connection); DocumentMK.Builder builder = new DocumentMK.Builder(); if (blobStore != null) { @@ -59,12 +64,12 @@ public class DocumentToExternalMigration @BeforeClass public static void checkMongoDbAvailable() { - Assume.assumeNotNull(MongoUtils.getConnection()); + Assume.assumeTrue(MongoUtils.isAvailable()); } @Override protected BlobStore createOldBlobStore(File repository) { - MongoConnection connection = MongoUtils.getConnection(); + MongoConnection connection = connectionFactory.getConnection(); return new MongoBlobStore(connection.getDB()); } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractDocumentStoreTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractDocumentStoreTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractDocumentStoreTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractDocumentStoreTest.java Wed Oct 28 09:51:11 2015 @@ -68,6 +68,7 @@ public abstract class AbstractDocumentSt LOG.info(removeMe.size() + " documents removed in " + elapsed + "ms (" + rate + "/ms)"); } } + ds.dispose(); dsf.dispose(); } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMongoConnectionTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMongoConnectionTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMongoConnectionTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMongoConnectionTest.java Wed Oct 28 09:51:11 2015 @@ -24,6 +24,7 @@ import org.junit.After; import org.junit.Assume; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Rule; /** * Base class for test cases that need a {@link MongoConnection} @@ -32,17 +33,20 @@ import org.junit.BeforeClass; */ public abstract class AbstractMongoConnectionTest extends DocumentMKTestBase { + @Rule + public MongoConnectionFactory connectionFactory = new MongoConnectionFactory(); + protected MongoConnection mongoConnection; protected DocumentMK mk; @BeforeClass public static void checkMongoDbAvailable() { - Assume.assumeNotNull(MongoUtils.getConnection()); + Assume.assumeTrue(MongoUtils.isAvailable()); } @Before public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); Revision.setClock(getTestClock()); mk = newBuilder(mongoConnection.getDB()).open(); @@ -59,11 +63,7 @@ public abstract class AbstractMongoConne @After public void tearDownConnection() throws Exception { mk.dispose(); - // the db might already be closed - mongoConnection.close(); - mongoConnection = MongoUtils.getConnection(); - MongoUtils.dropCollections(mongoConnection.getDB()); - mongoConnection.close(); + MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); Revision.resetClockToDefault(); } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMultiDocumentStoreTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMultiDocumentStoreTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMultiDocumentStoreTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMultiDocumentStoreTest.java Wed Oct 28 09:51:11 2015 @@ -30,6 +30,12 @@ public abstract class AbstractMultiDocum this.ds2 = dsf.createDocumentStore(2); } + @Override + public void cleanUp() throws Exception { + super.cleanUp(); + ds2.dispose(); + } + @Parameterized.Parameters public static Collection<Object[]> fixtures() { return fixtures(true); Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobTest.java Wed Oct 28 09:51:11 2015 @@ -43,6 +43,9 @@ public class BlobTest { @Rule public DocumentMKBuilderProvider builderProvider = new DocumentMKBuilderProvider(); + + @Rule + public MongoConnectionFactory connectionFactory = new MongoConnectionFactory(); private static final Logger LOG = LoggerFactory.getLogger(RandomizedClusterTest.class); @@ -55,12 +58,12 @@ public class BlobTest { private static final int DOCUMENT_COUNT = 10; DB openMongoConnection() { - return MONGO_DB ? MongoUtils.getConnection().getDB() : null; + return MONGO_DB ? connectionFactory.getConnection().getDB() : null; } void dropCollections() { if (MONGO_DB) { - MongoUtils.dropCollections(MongoUtils.getConnection().getDB()); + MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); } } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyTest.java Wed Oct 28 09:51:11 2015 @@ -46,7 +46,7 @@ public class CacheConsistencyTest extend @Before @Override public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); DB db = mongoConnection.getDB(); MongoUtils.dropCollections(db); DocumentMK.Builder builder = new DocumentMK.Builder() Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterJoinTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterJoinTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterJoinTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterJoinTest.java Wed Oct 28 09:51:11 2015 @@ -37,7 +37,7 @@ public class ClusterJoinTest extends Abs // because it started after the commit on the first DocumentMK DocumentMK mk2 = new DocumentMK.Builder(). setAsyncDelay(0). - setMongoDB(MongoUtils.getConnection().getDB()). + setMongoDB(connectionFactory.getConnection().getDB()). open(); try { Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterTest.java Wed Oct 28 09:51:11 2015 @@ -40,7 +40,6 @@ import org.apache.jackrabbit.oak.commons import org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore; import org.apache.jackrabbit.oak.spi.blob.BlobStore; import org.apache.jackrabbit.oak.spi.blob.MemoryBlobStore; -import org.apache.jackrabbit.oak.spi.commit.CommitHook; import org.apache.jackrabbit.oak.spi.commit.CommitInfo; import org.apache.jackrabbit.oak.spi.commit.EmptyHook; import org.apache.jackrabbit.oak.spi.commit.Observer; @@ -53,6 +52,7 @@ import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; /** @@ -60,6 +60,9 @@ import org.junit.Test; */ public class ClusterTest { + @Rule + public MongoConnectionFactory connectionFactory = new MongoConnectionFactory(); + private static final boolean MONGO_DB = false; // private static final boolean MONGO_DB = true; @@ -404,7 +407,7 @@ public class ClusterTest { } mks.clear(); if (MONGO_DB) { - DB db = MongoUtils.getConnection().getDB(); + DB db = connectionFactory.getConnection().getDB(); MongoUtils.dropCollections(db); } } @@ -420,7 +423,7 @@ public class ClusterTest { private DocumentMK createMK(int clusterId, int asyncDelay) { if (MONGO_DB) { - DB db = MongoUtils.getConnection().getDB(); + DB db = connectionFactory.getConnection().getDB(); return register(new DocumentMK.Builder().setMongoDB(db) .setClusterId(clusterId).setAsyncDelay(asyncDelay).open()); } else { Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentDiscoveryLiteServiceTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentDiscoveryLiteServiceTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentDiscoveryLiteServiceTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentDiscoveryLiteServiceTest.java Wed Oct 28 09:51:11 2015 @@ -84,6 +84,9 @@ import junitx.util.PrivateAccessor; */ public class DocumentDiscoveryLiteServiceTest { + @Rule + public MongoConnectionFactory connectionFactory = new MongoConnectionFactory(); + /** * container for what should represent an instance, but is not a complete * one, hence 'simplified'. it contains most importantly the @@ -1060,7 +1063,7 @@ public class DocumentDiscoveryLiteServic } mks.clear(); if (MONGO_DB) { - MongoConnection connection = MongoUtils.getConnection(); + MongoConnection connection = connectionFactory.getConnection(); if (connection != null) { DB db = connection.getDB(); if (db != null) { @@ -1072,7 +1075,7 @@ public class DocumentDiscoveryLiteServic private DocumentMK createMK(int clusterId, int asyncDelay) { if (MONGO_DB) { - DB db = MongoUtils.getConnection().getDB(); + DB db = connectionFactory.getConnection().getDB(); return register(new DocumentMK.Builder().setMongoDB(db).setLeaseCheck(false).setClusterId(clusterId) .setAsyncDelay(asyncDelay).open()); } else { Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentStoreFixture.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentStoreFixture.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentStoreFixture.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentStoreFixture.java Wed Oct 28 09:51:11 2015 @@ -16,6 +16,8 @@ */ package org.apache.jackrabbit.oak.plugins.document; +import java.util.List; + import javax.sql.DataSource; import org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore; @@ -28,6 +30,7 @@ import org.apache.jackrabbit.oak.plugins import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.Lists; import com.mongodb.BasicDBObject; import com.mongodb.DB; @@ -157,23 +160,12 @@ public abstract class DocumentStoreFixtu public DataSource getRDBDataSource() { return dataSource; } - - @Override - public void dispose() { - if (this.store1 != null) { - this.store1.dispose(); - this.store1 = null; - } - if (this.store2 != null) { - this.store2.dispose(); - this.store2 = null; - } - } } public static class MongoFixture extends DocumentStoreFixture { public static final String DEFAULT_URI = "mongodb://localhost:27017/oak-test"; private String uri; + private List<MongoConnection> connections = Lists.newArrayList(); public MongoFixture() { this(DEFAULT_URI); @@ -192,6 +184,7 @@ public abstract class DocumentStoreFixtu public DocumentStore createDocumentStore(int clusterId) { try { MongoConnection connection = new MongoConnection(uri); + connections.add(connection); DB db = connection.getDB(); MongoUtils.dropCollections(db); return new MongoDocumentStore(db, new DocumentMK.Builder().setClusterId(clusterId)); @@ -204,7 +197,11 @@ public abstract class DocumentStoreFixtu public boolean isAvailable() { try { MongoConnection connection = new MongoConnection(uri); - connection.getDB().command(new BasicDBObject("ping", 1)); + try { + connection.getDB().command(new BasicDBObject("ping", 1)); + } finally { + connection.close(); + } return true; } catch (Exception e) { return false; @@ -215,9 +212,17 @@ public abstract class DocumentStoreFixtu public void dispose() { try { MongoConnection connection = new MongoConnection(uri); - connection.getDB().dropDatabase(); + try { + connection.getDB().dropDatabase(); + } finally { + connection.close(); + } } catch (Exception ignore) { } + for (MongoConnection c : connections) { + c.close(); + } + connections.clear(); } } } Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoConnectionFactory.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoConnectionFactory.java?rev=1710972&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoConnectionFactory.java (added) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoConnectionFactory.java Wed Oct 28 09:51:11 2015 @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.oak.plugins.document; + +import java.util.List; + +import javax.annotation.CheckForNull; + +import com.google.common.collect.Lists; + +import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; +import org.junit.rules.ExternalResource; + +public class MongoConnectionFactory extends ExternalResource { + + private final List<MongoConnection> connections = Lists.newArrayList(); + + @CheckForNull + public MongoConnection getConnection() { + MongoConnection c = MongoUtils.getConnection(); + if (c != null) { + connections.add(c); + } + return c; + } + + @CheckForNull + public MongoConnection getConnection(String dbName) { + MongoConnection c = MongoUtils.getConnection(dbName); + if (c != null) { + connections.add(c); + } + return c; + } + + @Override + protected void after() { + for (MongoConnection c : connections) { + c.close(); + } + } +} Propchange: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoConnectionFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoDocumentStoreTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoDocumentStoreTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoDocumentStoreTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoDocumentStoreTest.java Wed Oct 28 09:51:11 2015 @@ -35,6 +35,7 @@ import org.apache.jackrabbit.oak.plugins import org.junit.After; import org.junit.Before; import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,6 +51,9 @@ public class MongoDocumentStoreTest { private static final Logger LOG = LoggerFactory.getLogger(MongoDocumentStoreTest.class); + @Rule + public MongoConnectionFactory connectionFactory = new MongoConnectionFactory(); + // private static final boolean MONGO_DB = true; // private static final int NODE_COUNT = 2000; @@ -58,14 +62,14 @@ public class MongoDocumentStoreTest { DocumentStore openDocumentStore() { if (MONGO_DB) { - return new MongoDocumentStore(MongoUtils.getConnection().getDB(), new DocumentMK.Builder()); + return new MongoDocumentStore(connectionFactory.getConnection().getDB(), new DocumentMK.Builder()); } return new MemoryDocumentStore(); } void dropCollections() { if (MONGO_DB) { - MongoUtils.dropCollections(MongoUtils.getConnection().getDB()); + MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); } } @@ -245,7 +249,7 @@ public class MongoDocumentStoreTest { private void doInsert(int n, boolean batch) throws Exception { dropCollections(); - DBCollection collection = MongoUtils.getConnection().getDB().getCollection("batchInsertTest"); + DBCollection collection = connectionFactory.getConnection().getDB().getCollection("batchInsertTest"); DBObject index = new BasicDBObject(); index.put("_path", 1L); DBObject options = new BasicDBObject(); Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoUtils.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoUtils.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoUtils.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoUtils.java Wed Oct 28 09:51:11 2015 @@ -119,4 +119,18 @@ public class MongoUtils { } } + /** + * @return true if MongoDB is available, false otherwise. + */ + public static boolean isAvailable() { + MongoConnection c = getConnection(); + try { + return c != null; + } finally { + if (c != null) { + c.close(); + } + } + } + } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/RandomizedClusterTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/RandomizedClusterTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/RandomizedClusterTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/RandomizedClusterTest.java Wed Oct 28 09:51:11 2015 @@ -32,6 +32,7 @@ import org.apache.jackrabbit.oak.commons import org.apache.jackrabbit.oak.commons.json.JsopBuilder; import org.apache.jackrabbit.oak.commons.json.JsopTokenizer; import org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore; +import org.junit.Rule; import org.junit.Test; import com.mongodb.DB; @@ -46,6 +47,9 @@ public class RandomizedClusterTest { private static final int MK_COUNT = 2; + @Rule + public MongoConnectionFactory connectionFactory = new MongoConnectionFactory(); + private MemoryDocumentStore ds; private MemoryBlobStore bs; @@ -373,7 +377,7 @@ public class RandomizedClusterTest { DocumentMK.Builder builder = new DocumentMK.Builder(); builder.setAsyncDelay(0); if (MONGO_DB) { - DB db = MongoUtils.getConnection().getDB(); + DB db = connectionFactory.getConnection().getDB(); MongoUtils.dropCollections(db); builder.setMongoDB(db); } else { Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/SimpleTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/SimpleTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/SimpleTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/SimpleTest.java Wed Oct 28 09:51:11 2015 @@ -43,7 +43,10 @@ public class SimpleTest { @Rule public DocumentMKBuilderProvider builderProvider = new DocumentMKBuilderProvider(); - + + @Rule + public MongoConnectionFactory connectionFactory = new MongoConnectionFactory(); + private static final boolean MONGO_DB = false; // private static final boolean MONGO_DB = true; @@ -434,7 +437,7 @@ public class SimpleTest { DocumentMK.Builder builder = builderProvider.newBuilder(); if (MONGO_DB) { - DB db = MongoUtils.getConnection().getDB(); + DB db = connectionFactory.getConnection().getDB(); MongoUtils.dropCollections(db); builder.setMongoDB(db); } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/MongoBlobStoreTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/MongoBlobStoreTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/MongoBlobStoreTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/MongoBlobStoreTest.java Wed Oct 28 09:51:11 2015 @@ -34,7 +34,7 @@ public class MongoBlobStoreTest extends @BeforeClass public static void checkMongoDbAvailable() { - Assume.assumeNotNull(MongoUtils.getConnection()); + Assume.assumeTrue(MongoUtils.isAvailable()); } @Before @@ -53,6 +53,7 @@ public class MongoBlobStoreTest extends @Override public void tearDown() throws Exception { MongoUtils.dropCollections(mongoConnection.getDB()); + mongoConnection.close(); super.tearDown(); } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudGetLengthTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudGetLengthTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudGetLengthTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudGetLengthTest.java Wed Oct 28 09:51:11 2015 @@ -41,7 +41,7 @@ public class DocumentMKCloudGetLengthTes @Override @Before public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); mk = new DocumentMK.Builder().setMongoDB(mongoConnection.getDB()) .setBlobStore(CloudStoreUtils.getBlobStore()).open(); @@ -52,10 +52,6 @@ public class DocumentMKCloudGetLengthTes public void tearDownConnection() throws Exception { ((CloudBlobStore) mk.getNodeStore().getBlobStore()).deleteBucket(); mk.dispose(); - // the db might already be closed - mongoConnection.close(); - mongoConnection = MongoUtils.getConnection(); - MongoUtils.dropCollections(mongoConnection.getDB()); - mongoConnection.close(); + MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); } } \ No newline at end of file Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudReadTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudReadTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudReadTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudReadTest.java Wed Oct 28 09:51:11 2015 @@ -42,7 +42,7 @@ public class DocumentMKCloudReadTest ext @Override @Before public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); mk = new DocumentMK.Builder().setMongoDB(mongoConnection.getDB()) .setBlobStore(CloudStoreUtils.getBlobStore()).open(); @@ -53,10 +53,6 @@ public class DocumentMKCloudReadTest ext public void tearDownConnection() throws Exception { ((CloudBlobStore) mk.getNodeStore().getBlobStore()).deleteBucket(); mk.dispose(); - // the db might already be closed - mongoConnection.close(); - mongoConnection = MongoUtils.getConnection(); - MongoUtils.dropCollections(mongoConnection.getDB()); - mongoConnection.close(); + MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); } } \ No newline at end of file Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudWriteTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudWriteTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudWriteTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/DocumentMKCloudWriteTest.java Wed Oct 28 09:51:11 2015 @@ -42,7 +42,7 @@ public class DocumentMKCloudWriteTest ex @Override @Before public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); mk = new DocumentMK.Builder().setMongoDB(mongoConnection.getDB()) .setBlobStore(CloudStoreUtils.getBlobStore()).open(); @@ -53,10 +53,6 @@ public class DocumentMKCloudWriteTest ex public void tearDownConnection() throws Exception { ((CloudBlobStore) mk.getNodeStore().getBlobStore()).deleteBucket(); mk.dispose(); - // the db might already be closed - mongoConnection.close(); - mongoConnection = MongoUtils.getConnection(); - MongoUtils.dropCollections(mongoConnection.getDB()); - mongoConnection.close(); + MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); } } \ No newline at end of file Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/MongoCloudBlobGCTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/MongoCloudBlobGCTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/MongoCloudBlobGCTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/MongoCloudBlobGCTest.java Wed Oct 28 09:51:11 2015 @@ -42,7 +42,7 @@ public class MongoCloudBlobGCTest extend @Before @Override public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); mk = new DocumentMK.Builder().setMongoDB(mongoConnection.getDB()) .setBlobStore(CloudStoreUtils.getBlobStore()).open(); @@ -53,10 +53,6 @@ public class MongoCloudBlobGCTest extend public void tearDownConnection() throws Exception { ((CloudBlobStore) mk.getNodeStore().getBlobStore()).deleteBucket(); mk.dispose(); - // the db might already be closed - mongoConnection.close(); - mongoConnection = MongoUtils.getConnection(); - MongoUtils.dropCollections(mongoConnection.getDB()); - mongoConnection.close(); + MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); } } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreGetLengthTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreGetLengthTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreGetLengthTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreGetLengthTest.java Wed Oct 28 09:51:11 2015 @@ -44,7 +44,7 @@ public class DocumentMKDataStoreGetLengt @Override @Before public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); mk = new DocumentMK.Builder().setMongoDB(mongoConnection.getDB()) .setBlobStore(DataStoreUtils.getBlobStore()).open(); @@ -55,10 +55,6 @@ public class DocumentMKDataStoreGetLengt public void tearDownConnection() throws Exception { FileUtils.deleteDirectory(new File(DataStoreUtils.getHomeDir())); mk.dispose(); - // the db might already be closed - mongoConnection.close(); - mongoConnection = MongoUtils.getConnection(); - MongoUtils.dropCollections(mongoConnection.getDB()); - mongoConnection.close(); + MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); } } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreReadTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreReadTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreReadTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreReadTest.java Wed Oct 28 09:51:11 2015 @@ -45,7 +45,7 @@ public class DocumentMKDataStoreReadTest @Override @Before public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); mk = new DocumentMK.Builder().setMongoDB(mongoConnection.getDB()) .setBlobStore(DataStoreUtils.getBlobStore()).open(); @@ -56,10 +56,6 @@ public class DocumentMKDataStoreReadTest public void tearDownConnection() throws Exception { FileUtils.deleteDirectory(new File(DataStoreUtils.getHomeDir())); mk.dispose(); - // the db might already be closed - mongoConnection.close(); - mongoConnection = MongoUtils.getConnection(); - MongoUtils.dropCollections(mongoConnection.getDB()); - mongoConnection.close(); + MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); } } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreWriteTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreWriteTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreWriteTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/DocumentMKDataStoreWriteTest.java Wed Oct 28 09:51:11 2015 @@ -45,7 +45,7 @@ public class DocumentMKDataStoreWriteTes @Override @Before public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); mk = new DocumentMK.Builder().setMongoDB(mongoConnection.getDB()) .setBlobStore(DataStoreUtils.getBlobStore()).open(); @@ -56,10 +56,6 @@ public class DocumentMKDataStoreWriteTes public void tearDownConnection() throws Exception { FileUtils.deleteDirectory(new File(DataStoreUtils.getHomeDir())); mk.dispose(); - // the db might already be closed - mongoConnection.close(); - mongoConnection = MongoUtils.getConnection(); - MongoUtils.dropCollections(mongoConnection.getDB()); - mongoConnection.close(); + MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); } } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/MongoDataStoreBlobGCTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/MongoDataStoreBlobGCTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/MongoDataStoreBlobGCTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/MongoDataStoreBlobGCTest.java Wed Oct 28 09:51:11 2015 @@ -51,7 +51,7 @@ public class MongoDataStoreBlobGCTest ex @Override public void setUpConnection() throws Exception { startDate = new Date(); - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); blobStore = DataStoreUtils.getBlobStore(); mk = new DocumentMK.Builder().clock(getTestClock()).setMongoDB(mongoConnection.getDB()) @@ -64,10 +64,6 @@ public class MongoDataStoreBlobGCTest ex DataStoreUtils.cleanup(blobStore.getDataStore(), startDate); FileUtils.deleteDirectory(new File(DataStoreUtils.getHomeDir())); mk.dispose(); - // the db might already be closed - mongoConnection.close(); - mongoConnection = MongoUtils.getConnection(); - MongoUtils.dropCollections(mongoConnection.getDB()); - mongoConnection.close(); + MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); } } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/impl/DocumentMKConcurrentAddTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/impl/DocumentMKConcurrentAddTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/impl/DocumentMKConcurrentAddTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/impl/DocumentMKConcurrentAddTest.java Wed Oct 28 09:51:11 2015 @@ -27,7 +27,6 @@ import java.util.concurrent.TimeUnit; import org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; -import org.apache.jackrabbit.oak.plugins.document.MongoUtils; import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; import org.junit.After; import org.junit.Test; @@ -45,7 +44,7 @@ public class DocumentMKConcurrentAddTest private List<DocumentMK> mks = new ArrayList<DocumentMK>(); private DocumentMK createMicroKernel() throws Exception { - MongoConnection connection = MongoUtils.getConnection(); + MongoConnection connection = connectionFactory.getConnection(); DB mongoDB = connection.getDB(); return new DocumentMK.Builder().memoryCacheSize(CACHE_SIZE).setMongoDB(mongoDB).open(); } Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheConsistencyIT.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheConsistencyIT.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheConsistencyIT.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheConsistencyIT.java Wed Oct 28 09:51:11 2015 @@ -49,7 +49,7 @@ public class CacheConsistencyIT extends @Before @Override public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); DB db = mongoConnection.getDB(); MongoUtils.dropCollections(db); DocumentMK.Builder builder = new DocumentMK.Builder() Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java Wed Oct 28 09:51:11 2015 @@ -27,7 +27,6 @@ import org.apache.jackrabbit.oak.plugins import org.apache.jackrabbit.oak.plugins.document.Collection; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; -import org.apache.jackrabbit.oak.plugins.document.MongoUtils; import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; import org.apache.jackrabbit.oak.plugins.document.util.Utils; import org.apache.jackrabbit.oak.spi.commit.CommitInfo; @@ -263,8 +262,8 @@ public class CacheInvalidationIT extends } } - private static DocumentNodeStore createNS(int clusterId) throws Exception { - MongoConnection mc = MongoUtils.getConnection(); + private DocumentNodeStore createNS(int clusterId) throws Exception { + MongoConnection mc = connectionFactory.getConnection(); return new DocumentMK.Builder() .setMongoDB(mc.getDB()) .setClusterId(clusterId) Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ClusterConflictTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ClusterConflictTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ClusterConflictTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ClusterConflictTest.java Wed Oct 28 09:51:11 2015 @@ -25,7 +25,6 @@ import org.apache.jackrabbit.oak.commons import org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; -import org.apache.jackrabbit.oak.plugins.document.MongoUtils; import org.apache.jackrabbit.oak.spi.commit.CommitHook; import org.apache.jackrabbit.oak.spi.commit.CommitInfo; import org.apache.jackrabbit.oak.spi.commit.EmptyHook; @@ -44,7 +43,7 @@ public class ClusterConflictTest extends @Override public void setUpConnection() throws Exception { super.setUpConnection(); - ns2 = newBuilder(MongoUtils.getConnection().getDB()).getNodeStore(); + ns2 = newBuilder(connectionFactory.getConnection().getDB()).getNodeStore(); } @Override Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CollisionMarkerTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CollisionMarkerTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CollisionMarkerTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CollisionMarkerTest.java Wed Oct 28 09:51:11 2015 @@ -16,6 +16,8 @@ */ package org.apache.jackrabbit.oak.plugins.document.mongo; +import com.mongodb.DB; + import org.apache.jackrabbit.oak.api.CommitFailedException; import org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; @@ -23,7 +25,6 @@ import org.apache.jackrabbit.oak.plugins import org.apache.jackrabbit.oak.plugins.document.MongoUtils; import org.apache.jackrabbit.oak.plugins.document.NodeDocument; import org.apache.jackrabbit.oak.plugins.document.Revision; -import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; import org.apache.jackrabbit.oak.plugins.document.util.Utils; import org.apache.jackrabbit.oak.spi.commit.CommitInfo; import org.apache.jackrabbit.oak.spi.commit.EmptyHook; @@ -47,9 +48,9 @@ public class CollisionMarkerTest extends @Override public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); - mk = newDocumentMK(mongoConnection); + mk = newDocumentMK(mongoConnection.getDB()); ns1 = mk.getNodeStore(); } @@ -68,7 +69,7 @@ public class CollisionMarkerTest extends ns1.runBackgroundOperations(); // initialize second node store after background ops // on ns1. this makes sure ns2 sees all changes done so far - ns2 = newDocumentMK(MongoUtils.getConnection()).getNodeStore(); + ns2 = newDocumentMK(connectionFactory.getConnection().getDB()).getNodeStore(); b1 = ns1.getRoot().builder(); b1.child("node").child("foo"); @@ -92,10 +93,10 @@ public class CollisionMarkerTest extends " committed revision", root.getValueMap(COLLISIONS).containsKey(head)); } - private static DocumentMK newDocumentMK(MongoConnection c) { + private static DocumentMK newDocumentMK(DB db) { DocumentMK mk = new DocumentMK.Builder().setAsyncDelay(0) .setLeaseCheck(false) - .setMongoDB(c.getDB()).open(); + .setMongoDB(db).open(); // do not retry on conflicts mk.getNodeStore().setMaxBackOffMillis(0); return mk; Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/JournalIT.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/JournalIT.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/JournalIT.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/JournalIT.java Wed Oct 28 09:51:11 2015 @@ -28,15 +28,15 @@ import org.apache.jackrabbit.oak.plugins import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; import org.apache.jackrabbit.oak.plugins.document.DocumentStore; import org.apache.jackrabbit.oak.plugins.document.JournalGarbageCollector; +import org.apache.jackrabbit.oak.plugins.document.MongoConnectionFactory; import org.apache.jackrabbit.oak.plugins.document.MongoUtils; import org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore; import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; import org.apache.jackrabbit.oak.spi.blob.MemoryBlobStore; import org.apache.jackrabbit.oak.stats.Clock; -import org.junit.After; import org.junit.Assume; -import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,17 +47,19 @@ public class JournalIT extends AbstractJ private static final Logger LOG = LoggerFactory.getLogger(JournalIT.class); + @Rule + public MongoConnectionFactory connectionFactory = new MongoConnectionFactory(); + @BeforeClass public static void checkMongoDbAvailable() { - Assume.assumeNotNull(MongoUtils.getConnection()); + Assume.assumeTrue(MongoUtils.isAvailable()); } @Override public void clear() { super.clear(); - MongoConnection mongoConnection = MongoUtils.getConnection(); + MongoConnection mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); - mongoConnection.close(); } @Test @@ -213,7 +215,7 @@ public class JournalIT extends AbstractJ } protected DocumentMK createMK(int clusterId, int asyncDelay) { - DB db = MongoUtils.getConnection().getDB(); + DB db = connectionFactory.getConnection().getDB(); builder = newDocumentMKBuilder(); return register(builder.setMongoDB(db) .setClusterId(clusterId).setAsyncDelay(asyncDelay).open()); Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java Wed Oct 28 09:51:11 2015 @@ -44,7 +44,7 @@ public class MongoDocumentStoreTest exte @Override public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); DocumentMK.Builder builder = new DocumentMK.Builder(); store = new TestStore(mongoConnection.getDB(), builder); Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/QueryHintTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/QueryHintTest.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/QueryHintTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/QueryHintTest.java Wed Oct 28 09:51:11 2015 @@ -27,23 +27,21 @@ import java.util.logging.LogRecord; import java.util.logging.Logger; import com.google.common.collect.Lists; + +import org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest; import org.apache.jackrabbit.oak.plugins.document.Collection; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; -import org.apache.jackrabbit.oak.plugins.document.MongoUtils; import org.apache.jackrabbit.oak.plugins.document.NodeDocument; -import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; import org.apache.jackrabbit.oak.plugins.document.util.Utils; import org.apache.jackrabbit.oak.stats.Clock; import org.junit.After; -import org.junit.Assume; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -public class QueryHintTest { +public class QueryHintTest extends AbstractMongoConnectionTest { final Logger TRACE_LOGGER = Logger.getLogger( "com.mongodb.TRACE" ); final TestHandler testHandler = new TestHandler(); @@ -51,19 +49,13 @@ public class QueryHintTest { private Clock clock; - @BeforeClass - public static void checkMongoDbAvailable() { - Assume.assumeNotNull(MongoUtils.getConnection()); - } - @Before public void prepareStores() throws Exception { clock = new Clock.Virtual(); - MongoConnection mc = MongoUtils.getConnection(); //TODO Temp mode to change the default setting so as to test it //If we retain this feature then need to have better config support for it System.setProperty("oak.mongo.maxDeltaForModTimeIdxSecs", "120"); - mongoDS = new MongoDocumentStore(mc.getDB(), new DocumentMK.Builder()); + mongoDS = new MongoDocumentStore(mongoConnection.getDB(), new DocumentMK.Builder()); mongoDS.setClock(clock); TRACE_LOGGER.addHandler(testHandler); TRACE_LOGGER.setLevel(Level.FINEST); Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java Wed Oct 28 09:51:11 2015 @@ -22,21 +22,16 @@ package org.apache.jackrabbit.oak.plugin import java.util.concurrent.TimeUnit; import com.mongodb.ReadPreference; + +import org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; -import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; -import org.apache.jackrabbit.oak.plugins.document.MongoUtils; import org.apache.jackrabbit.oak.plugins.document.Revision; -import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; import org.apache.jackrabbit.oak.plugins.document.util.Utils; import org.apache.jackrabbit.oak.spi.commit.CommitInfo; import org.apache.jackrabbit.oak.spi.commit.EmptyHook; import org.apache.jackrabbit.oak.spi.state.NodeBuilder; import org.apache.jackrabbit.oak.spi.state.NodeStore; import org.apache.jackrabbit.oak.stats.Clock; -import org.junit.After; -import org.junit.Assume; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES; @@ -45,10 +40,7 @@ import static org.apache.jackrabbit.oak. import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -public class ReadPreferenceIT { - - private DocumentNodeStore documentNodeStore; - private NodeStore nodeStore; +public class ReadPreferenceIT extends AbstractMongoConnectionTest { private MongoDocumentStore mongoDS; @@ -56,31 +48,18 @@ public class ReadPreferenceIT { private long replicationLag; - @BeforeClass - public static void checkMongoDbAvailable() { - Assume.assumeNotNull(MongoUtils.getConnection()); - } - - @Before - public void prepareStores() throws Exception { + @Override + public void setUpConnection() throws Exception { clock = new Clock.Virtual(); replicationLag = TimeUnit.SECONDS.toMillis(10); - MongoConnection mc = MongoUtils.getConnection(); - documentNodeStore = new DocumentMK.Builder() + mongoConnection = connectionFactory.getConnection(); + mk = new DocumentMK.Builder() .setMaxReplicationLag(replicationLag, TimeUnit.MILLISECONDS) - .setMongoDB(mc.getDB()) + .setMongoDB(mongoConnection.getDB()) .setClusterId(1) .setLeaseCheck(false) - .getNodeStore(); - mongoDS = (MongoDocumentStore) documentNodeStore.getDocumentStore(); - nodeStore = documentNodeStore; - } - - @After - public void clearDB() { - if (mongoDS != null) { - MongoUtils.dropCollections(mongoDS.getDBCollection(NODES).getDB()); - } + .open(); + mongoDS = (MongoDocumentStore) mk.getDocumentStore(); } @Test @@ -134,6 +113,7 @@ public class ReadPreferenceIT { ReadPreference testPref = ReadPreference.secondary(); mongoDS.getDBCollection(NODES).getDB().setReadPreference(testPref); + NodeStore nodeStore = mk.getNodeStore(); NodeBuilder b1 = nodeStore.getRoot().builder(); b1.child("x").child("y"); nodeStore.merge(b1, EmptyHook.INSTANCE, CommitInfo.EMPTY); Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/RetryReadIT.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/RetryReadIT.java?rev=1710972&r1=1710971&r2=1710972&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/RetryReadIT.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/RetryReadIT.java Wed Oct 28 09:51:11 2015 @@ -42,7 +42,7 @@ public class RetryReadIT extends Abstrac @Override public void setUpConnection() throws Exception { - mongoConnection = MongoUtils.getConnection(); + mongoConnection = connectionFactory.getConnection(); MongoUtils.dropCollections(mongoConnection.getDB()); DocumentMK.Builder builder = new DocumentMK.Builder(); builder.clock(getTestClock());