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;
25 * Checks that unexpected write callback failures do not crash the session and
26 * that the write request results were correctly committed.
28 * @author Tuukka Lehtonen
30 public class WriteCallbackFailureTest extends ExistingDatabaseTest {
32 private static final String AN_ENTITY = "An entity";
36 public void testFailingWriteCallback() throws DatabaseException{
37 final Semaphore sem = new Semaphore(0);
39 getSession().asyncRequest(new WriteRequest() {
41 public void perform(WriteGraph graph) throws DatabaseException {
42 Layer0 b = Layer0.getInstance(graph);
43 Resource test = graph.newResource();
44 graph.claim(test, b.InstanceOf, null, b.Entity);
45 Resource name = graph.newResource();
46 graph.claim(name, b.InstanceOf, null, b.String);
47 graph.claimValue(name, AN_ENTITY);
48 graph.claim(test, b.HasName, name);
54 throw new NullPointerException("intentional failure");
62 } catch (InterruptedException e) {
63 throw new DatabaseException(e);
66 // This will block until the previous request has finished.
67 getSession().syncRequest(new WriteRequest() {
69 public void perform(WriteGraph graph) throws DatabaseException {
70 Layer0 b = Layer0.getInstance(graph);
71 // Ensure that the previously written data exists.
72 assertTrue(graph.hasStatement(written, b.InstanceOf, b.Entity));
73 Resource name = graph.getSingleObject(written, b.HasName);
74 assertTrue(graph.hasStatement(name, b.InstanceOf, b.String));
75 assertTrue(graph.hasStatement(written, b.HasName, name));
76 Object value = graph.getValue(name);
77 assertTrue(value.equals(AN_ENTITY));