X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2FreadGraph%2FforEachObject%2FForEachObjectTest1.java;fp=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2FreadGraph%2FforEachObject%2FForEachObjectTest1.java;h=7cd4791d78312760ede17f26dae8674b49952c6a;hb=67fd62f9c742337ec80eef658192db198a0efaac;hp=0000000000000000000000000000000000000000;hpb=cde82ba81327d5515fdca362f7f4c70f5103ae80;p=simantics%2Fplatform.git diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/readGraph/forEachObject/ForEachObjectTest1.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/readGraph/forEachObject/ForEachObjectTest1.java new file mode 100644 index 000000000..7cd4791d7 --- /dev/null +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/readGraph/forEachObject/ForEachObjectTest1.java @@ -0,0 +1,102 @@ +/******************************************************************************* + * 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.readGraph.forEachObject; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Test; +import org.simantics.databoard.Bindings; +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.AsyncReadRequest; +import org.simantics.db.common.request.WriteResultRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.AsyncMultiProcedure; +import org.simantics.db.testing.base.ExistingDatabaseTest; +import org.simantics.layer0.Layer0; +import org.simantics.utils.DataContainer; + +public class ForEachObjectTest1 extends ExistingDatabaseTest { + + @Test + public void test() throws Exception { + + Session session = getSession(); + + try { + + final Layer0 L0 = Layer0.getInstance(session); + + final Resource res = session.syncRequest(new WriteResultRequest() { + + @Override + public Resource perform(WriteGraph graph) throws DatabaseException { + + Resource part = graph.newResource(); + graph.addLiteral(part, L0.HasName, L0.NameOf, L0.String, "A", Bindings.STRING); + graph.addLiteral(part, L0.HasName, L0.NameOf, L0.String, "B", Bindings.STRING); + return part; + + } + + }); + + final AtomicInteger finished = new AtomicInteger(0); + final AtomicInteger execute = new AtomicInteger(0); + final AtomicInteger exception = new AtomicInteger(0); + final DataContainer throwable = new DataContainer(); + + session.syncRequest(new AsyncReadRequest() { + + @Override + public void run(AsyncReadGraph graph) { + + graph.forEachObject(res, L0.HasName, new AsyncMultiProcedure() { + + @Override + public void finished(AsyncReadGraph graph) { + finished.incrementAndGet(); + } + + @Override + public void execute(AsyncReadGraph graph, Resource result) { + execute.incrementAndGet(); + } + + @Override + public void exception(AsyncReadGraph graph, Throwable t) { + exception.incrementAndGet(); + throwable.set(t); + } + + }); + + } + + }); + + if(finished.get() != 1) fail("finished was not 1"); + if(execute.get() != 2) fail("execute was not 2 (was " + execute.get() + ")"); + if(exception.get() != 0) fail("exception was not 0 (was " + exception.get() + ")"); + if(throwable.get() != null) fail("there was a throwable"); + + } catch(Throwable t) { + + fail("syncRequest threw unexpected throwable " + t); + + } + + } + +}