1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.api.request.exception;
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;
32 public class AsyncProcedureExecuteThrows extends ExistingDatabaseTest {
34 final RuntimeException exception = new RuntimeException();
36 private Read<Resource> syncRead(Resource r) {
37 return new ResourceRead<Resource>(r) {
40 public Resource perform(ReadGraph graph) throws DatabaseException {
41 return graph.getPossibleObject(resource, L0.InstanceOf);
47 private AsyncRead<Resource> asyncRead(Resource r) {
48 return new ResourceAsyncRead<Resource>(r) {
51 public void perform(AsyncReadGraph graph,AsyncProcedure<Resource> procedure) {
52 graph.forPossibleObject(resource, L0.InstanceOf, procedure);
58 private AsyncProcedure<Resource> procedure() {
59 return new AsyncProcedure<Resource>() {
62 public void execute(AsyncReadGraph graph, Resource result) {
67 public void exception(AsyncReadGraph graph, Throwable throwable) {
68 throw new RuntimeException();
74 public void syncSync(RequestProcessor processor) {
78 processor.syncRequest(syncRead(L0.Library), procedure());
79 fail("No exception was thrown.");
81 } catch (DatabaseException e) {
83 if(e.getCause() != exception) fail("Wrong exception was thrown.");
89 public void syncAsync(RequestProcessor processor) {
93 processor.syncRequest(asyncRead(L0.Library), procedure());
94 fail("No exception was thrown.");
96 } catch (DatabaseException e) {
98 if(e.getCause() != exception) fail("Wrong exception was thrown.");
104 public void asyncSync(AsyncRequestProcessor processor) {
106 processor.asyncRequest(syncRead(L0.Library), procedure());
110 public void asyncAsync(AsyncRequestProcessor processor) {
112 processor.asyncRequest(asyncRead(L0.Library), procedure());
118 public void test() throws Exception {
120 Session session = getSession();
124 session.syncRequest(new AsyncReadRequest() {
127 public void run(AsyncReadGraph graph) {
132 fail("No exception was thrown.");
134 } catch (DatabaseException e) {
136 if(e.getCause() != exception) fail("Wrong exception was thrown.");
142 session.syncRequest(new AsyncReadRequest() {
145 public void run(AsyncReadGraph graph) {
150 fail("No exception was thrown.");
152 } catch (DatabaseException e) {
154 if(e.getCause() != exception) fail("Wrong exception was thrown.");
158 session.syncRequest(new ReadRequest() {
161 public void run(ReadGraph graph) throws DatabaseException {