1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.regression.bugs;
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;
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.
32 public class WriteCancelTest3 extends ExistingDatabaseTest {
33 final String resourceName = "WriteCancelTest3";
35 public void testWriteCancelTest3() throws Exception {
36 final Session session = getSession();
37 boolean exceptionThrown = false;
39 session.syncRequest(new WriteRequest() {
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.");
53 } catch (AssumptionException e) {
54 exceptionThrown = true;
56 System.out.println("Catched AssumptionException as excepected:" + e.getMessage());
57 } catch (Throwable e) {
58 fail("Write transaction threw an unknown exception " + e);
60 assertTrue("Failed to throw exception as expected.", exceptionThrown);
61 session.sync(new ReadRequest() {
63 public void run(ReadGraph graph) throws DatabaseException {
64 checkThatInstanceDoesNotExist(graph);
68 private void checkThatInstanceDoesExist(final ReadGraph graph) {
70 String uri = ROOT_LIBRARY_URI + "/" + resourceName;
71 Resource r = graph.getResource(uri);
73 fail("Uri " + uri + " has not been defined.");
74 } catch (DatabaseException e) {
75 fail("Unexpected exception during read request: " + e.getMessage());
78 private void checkThatInstanceDoesNotExist(final ReadGraph graph) {
80 String uri = ROOT_LIBRARY_URI + "/" + resourceName;
81 Resource r = graph.getResource(uri);
83 fail("Uri " + uri + " has been defined.");
84 } catch (ResourceNotFoundException e) {
86 System.out.println("Catched ResourceNotFoundException as expected.");
87 } catch (DatabaseException e) {
88 fail("Unexpected exception during read request: " + e.getMessage());