]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/delayedWrite/DelayedWriteRequestCancelHandling.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / delayedWrite / DelayedWriteRequestCancelHandling.java
1 package org.simantics.db.tests.api.delayedWrite;
2
3 import java.util.concurrent.atomic.AtomicReference;
4
5 import org.junit.Assert;
6 import org.junit.Test;
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;
16
17 /**
18  * Checks that throwing CancelTransactionException from within
19  * {@link DelayedWriteRequest#perform(WriteGraph)} will
20  * <ol>
21  * <li>Cancel the transaction correctly without getting stuck</li>
22  * <li>Write no data into the database</li>
23  * </ol>
24  * 
25  * @author Tuukka Lehtonen
26  */
27 public class DelayedWriteRequestCancelHandling extends ExistingDatabaseTest {
28
29     @Test
30     @Fails
31     public void test() throws Exception {
32         final AtomicReference<Resource> ref = new AtomicReference<Resource>();
33         try {
34             DelayedWriteRequest r = new DelayedWriteRequest() {
35                 @Override
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");
41                 }
42             };
43             getSession().sync(r);
44         } catch (CancelTransactionException e) {
45             // Should happen.
46         } catch (DatabaseException e) {
47             // Shouldn't happen.
48             fail("Got DatabaseException " + e + ", should've received CancelTransactionException");
49         }
50
51         // Just to check the database session is still alive.
52         Resource r = getSession().sync(new org.simantics.db.common.primitiverequest.Resource("http:/"));
53
54         fail("No data should've been written", getSession().sync(new UniqueRead<Boolean>() {
55             @Override
56             public Boolean perform(ReadGraph graph) throws DatabaseException {
57                 return graph.hasStatement( ref.get() );
58             }
59         }));
60     }
61
62 }