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.api.write.request;
14 import java.util.concurrent.Semaphore;
16 import org.junit.Test;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.testing.base.ExistingDatabaseTest;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.utils.datastructures.Callback;
26 * Checks that unexpected write callback failures do not crash the session and
27 * that the write request results were correctly committed.
29 * @author Tuukka Lehtonen
31 public class WriteCallbackFailureTest extends ExistingDatabaseTest {
33 private static final String AN_ENTITY = "An entity";
37 public void testFailingWriteCallback() throws DatabaseException{
38 final Semaphore sem = new Semaphore(0);
40 getSession().asyncRequest(new WriteRequest() {
42 public void perform(WriteGraph graph) throws DatabaseException {
43 Layer0 b = Layer0.getInstance(graph);
44 Resource test = graph.newResource();
45 graph.claim(test, b.InstanceOf, null, b.Entity);
46 Resource name = graph.newResource();
47 graph.claim(name, b.InstanceOf, null, b.String);
48 graph.claimValue(name, AN_ENTITY);
49 graph.claim(test, b.HasName, name);
53 }, new Callback<DatabaseException>() {
55 public void run(DatabaseException parameter) {
57 throw new NullPointerException("intentional failure");
66 } catch (InterruptedException e) {
67 throw new DatabaseException(e);
70 // This will block until the previous request has finished.
71 getSession().syncRequest(new WriteRequest() {
73 public void perform(WriteGraph graph) throws DatabaseException {
74 Layer0 b = Layer0.getInstance(graph);
75 // Ensure that the previously written data exists.
76 assertTrue(graph.hasStatement(written, b.InstanceOf, b.Entity));
77 Resource name = graph.getSingleObject(written, b.HasName);
78 assertTrue(graph.hasStatement(name, b.InstanceOf, b.String));
79 assertTrue(graph.hasStatement(written, b.HasName, name));
80 Object value = graph.getValue(name);
81 assertTrue(value.equals(AN_ENTITY));