]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestProcessorTest1.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / request / misc / RequestProcessorTest1.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
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.procedure.adapter.ListenerAdapter;
20 import org.simantics.db.common.request.AsyncReadRequest;
21 import org.simantics.db.common.request.ResourceAsyncRead;
22 import org.simantics.db.common.request.ResourceRead;
23 import org.simantics.db.common.request.UnaryAsyncRead;
24 import org.simantics.db.common.request.UnaryRead;
25 import org.simantics.db.common.request.UniqueRead;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.procedure.AsyncProcedure;
28 import org.simantics.db.procedure.Listener;
29 import org.simantics.db.testing.base.WriteReadTest;
30 import org.simantics.layer0.Layer0;
31
32 public class RequestProcessorTest1 extends WriteReadTest {
33         
34         class R1 extends ResourceRead<String> {
35
36                 public R1(Resource resource) {
37                         super(resource);
38                 }
39
40                 @Override
41                 public String perform(ReadGraph graph) throws DatabaseException {
42                         return graph.getURI(resource);
43                 }
44                 
45         }
46
47         class R2 extends ResourceAsyncRead<String> {
48
49                 public R2(Resource resource) {
50                         super(resource);
51                 }
52
53                 @Override
54                 public void perform(AsyncReadGraph graph, AsyncProcedure<String> procedure) {
55                         graph.forURI(resource, procedure);
56                 }
57                 
58         }
59
60         class R3 extends UnaryRead<Resource, String> {
61
62                 public R3(Resource resource) {
63                         super(resource);
64                 }
65
66                 @Override
67                 public String perform(ReadGraph graph) throws DatabaseException {
68                         return graph.getURI(parameter);
69                 }
70                 
71         }
72
73         class R4 extends UnaryAsyncRead<Resource, String> {
74
75                 public R4(Resource resource) {
76                         super(resource);
77                 }
78
79                 @Override
80                 public void perform(AsyncReadGraph graph, AsyncProcedure<String> procedure) {
81                         graph.forURI(parameter, procedure);
82                 }
83                 
84         }
85
86         class R5 extends UniqueRead<String> {
87
88                 @Override
89                 public String perform(ReadGraph graph) throws DatabaseException {
90                         return graph.getURI(graph.getRootLibrary());
91                 }
92                 
93         }
94         
95         @Override
96         protected void read(ReadGraph graph) throws DatabaseException {
97                 
98                 final ArrayList<String> listeners = new ArrayList<String>();
99                 
100                 final Listener<String> listener = new ListenerAdapter<String>() {
101                         
102                         @Override
103                         public synchronized void execute(String result) {
104                                 listeners.add(result);
105                         }
106                         
107                         @Override
108                         public boolean isDisposed() {
109                                 return false;
110                         }
111                         
112                 };
113                 
114                 assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R1(L0.ConsistsOf)));
115                 assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R2(L0.ConsistsOf)));
116                 assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R3(L0.ConsistsOf)));
117                 assertEquals(Layer0.URIs.ConsistsOf, graph.sync(new R4(L0.ConsistsOf)));
118                 assertEquals("http:/", graph.sync(new R5()));
119
120                 graph.syncRequest(new AsyncReadRequest() {
121
122                         @Override
123                         public void run(AsyncReadGraph graph) {
124                                 
125                                 graph.async(new R1(L0.ConsistsOf), listener);
126                                 graph.async(new R2(L0.ConsistsOf), listener);
127                                 graph.async(new R3(L0.ConsistsOf), listener);
128                                 graph.async(new R4(L0.ConsistsOf), listener);
129                                 graph.async(new R5(), listener);
130                                 
131                         }
132                         
133                 });
134
135                 assertEquals(listeners.size(), 5);
136                 
137         }
138
139 }