Search in sources :

Example 1 with MongoServer

use of de.bwaldvogel.mongo.MongoServer in project books by aidanwhiteley.

the class MongoJavaServerConfig method mongoClient.

@Override
@NonNull
public MongoClient mongoClient() {
    preProdWarnings.displayMongoJavaServerWarningMessage();
    MongoServer server = new MongoServer(new MemoryBackend());
    // bind on a random local port
    InetSocketAddress serverAddress = server.bind();
    return MongoClients.create("mongodb://" + serverAddress.getHostName() + ":" + serverAddress.getPort());
}
Also used : MemoryBackend(de.bwaldvogel.mongo.backend.memory.MemoryBackend) InetSocketAddress(java.net.InetSocketAddress) MongoServer(de.bwaldvogel.mongo.MongoServer) NonNull(com.mongodb.lang.NonNull)

Example 2 with MongoServer

use of de.bwaldvogel.mongo.MongoServer in project selenium_java by sergueik.

the class BasicMongoServerClient method main.

public static void main(String[] args) {
    // Creating a Mongo client
    server = new MongoServer(new MemoryBackend());
    // bind on a random local port
    InetSocketAddress serverAddress = server.bind();
    // Creating a Mongo client
    client = new MongoClient(new ServerAddress(serverAddress));
    // Creating Credentials
    MongoCredential credential;
    credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray());
    System.out.println("Connected to the database successfully");
    // Accessing the database
    database = client.getDatabase("dummy");
    System.out.println("Credentials ::" + credential);
    // Creating a collection
    try {
        database.createCollection(collectionName);
        System.err.println(String.format("Collection \"%s\" created successfully", collectionName));
    } catch (MongoCommandException e) {
        System.err.println(String.format("Collection \"%s\" already exists", collectionName));
    } catch (Exception e) {
        System.out.println(String.format("Exception creating collection \"%s\":", collectionName) + e.toString());
        throw new RuntimeException(e);
    }
    // Retrieving a collection
    collection = database.getCollection(collectionName);
    System.out.println(String.format("Collection \"%s\" selected successfully", collectionName));
    // Listing all collections in the database
    for (String name : database.listCollectionNames()) {
        System.out.println(name);
    }
    Document document = new Document("title", "MongoDB").append("id", 1).append("description", "database").append("likes", 100).append("url", "http://www.tutorialspoint.com/mongodb/").append("by", "tutorials point");
    collection.insertOne(document);
    System.out.println("Document inserted successfully");
    collection.updateOne(Filters.eq("id", 1), Updates.set("likes", 150));
    System.out.println("Document update successfully...");
    // Getting the iterable object
    FindIterable<Document> iterDoc = collection.find();
    int i = 1;
    // Getting the iterator
    Iterator<Document> it = iterDoc.iterator();
    while (it.hasNext()) {
        System.out.println(it.next());
        i++;
    }
    // Deleting the documents
    collection.deleteOne(Filters.eq("id", 1));
    System.out.println("Document deleted successfully...");
    // Dropping a Collection
    collection.drop();
    System.out.println(String.format("Collection \"%s\" dropped successfully", collectionName));
    client.close();
    server.shutdown();
}
Also used : MongoCredential(com.mongodb.MongoCredential) InetSocketAddress(java.net.InetSocketAddress) ServerAddress(com.mongodb.ServerAddress) MongoServer(de.bwaldvogel.mongo.MongoServer) Document(org.bson.Document) MongoCommandException(com.mongodb.MongoCommandException) MongoClient(com.mongodb.MongoClient) MongoCommandException(com.mongodb.MongoCommandException) MemoryBackend(de.bwaldvogel.mongo.backend.memory.MemoryBackend)

Example 3 with MongoServer

use of de.bwaldvogel.mongo.MongoServer in project immutables by immutables.

the class MongoInstance method create.

static MongoInstance create() {
    final String uri = System.getProperty("mongo");
    if (uri != null) {
        // connect to remote mongo server
        return new MongoInstance(MongoClients.create(uri));
    }
    final MongoServer server = new MongoServer(new MemoryBackend());
    final InetSocketAddress address = server.bind();
    final Closer closer = Closer.create();
    closer.register(server::shutdownNow);
    final MongoClient client = MongoClients.create(String.format("mongodb://127.0.0.1:%d", address.getPort()));
    return new MongoInstance(client, closer);
}
Also used : Closer(com.google.common.io.Closer) MongoClient(com.mongodb.reactivestreams.client.MongoClient) MemoryBackend(de.bwaldvogel.mongo.backend.memory.MemoryBackend) InetSocketAddress(java.net.InetSocketAddress) MongoServer(de.bwaldvogel.mongo.MongoServer)

Example 4 with MongoServer

use of de.bwaldvogel.mongo.MongoServer in project immutables by immutables.

the class MongoContext method create.

public static MongoContext create() {
    final String uri = System.getProperty("mongo");
    final Closer closer = Closer.create();
    if (uri != null) {
        // remote mongo server
        return new MongoContext(new MongoClient(new MongoClientURI(uri)), closer);
    }
    final MongoServer server = new MongoServer(new MemoryBackend());
    closer.register(new Closeable() {

        @Override
        public void close() throws IOException {
            server.shutdownNow();
        }
    });
    final MongoClient client = new MongoClient(new ServerAddress(server.bind()));
    final MongoContext context = new MongoContext(client, closer);
    return context;
}
Also used : Closer(com.google.common.io.Closer) MongoClient(com.mongodb.MongoClient) MemoryBackend(de.bwaldvogel.mongo.backend.memory.MemoryBackend) MongoClientURI(com.mongodb.MongoClientURI) Closeable(java.io.Closeable) ServerAddress(com.mongodb.ServerAddress) MongoServer(de.bwaldvogel.mongo.MongoServer) IOException(java.io.IOException)

Example 5 with MongoServer

use of de.bwaldvogel.mongo.MongoServer in project presto by prestodb.

the class TestMongoPlugin method start.

@BeforeClass
public void start() {
    server = new MongoServer(new MemoryBackend());
    InetSocketAddress address = server.bind();
    seed = String.format("%s:%d", address.getHostString(), address.getPort());
}
Also used : MemoryBackend(de.bwaldvogel.mongo.backend.memory.MemoryBackend) InetSocketAddress(java.net.InetSocketAddress) MongoServer(de.bwaldvogel.mongo.MongoServer) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

MongoServer (de.bwaldvogel.mongo.MongoServer)5 MemoryBackend (de.bwaldvogel.mongo.backend.memory.MemoryBackend)5 InetSocketAddress (java.net.InetSocketAddress)4 Closer (com.google.common.io.Closer)2 MongoClient (com.mongodb.MongoClient)2 ServerAddress (com.mongodb.ServerAddress)2 MongoClientURI (com.mongodb.MongoClientURI)1 MongoCommandException (com.mongodb.MongoCommandException)1 MongoCredential (com.mongodb.MongoCredential)1 NonNull (com.mongodb.lang.NonNull)1 MongoClient (com.mongodb.reactivestreams.client.MongoClient)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 Document (org.bson.Document)1 BeforeClass (org.testng.annotations.BeforeClass)1