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.misc;
14 import java.util.ArrayList;
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.procedure.adapter.ListenerAdapter;
20 import org.simantics.db.common.request.ReadRequest;
21 import org.simantics.db.common.request.ResourceAsyncRead;
22 import org.simantics.db.common.request.ResourceRead;
23 import org.simantics.db.common.request.UnaryAsyncRead;
24 import org.simantics.db.common.request.UnaryRead;
25 import org.simantics.db.common.request.UniqueRead;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.procedure.AsyncProcedure;
28 import org.simantics.db.procedure.Listener;
29 import org.simantics.db.testing.base.WriteReadTest;
30 import org.simantics.layer0.Layer0;
32 public class RequestProcessorTest1 extends WriteReadTest {
34 class R1 extends ResourceRead<String> {
36 public R1(Resource resource) {
41 public String perform(ReadGraph graph) throws DatabaseException {
42 return graph.getURI(resource);
47 class R2 extends ResourceAsyncRead<String> {
49 public R2(Resource resource) {
54 public void perform(AsyncReadGraph graph, AsyncProcedure<String> procedure) {
55 graph.forURI(resource, procedure);
60 class R3 extends UnaryRead<Resource, String> {
62 public R3(Resource resource) {
67 public String perform(ReadGraph graph) throws DatabaseException {
68 return graph.getURI(parameter);
73 class R4 extends UnaryAsyncRead<Resource, String> {
75 public R4(Resource resource) {
80 public void perform(AsyncReadGraph graph, AsyncProcedure<String> procedure) {
81 graph.forURI(parameter, procedure);
86 class R5 extends UniqueRead<String> {
89 public String perform(ReadGraph graph) throws DatabaseException {
90 return graph.getURI(graph.getRootLibrary());
96 protected void read(ReadGraph graph) throws DatabaseException {
98 final ArrayList<String> listeners = new ArrayList<String>();
100 final Listener<String> listener = new ListenerAdapter<String>() {
103 public synchronized void execute(String result) {
104 listeners.add(result);
108 public boolean isDisposed() {
114 assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R1(L0.ConsistsOf)));
115 assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R2(L0.ConsistsOf)));
116 assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R3(L0.ConsistsOf)));
117 assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R4(L0.ConsistsOf)));
118 assertEquals("http:/", graph.sync(new R5()));
120 graph.sync(new ReadRequest() {
123 public void run(ReadGraph graph) throws DatabaseException {
125 graph.async(new R1(L0.ConsistsOf), listener);
126 graph.async(new R2(L0.ConsistsOf), listener);
127 graph.async(new R3(L0.ConsistsOf), listener);
128 graph.async(new R4(L0.ConsistsOf), listener);
129 graph.async(new R5(), listener);
135 assertEquals(listeners.size(), 5);