X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2Frequest%2Fexception%2FAsyncProcedureExecuteThrows.java;fp=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2Frequest%2Fexception%2FAsyncProcedureExecuteThrows.java;h=46a5575c9f41bea07ee03ff775fbf7ef14d05b92;hb=67fd62f9c742337ec80eef658192db198a0efaac;hp=0000000000000000000000000000000000000000;hpb=cde82ba81327d5515fdca362f7f4c70f5103ae80;p=simantics%2Fplatform.git diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/AsyncProcedureExecuteThrows.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/AsyncProcedureExecuteThrows.java new file mode 100644 index 000000000..46a5575c9 --- /dev/null +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/AsyncProcedureExecuteThrows.java @@ -0,0 +1,173 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db.tests.api.request.exception; + +import org.junit.Test; +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.AsyncRequestProcessor; +import org.simantics.db.ReadGraph; +import org.simantics.db.RequestProcessor; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.common.request.AsyncReadRequest; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.ResourceAsyncRead; +import org.simantics.db.common.request.ResourceRead; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.AsyncProcedure; +import org.simantics.db.request.AsyncRead; +import org.simantics.db.request.Read; +import org.simantics.db.testing.annotation.Fails; +import org.simantics.db.testing.base.ExistingDatabaseTest; + +public class AsyncProcedureExecuteThrows extends ExistingDatabaseTest { + + final RuntimeException exception = new RuntimeException(); + + private Read syncRead(Resource r) { + return new ResourceRead(r) { + + @Override + public Resource perform(ReadGraph graph) throws DatabaseException { + return graph.getPossibleObject(resource, L0.InstanceOf); + } + + }; + } + + private AsyncRead asyncRead(Resource r) { + return new ResourceAsyncRead(r) { + + @Override + public void perform(AsyncReadGraph graph,AsyncProcedure procedure) { + graph.forPossibleObject(resource, L0.InstanceOf, procedure); + } + + }; + } + + private AsyncProcedure procedure() { + return new AsyncProcedure() { + + @Override + public void execute(AsyncReadGraph graph, Resource result) { + throw exception; + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + throw new RuntimeException(); + } + + }; + } + + public void syncSync(RequestProcessor processor) { + + try { + + processor.syncRequest(syncRead(L0.Library), procedure()); + fail("No exception was thrown."); + + } catch (DatabaseException e) { + + if(e.getCause() != exception) fail("Wrong exception was thrown."); + + } + + } + + public void syncAsync(RequestProcessor processor) { + + try { + + processor.syncRequest(asyncRead(L0.Library), procedure()); + fail("No exception was thrown."); + + } catch (DatabaseException e) { + + if(e.getCause() != exception) fail("Wrong exception was thrown."); + + } + + } + + public void asyncSync(AsyncRequestProcessor processor) { + + processor.asyncRequest(syncRead(L0.Library), procedure()); + + } + + public void asyncAsync(AsyncRequestProcessor processor) { + + processor.asyncRequest(asyncRead(L0.Library), procedure()); + + } + + @Test + @Fails + public void test() throws Exception { + + Session session = getSession(); + + try { + + session.syncRequest(new AsyncReadRequest() { + + @Override + public void run(AsyncReadGraph graph) { + asyncSync(graph); + } + + }); + fail("No exception was thrown."); + + } catch (DatabaseException e) { + + if(e.getCause() != exception) fail("Wrong exception was thrown."); + + } + + try { + + session.syncRequest(new AsyncReadRequest() { + + @Override + public void run(AsyncReadGraph graph) { + asyncAsync(graph); + } + + }); + fail("No exception was thrown."); + + } catch (DatabaseException e) { + + if(e.getCause() != exception) fail("Wrong exception was thrown."); + + } + + session.syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + syncSync(graph); + syncAsync(graph); + } + + }); + + syncSync(session); + syncAsync(session); + + } + +}