]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/readGraph/forObjectSet/ForObjectSetTest1.java
Multiple reader thread support for db client
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / readGraph / forObjectSet / ForObjectSetTest1.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.forObjectSet;
13
14 import org.junit.Test;
15 import org.simantics.db.AsyncReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Session;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.procedure.AsyncSetListener;
22 import org.simantics.db.testing.base.ExistingDatabaseTest;
23 import org.simantics.layer0.Layer0;
24
25 public class ForObjectSetTest1 extends ExistingDatabaseTest {
26         
27         private Throwable exception; 
28         private Resource relation;
29         private int adds = 0;
30         private int removes = 0;
31         
32         @Test
33         public void testObjectSet() throws Exception {
34                 
35                 Session session = getSession();
36
37                 session.syncRequest(new WriteRequest() {
38
39                         @Override
40                         public void perform(WriteGraph graph) throws DatabaseException {
41                                 
42                                 Layer0 b = Layer0.getInstance(graph);
43                                 
44                                 relation = graph.newResource();
45                                 graph.claim(relation, b.SubrelationOf, b.IsRelatedTo);
46                                 graph.claim(relation, b.InverseOf, relation);
47                                 
48                         session.syncRequest(new TestAsyncReadRequest() {
49
50                             @Override
51                             public void run(AsyncReadGraph graph) throws Throwable {
52
53                                                 graph.forObjectSet(graph.getRootLibrary(), relation, new AsyncSetListener<Resource>() {
54
55                                                         @Override
56                                                         public void add(AsyncReadGraph graph, Resource result) {
57                                                                 synchronized(this) {
58                                                                         adds++;
59                                                                 }
60                                                         }
61
62                                                         @Override
63                                                         public void remove(AsyncReadGraph graph, Resource result) {
64                                                                 synchronized(this) {
65                                                                         removes++;
66                                                                 }
67                                                         }
68
69                                                         @Override
70                                                         public void exception(AsyncReadGraph graph, Throwable t) {
71                                                                 exception = t;
72                                                         }
73
74                                                         @Override
75                                                         public boolean isDisposed() {
76                                                                 return false;
77                                                         }
78                                                         
79                                                 });
80                                 
81                             }
82                             
83                         });
84                         
85                         }
86                         
87                 });
88                 
89                 session.syncRequest(new WriteRequest() {
90
91                         @Override
92                         public void perform(WriteGraph g) throws DatabaseException {
93                                 
94                                 Layer0 b = Layer0.getInstance(g);
95                                 // Add
96                                 g.claim(g.getRootLibrary(), relation, b.Abstract);
97                                 
98                         }
99                         
100                 });
101                 
102                 assert(adds == 1 && removes == 0);
103
104                 session.syncRequest(new WriteRequest() {
105
106                         @Override
107                         public void perform(WriteGraph g) throws DatabaseException {
108                                 
109                                 Layer0 b = Layer0.getInstance(g);
110                                 // Nop
111                                 g.claim(g.getRootLibrary(), relation, b.Abstract);
112                                 
113                         }
114                         
115                 });
116
117                 assert(adds == 1 && removes == 0);
118
119                 session.syncRequest(new WriteRequest() {
120
121                         @Override
122                         public void perform(WriteGraph g) throws DatabaseException {
123                                 
124                                 Layer0 b = Layer0.getInstance(g);
125                                 // Remove
126                                 g.deny(g.getRootLibrary(), relation, b.Abstract);
127                                 
128                         }
129                         
130                 });
131
132                 assert(adds == 1 && removes == 1);
133
134                 session.syncRequest(new WriteRequest() {
135
136                         @Override
137                         public void perform(WriteGraph g) throws DatabaseException {
138                                 
139                                 Layer0 b = Layer0.getInstance(g);
140                                 // Nop
141                                 g.deny(g.getRootLibrary(), relation, b.Abstract);
142                                 
143                         }
144                         
145                 });
146
147                 assert(adds == 1 && removes == 1);
148
149                 session.syncRequest(new WriteRequest() {
150
151                         @Override
152                         public void perform(WriteGraph g) throws DatabaseException {
153                                 
154                                 Layer0 b = Layer0.getInstance(g);
155                                 // nop
156                                 g.claim(g.getRootLibrary(), relation, b.Abstract);
157                                 g.deny(g.getRootLibrary(), relation, b.Abstract);
158                                 
159                         }
160                         
161                 });
162
163                 assert(adds == 1 && removes == 1);
164
165                 if(exception != null) {
166                         fail("Write transaction threw and exception (" + exception.getMessage() + ") which was not passed through ");
167                 }
168                 
169         }
170
171 }