]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/RecoveryFromExceptedState.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 / RecoveryFromExceptedState.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.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Session;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.procedure.adapter.ListenerAdapter;
20 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
21 import org.simantics.db.common.request.WriteRequest;
22 import org.simantics.db.common.request.WriteResultRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.exception.ResourceNotFoundException;
25 import org.simantics.db.request.Read;
26 import org.simantics.db.testing.base.ExistingDatabaseTest;
27 import org.simantics.layer0.Layer0;
28
29 public class RecoveryFromExceptedState extends ExistingDatabaseTest {
30         
31         @Test
32         public void test() throws Exception {
33                 
34                 Session session = getSession();
35                 
36                 final Layer0 L0 = Layer0.getInstance(session);
37
38                 final Resource A = session.syncRequest(new WriteResultRequest<Resource>() {
39
40                         @Override
41                         public Resource perform(WriteGraph graph) throws DatabaseException {
42
43                                 Resource root = graph.getRootLibrary();
44                                 Resource part = graph.newResource();
45                                 graph.claim(part, L0.InstanceOf, null, L0.Entity);
46                                 graph.claim(part, L0.PartOf, root);
47                                 graph.claimLiteral(part, L0.HasName, "A");
48                                 return part;
49
50                         }
51
52                 });
53
54                 try {
55
56                         /*
57                          * Create the request. Listening is needed to enforce update of the query in subsequent write 
58                          */
59                         session.syncRequest(new Read<Resource>() {
60
61                                 @Override
62                                 public Resource perform(ReadGraph graph) throws DatabaseException {
63                                         return graph.getResource("http://A");
64                                 }
65                                 
66                         }, new ListenerAdapter<Resource>());
67
68                         /*
69                          * Invalidates the request. Request is updated (is has a listener) and enters 'Excepted' state. 
70                          */
71                         session.syncRequest(new WriteRequest() {
72
73                                 @Override
74                                 public void perform(WriteGraph graph) throws DatabaseException {
75
76                                         graph.deny(A);
77                                         graph.deny(graph.getRootLibrary(), L0.ConsistsOf, A);
78
79                                 }
80
81                         });
82
83                         /*
84                          * After this write the result is again valid. The request should be updated to 'Ready' state. 
85                          */
86                         session.syncRequest(new WriteRequest() {
87
88                                 @Override
89                                 public void perform(WriteGraph graph) throws DatabaseException {
90
91                                         Resource root = graph.getRootLibrary();
92                                         Resource part = graph.newResource();
93                                         graph.claim(part, L0.InstanceOf, null, L0.Entity);
94                                         graph.claim(part, L0.PartOf, root);
95                                         graph.claimLiteral(part, L0.HasName, "A");
96
97                                 }
98
99                         });
100                         
101                         /*
102                          * Validate the situation. 
103                          */
104                         session.syncRequest(new Read<Resource>() {
105
106                                 @Override
107                                 public Resource perform(ReadGraph graph) throws DatabaseException {
108                                         return graph.getResource("http://A");
109                                 }
110                                 
111                         }, TransientCacheListener.<Resource>instance());
112
113                 } catch(ResourceNotFoundException e) {
114
115                 fail("getResource threw ResourceNotFoundException");
116
117                 }
118                 
119         }
120
121 }