Package org.bson

Examples of org.bson.BasicBSONObject


    buffer.clear();
    writer.writeTo(buffer, actualWrite);
    buffer.flip();
    byte[] actualBytes = new byte[buffer.remaining()];
    buffer.get(actualBytes);
    byte[] expectedBytes = BSON.encode(new BasicBSONObject((Map<?, ?>) actualWrite));
    assertArrayEquals(expectedBytes, actualBytes);
  }
View Full Code Here


  }
 
  @Test
  public void testUpdate() throws Exception {
   
    client.delete(new Delete("test", "update", new BasicBSONObject("_id", "updateId")));
    // Test null update
    client.update(new Update("test", "update",
        new BasicBSONObject("_id", "updateId"),
        new BasicBSONObject("name", "abcdefg")
    ));
    Response response = client.query(new Query("test", "update", 0, 1, new BasicBSONObject("_id", "updateId"), null));
    Assert.assertEquals(response.getNumberReturned(), 0);

    client.insert(new Insert("test", "update", new BasicBSONObject("_id", "updateId").append("name", "testname")));
    response = client.query(new Query("test", "update", 0, 1, new BasicBSONObject("_id", "updateId"), null));
    Assert.assertEquals(1, response.getNumberReturned());
   
    client.update(new Update("test", "update", new BasicBSONObject("_id", "updateId"), new BasicBSONObject("_id", "updateId").append("name", "testname updated")));
    response = client.query(new Query("test", "update", 0, 1, new BasicBSONObject("_id", "updateId"), null));
    Assert.assertEquals(1, response.getNumberReturned());
    BSONObject result = response.getDocuments().get(0);
   
    Assert.assertEquals("updateId", result.get("_id"));
    Assert.assertEquals("testname updated", result.get("name"));
View Full Code Here

  }
 
  @Test
  public void testUpsert() throws Exception {
   
    client.delete(new Delete("test", "update", new BasicBSONObject("_id", "updateId")));
   
    Update update = new Update("test", "update",
        new BasicBSONObject("_id", "updateId"),
        new BasicBSONObject()
          .append("_id", "updateId")
          .append("name", "test-name")
          .append("num", 100)
    ).upsert();
    client.update(update);
   
    Query query = new Query("test", "update", 0, 1, new BasicBSONObject("_id", "updateId"), null);
    Response response = client.query(query);
    Assert.assertNotNull(response);
    Assert.assertEquals(1, response.getNumberReturned());
    Assert.assertEquals(0, response.getStartingFrom());
    Assert.assertEquals(query.getWaitingRequestId(), response.getHeader().getResponseTo());
View Full Code Here

    client = new MongoClient();
    client.setHosts("127.0.0.1:27017");
    client.awaitOpen();
    Assert.assertTrue(client.isOpen());
    collection = client.getCollection("test", "concurrent");
    collection.remove(new BasicBSONObject());
  }
View Full Code Here

    }
    @Override
    public void run() {
      for (int i = 0; i < 1000; i++) {
        int key = id * 1000 + i;
        BSONObject obj = new BasicBSONObject("_id", key)
        .append("name","name-" + key)
        .append("value", "value-" + key);
        collection.insert(obj);
        int inserted = 0;
        if ((inserted = counter.incrementAndGet()) % 2000 == 0) {
          log.info("Inserted " + inserted + " records");
        }
        BSONObject saved = collection.find(new BasicBSONObject("_id", key));
        Assert.assertNotNull(saved);
        Assert.assertEquals(obj, saved);
      }
    }
View Full Code Here

    }
    @Override
    public void run() {
      for (int i = 0; i < 1000; i++) {
        int key = id * 1000 + i;
        BSONObject obj = new BasicBSONObject("$set",new BasicBSONObject("name", "updated-name-" + key));
        collection.update(new BasicBSONObject("_id", key), obj);
        int updated = 0;
        if ((updated = counter.incrementAndGet()) % 2000 == 0) {
          log.info("Updated " + updated + " records");
        }
        BSONObject saved = collection.find(new BasicBSONObject("_id", key));
        Assert.assertNotNull(saved);
        Assert.assertEquals(key, saved.get("_id"));
        Assert.assertEquals("updated-name-" + key, saved.get("name"));
        Assert.assertEquals("value-" + key, saved.get("value"));
      }
View Full Code Here

    }
    @Override
    public void run() {
      for (int i = 0; i < 1000; i++) {
        int key = id * 1000 + i;
        Assert.assertEquals(1, collection.count(new BasicBSONObject("_id", key)));
        collection.remove(new BasicBSONObject("_id", key));
        Assert.assertEquals(0, collection.count(new BasicBSONObject("_id", key)));
        Assert.assertNull(collection.find(new BasicBSONObject("_id", key)));
        int removed = 0;
        if ((removed = counter.incrementAndGet()) % 2000 == 0) {
          log.info("Removed " + removed + " records");
        }
      }
View Full Code Here

  }
 
  @Test
  public void testInsert() throws Exception {
   
    client.delete(new Delete("test", "insert", new BasicBSONObject("_id", 1)));
   
    // insert new document
    client.insert(
        new Insert("test", "insert",
          new BasicBSONObject("_id", 1)
            .append("name", "test-name")
            .append("integer", 100)
            .append("long", 1000L)
            .append("binary", "test binary".getBytes())
    ));

    // find
    Query query = new Query("test", "insert", 0, 1, new BasicBSONObject("_id", 1), null);
    Response response = client.query(query);
    Assert.assertNotNull(response);
    Assert.assertEquals(1, response.getNumberReturned());
    Assert.assertEquals(0, response.getStartingFrom());
    Assert.assertEquals(query.getWaitingRequestId(), response.getHeader().getResponseTo());
   
    BSONObject resultObject = response.getDocuments().get(0);
    Assert.assertEquals(1, resultObject.get("_id"));
    Assert.assertEquals("test-name", resultObject.get("name"));
    Assert.assertEquals(100, resultObject.get("integer"));
    Assert.assertEquals(1000L, resultObject.get("long"));
    Assert.assertArrayEquals("test binary".getBytes(), (byte[]) resultObject.get("binary"));
   
    // Duplicate error
    MongoException me = null;
    try {
      client.insert(
          new Insert("test", "insert",
              new BasicBSONObject("_id", 1)
                .append("name", "test-name2")
          )
      );
    } catch (MongoException e) {
      me = e;
View Full Code Here

 
  @Test
  public void testDrop() throws Exception {
   
    MongoCollection collection = client.getCollection("test", "client");
    collection.upsert(new BasicBSONObject("_id", 1), new BasicBSONObject("_id", 1).append("name", "name-1"));
    collection.drop();
   
    Assert.assertEquals(0, collection.count());
       
  }
View Full Code Here

  @Test
  public void testInsert() throws Exception {
   
    MongoCollection collection = client.getCollection("test", "client");
   
    collection.remove(new BasicBSONObject());
    Assert.assertEquals(0, collection.count());
   
    for (int i = 0; i < 100; i++) {
      BSONObject doc = new BasicBSONObject()
      .append("_id", i)
      .append("name", "name-" + i)
      .append("value", "value-" + i);
      collection.insert(doc);
    }
View Full Code Here

TOP

Related Classes of org.bson.BasicBSONObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.