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());
}
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);
}
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) {
}
}
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());
}
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);
}
Aggregations