]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/readGraph/forEachObject/ForEachObjectTest1.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / readGraph / forEachObject / ForEachObjectTest1.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.api.readGraph.forEachObject;
13
14 import java.util.concurrent.atomic.AtomicInteger;
15
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;
29
30 public class ForEachObjectTest1 extends ExistingDatabaseTest {
31         
32         @Test
33         public void test() throws Exception {
34                 
35                 Session session = getSession();
36                 
37                 try {
38
39                         final Layer0 L0 = Layer0.getInstance(session);
40                         
41                 final Resource res = session.syncRequest(new WriteResultRequest<Resource>() {
42
43                         @Override
44                         public Resource perform(WriteGraph graph) throws DatabaseException {
45                                 
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);
49                                 return part;
50                                 
51                         }
52                         
53                 });
54                 
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>();
59
60                 session.syncRequest(new AsyncReadRequest() {
61
62                         @Override
63                         public void run(AsyncReadGraph graph) {
64
65                                 graph.forEachObject(res, L0.HasName, new AsyncMultiProcedure<Resource>() {
66
67                                         @Override
68                                         public void finished(AsyncReadGraph graph) {
69                                                 finished.incrementAndGet();
70                                         }
71
72                                         @Override
73                                         public void execute(AsyncReadGraph graph, Resource result) {
74                                                 execute.incrementAndGet();
75                                         }
76
77                                         @Override
78                                         public void exception(AsyncReadGraph graph, Throwable t) {
79                                                 exception.incrementAndGet();
80                                                 throwable.set(t);
81                                         }
82
83                                 });
84
85                         }
86
87                 });
88
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");
93  
94                 } catch(Throwable t) {
95
96                         fail("syncRequest threw unexpected throwable " + t);
97
98                 }
99
100         }
101
102 }