/******************************************************************************* * 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); } }