]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/AsyncProcedureExecuteThrows.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 / exception / AsyncProcedureExecuteThrows.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.exception;
13
14 import org.junit.Test;
15 import org.simantics.db.AsyncReadGraph;
16 import org.simantics.db.AsyncRequestProcessor;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.RequestProcessor;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.common.request.AsyncReadRequest;
22 import org.simantics.db.common.request.ReadRequest;
23 import org.simantics.db.common.request.ResourceAsyncRead;
24 import org.simantics.db.common.request.ResourceRead;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.procedure.AsyncProcedure;
27 import org.simantics.db.request.AsyncRead;
28 import org.simantics.db.request.Read;
29 import org.simantics.db.testing.annotation.Fails;
30 import org.simantics.db.testing.base.ExistingDatabaseTest;
31
32 public class AsyncProcedureExecuteThrows extends ExistingDatabaseTest {
33
34         final RuntimeException exception = new RuntimeException();
35
36         private Read<Resource> syncRead(Resource r) {
37                 return new ResourceRead<Resource>(r) {
38
39                         @Override
40                         public Resource perform(ReadGraph graph) throws DatabaseException {
41                                 return graph.getPossibleObject(resource, L0.InstanceOf);
42                         }
43                         
44                 };
45         }
46         
47         private AsyncRead<Resource> asyncRead(Resource r) {
48                 return new ResourceAsyncRead<Resource>(r) {
49
50                         @Override
51                         public void perform(AsyncReadGraph graph,AsyncProcedure<Resource> procedure) {
52                                 graph.forPossibleObject(resource, L0.InstanceOf, procedure);
53                         }
54                         
55                 };
56         }
57         
58         private AsyncProcedure<Resource> procedure() {
59                 return new AsyncProcedure<Resource>() {
60                         
61                         @Override
62                         public void execute(AsyncReadGraph graph, Resource result) {
63                                 throw exception;
64                         }
65
66                         @Override
67                         public void exception(AsyncReadGraph graph, Throwable throwable) {
68                                 throw new RuntimeException();
69                         }
70                         
71                 };
72         }
73
74         public void syncSync(RequestProcessor processor) {
75
76                 try {
77                         
78                         processor.syncRequest(syncRead(L0.Library), procedure());
79                         fail("No exception was thrown.");
80
81                 } catch (DatabaseException e) {
82                         
83                         if(e.getCause() != exception) fail("Wrong exception was thrown.");
84                         
85                 }
86                 
87         }
88
89         public void syncAsync(RequestProcessor processor) {
90
91                 try {
92                         
93                         processor.syncRequest(asyncRead(L0.Library), procedure());
94                         fail("No exception was thrown.");
95
96                 } catch (DatabaseException e) {
97                         
98                         if(e.getCause() != exception) fail("Wrong exception was thrown.");
99                         
100                 }
101                 
102         }
103
104         public void asyncSync(AsyncRequestProcessor processor) {
105
106                 processor.asyncRequest(syncRead(L0.Library), procedure());
107                 
108         }
109
110         public void asyncAsync(AsyncRequestProcessor processor) {
111
112                 processor.asyncRequest(asyncRead(L0.Library), procedure());
113                 
114         }
115
116         @Test
117         @Fails
118         public void test() throws Exception {
119                 
120                 Session session = getSession();
121
122                 try {
123                         
124                         session.syncRequest(new AsyncReadRequest() {
125                                 
126                                 @Override
127                                 public void run(AsyncReadGraph graph) {
128                                         asyncSync(graph);
129                                 }
130                                 
131                         });
132                         fail("No exception was thrown.");
133
134                 } catch (DatabaseException e) {
135                         
136                         if(e.getCause() != exception) fail("Wrong exception was thrown.");
137                         
138                 }
139
140                 try {
141                         
142                         session.syncRequest(new AsyncReadRequest() {
143                                 
144                                 @Override
145                                 public void run(AsyncReadGraph graph) {
146                                         asyncAsync(graph);
147                                 }
148                                 
149                         });
150                         fail("No exception was thrown.");
151
152                 } catch (DatabaseException e) {
153                         
154                         if(e.getCause() != exception) fail("Wrong exception was thrown.");
155                         
156                 }
157
158                 session.syncRequest(new ReadRequest() {
159
160                         @Override
161                         public void run(ReadGraph graph) throws DatabaseException {
162                                 syncSync(graph);
163                                 syncAsync(graph);
164                         }
165                         
166                 });
167                 
168                 syncSync(session);
169                 syncAsync(session);
170                 
171         }
172
173 }