1 package org.simantics.db.tests.api.delayedWrite;
3 import java.util.concurrent.atomic.AtomicReference;
5 import org.junit.Assert;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.common.request.DelayedWriteRequest;
11 import org.simantics.db.common.request.UniqueRead;
12 import org.simantics.db.exception.CancelTransactionException;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.testing.annotation.Fails;
15 import org.simantics.db.testing.base.ExistingDatabaseTest;
18 * Checks that throwing CancelTransactionException from within
19 * {@link DelayedWriteRequest#perform(WriteGraph)} will
21 * <li>Cancel the transaction correctly without getting stuck</li>
22 * <li>Write no data into the database</li>
25 * @author Tuukka Lehtonen
27 public class DelayedWriteRequestCancelHandling extends ExistingDatabaseTest {
31 public void test() throws Exception {
32 final AtomicReference<Resource> ref = new AtomicReference<Resource>();
34 DelayedWriteRequest r = new DelayedWriteRequest() {
36 public void perform(WriteGraph graph) throws DatabaseException {
37 // Should throw an exception / error
38 ref.set(graph.newResource());
39 graph.claim(ref.get(), L0.InstanceOf, null, L0.String);
40 throw new CancelTransactionException("intentional cancel");
44 } catch (CancelTransactionException e) {
46 } catch (DatabaseException e) {
48 fail("Got DatabaseException " + e + ", should've received CancelTransactionException");
51 // Just to check the database session is still alive.
52 Resource r = getSession().sync(new org.simantics.db.common.primitiverequest.Resource("http:/"));
54 fail("No data should've been written", getSession().sync(new UniqueRead<Boolean>() {
56 public Boolean perform(ReadGraph graph) throws DatabaseException {
57 return graph.hasStatement( ref.get() );