]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/GraphSyncAsyncReadThrowsRuntimeException.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 / GraphSyncAsyncReadThrowsRuntimeException.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 org.junit.Test;
15 import org.simantics.db.AsyncReadGraph;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Session;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.db.request.AsyncRead;
21 import org.simantics.db.request.Read;
22 import org.simantics.db.testing.base.ExistingDatabaseTest;
23
24 public class GraphSyncAsyncReadThrowsRuntimeException extends ExistingDatabaseTest {
25         
26         private RuntimeException exception; 
27         
28         @Test
29         public void testWriteException() throws Exception {
30                 
31                 Session session = getSession();
32                 
33         session.syncRequest(new Read<Object>() {
34
35             @Override
36             public Object perform(ReadGraph graph) throws DatabaseException {
37                 
38                 try {
39
40                     graph.syncRequest(new AsyncRead<Object>() {
41
42                         @Override
43                         public void perform(AsyncReadGraph graph, AsyncProcedure<Object> procedure) {
44                             exception = new IllegalArgumentException();
45                             throw exception;
46                         }
47
48                             @Override
49                             public int threadHash() {
50                                 return hashCode();
51                             }
52
53                         @Override
54                         public int getFlags() {
55                             return 0;
56                         }
57
58                     });
59                 
60                 } catch (Throwable e) {
61
62                     if(!(e instanceof DatabaseException)) {
63                         fail("Request did not throw DatabaseException but " + e);
64                         return null;
65                     }
66
67                     DatabaseException ex = (DatabaseException)e;
68                     
69                     if(!exception.equals(ex.getCause())) {
70                         fail("Request did not report the correct cause but " + e);
71                         return null;
72                     }
73
74                                 if (DEBUG)
75                                         e.printStackTrace();
76                     
77                     return null;
78                     
79                 }
80                 
81                 fail("Request did not throw an exception.");
82
83                 return null;
84                 
85             }
86             
87         });
88                 
89                 
90                 
91         }
92
93 }