Repository: incubator-rya Updated Branches: refs/heads/master b71e892ac -> 2c1efd225
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2c1efd22/test/mongo/src/main/java/org/apache/rya/test/mongo/EmbeddedMongoSingleton.java ---------------------------------------------------------------------- diff --git a/test/mongo/src/main/java/org/apache/rya/test/mongo/EmbeddedMongoSingleton.java b/test/mongo/src/main/java/org/apache/rya/test/mongo/EmbeddedMongoSingleton.java new file mode 100644 index 0000000..e1e60dc --- /dev/null +++ b/test/mongo/src/main/java/org/apache/rya/test/mongo/EmbeddedMongoSingleton.java @@ -0,0 +1,90 @@ +/** + * 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.rya.test.mongo; + +import java.io.IOException; +import java.net.UnknownHostException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.mongodb.MongoClient; +import com.mongodb.MongoException; + +import de.flapdoodle.embed.mongo.config.IMongodConfig; + +/** + * To be used for tests. Creates a singleton {@link MongoClient} to be used + * throughout all of the MongoDB related tests. Without the singleton, the + * embedded mongo factory ends up orphaning processes, consuming resources. + */ +public class EmbeddedMongoSingleton { + + public static MongoClient getNewMongoClient() throws UnknownHostException, MongoException { + final MongoClient client = InstanceHolder.SINGLETON.factory.newMongoClient(); + + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + try { + client.close(); + } catch (final Throwable t) { + // logging frameworks will likely be shut down + t.printStackTrace(System.err); + } + } + }); + + return client; + } + + /** + * @return The singleton Mongo DB instance's server details. + */ + public static IMongodConfig getMongodConfig() { + return InstanceHolder.SINGLETON.mongodConfig; + } + + private EmbeddedMongoSingleton() { + // hiding implicit default constructor + } + + private enum InstanceHolder { + + SINGLETON; + + private final Logger log; + private IMongodConfig mongodConfig; + private EmbeddedMongoFactory factory; + + InstanceHolder() { + log = LoggerFactory.getLogger(EmbeddedMongoSingleton.class); + try { + factory = EmbeddedMongoFactory.newFactory(); + mongodConfig = factory.getMongoServerDetails(); + } catch (final IOException e) { + log.error("Unexpected error while starting mongo client", e); + } catch (final Throwable e) { + // catching throwable because failure to construct an enum + // instance will lead to another error being thrown downstream + log.error("Unexpected throwable while starting mongo client", e); + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2c1efd22/test/mongo/src/main/java/org/apache/rya/test/mongo/MongoITBase.java ---------------------------------------------------------------------- diff --git a/test/mongo/src/main/java/org/apache/rya/test/mongo/MongoITBase.java b/test/mongo/src/main/java/org/apache/rya/test/mongo/MongoITBase.java new file mode 100644 index 0000000..dbf43e9 --- /dev/null +++ b/test/mongo/src/main/java/org/apache/rya/test/mongo/MongoITBase.java @@ -0,0 +1,76 @@ +/** + * 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.rya.test.mongo; + +import java.net.UnknownHostException; + +import org.junit.Before; + +import com.mongodb.MongoClient; + +/** + * A base class that may be used when implementing Mongo DB tests that use the + * JUnit framework. + */ +public class MongoITBase { + + private MongoClient mongoClient = null; + + @Before + public void setupTest() throws Exception { + mongoClient = EmbeddedMongoSingleton.getNewMongoClient(); + + // Remove any DBs that were created by previous tests. + for(final String dbName : mongoClient.listDatabaseNames()) { + mongoClient.dropDatabase(dbName); + } + + // Let subclasses do more setup. + beforeTest(); + } + + /** + * Invoked before each test is run to allow tests to perform more setup. + */ + protected void beforeTest() throws Exception { + // Does nothing by default. + } + + /** + * @return A {@link MongoClient} that is connected to the embedded instance of Mongo DB. + */ + public MongoClient getMongoClient() { + return mongoClient; + } + + /** + * @return The embedded Mongo DB's hostname. + * @throws UnknownHostException The host couldn't be presented as a String. + */ + public String getMongoHostname() throws UnknownHostException { + return EmbeddedMongoSingleton.getMongodConfig().net().getServerAddress().getHostAddress(); + } + + /** + * @return The embedded Mongo DB's port. + */ + public int getMongoPort() { + return EmbeddedMongoSingleton.getMongodConfig().net().getPort(); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2c1efd22/test/pom.xml ---------------------------------------------------------------------- diff --git a/test/pom.xml b/test/pom.xml index 2517f6a..91385c1 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -36,6 +36,7 @@ <modules> <module>accumulo</module> <module>kafka</module> + <module>mongo</module> <module>rdf</module> </modules> </project>
