--- /dev/null
+/*******************************************************************************
+ * 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 java.util.ArrayList;
+import java.util.Arrays;
+
+import org.junit.Test;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.request.WriteResultRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.Listener;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.layer0.Layer0;
+
+
+public class ListenerExceptions extends ExistingDatabaseTest {
+
+ @Test
+ public void test() throws Exception {
+
+ Session session = getSession();
+
+ class R extends ResourceRead<Integer> {
+
+ public R(Resource r) {
+ super(r);
+ }
+
+ @Override
+ public Integer perform(ReadGraph graph) throws DatabaseException {
+ Layer0 b = Layer0.getInstance(graph);
+ Integer i = graph.getPossibleRelatedValue(resource, b.HasProperty);
+ if(i == 2) throw new DatabaseException("Exception2");
+ if(i == 4) throw new DatabaseException("Exception45");
+ if(i == 5) throw new DatabaseException("Exception45");
+ return i;
+ }
+
+ }
+
+ final Resource literal = session.syncRequest(new WriteResultRequest<Resource>() {
+
+ @Override
+ public Resource perform(WriteGraph graph) throws DatabaseException {
+
+ Resource literal = graph.newResource();
+ Layer0 b = Layer0.getInstance(graph);
+ graph.claimLiteral(literal, b.HasProperty, 1, Bindings.INTEGER);
+ return literal;
+
+ }
+
+ });
+
+ final ArrayList<String> results = new ArrayList<String>();
+
+ session.syncRequest(new R(literal), new Listener<Integer>() {
+
+ @Override
+ public void execute(Integer result) {
+ results.add(result != null ? result.toString() : null);
+ }
+
+ @Override
+ public void exception(Throwable t) {
+ results.add(t.getMessage());
+ }
+
+ @Override
+ public boolean isDisposed() {
+ return false;
+ }
+
+ });
+
+ class Claim extends WriteRequest {
+
+ private Integer i;
+
+ public Claim(Integer i) {
+ this.i = i;
+ }
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ Layer0 b = Layer0.getInstance(graph);
+ graph.claimLiteral(literal, b.HasProperty, i, Bindings.INTEGER);
+ }
+
+ }
+
+ session.syncRequest(new Claim(2));
+ session.syncRequest(new Claim(3));
+ session.syncRequest(new Claim(4));
+ session.syncRequest(new Claim(5));
+ session.syncRequest(new Claim(6));
+
+ String[] test = new String[] { "1", "Exception2", "3", "Exception45", "Exception45", "6" };
+
+ assert(Arrays.equals(test, results.toArray(new String[results.size()])));
+
+ }
+
+}