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 java.util.ArrayList;
15 import java.util.Arrays;
17 import org.junit.Test;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.request.ResourceRead;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.common.request.WriteResultRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.procedure.Listener;
28 import org.simantics.db.testing.base.ExistingDatabaseTest;
29 import org.simantics.layer0.Layer0;
32 public class ListenerExceptions extends ExistingDatabaseTest {
35 public void test() throws Exception {
37 Session session = getSession();
39 class R extends ResourceRead<Integer> {
41 public R(Resource r) {
46 public Integer perform(ReadGraph graph) throws DatabaseException {
47 Layer0 b = Layer0.getInstance(graph);
48 Integer i = graph.getPossibleRelatedValue(resource, b.HasProperty);
49 if(i == 2) throw new DatabaseException("Exception2");
50 if(i == 4) throw new DatabaseException("Exception45");
51 if(i == 5) throw new DatabaseException("Exception45");
57 final Resource literal = session.syncRequest(new WriteResultRequest<Resource>() {
60 public Resource perform(WriteGraph graph) throws DatabaseException {
62 Resource literal = graph.newResource();
63 Layer0 b = Layer0.getInstance(graph);
64 graph.claimLiteral(literal, b.HasProperty, 1, Bindings.INTEGER);
71 final ArrayList<String> results = new ArrayList<String>();
73 session.syncRequest(new R(literal), new Listener<Integer>() {
76 public void execute(Integer result) {
77 results.add(result != null ? result.toString() : null);
81 public void exception(Throwable t) {
82 results.add(t.getMessage());
86 public boolean isDisposed() {
92 class Claim extends WriteRequest {
96 public Claim(Integer i) {
101 public void perform(WriteGraph graph) throws DatabaseException {
102 Layer0 b = Layer0.getInstance(graph);
103 graph.claimLiteral(literal, b.HasProperty, i, Bindings.INTEGER);
108 session.syncRequest(new Claim(2));
109 session.syncRequest(new Claim(3));
110 session.syncRequest(new Claim(4));
111 session.syncRequest(new Claim(5));
112 session.syncRequest(new Claim(6));
114 String[] test = new String[] { "1", "Exception2", "3", "Exception45", "Exception45", "6" };
116 assert(Arrays.equals(test, results.toArray(new String[results.size()])));