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