]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncAsyncSyncTest4.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / request / misc / SyncAsyncSyncTest4.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.request.misc;
13
14 import java.util.ArrayList;
15 import java.util.concurrent.ConcurrentSkipListSet;
16
17 import org.junit.Test;
18 import org.simantics.db.AsyncReadGraph;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.procedure.AsyncProcedure;
24 import org.simantics.db.procedure.Procedure;
25 import org.simantics.db.request.AsyncRead;
26 import org.simantics.db.request.Read;
27 import org.simantics.db.testing.base.ExistingDatabaseTest;
28 import org.simantics.layer0.Layer0;
29
30 public class SyncAsyncSyncTest4 extends ExistingDatabaseTest {
31         
32         @Test
33         public void test() throws Exception {
34                 
35                 Session session = getSession();
36                 
37                 final ConcurrentSkipListSet<String> names = new ConcurrentSkipListSet<String>();
38                 final ArrayList<Resource> resources = new ArrayList<Resource>();
39                 final Resource root = SyncAsyncSyncUtils.createNames(session, names, resources);
40         final Layer0 b = Layer0.getInstance(getSession());
41
42                 session.syncRequest(new AsyncRead<Resource>() {
43
44             @Override
45                     public int threadHash() {
46                         return hashCode();
47                     }
48
49             @Override
50             public void perform(AsyncReadGraph graph, final AsyncProcedure<Resource> procedure) {
51
52                 graph.forPossibleObject(root, b.InstanceOf, new AsyncProcedure<Resource>() {
53
54                     @Override
55                     public void exception(AsyncReadGraph graph, Throwable throwable) {
56                     }
57
58                     @Override
59                     public void execute(AsyncReadGraph graph, Resource result) {
60
61                         procedure.execute(graph, result);
62                         
63                         for(final Resource r : resources) {
64
65                             graph.asyncRequest(new Read<String>() {
66
67                                 @Override
68                                 public String perform(ReadGraph graph) throws DatabaseException {
69                                     return graph.getRelatedValue(r, b.HasName);
70                                 }
71
72                                 @Override
73                                 public String toString() {
74                                     return "Read " + r.getResourceId();
75                                 }
76
77                             }, new Procedure<String>() {
78
79                                 @Override
80                                 public void exception(Throwable t) {
81                                     t.printStackTrace();
82                                 }
83
84                                 @Override
85                                 public void execute(String result) {
86                                     names.remove(result);
87                                 }
88
89                             });
90
91                         }
92                         
93                     }
94                     
95                 });
96             }
97             
98             @Override
99             public int getFlags() {
100                 return 0;
101             }
102
103                 });
104                 
105                 assert(names.isEmpty());
106                 
107         }
108
109 }