From 61fa5ff781f8093082379d3f2ac29f42410c5205 Mon Sep 17 00:00:00 2001 From: eugenp Date: Thu, 19 Jan 2017 23:12:01 +0200 Subject: [PATCH] minor cleanup --- .../main/java/com/baeldung/MongoExample.java | 84 +++++++++---------- .../java/com/baeldung/AppIntegrationTest.java | 72 ++++++++++++++++ .../src/test/java/com/baeldung/AppTest.java | 72 ---------------- 3 files changed, 114 insertions(+), 114 deletions(-) create mode 100644 java-mongodb/src/test/java/com/baeldung/AppIntegrationTest.java delete mode 100644 java-mongodb/src/test/java/com/baeldung/AppTest.java diff --git a/java-mongodb/src/main/java/com/baeldung/MongoExample.java b/java-mongodb/src/main/java/com/baeldung/MongoExample.java index fbef17a15f..9af1e1f6a4 100644 --- a/java-mongodb/src/main/java/com/baeldung/MongoExample.java +++ b/java-mongodb/src/main/java/com/baeldung/MongoExample.java @@ -7,47 +7,47 @@ import com.mongodb.DBCursor; import com.mongodb.MongoClient; public class MongoExample { - public static void main( String[] args ) { - - MongoClient mongoClient = new MongoClient("localhost", 27017); - - DB database = mongoClient.getDB("myMongoDb"); - - // print existing databases - mongoClient.getDatabaseNames().forEach(System.out::println); - - database.createCollection("customers", null); - - // print all collections in customers database - database.getCollectionNames().forEach(System.out::println); - - // create data - DBCollection collection = database.getCollection("customers"); - BasicDBObject document = new BasicDBObject(); - document.put("name", "Shubham"); - document.put("company", "Baeldung"); - collection.insert(document); - - // update data - BasicDBObject query = new BasicDBObject(); - query.put("name", "Shubham"); - BasicDBObject newDocument = new BasicDBObject(); - newDocument.put("name", "John"); - BasicDBObject updateObject = new BasicDBObject(); - updateObject.put("$set", newDocument); - collection.update(query, updateObject); - - // read data - BasicDBObject searchQuery = new BasicDBObject(); - searchQuery.put("name", "John"); - DBCursor cursor = collection.find(searchQuery); - while (cursor.hasNext()) { - System.out.println(cursor.next()); - } - - // delete data - BasicDBObject deleteQuery = new BasicDBObject(); - deleteQuery.put("name", "John"); - collection.remove(deleteQuery); + public static void main(String[] args) { + + MongoClient mongoClient = new MongoClient("localhost", 27017); + + DB database = mongoClient.getDB("myMongoDb"); + + // print existing databases + mongoClient.getDatabaseNames().forEach(System.out::println); + + database.createCollection("customers", null); + + // print all collections in customers database + database.getCollectionNames().forEach(System.out::println); + + // create data + DBCollection collection = database.getCollection("customers"); + BasicDBObject document = new BasicDBObject(); + document.put("name", "Shubham"); + document.put("company", "Baeldung"); + collection.insert(document); + + // update data + BasicDBObject query = new BasicDBObject(); + query.put("name", "Shubham"); + BasicDBObject newDocument = new BasicDBObject(); + newDocument.put("name", "John"); + BasicDBObject updateObject = new BasicDBObject(); + updateObject.put("$set", newDocument); + collection.update(query, updateObject); + + // read data + BasicDBObject searchQuery = new BasicDBObject(); + searchQuery.put("name", "John"); + DBCursor cursor = collection.find(searchQuery); + while (cursor.hasNext()) { + System.out.println(cursor.next()); + } + + // delete data + BasicDBObject deleteQuery = new BasicDBObject(); + deleteQuery.put("name", "John"); + collection.remove(deleteQuery); } } diff --git a/java-mongodb/src/test/java/com/baeldung/AppIntegrationTest.java b/java-mongodb/src/test/java/com/baeldung/AppIntegrationTest.java new file mode 100644 index 0000000000..94d1a01492 --- /dev/null +++ b/java-mongodb/src/test/java/com/baeldung/AppIntegrationTest.java @@ -0,0 +1,72 @@ +package com.baeldung; + +import static org.junit.Assert.assertEquals; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.mongodb.BasicDBObject; +import com.mongodb.DB; +import com.mongodb.DBCollection; +import com.mongodb.DBCursor; +import com.mongodb.Mongo; + +import de.flapdoodle.embedmongo.MongoDBRuntime; +import de.flapdoodle.embedmongo.MongodExecutable; +import de.flapdoodle.embedmongo.MongodProcess; +import de.flapdoodle.embedmongo.config.MongodConfig; +import de.flapdoodle.embedmongo.distribution.Version; +import de.flapdoodle.embedmongo.runtime.Network; + +public class AppIntegrationTest { + + private static final String DB_NAME = "myMongoDb"; + private MongodExecutable mongodExe; + private MongodProcess mongod; + private Mongo mongo; + private DB db; + private DBCollection collection; + + @Before + public void setup() throws Exception { + // Creating Mongodbruntime instance + MongoDBRuntime runtime = MongoDBRuntime.getDefaultInstance(); + + // Creating MongodbExecutable + mongodExe = runtime.prepare(new MongodConfig(Version.V2_0_1, 12345, Network.localhostIsIPv6())); + + // Starting Mongodb + mongod = mongodExe.start(); + mongo = new Mongo("localhost", 12345); + + // Creating DB + db = mongo.getDB(DB_NAME); + + // Creating collection Object and adding values + collection = db.getCollection("customers"); + } + + @After + public void teardown() throws Exception { + mongod.stop(); + mongodExe.cleanup(); + } + + @Test + public void testAddressPersistance() { + BasicDBObject contact = new BasicDBObject(); + contact.put("name", "John"); + contact.put("company", "Baeldung"); + + // Inserting document + collection.insert(contact); + DBCursor cursorDoc = collection.find(); + BasicDBObject contact1 = new BasicDBObject(); + while (cursorDoc.hasNext()) { + contact1 = (BasicDBObject) cursorDoc.next(); + System.out.println(contact1); + } + assertEquals(contact1.get("name"), "John"); + } +} \ No newline at end of file diff --git a/java-mongodb/src/test/java/com/baeldung/AppTest.java b/java-mongodb/src/test/java/com/baeldung/AppTest.java deleted file mode 100644 index 5dd6bb66a9..0000000000 --- a/java-mongodb/src/test/java/com/baeldung/AppTest.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.baeldung; - -import static org.junit.Assert.assertEquals; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import com.mongodb.BasicDBObject; -import com.mongodb.DB; -import com.mongodb.DBCollection; -import com.mongodb.DBCursor; -import com.mongodb.Mongo; -import de.flapdoodle.embedmongo.MongoDBRuntime; -import de.flapdoodle.embedmongo.MongodExecutable; -import de.flapdoodle.embedmongo.MongodProcess; -import de.flapdoodle.embedmongo.config.MongodConfig; -import de.flapdoodle.embedmongo.distribution.Version; -import de.flapdoodle.embedmongo.runtime.Network; - -public class AppTest { - - private static final String DB_NAME = "myMongoDb"; - private MongodExecutable mongodExe; - private MongodProcess mongod; - private Mongo mongo; - private DB db; - private DBCollection collection; - - @Before - public void setup() throws Exception { - - // Creating Mongodbruntime instance - MongoDBRuntime runtime = MongoDBRuntime.getDefaultInstance(); - - // Creating MongodbExecutable - mongodExe = runtime.prepare(new MongodConfig(Version.V2_0_1, 12345, - Network.localhostIsIPv6())); - - // Starting Mongodb - mongod = mongodExe.start(); - mongo = new Mongo("localhost", 12345); - - // Creating DB - db = mongo.getDB(DB_NAME); - - // Creating collection Object and adding values - collection = db.getCollection("customers"); - } - - @After - public void teardown() throws Exception { - mongod.stop(); - mongodExe.cleanup(); - } - - @Test - public void testAddressPersistance() { - - BasicDBObject contact = new BasicDBObject(); - contact.put("name", "John"); - contact.put("company", "Baeldung"); - - // Inserting document - collection.insert(contact); - DBCursor cursorDoc = collection.find(); - BasicDBObject contact1 = new BasicDBObject(); - while (cursorDoc.hasNext()) { - contact1 = (BasicDBObject) cursorDoc.next(); - System.out.println(contact1); - } - assertEquals(contact1.get("name"), "John"); - } -} \ No newline at end of file