/** @see DATADOC-202 */ @Test public void executeDocumentWithCursorPreparer() { template.insert(new Person("Tom")); template.insert(new Person("Dick")); template.insert(new Person("Harry")); final List<String> names = new ArrayList<String>(); template.executeQuery( new Query(), template.getCollectionName(Person.class), new DocumentCallbackHandler() { public void processDocument(DBObject dbObject) { String name = (String) dbObject.get("firstName"); if (name != null) { names.add(name); } } }, new CursorPreparer() { public DBCursor prepare(DBCursor cursor) { cursor.limit(1); return cursor; } }); assertEquals(1, names.size()); // template.remove(new Query(), Person.class); }
@Test public void testEnsureIndex() throws Exception { Person p1 = new Person("Oliver"); p1.setAge(25); template.insert(p1); Person p2 = new Person("Sven"); p2.setAge(40); template.insert(p2); template .indexOps(Person.class) .ensureIndex(new Index().on("age", Order.DESCENDING).unique(Duplicates.DROP)); DBCollection coll = template.getCollection(template.getCollectionName(Person.class)); List<DBObject> indexInfo = coll.getIndexInfo(); assertThat(indexInfo.size(), is(2)); String indexKey = null; boolean unique = false; boolean dropDupes = false; for (DBObject ix : indexInfo) { if ("age_-1".equals(ix.get("name"))) { indexKey = ix.get("key").toString(); unique = (Boolean) ix.get("unique"); dropDupes = (Boolean) ix.get("dropDups"); } } assertThat(indexKey, is("{ \"age\" : -1}")); assertThat(unique, is(true)); assertThat(dropDupes, is(true)); List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo(); System.out.println(indexInfoList); assertThat(indexInfoList.size(), is(2)); IndexInfo ii = indexInfoList.get(1); assertThat(ii.isUnique(), is(true)); assertThat(ii.isDropDuplicates(), is(true)); assertThat(ii.isSparse(), is(false)); assertThat(ii.getFieldSpec().containsKey("age"), is(true)); assertThat(ii.getFieldSpec().containsValue(Order.DESCENDING), is(true)); }
protected void cleanDb() { template.dropCollection(template.getCollectionName(Person.class)); template.dropCollection(template.getCollectionName(PersonWithAList.class)); template.dropCollection(template.getCollectionName(PersonWith_idPropertyOfTypeObjectId.class)); template.dropCollection(template.getCollectionName(PersonWith_idPropertyOfTypeString.class)); template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfTypeObjectId.class)); template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfTypeString.class)); template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfTypeInteger.class)); template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfPrimitiveInt.class)); template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfTypeLong.class)); template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfPrimitiveLong.class)); template.dropCollection(template.getCollectionName(TestClass.class)); }
@Before public void setUp() { template1.dropCollection(template1.getCollectionName(Person.class)); }