]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/request/WriteCancelTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / write / request / WriteCancelTest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.api.write.request;
13
14 import org.junit.Test;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.common.request.WriteRequest;
18 import org.simantics.db.exception.CancelTransactionException;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.testing.base.ExistingDatabaseTest;
21 import org.simantics.layer0.Layer0;
22
23 /**
24  * Checks that unexpected write callback failures do not crash the session and
25  * that the write request results were correctly committed.
26  * 
27  * @author Tuukka Lehtonen
28  */
29 public class WriteCancelTest extends ExistingDatabaseTest {
30
31     private static final String AN_ENTITY = "An entity";
32     Resource written;
33     Resource writtenName;
34
35         @Test
36     public void testWriteCancel() throws DatabaseException{
37
38         try {
39             getSession().syncRequest(new WriteRequest() {
40                 @Override
41                 public void perform(WriteGraph graph) throws DatabaseException {
42                     Layer0 b = Layer0.getInstance(graph);
43                     written = graph.newResource();
44                     writtenName = graph.newResource();
45                     graph.claim(written, b.InstanceOf, null, b.Entity);
46                     graph.claim(writtenName, b.InstanceOf, null, b.String);
47                     graph.claimValue(writtenName, AN_ENTITY);
48                     graph.claim(written, b.HasName, writtenName);
49
50                     //throw new DatabaseException("Intentional write request cancellation by unexpected exception");
51                     throw new CancelTransactionException("Intentional write request cancellation");
52                 }
53             });
54         } catch (DatabaseException e) {
55             if (DEBUG)
56                 e.printStackTrace();
57         }
58
59         // Make sure that we can at least start a new transaction after the
60         // cancelled one, but don't do anything yet.
61         getSession().syncRequest(new WriteRequest() {
62             @Override
63             public void perform(WriteGraph graph) throws DatabaseException {
64             }
65         });
66
67         // Ensure that the previously written data does not exist.
68         getSession().syncRequest(new WriteRequest() {
69             @Override
70             public void perform(WriteGraph graph) throws DatabaseException {
71                 Layer0 b = Layer0.getInstance(graph);
72                 assertTrue(!graph.hasStatement(written, b.InstanceOf, b.Entity));
73                 assertTrue(!graph.hasStatement(written, b.HasName, writtenName));
74                 assertTrue(!graph.hasStatement(writtenName, b.InstanceOf, b.String));
75                 assertTrue(!graph.hasValue(writtenName));
76             }
77         });
78     }
79
80 }