]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/readGraph/forObjectSet/ForObjectSetTest1.java
1a6a3a0b44a01604c301b73b6f899af061dcd527
[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                                 graph.forObjectSet(graph.getRootLibrary(), relation, new AsyncSetListener<Resource>() {
49
50                                         @Override
51                                         public void add(AsyncReadGraph graph, Resource result) {
52                                                 synchronized(this) {
53                                                         adds++;
54                                                 }
55                                         }
56
57                                         @Override
58                                         public void remove(AsyncReadGraph graph, Resource result) {
59                                                 synchronized(this) {
60                                                         removes++;
61                                                 }
62                                         }
63
64                                         @Override
65                                         public void exception(AsyncReadGraph graph, Throwable t) {
66                                                 exception = t;
67                                         }
68
69                                         @Override
70                                         public boolean isDisposed() {
71                                                 return false;
72                                         }
73                                         
74                                 });
75                         }
76                         
77                 });
78                 
79                 session.syncRequest(new WriteRequest() {
80
81                         @Override
82                         public void perform(WriteGraph g) throws DatabaseException {
83                                 
84                                 Layer0 b = Layer0.getInstance(g);
85                                 // Add
86                                 g.claim(g.getRootLibrary(), relation, b.Abstract);
87                                 
88                         }
89                         
90                 });
91                 
92                 assert(adds == 1 && removes == 0);
93
94                 session.syncRequest(new WriteRequest() {
95
96                         @Override
97                         public void perform(WriteGraph g) throws DatabaseException {
98                                 
99                                 Layer0 b = Layer0.getInstance(g);
100                                 // Nop
101                                 g.claim(g.getRootLibrary(), relation, b.Abstract);
102                                 
103                         }
104                         
105                 });
106
107                 assert(adds == 1 && removes == 0);
108
109                 session.syncRequest(new WriteRequest() {
110
111                         @Override
112                         public void perform(WriteGraph g) throws DatabaseException {
113                                 
114                                 Layer0 b = Layer0.getInstance(g);
115                                 // Remove
116                                 g.deny(g.getRootLibrary(), relation, b.Abstract);
117                                 
118                         }
119                         
120                 });
121
122                 assert(adds == 1 && removes == 1);
123
124                 session.syncRequest(new WriteRequest() {
125
126                         @Override
127                         public void perform(WriteGraph g) throws DatabaseException {
128                                 
129                                 Layer0 b = Layer0.getInstance(g);
130                                 // Nop
131                                 g.deny(g.getRootLibrary(), relation, b.Abstract);
132                                 
133                         }
134                         
135                 });
136
137                 assert(adds == 1 && removes == 1);
138
139                 session.syncRequest(new WriteRequest() {
140
141                         @Override
142                         public void perform(WriteGraph g) throws DatabaseException {
143                                 
144                                 Layer0 b = Layer0.getInstance(g);
145                                 // nop
146                                 g.claim(g.getRootLibrary(), relation, b.Abstract);
147                                 g.deny(g.getRootLibrary(), relation, b.Abstract);
148                                 
149                         }
150                         
151                 });
152
153                 assert(adds == 1 && removes == 1);
154
155                 if(exception != null) {
156                         fail("Write transaction threw and exception (" + exception.getMessage() + ") which was not passed through ");
157                 }
158                 
159         }
160
161 }