Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial checkin... no morphia shit yet

  • Loading branch information...
commit 76d1e726e4361c325b255e07e2571952455f8b9b 0 parents
@phatduckk authored
5 .gitignore
@@ -0,0 +1,5 @@
+target
+CollectionBasicsTestDB*
+.idea
+*.iml
+mongod.lock
37 pom.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>learning-morphia-with-junit</groupId>
+ <artifactId>learning-morphia-with-junit</artifactId>
+ <version>1.0</version>
+
+
+ <repositories>
+ <repository>
+ <id>morhpia</id>
+ <url>http://morphia.googlecode.com/svn/mavenrepo/</url>
+ </repository>
+ </repositories>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.mongodb</groupId>
+ <artifactId>mongo-java-driver</artifactId>
+ <version>2.5</version>
+ </dependency>
+ <dependency>
+ <groupId>com.google.code.morphia</groupId>
+ <artifactId>morphia</artifactId>
+ <version>0.99</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.6</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
35 src/test/java/me/arin/mwjt/SetupMongo.java
@@ -0,0 +1,35 @@
+package me.arin.mwjt;
+
+import com.mongodb.Mongo;
+import org.junit.BeforeClass;
+
+public class SetupMongo {
+ protected static Mongo mongo;
+ private static final String PORT = "27666";
+
+ @BeforeClass
+ public static void setUp() throws Exception {
+ final int port = Integer.parseInt(System.getProperty("mongo.port", PORT));
+
+ try {
+ mongo = new Mongo("localhost", port);
+ for (String dbName : mongo.getDatabaseNames()) {
+ mongo.dropDatabase(dbName);
+ }
+ } catch (Exception e) {
+ throw new SetupMongoException();
+ }
+ }
+
+ public static Mongo getMongo() {
+ return mongo;
+ }
+
+ private static class SetupMongoException extends Exception {
+ public SetupMongoException() {
+ super(new StringBuilder("Run mongo on port ").append(PORT)
+ .append(". via: /path/to/executable/mongod --port ")
+ .append(PORT).toString());
+ }
+ }
+}
65 src/test/java/me/arin/mwjt/mongoDriver/CollectionBasicsTest.java
@@ -0,0 +1,65 @@
+package me.arin.mwjt.mongoDriver;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import me.arin.mwjt.SetupMongo;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class CollectionBasicsTest extends SetupMongo {
+ public static final String DB_NAME = "CollectionBasicsTestDB";
+ public static final String COLLECTION_NAME = "myRadCollection";
+
+ protected static DB db = null;
+
+ @BeforeClass
+ public static void beforeClass() throws Exception {
+ SetupMongo.setUp();
+ db = mongo.getDB(DB_NAME);
+ }
+
+ @Before
+ public void beforeMethod() {
+ db.getCollection(COLLECTION_NAME).drop();
+ }
+
+ @Test
+ public void collectionDoesntExist() {
+ Assert.assertFalse(db.collectionExists(COLLECTION_NAME));
+ }
+
+ @Test
+ public void newCollection() {
+ Assert.assertFalse(db.collectionExists(COLLECTION_NAME));
+
+ // create the collection & ensure its there...
+ final DBCollection collection = db.createCollection(COLLECTION_NAME, new BasicDBObject());
+ Assert.assertTrue(db.collectionExists(COLLECTION_NAME));
+
+ // check the name
+ Assert.assertEquals(COLLECTION_NAME, collection.getName());
+
+ // check that there's nothing in it
+ Assert.assertEquals(0, collection.count());
+
+ // make sure its in the right DB
+ Assert.assertEquals(DB_NAME, collection.getDB().getName());
+ }
+
+ @Test
+ public void dropCollection() {
+ // make sure it aint there to begin with
+ Assert.assertFalse(db.collectionExists(COLLECTION_NAME));
+
+ // create the collection & ensure its there...
+ final DBCollection collection = db.createCollection(COLLECTION_NAME, new BasicDBObject());
+ Assert.assertTrue(db.collectionExists(COLLECTION_NAME));
+
+ // drop it and make sure its gone
+ collection.drop();
+ Assert.assertFalse(db.collectionExists(COLLECTION_NAME));
+ }
+}
61 src/test/java/me/arin/mwjt/mongoDriver/DatabaseBasicsTest.java
@@ -0,0 +1,61 @@
+package me.arin.mwjt.mongoDriver;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DB;
+import me.arin.mwjt.SetupMongo;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.List;
+
+public class DatabaseBasicsTest extends SetupMongo {
+ private static final String ADMIN = "admin";
+
+ @Test
+ public void onlyHaveAdminDbAtStart() {
+ final List<String> dbNames = mongo.getDatabaseNames();
+ Assert.assertEquals(1, dbNames.size());
+ Assert.assertEquals(ADMIN, dbNames.get(0));
+ }
+
+ @Test
+ public void createDatabase() {
+ final String newDbName = "newDb";
+ final DB db = mongo.getDB(newDbName);
+
+ // still only 1 DB cuz we haven't put anything in the new one yet...
+ Assert.assertEquals(1, mongo.getDatabaseNames().size());
+
+ // now lets create a collection w/ default options so make the DB creation "official"
+ db.createCollection("testCollection", new BasicDBObject());
+
+ final List<String> dbNames = mongo.getDatabaseNames();
+ boolean foundAdmin = false;
+ boolean foundNewDb = false;
+
+ for (String name : dbNames) {
+ if (name.equals(newDbName)) {
+ foundNewDb = true;
+ } else if (name.equals(ADMIN)) {
+ foundAdmin = true;
+ }
+ }
+
+ // make sure we've got admin and the new DB
+ Assert.assertEquals(2, dbNames.size());
+ Assert.assertTrue(foundAdmin && foundNewDb);
+ }
+
+ @Test
+ public void dropDatabase() {
+ final String someDB = "someDB";
+ mongo.getDB(someDB).createCollection("someCollection", new BasicDBObject());
+
+ // make sure someDB was created
+ Assert.assertTrue(mongo.getDatabaseNames().contains(someDB));
+
+ // drop it and make sure it's gone...
+ mongo.dropDatabase(someDB);
+ Assert.assertFalse(mongo.getDatabaseNames().contains(someDB));
+ }
+}
94 src/test/java/me/arin/mwjt/mongoDriver/InsertDocument.java
@@ -0,0 +1,94 @@
+package me.arin.mwjt.mongoDriver;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBCollection;
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import org.bson.types.ObjectId;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class InsertDocument extends CollectionBasicsTest {
+ @Test
+ public void insertADocument() {
+ final DBCollection collection = db.getCollection(COLLECTION_NAME);
+ final BasicDBObject document = new BasicDBObject("name", "Arin").append("age", 32).append(
+ "favoriteMovies",
+ new String[]{"Scream", "Halloween", "Donnie Darko"});
+
+ collection.insert(document);
+ Assert.assertEquals(1, collection.count());
+ }
+
+ @Test
+ public void insertMultipleDocuments() {
+ final DBCollection collection = db.getCollection(COLLECTION_NAME);
+
+ final List<DBObject> docs = new ArrayList<DBObject>();
+ docs.add(new BasicDBObject("hello", "there"));
+ docs.add(new BasicDBObject("deez", "nuts"));
+ docs.add(new BasicDBObject("numberOfTheBeast", 666));
+
+ // insert a list
+ collection.insert(docs);
+ Assert.assertEquals(docs.size(), collection.count());
+
+ // we can insert an array of docs too
+ final BasicDBObject[] docArray = {new BasicDBObject("yo", "dawg"), new BasicDBObject("a", 1)};
+ collection.insert(docArray);
+ Assert.assertEquals(docArray.length + docs.size(), collection.count());
+
+ // you can use the ... thing too
+ collection.insert(new BasicDBObject("9", "99"), new BasicDBObject("kill", "bill"));
+ Assert.assertEquals(2 + docArray.length + docs.size(), collection.count());
+ }
+
+ @Test
+ public void automaticallyAssigned_id() {
+ final DBCollection collection = db.getCollection(COLLECTION_NAME);
+ final String idField = "_id";
+
+ final BasicDBObject basicDBObject = new BasicDBObject("gimmie", "anId");
+ Assert.assertFalse(basicDBObject.containsField(idField));
+
+ // look ma - the driver gives me an ObjectId if I don't specify _id myself
+ collection.insert(basicDBObject);
+ Assert.assertTrue(basicDBObject.containsField(idField));
+ Assert.assertTrue(basicDBObject.get(idField) instanceof ObjectId);
+ }
+
+ @Test
+ public void dontHaveToUseObjectIdfor_id() {
+ int i = 0;
+
+ final DBCollection collection = db.getCollection(COLLECTION_NAME);
+ final String idField = "_id";
+ final String myId = "myId";
+
+ final BasicDBObject basicDBObject = new BasicDBObject(idField, myId).append("i", i++);
+ collection.insert(basicDBObject);
+
+ // just proving that it inserted fine
+ Assert.assertEquals(1, collection.count());
+
+ // grab the first (aka only) thing in this collection - the doc we just inserted
+ final DBObject docFromDB = collection.findOne();
+ Assert.assertNotNull(docFromDB);
+ Assert.assertEquals(myId, docFromDB.get(idField));
+
+ // _id doesnt have to be an ObjectId. It can be an int (or whatever else) too...
+ // lets insert a doc with an int for _id
+ collection.insert(new BasicDBObject(idField, 1).append("i", i));
+
+ // sorting "backwards" - more on this stuff in the querying tests
+ // for now just trust me that we're just getting the last doc
+ final DBCursor cursor = collection.find().sort(new BasicDBObject("i", -1)).limit(1);
+ final DBObject lastItemInCollection = cursor.iterator().next();
+
+ Assert.assertEquals(1, lastItemInCollection.get("_id"));
+
+ }
+}
624 src/test/java/me/arin/mwjt/mongoDriver/QueryingCollectionsTest.java
@@ -0,0 +1,624 @@
+package me.arin.mwjt.mongoDriver;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBCollection;
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.*;
+
+public class QueryingCollectionsTest extends CollectionBasicsTest {
+ private DBCollection collection;
+
+ @Override
+ @Before
+ public void beforeMethod() {
+ super.beforeMethod();
+ collection = db.createCollection(COLLECTION_NAME, new BasicDBObject());
+ }
+
+ @Test
+ public void getAllDocs() {
+ final int num = 10;
+ for (int i = 0; i < num; i++) {
+ collection.insert(new BasicDBObject("name", "arin" + i));
+ }
+
+ // collection.find() gets em all
+ final DBCursor dbCursor = collection.find();
+ Assert.assertEquals(num, dbCursor.length());
+
+ int i = 0;
+ for (DBObject dbObject : dbCursor) {
+ String name = "arin" + i++;
+ Assert.assertTrue(name.equals(dbObject.get("name")));
+ }
+ }
+
+ @Test
+ public void introToDBCursor() {
+ for (int i = 0; i < 10; i++) {
+ collection.insert(new BasicDBObject("i", i));
+ }
+
+ // the queries return a DBCursor which is just an iterable object w/ some extra methods
+ final DBCursor dbCursor = collection.find();
+ Assert.assertTrue(dbCursor instanceof Iterable);
+ Assert.assertTrue(dbCursor instanceof Iterator);
+ Assert.assertTrue("u can call hasNext() like w/ other Iterators", dbCursor.hasNext());
+
+ // iterate over it and you'll bget back your DBObjects
+ for (DBObject dbObject : dbCursor) {
+ Assert.assertTrue(dbObject instanceof DBObject);
+ }
+
+ Assert.assertEquals("# of dbObjects in the cursor", 10, dbCursor.length());
+ Assert.assertEquals("# of dbObjects matched the query (regardless of limit/offset)",
+ 10,
+ dbCursor.count());
+
+ // returns void so i cant assert anything meaningful but closes cursor on the server...
+ dbCursor.close();
+ }
+
+ @Test
+ public void getNumberOfDocsInACollection() {
+ final int num = 10;
+
+ for (int i = 0; i < num; i++) {
+ collection.insert(new BasicDBObject("i", i));
+ }
+
+ // ask the collection for count....
+ Assert.assertEquals(num, collection.count());
+
+ // query, fetch everything & count (not a good idea)
+ Assert.assertEquals(num, collection.find().length());
+ }
+
+ @Test
+ public void limitNumDocs() {
+ final int num = 10;
+ for (int i = 0; i < num; i++) {
+ collection.insert(new BasicDBObject("name", "arin" + i));
+ }
+
+ // lets limit to 4
+ final int limit = 4;
+ final DBCursor data = collection.find().limit(limit);
+
+ // cursor only hold 4
+ Assert.assertEquals(limit, data.length());
+
+ // but it knows that there's 10 total if limit wasn't used...
+ Assert.assertEquals(num, data.count());
+ }
+
+ @Test
+ public void limitNumDocsButCheckTheTotalThatMatched() {
+ final int num = 10;
+ for (int i = 0; i < num; i++) {
+ collection.insert(new BasicDBObject("name", "arin" + i));
+ }
+
+ // lets limit to 4
+ final int limit = 4;
+ final DBCursor data = collection.find().limit(limit);
+ Assert.assertEquals(limit, data.length());
+
+ // cursor can tell you the total # of matches when you use a limit
+ Assert.assertEquals(num, data.count());
+ }
+
+ @Test
+ public void getByOffset() {
+ final int num = 10;
+ for (int i = 0; i < num; i++) {
+ collection.insert(new BasicDBObject("name", "arin" + i));
+ }
+
+ int offset = 1;
+ // they call it skip
+ final DBCursor data = collection.find().skip(offset);
+ Assert.assertEquals("arin" + offset, data.iterator().next().get("name"));
+ }
+
+ @Test
+ public void getFirstDocFromCollection() {
+ for (int i = 0; i < 10; i++) {
+ collection.insert(new BasicDBObject("name", "arin" + i));
+ }
+
+ // collection.findOne() gets the first doc
+ final DBObject first = collection.findOne();
+ Assert.assertTrue("arin0".equals(first.get("name")));
+ }
+
+ @Test
+ public void restrictReturnedFields() {
+ final BasicDBObject dbObject = new BasicDBObject("name", "arin").append("x", 1)
+ .append("age", 32);
+ collection.insert(dbObject);
+
+ // only get back the "x" field ... _id is always returned
+ DBObject doc = collection.findOne(new BasicDBObject(), new BasicDBObject("x", true));
+ Assert.assertTrue(doc.containsField("x"));
+ Assert.assertTrue(doc.containsField("_id"));
+ Assert.assertFalse(doc.containsField("name"));
+ Assert.assertFalse(doc.containsField("age"));
+
+ // now we can get back all fields except for x
+ doc = collection.findOne(new BasicDBObject(), new BasicDBObject("x", false));
+ Assert.assertFalse(doc.containsField("x"));
+ Assert.assertTrue(doc.containsField("_id"));
+ Assert.assertTrue(doc.containsField("age"));
+ Assert.assertTrue(doc.containsField("name"));
+ }
+
+ @Test
+ public void querySingleFieldEquals() {
+ // insert 10 docs w/ incrementing value for i
+ for (int i = 0; i < 10; i++) {
+ collection.insert(new BasicDBObject("i", i));
+ }
+
+ // add an extra doc where i == 5
+ final int valueWeCareAbout = 5;
+ collection.insert(new BasicDBObject("i", valueWeCareAbout));
+
+ // now select stuff where i == 5
+ final BasicDBObject query = new BasicDBObject("i", valueWeCareAbout);
+ final DBCursor dbCursor = collection.find(query);
+
+ // should get 2 results..
+ Assert.assertEquals(2, dbCursor.length());
+
+ // make sure i == 5 in both
+ for (DBObject dbObject : dbCursor) {
+ Assert.assertEquals(valueWeCareAbout, dbObject.get("i"));
+ }
+ }
+
+ @Test
+ public void querySingleFieldNotEqual() {
+ // insert 10 docs w/ incrementing value for i
+ for (int i = 0; i < 10; i++) {
+ collection.insert(new BasicDBObject("i", i));
+ }
+
+ final int valueWeCareAbout = 5;
+
+ // now select stuff where i != 5
+ final BasicDBObject query = new BasicDBObject("i", new BasicDBObject("$ne", valueWeCareAbout));
+ final DBCursor dbCursor = collection.find(query);
+
+ // should get 2 results..
+ Assert.assertEquals(9, dbCursor.length());
+
+ // make sure i == 5 in both
+ for (DBObject dbObject : dbCursor) {
+ Assert.assertNotSame(valueWeCareAbout, dbObject.get("i"));
+ }
+ }
+
+ @Test
+ public void querySingleFieldEqualsAndShowItsCaseSensitive() {
+ final String targetName = "ARIN";
+ final String fieldName = "name";
+
+ collection.insert(new BasicDBObject(fieldName, targetName));
+
+ // add an extra doc where name == arin (all lowercase)
+ final BasicDBObject query = new BasicDBObject(fieldName, targetName.toLowerCase());
+ final DBCursor dbCursor = collection.find(query);
+
+ // should get NO results
+ Assert.assertEquals(0, dbCursor.length());
+ }
+
+ @Test
+ public void queryMultipleFieldsViaAnd() {
+ // insert some docs...
+ final String targetName = "arin";
+ final int targetI = 5;
+
+ collection.insert(new BasicDBObject("i", targetI).append("name", targetName));
+ collection.insert(new BasicDBObject("i", targetI).append("name", targetName));
+ collection.insert(new BasicDBObject("i", 6).append("name", targetName));
+ collection.insert(new BasicDBObject("i", 6).append("name", "bob"));
+ collection.insert(new BasicDBObject("i", 6).append("name", "arin"));
+ collection.insert(new BasicDBObject("i", targetI).append("name", "bob"));
+
+ // now select stuff where i == 5 and name == arin
+ // and is default for multiple criteria
+ final BasicDBObject query = new BasicDBObject("i", targetI).append("name", targetName);
+ final DBCursor dbCursor = collection.find(query);
+
+ // should get 2 results..
+ Assert.assertEquals(2, dbCursor.length());
+
+ // make sure i == 5 and name == arin in both
+ for (DBObject dbObject : dbCursor) {
+ Assert.assertEquals(targetI, dbObject.get("i"));
+ Assert.assertEquals(targetName, dbObject.get("name"));
+ }
+ }
+
+ @Test
+ public void queryMultipleFieldsViaOr() {
+ final int targetI = 5;
+ final String fieldI = "i";
+
+ final String targetName = "arin";
+ final String fieldName = "name";
+
+ // we wanna find these 2: where i == 5 or name == "arin"
+ collection.insert(new BasicDBObject(fieldI, targetI).append(fieldName, "not arin"));
+ collection.insert(new BasicDBObject(fieldI, 666).append(fieldName, targetName));
+
+ // add some garbage data
+ collection.insert(new BasicDBObject(fieldI, 666));
+ collection.insert(new BasicDBObject(fieldName, targetName.toUpperCase()));
+ collection.insert(new BasicDBObject(fieldI, 888));
+ collection.insert(new BasicDBObject(fieldI, 999));
+
+ // lets construct the or
+ // in json it would look like [{ i : 5 } , { name : 'arin' }]
+ final List<BasicDBObject> or = new ArrayList<BasicDBObject>();
+ or.add(new BasicDBObject(fieldI, targetI));
+ or.add(new BasicDBObject(fieldName, targetName));
+
+ final BasicDBObject query = new BasicDBObject("$or", or);
+ final DBCursor cursor = collection.find(query);
+
+ // we have 2 results - both total and returned
+ Assert.assertEquals(2, cursor.count());
+ Assert.assertEquals(2, cursor.length());
+
+ // assert i == 5 || name.equals(arin)
+ for (DBObject dbObject : cursor) {
+ final Integer i = (Integer) dbObject.get(fieldI);
+ final String name = (String) dbObject.get(fieldName);
+
+ Assert.assertTrue(i == targetI || targetName.equals(name));
+ }
+ }
+
+ @Test
+ public void queryGreaterThan() {
+ int total = 10;
+ int targetInt = 5;
+
+ for (int i = 0; i < total; i++) {
+ collection.insert(new BasicDBObject("i", i));
+ }
+
+ BasicDBObject queryGreaterThan = new BasicDBObject("i", new BasicDBObject("$gt", targetInt));
+ final DBCursor greaterThanCursor = collection.find(queryGreaterThan);
+ for (DBObject dbObject : greaterThanCursor) {
+ final Integer iValue = (Integer) dbObject.get("i");
+ Assert.assertTrue(iValue > targetInt);
+ }
+
+ BasicDBObject queryGreaterThanEqual = new BasicDBObject("i",
+ new BasicDBObject("$gte", targetInt));
+ final DBCursor greaterEqualThanCursor = collection.find(queryGreaterThanEqual);
+ for (DBObject dbObject : greaterEqualThanCursor) {
+ final Integer iValue = (Integer) dbObject.get("i");
+ Assert.assertTrue(iValue >= targetInt);
+ }
+ }
+
+ @Test
+ public void queryLessThan() {
+ int total = 10;
+ int targetInt = 5;
+
+ for (int i = 0; i < total; i++) {
+ collection.insert(new BasicDBObject("i", i));
+ }
+
+ BasicDBObject queryLessThan = new BasicDBObject("i", new BasicDBObject("$lt", targetInt));
+ final DBCursor lessThanCursor = collection.find(queryLessThan);
+ for (DBObject dbObject : lessThanCursor) {
+ final Integer iValue = (Integer) dbObject.get("i");
+ Assert.assertTrue(iValue < targetInt);
+ }
+
+ BasicDBObject queryLessThanEqual = new BasicDBObject("i", new BasicDBObject("$lte", targetInt));
+ final DBCursor lessThanEqualCursor = collection.find(queryLessThanEqual);
+ for (DBObject dbObject : lessThanEqualCursor) {
+ final Integer iValue = (Integer) dbObject.get("i");
+ Assert.assertTrue(iValue <= targetInt);
+ }
+ }
+
+ @Test
+ public void queryValueInAListEquals() {
+ // insert a bunch of docs who's field names contains an array of names
+ collection.insert(new BasicDBObject("names", new String[]{"arin", "william", "deez", "nuts"}));
+ collection.insert(new BasicDBObject("names", new String[]{"deez", "nuts"}));
+ collection.insert(new BasicDBObject("names", new String[]{"josh", "scorpion"}));
+ collection.insert(new BasicDBObject("names", new String[]{"sub zero"}));
+
+ // now lets get back all the docs where the names field contains "arin"
+ BasicDBObject queryViaEquals = new BasicDBObject("names", "arin");
+ DBCursor dbCursorViaEquals = collection.find(queryViaEquals);
+ Assert.assertTrue(1 == dbCursorViaEquals.length());
+
+ // now lets get back all the docs where the names field contains "deez"
+ queryViaEquals = new BasicDBObject("names", "deez");
+ dbCursorViaEquals = collection.find(queryViaEquals);
+ Assert.assertTrue(2 == dbCursorViaEquals.length());
+
+ // now lets get back all the docs where the names field contains "deez"
+ queryViaEquals = new BasicDBObject("names", "random ass string");
+ dbCursorViaEquals = collection.find(queryViaEquals);
+ Assert.assertTrue(0 == dbCursorViaEquals.length());
+ }
+
+ @Test
+ public void queryListValueIsInASet() {
+ // insert a bunch of docs who's field names contains an array of names
+ collection.insert(new BasicDBObject("names", new String[]{"arin", "william", "deez", "nuts"}));
+ collection.insert(new BasicDBObject("names", new String[]{"deez", "nuts"}));
+ collection.insert(new BasicDBObject("names", new String[]{"josh", "scorpion"}));
+ collection.insert(new BasicDBObject("names", new String[]{"sub zero"}));
+
+ final Set<String> findAnyOfThese = new HashSet<String>();
+ findAnyOfThese.add("nuts");
+ findAnyOfThese.add("josh");
+
+ // now lets get back all the docs where the names field is in the target set
+ BasicDBObject in = new BasicDBObject("$in", findAnyOfThese);
+ BasicDBObject queryViaIn = new BasicDBObject("names", in);
+
+ // we should get 3 docs... any doc with names.contains(nuts) || names.contains(josh)
+ final DBCursor dbCursor = collection.find(queryViaIn);
+ Assert.assertEquals(3, dbCursor.length());
+
+ for (DBObject dbObject : dbCursor) {
+ List<String> names = (List<String>) dbObject.get("names");
+ Assert.assertTrue(names.contains("nuts") || names.contains("josh"));
+ }
+ }
+
+ @Test
+ public void queryExistenceOfAField() {
+ collection.insert(new BasicDBObject("i", 666));
+ collection.insert(new BasicDBObject("i", 666));
+ collection.insert(new BasicDBObject("i", 666));
+ collection.insert(new BasicDBObject("i", 666));
+ collection.insert(new BasicDBObject("kill", 666));
+ collection.insert(new BasicDBObject("kill", 666));
+
+ // now get all docs that have a field named kill
+ final BasicDBObject existsQuery = new BasicDBObject("kill", new BasicDBObject("$exists", true));
+
+ DBCursor dbCursor = collection.find(existsQuery);
+ Assert.assertEquals(2, dbCursor.length());
+ for (DBObject dbObject : dbCursor) {
+ Assert.assertTrue(dbObject.containsField("kill"));
+ }
+
+ // ok - lets get docs that DO NOT bave a kill field
+ final BasicDBObject notExistsQuery = new BasicDBObject("kill",
+ new BasicDBObject("$exists", false));
+
+ dbCursor = collection.find(notExistsQuery);
+ Assert.assertEquals(4, dbCursor.length());
+ for (DBObject dbObject : dbCursor) {
+ Assert.assertFalse(dbObject.containsField("kill"));
+ }
+ }
+
+ @Test
+ public void queryListValueIsNotInASet() {
+ // insert a bunch of docs who's field names contains an array of names
+ collection.insert(new BasicDBObject("names", new String[]{"arin", "william", "deez", "nuts"}));
+ collection.insert(new BasicDBObject("names", new String[]{"deez", "nuts"}));
+ collection.insert(new BasicDBObject("names", new String[]{"josh", "scorpion"}));
+ collection.insert(new BasicDBObject("names", new String[]{"sub zero"}));
+
+ final Set<String> notIN = new HashSet<String>();
+ notIN.add("nuts");
+
+ // now lets get back all the docs where the names field is in the target set
+ BasicDBObject nin = new BasicDBObject("$nin", notIN);
+ BasicDBObject qryNin = new BasicDBObject("names", nin);
+
+ // we should get 2 docs... any doc with !names.contains(nuts)
+ final DBCursor dbCursor = collection.find(qryNin);
+ Assert.assertEquals(2, dbCursor.length());
+
+ for (DBObject dbObject : dbCursor) {
+ List<String> names = (List<String>) dbObject.get("names");
+ Assert.assertFalse(names.contains("nuts"));
+ }
+ }
+
+ @Test
+ public void findDocsWithAListOfCertainSize() {
+ collection.insert(new BasicDBObject("names", new String[]{"arin", "william", "deez", "nuts"}));
+ collection.insert(new BasicDBObject("names", new String[]{"deez", "nuts"}));
+ collection.insert(new BasicDBObject("names", new String[]{"josh", "scorpion"}));
+ collection.insert(new BasicDBObject("names", new String[]{"sub zero"}));
+
+ int size = 2;
+ DBObject sizeQry = new BasicDBObject("names", new BasicDBObject("$size", size));
+ final DBCursor dbCursor = collection.find(sizeQry);
+
+ // wanna find any doc where names.length == 2
+ Assert.assertEquals(2, dbCursor.length());
+
+ for (DBObject dbObject : dbCursor) {
+ final List names = (List) dbObject.get("names");
+ Assert.assertEquals(size, names.size());
+ }
+ }
+
+ @Test
+ public void matchAllEntriesInEmbededList() {
+ final String[] allOfThese = {"deez", "nuts"};
+
+ collection.insert(new BasicDBObject("names", allOfThese));
+ collection.insert(new BasicDBObject("names", new String[]{"deez"}));
+ collection.insert(new BasicDBObject("names", new String[]{"nuts"}));
+
+ BasicDBObject allQry = new BasicDBObject("names", new BasicDBObject("$all", allOfThese));
+ final DBCursor dbCursor = collection.find(allQry);
+
+ Assert.assertEquals(1, dbCursor.length());
+ final List names = (List) dbCursor.iterator().next().get("names");
+ Assert.assertArrayEquals(allOfThese, names.toArray(new String[]{}));
+ }
+
+ /**
+ * type codes listed at: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24type
+ * <p/>
+ * codes used in this test
+ * String == 2
+ * 32-bit int == 16
+ */
+ @Test
+ public void queryFieldIsCertainType() {
+ collection.insert(new BasicDBObject("i", "i is a string"));
+ collection.insert(new BasicDBObject("i", 666));
+
+ final BasicDBObject qryIIsString = new BasicDBObject("i", new BasicDBObject("$type", 2));
+ final DBCursor stringCursor = collection.find(qryIIsString);
+ Assert.assertTrue(stringCursor.length() == 1);
+ Assert.assertTrue(stringCursor.iterator().next().get("i") instanceof String);
+
+ final BasicDBObject qryIIsIntegre = new BasicDBObject("i", new BasicDBObject("$type", 16));
+ final DBCursor integerCursor = collection.find(qryIIsIntegre);
+ Assert.assertTrue(integerCursor.length() == 1);
+ Assert.assertTrue(integerCursor.iterator().next().get("i") instanceof Integer);
+ }
+
+ @Test
+ public void queryStringsByRegex() {
+ final ArrayList<String> aNames = new ArrayList<String>();
+ aNames.add("arin");
+ aNames.add("alice");
+ aNames.add("Alex");
+
+ collection.insert(new BasicDBObject("name", aNames.get(0)));
+ collection.insert(new BasicDBObject("name", aNames.get(1)));
+ collection.insert(new BasicDBObject("name", aNames.get(2)));
+ collection.insert(new BasicDBObject("name", "bob"));
+ collection.insert(new BasicDBObject("name", "lucifer"));
+
+ // find stuff that starts with an 'a' - arin & alice
+ BasicDBObject startsWithAQuery = new BasicDBObject("name", new BasicDBObject("$regex", "^a"));
+ DBCursor aResults = collection.find(startsWithAQuery);
+ Assert.assertEquals(2, aResults.length());
+ for (DBObject aResult : aResults) {
+ final String name = (String) aResult.get("name");
+ Assert.assertTrue(name.equals("arin") || name.equals("alice"));
+ Assert.assertTrue(name.startsWith("a"));
+ Assert.assertFalse(name.startsWith("A"));
+ }
+
+ // ok now case insensitive - using the 'i' option
+ startsWithAQuery = new BasicDBObject("name",
+ new BasicDBObject("$regex", "^a").append("$options", "i"));
+ aResults = collection.find(startsWithAQuery);
+
+ Assert.assertEquals(3, aResults.length());
+ for (DBObject aResult : aResults) {
+ final String name = (String) aResult.get("name");
+ Assert.assertTrue(name.equals("arin") || name.equals("alice") || name.equals("Alex"));
+ Assert.assertTrue(name.startsWith("a") || name.startsWith("A"));
+ }
+ }
+
+ @Test
+ /**
+ * these docs look like
+ * {
+ * address: {"city": STRING, "zip" INTEGER}}
+ * }
+ */
+ public void valuesInEmbededObjectEquals() {
+ final BasicDBObject user1 = new BasicDBObject();
+ user1.put("address", new BasicDBObject("city", "SF").append("zip", 94107));
+
+ final BasicDBObject user2 = new BasicDBObject();
+ user2.put("address", new BasicDBObject("city", "LA").append("zip", 90210));
+
+ collection.insert(user1, user2);
+
+ // now lets query and find docs where address.city == SF
+ final BasicDBObject query = new BasicDBObject("address.city", "SF");
+ final DBObject sfUser = collection.findOne(query);
+ final Map address = (Map) sfUser.get("address");
+ Assert.assertEquals(94107, address.get("zip"));
+ }
+
+ @Test
+ /**
+ * these docs look like
+ * {
+ * profile: {"age": INT, "email" INTEGER}}
+ * }
+ */
+ public void valuesInEmbededObjectMeetCriteria() {
+ final BasicDBObject user1 = new BasicDBObject();
+ user1.put("profile", new BasicDBObject("age", 32).append("email", "a@example.com"));
+
+ final BasicDBObject user2 = new BasicDBObject();
+ user2.put("profile", new BasicDBObject("age", 55).append("email", "b@example.com"));
+
+ final BasicDBObject user3 = new BasicDBObject();
+ user3.put("profile", new BasicDBObject("age", 20).append("email", "c@example.com"));
+
+ collection.insert(user1, user2, user3);
+
+ // now lets query and find users who are older than 20 - should be 2 of em
+ final BasicDBObject query = new BasicDBObject("profile.age", new BasicDBObject("$gt", 20));
+ final DBCursor results = collection.find(query);
+ Assert.assertEquals(2, results.length());
+
+ for (DBObject result : results) {
+ Map profile = (Map) result.get("profile");
+ final Integer age = (Integer) profile.get("age");
+ Assert.assertTrue(age > 20);
+ }
+ }
+
+ @Test
+ public void sortResultsByField() {
+ final List<DBObject> list = new ArrayList<DBObject>();
+ for (int i = 1; i < 100; i++) {
+ list.add(new BasicDBObject("age", i));
+ }
+
+ // shuffle the list so the docs are inserted in random order...
+ Collections.shuffle(list);
+ collection.insert(list);
+
+ // get by age ASC
+ final DBCursor asc = collection.find().sort(new BasicDBObject("age", 1));
+ int lastAge = 0;
+
+ for (DBObject dbObject : asc) {
+ int age = ((Integer) dbObject.get("age")).intValue();
+ Assert.assertTrue(age > lastAge);
+ lastAge = age;
+ }
+
+ // get by age DESC
+ final DBCursor desc = collection.find().sort(new BasicDBObject("age", -1));
+ lastAge = 100;
+
+ for (DBObject dbObject : desc) {
+ int age = ((Integer) dbObject.get("age")).intValue();
+ Assert.assertTrue(age < lastAge);
+ lastAge = age;
+ }
+ }
+}
6 src/test/java/me/arin/mwjt/morphia/dao/DaoBase.java
@@ -0,0 +1,6 @@
+package me.arin.mwjt.morphia.dao;
+
+import me.arin.mwjt.SetupMongo;
+
+public class DaoBase extends SetupMongo {
+}
6 src/test/java/me/arin/mwjt/morphia/datastore/DatastoreBase.java
@@ -0,0 +1,6 @@
+package me.arin.mwjt.morphia.datastore;
+
+import me.arin.mwjt.SetupMongo;
+
+public class DatastoreBase extends SetupMongo {
+}
39 src/test/java/me/arin/mwjt/noQueryingStuff/DBObjectTest.java
@@ -0,0 +1,39 @@
+package me.arin.mwjt.noQueryingStuff;
+
+import com.mongodb.BasicDBObject;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Map;
+
+public class DBObjectTest {
+ @Test
+ public void itsAMap() {
+ final BasicDBObject basicDBObject = new BasicDBObject();
+ Assert.assertTrue(basicDBObject instanceof Map);
+ }
+
+ @Test
+ public void lookItsFluent() {
+ final String k1 = "k1";
+ final String k2 = "k2";
+ final String k3 = "k3";
+
+ final String v1 = "v1";
+ final String v2 = "v2";
+
+ final BasicDBObject basicDBObject = new BasicDBObject(k1, v1).append(k2, v2);
+ basicDBObject.append(k3, new BasicDBObject());
+
+ Assert.assertTrue(basicDBObject.containsField(k1));
+ Assert.assertTrue(basicDBObject.containsField(k2));
+ Assert.assertTrue(basicDBObject.containsField(k3));
+
+ Assert.assertEquals(basicDBObject.get(k1), v1);
+ Assert.assertEquals(basicDBObject.get(k2), v2);
+ Assert.assertTrue(basicDBObject.get(k3) instanceof BasicDBObject);
+
+ basicDBObject.removeField(k1);
+ Assert.assertEquals(null, basicDBObject.get(k1));
+ }
+}
55 src/test/java/me/arin/mwjt/noQueryingStuff/ObjectIdTest.java
@@ -0,0 +1,55 @@
+package me.arin.mwjt.noQueryingStuff;
+
+import org.bson.types.ObjectId;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+import java.security.NoSuchAlgorithmException;
+
+public class ObjectIdTest {
+ @Test
+ public void getAnObjectId() {
+ final ObjectId objectId = ObjectId.get();
+
+ // just set a breakpoint here and scope out what an ObjectId looks like in the debugger
+ // this assert is pretty worthless - but u should check your debugger
+ Assert.assertTrue(objectId instanceof ObjectId);
+ }
+
+ @Test
+ public void cantUseARandomString() {
+ try {
+ new ObjectId("thisIsARandomString");
+ Assert.assertTrue(false);
+ } catch (IllegalArgumentException e) {
+ Assert.assertTrue("see you cant use random strings. ObjectIds have a special format", true);
+ }
+
+ Assert.assertFalse(ObjectId.isValid("something"));
+ Assert.assertFalse(ObjectId.isValid("1"));
+ Assert.assertFalse(ObjectId.isValid("666"));
+ Assert.assertTrue(ObjectId.isValid(ObjectId.get().toString()));
+ }
+
+ @Test
+ public void objectIdPeices() throws NoSuchAlgorithmException {
+ final ObjectId objectId = ObjectId.get();
+ final int nowIsh = (int) (System.currentTimeMillis()/1000L);
+
+ // its 12 bytes & looks like:
+ // ----------------------------------
+ // 0123 | 456 | 78 | 9 10 11
+ // time | machine | pid | inc
+ // ----------------------------------
+ final byte[] bytes = objectId.toByteArray();
+ Assert.assertEquals(12, bytes.length);
+
+ // lets look at the bytes...
+ ByteBuffer bb = ByteBuffer.allocate(12).put(bytes);
+
+ // see - the first 4 bytes are seconds since epoch...
+ Assert.assertEquals(objectId.getTimeSecond(), bb.getInt(0));
+ Assert.assertEquals(nowIsh, bb.getInt(0));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.