]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/request/WriteCallbackFailureTest.java
Merge "Use Consumer interface instead of deprecated Callback interface"
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / write / request / WriteCallbackFailureTest.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 java.util.concurrent.Semaphore;
15
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
24 /**
25  * Checks that unexpected write callback failures do not crash the session and
26  * that the write request results were correctly committed.
27  * 
28  * @author Tuukka Lehtonen
29  */
30 public class WriteCallbackFailureTest extends ExistingDatabaseTest {
31
32     private static final String AN_ENTITY = "An entity";
33     Resource written;
34
35         @Test
36     public void testFailingWriteCallback() throws DatabaseException{
37         final Semaphore sem = new Semaphore(0);
38
39         getSession().asyncRequest(new WriteRequest() {
40             @Override
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);
49
50                 written = test;
51             }
52         }, parameter -> {
53             try {
54                 throw new NullPointerException("intentional failure");
55             } finally {
56                 sem.release();
57             }
58         });
59
60         try {
61             sem.acquire();
62         } catch (InterruptedException e) {
63             throw new DatabaseException(e);
64         }
65
66         // This will block until the previous request has finished.
67         getSession().syncRequest(new WriteRequest() {
68             @Override
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));
78             }
79         });
80     }
81
82 }