Search in sources :

Example 1 with Rectangle

use of dev.morphia.test.models.Rectangle in project morphia by mongodb.

the class TestTransactions method manual.

@Test
public void manual() {
    boolean success = false;
    int count = 0;
    while (!success && count < 5) {
        try (MorphiaSession session = getDs().startSession()) {
            session.startTransaction();
            Rectangle rectangle = new Rectangle(1, 1);
            session.save(rectangle);
            session.save(new User("transactions", LocalDate.now()));
            assertNull(getDs().find(Rectangle.class).first());
            assertNull(getDs().find(User.class).first());
            assertNotNull(session.find(Rectangle.class).first());
            assertNotNull(session.find(User.class).first());
            session.commitTransaction();
            success = true;
        } catch (MongoQueryException e) {
            if (e.getErrorCode() == 251 && e.getErrorMessage().contains("has been aborted")) {
                count++;
            } else {
                throw e;
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }
    assertNotNull(getDs().find(Rectangle.class).first());
    assertNotNull(getDs().find(User.class).first());
}
Also used : User(dev.morphia.test.models.User) MorphiaSession(dev.morphia.experimental.MorphiaSession) Rectangle(dev.morphia.test.models.Rectangle) MongoQueryException(com.mongodb.MongoQueryException) Test(org.testng.annotations.Test)

Example 2 with Rectangle

use of dev.morphia.test.models.Rectangle in project morphia by mongodb.

the class TestTransactions method modify.

@Test
public void modify() {
    Rectangle rectangle = new Rectangle(1, 1);
    getDs().withTransaction((session) -> {
        session.save(rectangle);
        assertNull(getDs().find(Rectangle.class).first());
        Rectangle modified = session.find(Rectangle.class).modify(inc("width", 13)).execute();
        assertNull(getDs().find(Rectangle.class).first());
        assertEquals(rectangle.getWidth(), modified.getWidth(), 0.5);
        assertEquals(rectangle.getWidth() + 13, session.find(Rectangle.class).first().getWidth(), 0.5);
        return null;
    });
    assertEquals(getDs().find(Rectangle.class).first().getWidth(), rectangle.getWidth() + 13, 0.5);
}
Also used : Rectangle(dev.morphia.test.models.Rectangle) Test(org.testng.annotations.Test)

Example 3 with Rectangle

use of dev.morphia.test.models.Rectangle in project morphia by mongodb.

the class TestUpdateOperations method testIncDec.

@Test
public void testIncDec() {
    final Rectangle[] array = { new Rectangle(1, 10), new Rectangle(1, 10), new Rectangle(1, 10), new Rectangle(10, 10), new Rectangle(10, 10) };
    for (Rectangle rect : array) {
        getDs().save(rect);
    }
    final Query<Rectangle> heightOf1 = getDs().find(Rectangle.class).filter(eq("height", 1D));
    final Query<Rectangle> heightOf2 = getDs().find(Rectangle.class).filter(eq("height", 2D));
    final Query<Rectangle> heightOf35 = getDs().find(Rectangle.class).filter(eq("height", 3.5D));
    assertThat(heightOf1.count(), is(3L));
    assertThat(heightOf2.count(), is(0L));
    UpdateResult results = heightOf1.update(inc("height")).execute(new UpdateOptions().multi(true));
    assertUpdated(results, 3);
    assertThat(heightOf1.count(), is(0L));
    assertThat(heightOf2.count(), is(3L));
    heightOf2.update(dec("height")).execute(new UpdateOptions().multi(true));
    assertThat(heightOf1.count(), is(3L));
    assertThat(heightOf2.count(), is(0L));
    heightOf1.update(inc("height", 2.5D)).execute(new UpdateOptions().multi(true));
    assertThat(heightOf1.count(), is(0L));
    assertThat(heightOf35.count(), is(3L));
    heightOf35.update(dec("height", 2.5D)).execute(new UpdateOptions().multi(true));
    assertThat(heightOf1.count(), is(3L));
    assertThat(heightOf35.count(), is(0L));
    getDs().find(Rectangle.class).filter(eq("height", 1D)).update(set("height", 1D), inc("width", 20D)).execute();
    MatcherAssert.assertThat(getDs().find(Rectangle.class).count(), is(5L));
    MatcherAssert.assertThat(getDs().find(Rectangle.class).filter(eq("height", 1D)).iterator(new FindOptions().limit(1)).next(), is(notNullValue()));
    MatcherAssert.assertThat(getDs().find(Rectangle.class).filter(eq("width", 30D)).iterator(new FindOptions().limit(1)).next(), is(notNullValue()));
    getDs().find(Rectangle.class).filter(eq("width", 30D)).update(set("height", 2D), set("width", 2D)).execute();
    MatcherAssert.assertThat(getDs().find(Rectangle.class).filter(eq("width", 1D)).iterator(new FindOptions().limit(1)).tryNext(), is(nullValue()));
    MatcherAssert.assertThat(getDs().find(Rectangle.class).filter(eq("width", 2D)).iterator(new FindOptions().limit(1)).next(), is(notNullValue()));
    heightOf35.update(dec("height", 1)).execute();
    heightOf35.update(dec("height", Long.MAX_VALUE)).execute();
    heightOf35.update(dec("height", 1.5f)).execute();
    heightOf35.update(dec("height", Double.MAX_VALUE)).execute();
    try {
        heightOf35.update(dec("height", new AtomicInteger(1)));
        Assert.fail("Wrong data type not recognized.");
    } catch (IllegalArgumentException ignore) {
    }
}
Also used : FindOptions(dev.morphia.query.FindOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Rectangle(dev.morphia.test.models.Rectangle) UpdateResult(com.mongodb.client.result.UpdateResult) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 4 with Rectangle

use of dev.morphia.test.models.Rectangle in project morphia by mongodb.

the class TestTransactions method insert.

@Test
public void insert() {
    Rectangle rectangle = new Rectangle(1, 1);
    getDs().withTransaction((session) -> {
        session.insert(rectangle);
        assertNull(getDs().find(Rectangle.class).first());
        assertEquals(session.find(Rectangle.class).count(), 1);
        return null;
    });
    assertNotNull(getDs().find(Rectangle.class).first());
}
Also used : Rectangle(dev.morphia.test.models.Rectangle) Test(org.testng.annotations.Test)

Example 5 with Rectangle

use of dev.morphia.test.models.Rectangle in project morphia by mongodb.

the class TestTransactions method update.

@Test
public void update() {
    Rectangle rectangle = new Rectangle(1, 1);
    getDs().withTransaction((session) -> {
        session.save(rectangle);
        assertNull(getDs().find(Rectangle.class).first());
        session.find(Rectangle.class).update(inc("width", 13)).execute();
        assertEquals(session.find(Rectangle.class).first().getWidth(), rectangle.getWidth() + 13, 0.5);
        assertNull(getDs().find(Rectangle.class).first());
        return null;
    });
    assertEquals(getDs().find(Rectangle.class).first().getWidth(), rectangle.getWidth() + 13, 0.5);
}
Also used : Rectangle(dev.morphia.test.models.Rectangle) Test(org.testng.annotations.Test)

Aggregations

Rectangle (dev.morphia.test.models.Rectangle)26 Test (org.testng.annotations.Test)25 FindOptions (dev.morphia.query.FindOptions)12 Datastore (dev.morphia.Datastore)2 Key (dev.morphia.Key)2 User (dev.morphia.test.models.User)2 ArrayList (java.util.ArrayList)2 MongoQueryException (com.mongodb.MongoQueryException)1 UpdateResult (com.mongodb.client.result.UpdateResult)1 UpdateOptions (dev.morphia.UpdateOptions)1 MorphiaSession (dev.morphia.experimental.MorphiaSession)1 FacebookUser (dev.morphia.test.models.FacebookUser)1 Keys (dev.morphia.test.models.Keys)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BeforeMethod (org.testng.annotations.BeforeMethod)1