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.readGraph.forEachObject;
14 import java.util.concurrent.atomic.AtomicInteger;
16 import org.junit.Test;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.AsyncReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.AsyncReadRequest;
23 import org.simantics.db.common.request.WriteResultRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.procedure.AsyncMultiProcedure;
26 import org.simantics.db.testing.base.ExistingDatabaseTest;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.utils.DataContainer;
30 public class ForEachObjectTest1 extends ExistingDatabaseTest {
33 public void test() throws Exception {
35 Session session = getSession();
39 final Layer0 L0 = Layer0.getInstance(session);
41 final Resource res = session.syncRequest(new WriteResultRequest<Resource>() {
44 public Resource perform(WriteGraph graph) throws DatabaseException {
46 Resource part = graph.newResource();
47 graph.addLiteral(part, L0.HasName, L0.NameOf, L0.String, "A", Bindings.STRING);
48 graph.addLiteral(part, L0.HasName, L0.NameOf, L0.String, "B", Bindings.STRING);
55 final AtomicInteger finished = new AtomicInteger(0);
56 final AtomicInteger execute = new AtomicInteger(0);
57 final AtomicInteger exception = new AtomicInteger(0);
58 final DataContainer<Throwable> throwable = new DataContainer<Throwable>();
60 session.syncRequest(new AsyncReadRequest() {
63 public void run(AsyncReadGraph graph) {
65 graph.forEachObject(res, L0.HasName, new AsyncMultiProcedure<Resource>() {
68 public void finished(AsyncReadGraph graph) {
69 finished.incrementAndGet();
73 public void execute(AsyncReadGraph graph, Resource result) {
74 execute.incrementAndGet();
78 public void exception(AsyncReadGraph graph, Throwable t) {
79 exception.incrementAndGet();
89 if(finished.get() != 1) fail("finished was not 1");
90 if(execute.get() != 2) fail("execute was not 2 (was " + execute.get() + ")");
91 if(exception.get() != 0) fail("exception was not 0 (was " + exception.get() + ")");
92 if(throwable.get() != null) fail("there was a throwable");
94 } catch(Throwable t) {
96 fail("syncRequest threw unexpected throwable " + t);