예제 #1
0
  @Test
  public void canUpdateWithACustomTypeId() throws Exception {

    ExternalFriend friend = new ExternalFriend(999, "Robert");
    collection.save(friend);

    friend.setName("Robert");
    collection.save(friend);

    ExternalFriend result = collection.findOne().as(ExternalFriend.class);
    assertThat(result.getId()).isEqualTo(999);
  }
예제 #2
0
  @Test
  public void canUpdateAnEntity() throws Exception {

    Friend john = new Friend("John", "21 Jump Street");
    collection.save(john);

    john.setAddress("new address");
    collection.save(john);

    ObjectId johnId = john.getId();
    Friend johnWithNewAddress = collection.findOne(johnId).as(Friend.class);
    assertThat(johnWithNewAddress.getId()).isEqualTo(johnId);
    assertThat(johnWithNewAddress.getAddress()).isEqualTo("new address");
  }
예제 #3
0
  @Test
  public void canUpdateWithObjectIdAsString() throws Exception {
    String id = ObjectId.get().toString();
    ExposableFriend robert = new ExposableFriend(id, "Robert");

    collection.save(robert);
    String johnId = robert.getId();

    robert.setName("Hue"); // <-- "famous" french Robert
    collection.save(robert);

    ExposableFriend robertHue = collection.findOne("{_id:#}", johnId).as(ExposableFriend.class);
    assertThat(robertHue.getId()).isEqualTo(johnId);
    assertThat(robertHue.getName()).isEqualTo("Hue");
  }
예제 #4
0
 public static void main(String[] args) throws Exception {
   Configuration config = new Configuration();
   Mongo mongo = new MongoImpl(config, "127.0.0.1:27018", "127.0.0.1:27017", "127.0.0.1:27019");
   MongoCollection collection = mongo.getDB("goojia").getCollection("goojia_common.ppc_map");
   BSONDocument doc = new MongoDocument();
   doc.put("city", "sh");
   doc.put("category", 1);
   doc.put("relate_id", 123);
   doc.put("relate_id_2", 0);
   collection.save(doc);
   Thread.sleep(20000);
   doc = new MongoDocument();
   doc.put("city", "bj");
   doc.put("category", 12);
   doc.put("relate_id", 12344);
   doc.put("relate_id_2", 0);
   collection.save(doc);
   mongo.close();
 }
예제 #5
0
  @Test
  public void shouldUseDefaultWriteConcern() throws Exception {

    Friend friend = new Friend("John", "22 Wall Street Avenue");

    WriteResult writeResult = collection.save(friend);

    assertThat(writeResult.getLastConcern())
        .isEqualTo(collection.getDBCollection().getWriteConcern());
  }
예제 #6
0
  @Test
  public void canSaveWithACustomTypeId() throws Exception {

    ExternalFriend john = new ExternalFriend(999, "Robert");

    collection.save(john);

    ExternalFriend result = collection.findOne().as(ExternalFriend.class);
    assertThat(result.getId()).isEqualTo(999);
  }
예제 #7
0
  @Test
  public void shouldFailWhenMarshallerFail() throws Exception {

    try {
      collection.save(new ErrorObject());
      Assert.fail();
    } catch (IllegalArgumentException e) {
      assertThat(e.getMessage()).contains("Unable to save object");
    }
  }
예제 #8
0
  @Test
  public void canSaveWithAnEmptyObjectIdAsString() throws Exception {

    ExposableFriend john = new ExposableFriend("Robert");

    collection.save(john);

    ExposableFriend result = collection.findOne().as(ExposableFriend.class);
    assertThat(result).isNotNull();
    assertThat(result.getId()).isNotNull();
  }
예제 #9
0
  @Test
  public void canSaveAnObjectWithoutIdAnnotation() throws Exception {

    Coordinate noId = new Coordinate(123, 1);

    collection.save(noId);

    Coordinate result = collection.findOne().as(Coordinate.class);
    assertThat(result).isNotNull();
    assertThat(result.lat).isEqualTo(123);
  }
예제 #10
0
  @Test
  public void canSaveWithObjectId() throws Exception {

    ObjectId oid = ObjectId.get();
    Friend john = new Friend(oid, "John");

    collection.save(john);

    Friend result = collection.findOne(oid).as(Friend.class);
    assertThat(result.getId()).isEqualTo(oid);
    assertThat(oid.isNew()).isFalse(); // insert
  }
예제 #11
0
  @Test
  public void canFindUsingSubProperty() throws Exception {
    /* given */
    collection.save(new Friend("John", new Coordinate(2, 31)));

    /* when */
    Iterator<Friend> results = collection.find("{'coordinate.lat':2}").as(Friend.class).iterator();

    /* then */
    assertThat(results.next().getCoordinate().lat).isEqualTo(2);
    assertThat(results.hasNext()).isFalse();
  }
예제 #12
0
  @Test
  public void canSaveWithObjectIdAsString() throws Exception {

    String id = ObjectId.get().toString();
    ExposableFriend john = new ExposableFriend(id, "Robert");

    collection.save(john);

    ExposableFriend result = collection.findOne().as(ExposableFriend.class);
    assertThat(result).isNotNull();
    assertThat(result.getId()).isEqualTo(id);
  }
예제 #13
0
  @Test
  public void canSave() throws Exception {

    Friend friend = new Friend("John", "22 Wall Street Avenue");

    collection.save(friend);

    Friend john = collection.findOne("{name:'John'}").as(Friend.class);
    assertThat(john).isNotNull();
    assertThat(john.getId()).isNotNull();
    assertThat(john.getId().isNew()).isFalse();
  }
예제 #14
0
  @Test
  public void canFind() throws Exception {
    /* given */
    Friend friend = new Friend(new ObjectId(), "John");
    collection.save(friend);

    /* when */
    Iterator<Friend> friends = collection.find("{name:'John'}").as(Friend.class).iterator();

    /* then */
    assertThat(friends.hasNext()).isTrue();
    assertThat(friends.next().getName()).isEqualTo("John");
    assertThat(friends.hasNext()).isFalse();
  }
예제 #15
0
  @Test
  public void canFindWithOid() throws Exception {
    /* given */
    ObjectId id = new ObjectId();
    Friend john = new Friend(id, "John");
    collection.save(john);

    Iterator<Friend> friends =
        collection.find("{_id:{$oid:#}}", id.toString()).as(Friend.class).iterator();

    /* then */
    assertThat(friends.hasNext()).isTrue();
    assertThat(friends.next().getId()).isEqualTo(id);
  }
예제 #16
0
  @Test
  public void canFindWithStringAsObjectId() {
    /* given */
    String id = ObjectId.get().toString();
    ExposableFriend friend = new ExposableFriend(id, "John");
    collection.save(friend);

    /* when */
    Iterator<ExposableFriend> friends =
        collection.find(withOid(friend.getId())).as(ExposableFriend.class).iterator();

    /* then */
    ExposableFriend john = friends.next();
    assertThat(john.getId()).isEqualTo(id);
    assertThat(john.getName()).isEqualTo("John");
    assertThat(friends.hasNext()).isFalse();
  }
예제 #17
0
  @Test
  public void canFindWithReadPreference() throws Exception {
    /* given */
    Friend friend = new Friend(new ObjectId(), "John");
    collection.save(friend);

    /* when */
    Iterator<Friend> friends =
        collection
            .withReadPreference(ReadPreference.primaryPreferred())
            .find("{name:'John'}")
            .as(Friend.class)
            .iterator();

    /* then */
    assertThat(friends.hasNext()).isTrue();
    assertThat(friends.next().getName()).isEqualTo("John");
    assertThat(friends.hasNext()).isFalse();

    // warning: we cannot check that ReadPreference is really used by driver, this unit test only
    // checks the API
  }