--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.tests.api.request.exception;
+
+import org.junit.Test;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.procedure.adapter.ListenerAdapter;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.request.WriteResultRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ResourceNotFoundException;
+import org.simantics.db.request.Read;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.layer0.Layer0;
+
+public class RecoveryFromExceptedState extends ExistingDatabaseTest {
+
+ @Test
+ public void test() throws Exception {
+
+ Session session = getSession();
+
+ final Layer0 L0 = Layer0.getInstance(session);
+
+ final Resource A = session.syncRequest(new WriteResultRequest<Resource>() {
+
+ @Override
+ public Resource perform(WriteGraph graph) throws DatabaseException {
+
+ Resource root = graph.getRootLibrary();
+ Resource part = graph.newResource();
+ graph.claim(part, L0.InstanceOf, null, L0.Entity);
+ graph.claim(part, L0.PartOf, root);
+ graph.claimLiteral(part, L0.HasName, "A");
+ return part;
+
+ }
+
+ });
+
+ try {
+
+ /*
+ * Create the request. Listening is needed to enforce update of the query in subsequent write
+ */
+ session.syncRequest(new Read<Resource>() {
+
+ @Override
+ public Resource perform(ReadGraph graph) throws DatabaseException {
+ return graph.getResource("http://A");
+ }
+
+ }, new ListenerAdapter<Resource>());
+
+ /*
+ * Invalidates the request. Request is updated (is has a listener) and enters 'Excepted' state.
+ */
+ session.syncRequest(new WriteRequest() {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+
+ graph.deny(A);
+ graph.deny(graph.getRootLibrary(), L0.ConsistsOf, A);
+
+ }
+
+ });
+
+ /*
+ * After this write the result is again valid. The request should be updated to 'Ready' state.
+ */
+ session.syncRequest(new WriteRequest() {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+
+ Resource root = graph.getRootLibrary();
+ Resource part = graph.newResource();
+ graph.claim(part, L0.InstanceOf, null, L0.Entity);
+ graph.claim(part, L0.PartOf, root);
+ graph.claimLiteral(part, L0.HasName, "A");
+
+ }
+
+ });
+
+ /*
+ * Validate the situation.
+ */
+ session.syncRequest(new Read<Resource>() {
+
+ @Override
+ public Resource perform(ReadGraph graph) throws DatabaseException {
+ return graph.getResource("http://A");
+ }
+
+ }, TransientCacheListener.<Resource>instance());
+
+ } catch(ResourceNotFoundException e) {
+
+ fail("getResource threw ResourceNotFoundException");
+
+ }
+
+ }
+
+}