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%2FListenerExceptions.java;fp=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2Frequest%2Fexception%2FListenerExceptions.java;h=223fa686cd1e67f2c613d5b41eec3611089f2985;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/ListenerExceptions.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/ListenerExceptions.java new file mode 100644 index 000000000..223fa686c --- /dev/null +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/ListenerExceptions.java @@ -0,0 +1,120 @@ +/******************************************************************************* + * 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 { + + 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() { + + @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 results = new ArrayList(); + + session.syncRequest(new R(literal), new Listener() { + + @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()]))); + + } + +}