]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/exception/RecoveryFromExceptedState.java
Made DB ListenerAdapter abstract to force isDisposed implementation
[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                                 @Override
68                                 public boolean isDisposed() {
69                                         return false;
70                                 }
71                         });
72
73                         /*
74                          * Invalidates the request. Request is updated (is has a listener) and enters 'Excepted' state. 
75                          */
76                         session.syncRequest(new WriteRequest() {
77
78                                 @Override
79                                 public void perform(WriteGraph graph) throws DatabaseException {
80
81                                         graph.deny(A);
82                                         graph.deny(graph.getRootLibrary(), L0.ConsistsOf, A);
83
84                                 }
85
86                         });
87
88                         /*
89                          * After this write the result is again valid. The request should be updated to 'Ready' state. 
90                          */
91                         session.syncRequest(new WriteRequest() {
92
93                                 @Override
94                                 public void perform(WriteGraph graph) throws DatabaseException {
95
96                                         Resource root = graph.getRootLibrary();
97                                         Resource part = graph.newResource();
98                                         graph.claim(part, L0.InstanceOf, null, L0.Entity);
99                                         graph.claim(part, L0.PartOf, root);
100                                         graph.claimLiteral(part, L0.HasName, "A");
101
102                                 }
103
104                         });
105                         
106                         /*
107                          * Validate the situation. 
108                          */
109                         session.syncRequest(new Read<Resource>() {
110
111                                 @Override
112                                 public Resource perform(ReadGraph graph) throws DatabaseException {
113                                         return graph.getResource("http://A");
114                                 }
115                                 
116                         }, TransientCacheListener.<Resource>instance());
117
118                 } catch(ResourceNotFoundException e) {
119
120                 fail("getResource threw ResourceNotFoundException");
121
122                 }
123                 
124         }
125
126 }