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();
}
}
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());
}
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());
}
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();
}
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());
}
Aggregations