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.request.exception;
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.procedure.adapter.ListenerAdapter;
20 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
21 import org.simantics.db.common.request.WriteRequest;
22 import org.simantics.db.common.request.WriteResultRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.exception.ResourceNotFoundException;
25 import org.simantics.db.request.Read;
26 import org.simantics.db.testing.base.ExistingDatabaseTest;
27 import org.simantics.layer0.Layer0;
29 public class RecoveryFromExceptedState extends ExistingDatabaseTest {
32 public void test() throws Exception {
34 Session session = getSession();
36 final Layer0 L0 = Layer0.getInstance(session);
38 final Resource A = session.syncRequest(new WriteResultRequest<Resource>() {
41 public Resource perform(WriteGraph graph) throws DatabaseException {
43 Resource root = graph.getRootLibrary();
44 Resource part = graph.newResource();
45 graph.claim(part, L0.InstanceOf, null, L0.Entity);
46 graph.claim(part, L0.PartOf, root);
47 graph.claimLiteral(part, L0.HasName, "A");
57 * Create the request. Listening is needed to enforce update of the query in subsequent write
59 session.syncRequest(new Read<Resource>() {
62 public Resource perform(ReadGraph graph) throws DatabaseException {
63 return graph.getResource("http://A");
66 }, new ListenerAdapter<Resource>());
69 * Invalidates the request. Request is updated (is has a listener) and enters 'Excepted' state.
71 session.syncRequest(new WriteRequest() {
74 public void perform(WriteGraph graph) throws DatabaseException {
77 graph.deny(graph.getRootLibrary(), L0.ConsistsOf, A);
84 * After this write the result is again valid. The request should be updated to 'Ready' state.
86 session.syncRequest(new WriteRequest() {
89 public void perform(WriteGraph graph) throws DatabaseException {
91 Resource root = graph.getRootLibrary();
92 Resource part = graph.newResource();
93 graph.claim(part, L0.InstanceOf, null, L0.Entity);
94 graph.claim(part, L0.PartOf, root);
95 graph.claimLiteral(part, L0.HasName, "A");
102 * Validate the situation.
104 session.syncRequest(new Read<Resource>() {
107 public Resource perform(ReadGraph graph) throws DatabaseException {
108 return graph.getResource("http://A");
111 }, TransientCacheListener.<Resource>instance());
113 } catch(ResourceNotFoundException e) {
115 fail("getResource threw ResourceNotFoundException");