]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/ListenerExceptions.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / request / exception / ListenerExceptions.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.api.request.exception;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16
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;
30
31
32 public class ListenerExceptions extends ExistingDatabaseTest {
33         
34         @Test
35         public void test() throws Exception {
36                 
37                 Session session = getSession();
38
39                 class R extends ResourceRead<Integer> {
40
41                         public R(Resource r) {
42                                 super(r);
43                         }
44                         
45                         @Override
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");
52                                 return i;
53                         }
54                         
55                 }
56                 
57                 final Resource literal = session.syncRequest(new WriteResultRequest<Resource>() {
58
59                         @Override
60                         public Resource perform(WriteGraph graph) throws DatabaseException {
61                                 
62                                 Resource literal = graph.newResource();
63                                 Layer0 b = Layer0.getInstance(graph);
64                                 graph.claimLiteral(literal, b.HasProperty, 1, Bindings.INTEGER);
65                                 return literal;
66                                 
67                         }
68                         
69                 });
70                 
71                 final ArrayList<String> results = new ArrayList<String>();
72                 
73                 session.syncRequest(new R(literal), new Listener<Integer>() {
74
75                         @Override
76                         public void execute(Integer result) {
77                                 results.add(result != null ? result.toString() : null);
78                         }
79
80                         @Override
81                         public void exception(Throwable t) {
82                                 results.add(t.getMessage());
83                         }
84
85                         @Override
86                         public boolean isDisposed() {
87                                 return false;
88                         }
89                         
90                 });
91                 
92                 class Claim extends WriteRequest {
93
94                         private Integer i;
95                         
96                         public Claim(Integer i) {
97                                 this.i = i;
98                         }
99                         
100                         @Override
101                         public void perform(WriteGraph graph) throws DatabaseException {
102                             Layer0 b = Layer0.getInstance(graph);
103                             graph.claimLiteral(literal, b.HasProperty, i, Bindings.INTEGER);
104                         }
105                         
106                 }
107                 
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));
113                 
114                 String[] test = new String[] { "1", "Exception2", "3", "Exception45", "Exception45", "6" };
115                 
116                 assert(Arrays.equals(test, results.toArray(new String[results.size()])));
117                 
118         }
119         
120 }