コード例 #1
0
  /** @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);
  }
コード例 #2
0
 @Test
 public void testUsingReadPreference() throws Exception {
   this.template.execute(
       "readPref",
       new CollectionCallback<Object>() {
         public Object doInCollection(DBCollection collection)
             throws MongoException, DataAccessException {
           assertThat(collection.getOptions(), is(0));
           assertThat(collection.getDB().getOptions(), is(0));
           return null;
         }
       });
   MongoTemplate slaveTemplate = new MongoTemplate(factory);
   slaveTemplate.setReadPreference(ReadPreference.SECONDARY);
   slaveTemplate.execute(
       "readPref",
       new CollectionCallback<Object>() {
         public Object doInCollection(DBCollection collection)
             throws MongoException, DataAccessException {
           assertThat(collection.getReadPreference(), is(ReadPreference.SECONDARY));
           assertThat(collection.getDB().getOptions(), is(0));
           return null;
         }
       });
 }
コード例 #3
0
  private void addAndRetrievePerson(MongoTemplate template) {
    Person person = new Person("Oliver");
    person.setAge(25);
    template.insert(person);

    Person result = template.findById(person.getId(), Person.class);
    assertThat(result.getFirstName(), is("Oliver"));
    assertThat(result.getAge(), is(25));
  }
コード例 #4
0
  @Test
  public void insertsSimpleEntityCorrectly() throws Exception {

    Person person = new Person("Oliver");
    person.setAge(25);
    template.insert(person);

    List<Person> result =
        template.find(new Query(Criteria.where("_id").is(person.getId())), Person.class);
    assertThat(result.size(), is(1));
    assertThat(result, hasItem(person));
  }
コード例 #5
0
 @Test
 public void testFindAndUpdateUpsert() {
   template.insert(new Person("Tom", 21));
   template.insert(new Person("Dick", 22));
   Query query = new Query(Criteria.where("firstName").is("Harry"));
   Update update = new Update().set("age", 23);
   Person p =
       template.findAndModify(
           query, update, new FindAndModifyOptions().upsert(true).returnNew(true), Person.class);
   assertThat(p.getFirstName(), is("Harry"));
   assertThat(p.getAge(), is(23));
 }
コード例 #6
0
  @Test
  public void returnsEntityWhenQueryingForDateTime() {

    DateTime dateTime = new DateTime(2011, 3, 3, 12, 0, 0, 0);
    TestClass testClass = new TestClass(dateTime);
    mappingTemplate.save(testClass);

    List<TestClass> testClassList =
        mappingTemplate.find(
            new Query(Criteria.where("myDate").is(dateTime.toDate())), TestClass.class);
    assertThat(testClassList.size(), is(1));
    assertThat(testClassList.get(0).myDate, is(testClass.myDate));
  }
コード例 #7
0
  /** @see DATADOC-230 */
  @Test
  public void removesEntityFromCollection() {

    template.remove(new Query(), "mycollection");

    Person person = new Person("Dave");

    template.save(person, "mycollection");
    assertThat(template.findAll(TestClass.class, "mycollection").size(), is(1));

    template.remove(person, "mycollection");
    assertThat(template.findAll(Person.class, "mycollection").isEmpty(), is(true));
  }
コード例 #8
0
  /** @see DATADOC-183 */
  @Test
  public void countsDocumentsCorrectly() {

    assertThat(template.count(new Query(), Person.class), is(0L));

    Person dave = new Person("Dave");
    Person carter = new Person("Carter");

    template.save(dave);
    template.save(carter);

    assertThat(template.count(null, Person.class), is(2L));
    assertThat(template.count(query(where("firstName").is("Carter")), Person.class), is(1L));
  }
コード例 #9
0
  @Test
  public void testUsingAnInQueryWithStringId() throws Exception {

    template.remove(new Query(), PersonWithIdPropertyOfTypeString.class);

    PersonWithIdPropertyOfTypeString p1 = new PersonWithIdPropertyOfTypeString();
    p1.setFirstName("Sven");
    p1.setAge(11);
    template.insert(p1);
    PersonWithIdPropertyOfTypeString p2 = new PersonWithIdPropertyOfTypeString();
    p2.setFirstName("Mary");
    p2.setAge(21);
    template.insert(p2);
    PersonWithIdPropertyOfTypeString p3 = new PersonWithIdPropertyOfTypeString();
    p3.setFirstName("Ann");
    p3.setAge(31);
    template.insert(p3);
    PersonWithIdPropertyOfTypeString p4 = new PersonWithIdPropertyOfTypeString();
    p4.setFirstName("John");
    p4.setAge(41);
    template.insert(p4);

    Query q1 = new Query(Criteria.where("age").in(11, 21, 41));
    List<PersonWithIdPropertyOfTypeString> results1 =
        template.find(q1, PersonWithIdPropertyOfTypeString.class);
    Query q2 = new Query(Criteria.where("firstName").in("Ann", "Mary"));
    List<PersonWithIdPropertyOfTypeString> results2 =
        template.find(q2, PersonWithIdPropertyOfTypeString.class);
    Query q3 = new Query(Criteria.where("id").in(p3.getId(), p4.getId()));
    List<PersonWithIdPropertyOfTypeString> results3 =
        template.find(q3, PersonWithIdPropertyOfTypeString.class);
    assertThat(results1.size(), is(3));
    assertThat(results2.size(), is(2));
    assertThat(results3.size(), is(2));
  }
コード例 #10
0
  @Test
  public void testRemovingDocument() throws Exception {

    PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId();
    p1.setFirstName("Sven_to_be_removed");
    p1.setAge(51);
    template.insert(p1);

    Query q1 = new Query(Criteria.where("id").is(p1.getId()));
    PersonWithIdPropertyOfTypeObjectId found1 =
        template.findOne(q1, PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(found1, notNullValue());
    Query _q = new Query(Criteria.where("_id").is(p1.getId()));
    template.remove(_q, PersonWithIdPropertyOfTypeObjectId.class);
    PersonWithIdPropertyOfTypeObjectId notFound1 =
        template.findOne(q1, PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(notFound1, nullValue());

    PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId();
    p2.setFirstName("Bubba_to_be_removed");
    p2.setAge(51);
    template.insert(p2);

    Query q2 = new Query(Criteria.where("id").is(p2.getId()));
    PersonWithIdPropertyOfTypeObjectId found2 =
        template.findOne(q2, PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(found2, notNullValue());
    template.remove(q2, PersonWithIdPropertyOfTypeObjectId.class);
    PersonWithIdPropertyOfTypeObjectId notFound2 =
        template.findOne(q2, PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(notFound2, nullValue());
  }
コード例 #11
0
  @Test
  public void bogusUpdateDoesNotTriggerException() throws Exception {

    MongoTemplate mongoTemplate = new MongoTemplate(factory);
    mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);

    Person person = new Person("Oliver2");
    person.setAge(25);
    mongoTemplate.insert(person);

    Query q = new Query(Criteria.where("BOGUS").gt(22));
    Update u = new Update().set("firstName", "Sven");
    mongoTemplate.updateFirst(q, u, Person.class);
  }
コード例 #12
0
  /** @see DATADOC-349 */
  @Test
  public void removesEntityWithAnnotatedIdIfIdNeedsMassaging() {

    String id = new ObjectId().toString();

    Sample sample = new Sample();
    sample.id = id;

    template.save(sample);

    assertThat(template.findOne(query(where("id").is(id)), Sample.class).id, is(id));

    template.remove(sample);
    assertThat(template.findOne(query(where("id").is(id)), Sample.class), is(nullValue()));
  }
コード例 #13
0
 @Override
 protected void storeAssociationValue(
     AssociationValue associationValue, String sagaType, String sagaIdentifier) {
   AssociationValueEntry associationValueEntry =
       new AssociationValueEntry(sagaIdentifier, sagaType, associationValue);
   mongoTemplate.associationsCollection().save(associationValueEntry.asDBObject());
 }
コード例 #14
0
 @Override
 protected void updateSaga(Saga saga) {
   SagaEntry sagaEntry = new SagaEntry(saga, serializer);
   mongoTemplate
       .sagaCollection()
       .findAndModify(
           SagaEntry.queryByIdentifier(saga.getSagaIdentifier()), sagaEntry.asDBObject());
 }
コード例 #15
0
  @Test
  public void testFindAndRemove() throws Exception {

    Message m1 = new Message("Hello Spring");
    template.insert(m1);
    Message m2 = new Message("Hello Mongo");
    template.insert(m2);

    Query q = new Query(Criteria.where("text").regex("^Hello.*"));
    Message found1 = template.findAndRemove(q, Message.class);
    Message found2 = template.findAndRemove(q, Message.class);
    // Message notFound = template.findAndRemove(q, Message.class);
    DBObject notFound = template.getCollection("").findAndRemove(q.getQueryObject());
    assertThat(found1, notNullValue());
    assertThat(found2, notNullValue());
    assertThat(notFound, nullValue());
  }
コード例 #16
0
 @Override
 protected void removeAssociationValue(
     AssociationValue associationValue, String sagaType, String sagaIdentifier) {
   DBObject query =
       AssociationValueEntry.queryBySagaIdentifierAndAssociationKeyValue(
           sagaIdentifier, associationValue.getKey(), associationValue.getValue());
   mongoTemplate.associationsCollection().findAndRemove(query);
 }
コード例 #17
0
  @Test
  public void testUsingRegexQueryWithOptions() throws Exception {

    template.remove(new Query(), PersonWithIdPropertyOfTypeObjectId.class);

    PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId();
    p1.setFirstName("Sven");
    p1.setAge(11);
    template.insert(p1);
    PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId();
    p2.setFirstName("Mary");
    p2.setAge(21);
    template.insert(p2);
    PersonWithIdPropertyOfTypeObjectId p3 = new PersonWithIdPropertyOfTypeObjectId();
    p3.setFirstName("Ann");
    p3.setAge(31);
    template.insert(p3);
    PersonWithIdPropertyOfTypeObjectId p4 = new PersonWithIdPropertyOfTypeObjectId();
    p4.setFirstName("samantha");
    p4.setAge(41);
    template.insert(p4);

    Query q1 = new Query(Criteria.where("firstName").regex("S.*"));
    List<PersonWithIdPropertyOfTypeObjectId> results1 =
        template.find(q1, PersonWithIdPropertyOfTypeObjectId.class);
    Query q2 = new Query(Criteria.where("firstName").regex("S.*", "i"));
    List<PersonWithIdPropertyOfTypeObjectId> results2 =
        template.find(q2, PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(results1.size(), is(1));
    assertThat(results2.size(), is(2));
  }
コード例 #18
0
  /** @see DATADOC-246 */
  @Test
  public void updatesDBRefsCorrectly() {

    DBRef first = new DBRef(factory.getDb(), "foo", new ObjectId());
    DBRef second = new DBRef(factory.getDb(), "bar", new ObjectId());

    template.updateFirst(
        null, Update.update("dbRefs", Arrays.asList(first, second)), ClassWithDBRefs.class);
  }
コード例 #19
0
  /** @see DATADOC-240, DATADOC-212 */
  @Test
  public void updatesObjectIdsCorrectly() {

    PersonWithIdPropertyOfTypeObjectId person = new PersonWithIdPropertyOfTypeObjectId();
    person.setId(new ObjectId());
    person.setFirstName("Dave");

    template.save(person);
    template.updateFirst(
        query(where("id").is(person.getId())),
        update("firstName", "Carter"),
        PersonWithIdPropertyOfTypeObjectId.class);

    PersonWithIdPropertyOfTypeObjectId result =
        template.findById(person.getId(), PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(result, is(notNullValue()));
    assertThat(result.getId(), is(person.getId()));
    assertThat(result.getFirstName(), is("Carter"));
  }
コード例 #20
0
 private void checkPersonPersisted(MongoTemplate template) {
   template.execute(
       Person.class,
       new CollectionCallback<Object>() {
         public Object doInCollection(DBCollection collection)
             throws MongoException, DataAccessException {
           DBObject dbo = collection.findOne();
           assertThat((String) dbo.get("name"), is("Oliver"));
           return null;
         }
       });
 }
コード例 #21
0
 private synchronized void initialize() {
   if (!initialized) {
     DBCursor dbCursor = mongoTemplate.associationsCollection().find();
     getAssociationValueMap().clear();
     while (dbCursor.hasNext()) {
       AssociationValueEntry entry = new AssociationValueEntry(dbCursor.next());
       getAssociationValueMap()
           .add(entry.getAssociationValue(), entry.getSagaType(), entry.getSagaIdentifier());
     }
     initialized = true;
   }
 }
コード例 #22
0
  @Test
  public void testUsingUpdateWithMultipleSet() throws Exception {

    template.remove(new Query(), PersonWithIdPropertyOfTypeObjectId.class);

    PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId();
    p1.setFirstName("Sven");
    p1.setAge(11);
    template.insert(p1);
    PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId();
    p2.setFirstName("Mary");
    p2.setAge(21);
    template.insert(p2);

    Update u = new Update().set("firstName", "Bob").set("age", 10);

    WriteResult wr = template.updateMulti(new Query(), u, PersonWithIdPropertyOfTypeObjectId.class);

    assertThat(wr.getN(), is(2));

    Query q1 = new Query(Criteria.where("age").in(11, 21));
    List<PersonWithIdPropertyOfTypeObjectId> r1 =
        template.find(q1, PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(r1.size(), is(0));
    Query q2 = new Query(Criteria.where("age").is(10));
    List<PersonWithIdPropertyOfTypeObjectId> r2 =
        template.find(q2, PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(r2.size(), is(2));
    for (PersonWithIdPropertyOfTypeObjectId p : r2) {
      assertThat(p.getAge(), is(10));
      assertThat(p.getFirstName(), is("Bob"));
    }
  }
コード例 #23
0
  @Test
  public void testUsingAnOrQuery() throws Exception {

    template.remove(new Query(), PersonWithIdPropertyOfTypeObjectId.class);

    PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId();
    p1.setFirstName("Sven");
    p1.setAge(11);
    template.insert(p1);
    PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId();
    p2.setFirstName("Mary");
    p2.setAge(21);
    template.insert(p2);
    PersonWithIdPropertyOfTypeObjectId p3 = new PersonWithIdPropertyOfTypeObjectId();
    p3.setFirstName("Ann");
    p3.setAge(31);
    template.insert(p3);
    PersonWithIdPropertyOfTypeObjectId p4 = new PersonWithIdPropertyOfTypeObjectId();
    p4.setFirstName("John");
    p4.setAge(41);
    template.insert(p4);

    Query orQuery =
        new Query(new Criteria().orOperator(where("age").in(11, 21), where("age").is(31)));
    List<PersonWithIdPropertyOfTypeObjectId> results =
        template.find(orQuery, PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(results.size(), is(3));
    for (PersonWithIdPropertyOfTypeObjectId p : results) {
      assertThat(p.getAge(), isOneOf(11, 21, 31));
    }
  }
コード例 #24
0
  @Test
  public void testAddingToList() {
    PersonWithAList p = new PersonWithAList();
    p.setFirstName("Sven");
    p.setAge(22);
    template.insert(p);

    Query q1 = new Query(Criteria.where("id").is(p.getId()));
    PersonWithAList p2 = template.findOne(q1, PersonWithAList.class);
    assertThat(p2, notNullValue());
    assertThat(p2.getWishList().size(), is(0));

    p2.addToWishList("please work!");

    template.save(p2);

    PersonWithAList p3 = template.findOne(q1, PersonWithAList.class);
    assertThat(p3, notNullValue());
    assertThat(p3.getWishList().size(), is(1));

    Friend f = new Friend();
    p.setFirstName("Erik");
    p.setAge(21);

    p3.addFriend(f);
    template.save(p3);

    PersonWithAList p4 = template.findOne(q1, PersonWithAList.class);
    assertThat(p4, notNullValue());
    assertThat(p4.getWishList().size(), is(1));
    assertThat(p4.getFriends().size(), is(1));
  }
コード例 #25
0
  @Override
  protected Set<String> findAssociatedSagaIdentifiers(
      Class<? extends Saga> type, AssociationValue associationValue) {
    final BasicDBObject value = associationValueQuery(type, associationValue);

    DBCursor dbCursor =
        mongoTemplate.sagaCollection().find(value, new BasicDBObject("sagaIdentifier", 1));
    Set<String> found = new TreeSet<String>();
    while (dbCursor.hasNext()) {
      found.add((String) dbCursor.next().get("sagaIdentifier"));
    }
    return found;
  }
コード例 #26
0
 @Override
 protected void removeAssociationValue(
     AssociationValue associationValue, String sagaType, String sagaIdentifier) {
   mongoTemplate
       .sagaCollection()
       .update(
           new BasicDBObject("sagaIdentifier", sagaIdentifier).append("sagaType", sagaType),
           new BasicDBObject(
               "$pull",
               new BasicDBObject(
                   "associations",
                   new BasicDBObject("key", associationValue.getKey())
                       .append("value", associationValue.getValue()))));
 }
コード例 #27
0
  @Test
  public void testFindOneWithSort() {
    PersonWithAList p = new PersonWithAList();
    p.setFirstName("Sven");
    p.setAge(22);
    template.insert(p);

    PersonWithAList p2 = new PersonWithAList();
    p2.setFirstName("Erik");
    p2.setAge(21);
    template.insert(p2);

    PersonWithAList p3 = new PersonWithAList();
    p3.setFirstName("Mark");
    p3.setAge(40);
    template.insert(p3);

    // test query with a sort
    Query q2 = new Query(Criteria.where("age").gt(10));
    q2.sort().on("age", Order.DESCENDING);
    PersonWithAList p5 = template.findOne(q2, PersonWithAList.class);
    assertThat(p5.getFirstName(), is("Mark"));
  }
コード例 #28
0
 @Override
 public Saga load(String sagaIdentifier) {
   DBObject dbSaga =
       mongoTemplate.sagaCollection().findOne(SagaEntry.queryByIdentifier(sagaIdentifier));
   if (dbSaga == null) {
     return null;
   }
   SagaEntry sagaEntry = new SagaEntry(dbSaga);
   Saga loadedSaga = sagaEntry.getSaga(serializer);
   if (injector != null) {
     injector.injectResources(loadedSaga);
   }
   return loadedSaga;
 }
コード例 #29
0
  @Test
  public void testWriteConcernResolver() {

    PersonWithIdPropertyOfTypeObjectId person = new PersonWithIdPropertyOfTypeObjectId();
    person.setId(new ObjectId());
    person.setFirstName("Dave");

    template.setWriteConcern(WriteConcern.NONE);
    template.save(person);
    WriteResult result =
        template.updateFirst(
            query(where("id").is(person.getId())),
            update("firstName", "Carter"),
            PersonWithIdPropertyOfTypeObjectId.class);
    WriteConcern lastWriteConcern = result.getLastConcern();
    assertThat(lastWriteConcern, equalTo(WriteConcern.NONE));

    FsyncSafeWriteConcernResolver resolver = new FsyncSafeWriteConcernResolver();
    template.setWriteConcernResolver(resolver);
    Query q = query(where("_id").is(person.getId()));
    Update u = update("firstName", "Carter");
    result = template.updateFirst(q, u, PersonWithIdPropertyOfTypeObjectId.class);
    lastWriteConcern = result.getLastConcern();
    assertThat(lastWriteConcern, equalTo(WriteConcern.FSYNC_SAFE));

    MongoAction lastMongoAction = resolver.getMongoAction();
    assertThat(lastMongoAction.getCollectionName(), is("personWithIdPropertyOfTypeObjectId"));
    assertThat(lastMongoAction.getDefaultWriteConcern(), equalTo(WriteConcern.NONE));
    assertThat(lastMongoAction.getDocument(), notNullValue());
    assertThat(
        lastMongoAction.getEntityClass().toString(),
        is(PersonWithIdPropertyOfTypeObjectId.class.toString()));
    assertThat(lastMongoAction.getMongoActionOperation(), is(MongoActionOperation.UPDATE));
    assertThat(lastMongoAction.getQuery(), equalTo(q.getQueryObject()));
    assertThat(lastMongoAction.getDocument(), equalTo(u.getUpdateObject()));
  }
コード例 #30
0
  @Test
  public void testUsingInQueryWithList() throws Exception {

    template.remove(new Query(), PersonWithIdPropertyOfTypeObjectId.class);

    PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId();
    p1.setFirstName("Sven");
    p1.setAge(11);
    template.insert(p1);
    PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId();
    p2.setFirstName("Mary");
    p2.setAge(21);
    template.insert(p2);
    PersonWithIdPropertyOfTypeObjectId p3 = new PersonWithIdPropertyOfTypeObjectId();
    p3.setFirstName("Ann");
    p3.setAge(31);
    template.insert(p3);
    PersonWithIdPropertyOfTypeObjectId p4 = new PersonWithIdPropertyOfTypeObjectId();
    p4.setFirstName("John");
    p4.setAge(41);
    template.insert(p4);

    List<Integer> l1 = new ArrayList<Integer>();
    l1.add(11);
    l1.add(21);
    l1.add(41);
    Query q1 = new Query(Criteria.where("age").in(l1));
    List<PersonWithIdPropertyOfTypeObjectId> results1 =
        template.find(q1, PersonWithIdPropertyOfTypeObjectId.class);
    Query q2 = new Query(Criteria.where("age").in(l1.toArray()));
    List<PersonWithIdPropertyOfTypeObjectId> results2 =
        template.find(q2, PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(results1.size(), is(3));
    assertThat(results2.size(), is(3));
    try {
      List<Integer> l2 = new ArrayList<Integer>();
      l2.add(31);
      Query q3 = new Query(Criteria.where("age").in(l1, l2));
      template.find(q3, PersonWithIdPropertyOfTypeObjectId.class);
      Assert.fail("Should have trown an InvalidDocumentStoreApiUsageException");
    } catch (InvalidMongoDbApiUsageException e) {
    }
  }