/******************************************************************************* * 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.AsyncReadRequest; 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 { public R1(Resource resource) { super(resource); } @Override public String perform(ReadGraph graph) throws DatabaseException { return graph.getURI(resource); } } class R2 extends ResourceAsyncRead { public R2(Resource resource) { super(resource); } @Override public void perform(AsyncReadGraph graph, AsyncProcedure procedure) { graph.forURI(resource, procedure); } } class R3 extends UnaryRead { public R3(Resource resource) { super(resource); } @Override public String perform(ReadGraph graph) throws DatabaseException { return graph.getURI(parameter); } } class R4 extends UnaryAsyncRead { public R4(Resource resource) { super(resource); } @Override public void perform(AsyncReadGraph graph, AsyncProcedure procedure) { graph.forURI(parameter, procedure); } } class R5 extends UniqueRead { @Override public String perform(ReadGraph graph) throws DatabaseException { return graph.getURI(graph.getRootLibrary()); } } @Override protected void read(ReadGraph graph) throws DatabaseException { final ArrayList listeners = new ArrayList(); final Listener listener = new ListenerAdapter() { @Override public synchronized void execute(String result) { listeners.add(result); } @Override public boolean isDisposed() { return false; } }; 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.syncRequest(new AsyncReadRequest() { @Override public void run(AsyncReadGraph graph) { 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); } }