]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/GraphSupportTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / request / misc / GraphSupportTest.java
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/GraphSupportTest.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/GraphSupportTest.java
new file mode 100644 (file)
index 0000000..d204c89
--- /dev/null
@@ -0,0 +1,107 @@
+/*******************************************************************************
+ * 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.misc;
+
+import java.util.Collection;
+import java.util.concurrent.atomic.AtomicInteger;
+
+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.request.ReadRequest;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.request.WriteResultRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.SyncListener;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.layer0.Layer0;
+
+public class GraphSupportTest extends ExistingDatabaseTest {
+       
+       @Test
+    public void test() throws Exception {
+               
+       final AtomicInteger runs = new AtomicInteger(0);
+       
+               Session session = getSession();
+               
+               final Resource test = session.syncRequest(new WriteResultRequest<Resource>() {
+            @Override
+            public Resource perform(WriteGraph graph) throws DatabaseException {
+                return graph.newResource();
+            }
+               });
+
+               class Request extends ResourceRead<Collection<Resource>> {
+                       
+                       public Request(Resource resource) {
+                               super(resource);
+                       }
+
+                       @Override
+                       public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
+                               return graph.getObjects(resource, Layer0.getInstance(graph).ConsistsOf);
+                       }
+                       
+               }
+               
+               session.syncRequest(new ReadRequest() {
+
+                       @Override
+                       public void run(ReadGraph graph) throws DatabaseException {
+                               
+                               graph.syncRequest(new Request(test), new SyncListener<Collection<Resource>>() {
+
+                                       @Override
+                                       public void execute(ReadGraph graph, Collection<Resource> result) throws DatabaseException {
+                                               
+                                               graph.syncRequest(new ReadRequest() {
+
+                                                       @Override
+                                                       public void run(ReadGraph graph) throws DatabaseException {
+                                                               runs.incrementAndGet();
+                                                       }
+                                                       
+                                               });
+                                       }
+
+                                       @Override
+                                       public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
+                                       }
+
+                                       @Override
+                                       public boolean isDisposed() {
+                                               return false;
+                                       }
+                               });
+                               
+                       }
+                       
+               });
+               
+               assert(runs.get() == 1);
+
+               session.syncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                graph.claim(test, Layer0.getInstance(graph).ConsistsOf, graph.newResource());
+            }
+               });
+
+               assert(runs.get() == 2);
+               
+       }
+
+}