]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/WriteCancelTest3.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / regression / bugs / WriteCancelTest3.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.regression.bugs;
13
14 import org.junit.Test;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Session;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.ReadRequest;
20 import org.simantics.db.common.request.WriteRequest;
21 import org.simantics.db.exception.AssumptionException;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.exception.ResourceNotFoundException;
24 import org.simantics.db.testing.base.ExistingDatabaseTest;
25 import org.simantics.layer0.Layer0;
26
27 /**
28  * Tests that exception during write causes canceling of changes.
29  * This tests must be added at least twice in order for the bug to appear.
30  * Even then the bug is not deterministic.
31  */
32 public class WriteCancelTest3 extends ExistingDatabaseTest {
33     final String resourceName = "WriteCancelTest3";
34     @Test
35     public void testWriteCancelTest3() throws Exception {
36         final Session session = getSession();
37         boolean exceptionThrown = false;
38         try {
39             session.syncRequest(new WriteRequest() {
40                 @Override
41                 public void perform(WriteGraph graph) throws DatabaseException {
42                     Layer0 L0 = Layer0.getInstance(graph);
43                     checkThatInstanceDoesNotExist(graph);
44                     Resource r1 = graph.newResource();
45                     graph.claim(r1, L0.InstanceOf, null, L0.Library);
46                     graph.claimLiteral(r1, L0.HasName, resourceName);
47                     Resource root = graph.getResource(ROOT_LIBRARY_URI);
48                     graph.claim(root, L0.ConsistsOf, L0.PartOf, r1);
49                     checkThatInstanceDoesExist(graph);
50                     throw new AssumptionException("Intentional exception during write.");
51                 }
52             });
53         } catch (AssumptionException e) {
54             exceptionThrown = true;
55             if (DEBUG)
56                 System.out.println("Catched AssumptionException as excepected:" + e.getMessage());
57         } catch (Throwable e) {
58             fail("Write transaction threw an unknown exception " + e);
59         }
60         assertTrue("Failed to throw exception as expected.", exceptionThrown);
61         session.sync(new ReadRequest() {
62             @Override
63             public void run(ReadGraph graph) throws DatabaseException {
64                 checkThatInstanceDoesNotExist(graph);
65             }
66         });
67     }
68     private void checkThatInstanceDoesExist(final ReadGraph graph) {
69         try {
70             String uri = ROOT_LIBRARY_URI + "/" + resourceName;
71             Resource r = graph.getResource(uri);
72             if (null == r)
73                 fail("Uri " + uri + " has not been defined.");
74         } catch (DatabaseException e) {
75             fail("Unexpected exception during read request: " + e.getMessage());
76         }
77     }
78     private void checkThatInstanceDoesNotExist(final ReadGraph graph) {
79         try {
80             String uri = ROOT_LIBRARY_URI + "/" + resourceName;
81             Resource r = graph.getResource(uri);
82             if (null != r)
83                 fail("Uri " + uri + " has been defined.");
84         } catch (ResourceNotFoundException e) {
85             if (DEBUG)
86                 System.out.println("Catched ResourceNotFoundException as expected.");
87         } catch (DatabaseException e) {
88             fail("Unexpected exception during read request: " + e.getMessage());
89         }
90     }
91 }