]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/GraphSupportTest.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 / GraphSupportTest.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.Collection;
15 import java.util.concurrent.atomic.AtomicInteger;
16
17 import org.junit.Test;
18 import org.simantics.db.ReadGraph;
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.ReadRequest;
23 import org.simantics.db.common.request.ResourceRead;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.common.request.WriteResultRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.procedure.SyncListener;
28 import org.simantics.db.testing.base.ExistingDatabaseTest;
29 import org.simantics.layer0.Layer0;
30
31 public class GraphSupportTest extends ExistingDatabaseTest {
32         
33         @Test
34     public void test() throws Exception {
35                 
36         final AtomicInteger runs = new AtomicInteger(0);
37         
38                 Session session = getSession();
39                 
40                 final Resource test = session.syncRequest(new WriteResultRequest<Resource>() {
41             @Override
42             public Resource perform(WriteGraph graph) throws DatabaseException {
43                 return graph.newResource();
44             }
45                 });
46
47                 class Request extends ResourceRead<Collection<Resource>> {
48                         
49                         public Request(Resource resource) {
50                                 super(resource);
51                         }
52
53                         @Override
54                         public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
55                                 return graph.getObjects(resource, Layer0.getInstance(graph).ConsistsOf);
56                         }
57                         
58                 }
59                 
60                 session.syncRequest(new ReadRequest() {
61
62                         @Override
63                         public void run(ReadGraph graph) throws DatabaseException {
64                                 
65                                 graph.syncRequest(new Request(test), new SyncListener<Collection<Resource>>() {
66
67                                         @Override
68                                         public void execute(ReadGraph graph, Collection<Resource> result) throws DatabaseException {
69                                                 
70                                                 graph.syncRequest(new ReadRequest() {
71
72                                                         @Override
73                                                         public void run(ReadGraph graph) throws DatabaseException {
74                                                                 runs.incrementAndGet();
75                                                         }
76                                                         
77                                                 });
78                                         }
79
80                                         @Override
81                                         public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
82                                         }
83
84                                         @Override
85                                         public boolean isDisposed() {
86                                                 return false;
87                                         }
88                                 });
89                                 
90                         }
91                         
92                 });
93                 
94                 assert(runs.get() == 1);
95
96                 session.syncRequest(new WriteRequest() {
97             @Override
98             public void perform(WriteGraph graph) throws DatabaseException {
99                 graph.claim(test, Layer0.getInstance(graph).ConsistsOf, graph.newResource());
100             }
101                 });
102
103                 assert(runs.get() == 2);
104                 
105         }
106
107 }