Search in sources :

Example 31 with DBCollection

use of com.mongodb.DBCollection in project morphia by mongodb.

the class ZipCodeDataSetTest method installSampleData.

public void installSampleData() throws IOException, TimeoutException, InterruptedException {
    File file = new File("zips.json");
    if (!file.exists()) {
        file = new File(System.getProperty("java.io.tmpdir"), "zips.json");
        if (!file.exists()) {
            download(new URL("http://media.mongodb.org/zips.json"), file);
        }
    }
    DBCollection zips = getDb().getCollection("zips");
    if (zips.count() == 0) {
        new ProcessExecutor().command(MONGO_IMPORT, "--db", getDb().getName(), "--collection", "zipcodes", "--file", file.getAbsolutePath()).redirectError(System.err).execute();
    }
}
Also used : DBCollection(com.mongodb.DBCollection) ProcessExecutor(org.zeroturnaround.exec.ProcessExecutor) File(java.io.File) URL(java.net.URL)

Example 32 with DBCollection

use of com.mongodb.DBCollection in project morphia by mongodb.

the class TestMapping method testMaps.

@Test
public void testMaps() throws Exception {
    final DBCollection articles = getDb().getCollection("articles");
    getMorphia().map(Article.class).map(Translation.class).map(Circle.class);
    final Article related = new Article();
    final BasicDBObject relatedDbObj = (BasicDBObject) getMorphia().toDBObject(related);
    articles.save(relatedDbObj);
    final Article relatedLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject(Mapper.ID_KEY, relatedDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    final Article article = new Article();
    article.setTranslation("en", new Translation("Hello World", "Just a test"));
    article.setTranslation("is", new Translation("Halló heimur", "Bara að prófa"));
    article.setAttribute("myDate", new Date());
    article.setAttribute("myString", "Test");
    article.setAttribute("myInt", 123);
    article.putRelated("test", relatedLoaded);
    final BasicDBObject articleDbObj = (BasicDBObject) getMorphia().toDBObject(article);
    articles.save(articleDbObj);
    final Article articleLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject(Mapper.ID_KEY, articleDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    assertEquals(article.getTranslations().size(), articleLoaded.getTranslations().size());
    assertEquals(article.getTranslation("en").getTitle(), articleLoaded.getTranslation("en").getTitle());
    assertEquals(article.getTranslation("is").getBody(), articleLoaded.getTranslation("is").getBody());
    assertEquals(article.getAttributes().size(), articleLoaded.getAttributes().size());
    assertEquals(article.getAttribute("myDate"), articleLoaded.getAttribute("myDate"));
    assertEquals(article.getAttribute("myString"), articleLoaded.getAttribute("myString"));
    assertEquals(article.getAttribute("myInt"), articleLoaded.getAttribute("myInt"));
    assertEquals(article.getRelated().size(), articleLoaded.getRelated().size());
    assertEquals(article.getRelated("test").getId(), articleLoaded.getRelated("test").getId());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Translation(org.mongodb.morphia.testmodel.Translation) Article(org.mongodb.morphia.testmodel.Article) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) Date(java.util.Date) Test(org.junit.Test)

Example 33 with DBCollection

use of com.mongodb.DBCollection in project morphia by mongodb.

the class TestMapping method testRecursiveReference.

@Test
public void testRecursiveReference() throws Exception {
    final DBCollection stuff = getDb().getCollection("stuff");
    getMorphia().map(RecursiveParent.class).map(RecursiveChild.class);
    final RecursiveParent parent = new RecursiveParent();
    final DBObject parentDbObj = getMorphia().toDBObject(parent);
    stuff.save(parentDbObj);
    final RecursiveChild child = new RecursiveChild();
    final DBObject childDbObj = getMorphia().toDBObject(child);
    stuff.save(childDbObj);
    final RecursiveParent parentLoaded = getMorphia().fromDBObject(getDs(), RecursiveParent.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, parentDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    final RecursiveChild childLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, childDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    parentLoaded.setChild(childLoaded);
    childLoaded.setParent(parentLoaded);
    stuff.save(getMorphia().toDBObject(parentLoaded));
    stuff.save(getMorphia().toDBObject(childLoaded));
    final RecursiveParent finalParentLoaded = getMorphia().fromDBObject(getDs(), RecursiveParent.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, parentDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    final RecursiveChild finalChildLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, childDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    assertNotNull(finalParentLoaded.getChild());
    assertNotNull(finalChildLoaded.getParent());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) RecursiveChild(org.mongodb.morphia.testmodel.RecursiveChild) RecursiveParent(org.mongodb.morphia.testmodel.RecursiveParent) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 34 with DBCollection

use of com.mongodb.DBCollection in project morphia by mongodb.

the class CharacterMappingTest method testMapping.

private Characters testMapping(final String field, final String value) {
    getMorphia().map(Characters.class);
    final DBCollection collection = getDs().getCollection(Characters.class);
    collection.insert(new BasicDBObject(field, value));
    return getDs().find(Characters.class).get();
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject)

Example 35 with DBCollection

use of com.mongodb.DBCollection in project morphia by mongodb.

the class QueryFactoryTest method createQuery.

@Test
public void createQuery() {
    final AtomicInteger counter = new AtomicInteger();
    final QueryFactory queryFactory = new DefaultQueryFactory() {

        @Override
        public <T> Query<T> createQuery(final Datastore datastore, final DBCollection collection, final Class<T> type, final DBObject query) {
            counter.incrementAndGet();
            return super.createQuery(datastore, collection, type, query);
        }
    };
    getDs().setQueryFactory(queryFactory);
    final Query<String> query = getDs().find(String.class);
    final Query<String> other = getDs().find(String.class);
    Assert.assertNotSame(other, query);
    Assert.assertEquals(2, counter.get());
}
Also used : DBCollection(com.mongodb.DBCollection) Datastore(org.mongodb.morphia.Datastore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Aggregations

DBCollection (com.mongodb.DBCollection)174 DBObject (com.mongodb.DBObject)92 BasicDBObject (com.mongodb.BasicDBObject)91 Test (org.junit.Test)70 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)29 DBCursor (com.mongodb.DBCursor)25 DB (com.mongodb.DB)23 MongoException (com.mongodb.MongoException)22 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)17 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 JSONObject (org.json.JSONObject)12 MongoClientURI (com.mongodb.MongoClientURI)11 List (java.util.List)11 Map (java.util.Map)11 QueryBuilder (com.mongodb.QueryBuilder)10 HashMap (java.util.HashMap)10 Stopwatch (com.google.common.base.Stopwatch)9 WriteResult (com.mongodb.WriteResult)9 MongoClient (com.mongodb.MongoClient)8 IOException (java.io.IOException)8