--- /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.support.virtualGraphSupport;
+
+import java.util.UUID;
+
+import org.junit.Test;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.VirtualGraph;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.request.WriteResultRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.VirtualGraphSupport;
+import org.simantics.db.testing.base.TestCleanDb;
+import org.simantics.layer0.Layer0;
+
+public class VirtualGraphTest extends TestCleanDb {
+
+ private Error exception;
+
+ @Test
+ public void test() throws DatabaseException {
+
+ Session session = getSession();
+
+ session.syncRequest(new WriteRequest() {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+
+ Layer0 L0 = Layer0.getInstance(graph);
+
+ final Resource newResource = graph.syncRequest(new WriteResultRequest<Resource>() {
+
+ @Override
+ public Resource perform(WriteGraph graph) throws DatabaseException {
+
+ Layer0 L0 = Layer0.getInstance(graph);
+ Resource result = graph.newResource();
+ graph.addLiteral(result, L0.HasName, L0.NameOf, L0.String, "RootLibrary", Bindings.STRING);
+ return result;
+
+ }
+
+ });
+
+ String rootName = graph.getRelatedValue(newResource, L0.HasName);
+ if(!rootName.equals("RootLibrary")) {
+ exception = new AssertionError("rootName != 'RootLibrary'");
+ throw exception;
+ }
+
+ VirtualGraphSupport support = graph.getSession().getService(VirtualGraphSupport.class);
+ final VirtualGraph virtual = support.getMemoryPersistent(UUID.randomUUID().toString());
+
+ graph.syncRequest(new WriteRequest(virtual) {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+
+ Layer0 L0 = Layer0.getInstance(graph);
+ graph.addLiteral(newResource, L0.HasName, L0.NameOf, L0.String, "RootLibrary2", Bindings.STRING);
+
+ }
+
+ });
+
+ rootName = graph.getRelatedValue(newResource, L0.HasName);
+ if(!rootName.equals("RootLibrary2")) {
+ exception = new AssertionError("rootName != 'RootLibrary2'");
+ throw exception;
+ }
+
+ support.discard(virtual);
+
+ rootName = graph.getRelatedValue(newResource, L0.HasName);
+ if(!rootName.equals("RootLibrary")) {
+ exception = new AssertionError("rootName != 'RootLibrary'");
+ throw exception;
+ }
+
+ }
+
+ });
+
+ if(exception != null) {
+ fail("Write transaction threw and exception (" + exception.getMessage() + ") which was not passed through ");
+ }
+
+ }
+
+}