+/*******************************************************************************
+ * 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<Resource>() {
+
+ @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> throwable = new DataContainer<Throwable>();
+
+ session.syncRequest(new AsyncReadRequest() {
+
+ @Override
+ public void run(AsyncReadGraph graph) {
+
+ graph.forEachObject(res, L0.HasName, new AsyncMultiProcedure<Resource>() {
+
+ @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);
+
+ }
+
+ }
+
+}