]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestProcessorTest1.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 / RequestProcessorTest1.java
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestProcessorTest1.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestProcessorTest1.java
new file mode 100644 (file)
index 0000000..be32a81
--- /dev/null
@@ -0,0 +1,134 @@
+/*******************************************************************************
+ * 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.ArrayList;
+
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.procedure.adapter.ListenerAdapter;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.ResourceAsyncRead;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.UnaryAsyncRead;
+import org.simantics.db.common.request.UnaryRead;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.db.procedure.Listener;
+import org.simantics.db.testing.base.WriteReadTest;
+import org.simantics.layer0.Layer0;
+
+public class RequestProcessorTest1 extends WriteReadTest {
+       
+       class R1 extends ResourceRead<String> {
+
+               public R1(Resource resource) {
+                       super(resource);
+               }
+
+               @Override
+               public String perform(ReadGraph graph) throws DatabaseException {
+                       return graph.getURI(resource);
+               }
+               
+       }
+
+       class R2 extends ResourceAsyncRead<String> {
+
+               public R2(Resource resource) {
+                       super(resource);
+               }
+
+               @Override
+               public void perform(AsyncReadGraph graph, AsyncProcedure<String> procedure) {
+                       graph.forURI(resource, procedure);
+               }
+               
+       }
+
+       class R3 extends UnaryRead<Resource, String> {
+
+               public R3(Resource resource) {
+                       super(resource);
+               }
+
+               @Override
+               public String perform(ReadGraph graph) throws DatabaseException {
+                       return graph.getURI(parameter);
+               }
+               
+       }
+
+       class R4 extends UnaryAsyncRead<Resource, String> {
+
+               public R4(Resource resource) {
+                       super(resource);
+               }
+
+               @Override
+               public void perform(AsyncReadGraph graph, AsyncProcedure<String> procedure) {
+                       graph.forURI(parameter, procedure);
+               }
+               
+       }
+
+       class R5 extends UniqueRead<String> {
+
+               @Override
+               public String perform(ReadGraph graph) throws DatabaseException {
+                       return graph.getURI(graph.getRootLibrary());
+               }
+               
+       }
+       
+       @Override
+       protected void read(ReadGraph graph) throws DatabaseException {
+               
+               final ArrayList<String> listeners = new ArrayList<String>();
+               
+               final Listener<String> listener = new ListenerAdapter<String>() {
+                       
+                       @Override
+                       public synchronized void execute(String result) {
+                               listeners.add(result);
+                       }
+                       
+               };
+               
+               assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R1(L0.ConsistsOf)));
+               assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R2(L0.ConsistsOf)));
+               assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R3(L0.ConsistsOf)));
+               assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R4(L0.ConsistsOf)));
+               assertEquals("http:/", graph.sync(new R5()));
+
+               graph.sync(new ReadRequest() {
+
+                       @Override
+                       public void run(ReadGraph graph) throws DatabaseException {
+                               
+                               graph.async(new R1(L0.ConsistsOf), listener);
+                               graph.async(new R2(L0.ConsistsOf), listener);
+                               graph.async(new R3(L0.ConsistsOf), listener);
+                               graph.async(new R4(L0.ConsistsOf), listener);
+                               graph.async(new R5(), listener);
+                               
+                       }
+                       
+               });
+
+               assertEquals(listeners.size(), 5);
+               
+       }
+
+}
\ No newline at end of file