]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/client/TransactionTest2.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / client / TransactionTest2.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.client;
13
14 import org.junit.Test;
15 import org.simantics.databoard.Bindings;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.ReadRequest;
20 import org.simantics.db.common.request.WriteRequest;
21 import org.simantics.db.common.request.WriteResultRequest;
22 import org.simantics.db.exception.CancelTransactionException;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.exception.ValidationException;
25 import org.simantics.db.service.SerialisationSupport;
26 import org.simantics.db.testing.base.ExistingDatabaseTest;
27 import org.simantics.layer0.Layer0;
28
29 public class TransactionTest2 extends ExistingDatabaseTest {
30         @Test
31     public void testTransaction2()
32     throws DatabaseException {
33         cancelTest1();
34     }
35     void cancelTest1() throws DatabaseException {
36         TransactionClientCancel client = new TransactionClientCancel(this);
37         TransactionClientCancel client2 = new TransactionClientCancel(this);
38         TransactionClientCancel client3 = null;
39         try {
40             Resource r = client.createResource();
41             client.validateResource(r);
42             client.modifyAndCancel(r);
43             client.validateResource(r);
44             long rid = client.serializeResource(r);
45             Resource r2 = client2.deserializeResource(rid);
46             client2.validateResource(r2);
47             client3 = new TransactionClientCancel(this);
48             Resource r3 = client3.deserializeResource(rid);
49             client3.validateResource(r3);
50         } finally {
51             if (null != client)
52                 client.close();
53             if (null != client2)
54                 client2.close();
55             if (null != client3)
56                 client3.close();
57         }
58     }
59 }
60
61 class TransactionClientCancel extends TransctionCommon {
62     private final String message = "Transaction was cancelled.";
63     private final String value = "value";
64     private final String newValue = "new value";
65     TransactionClientCancel(ExistingDatabaseTest testCommon)
66     throws DatabaseException {
67         super(testCommon);
68     }
69     long serializeResource(Resource r)
70     throws DatabaseException {
71         SerialisationSupport ss = session.getService(SerialisationSupport.class);
72         return ss.getRandomAccessId(r);
73     }
74     Resource deserializeResource(long rid)
75     throws DatabaseException {
76         SerialisationSupport ss = session.getService(SerialisationSupport.class);
77         return ss.getResource(rid);
78     }
79     public Resource createResource()
80     throws DatabaseException {
81         return session.syncRequest(new WriteResultRequest<Resource>() {
82             @Override
83             public Resource perform(WriteGraph g) throws DatabaseException {
84                 Layer0 b = Layer0.getInstance(g);
85                 Resource r = g.newResource();
86                 g.claim(r, b.InstanceOf, b.Entity);
87                 g.claimValue(r, value);
88                 return r;
89             }
90         });
91     }
92     public void modifyAndCancel(final Resource r)
93     throws DatabaseException {
94         try {
95             session.syncRequest(new WriteRequest() {
96                 @Override
97                 public void perform(WriteGraph g) throws DatabaseException {
98                     g.claimValue(r, newValue);
99                     String t = g.getValue(r, Bindings.STRING);
100                     if (!t.equals(newValue))
101                         throw new ValidationException("Failed to modify value.");
102                     g.flushCluster(r);
103                     throw new CancelTransactionException(message);
104                 }
105             });
106         } catch (CancelTransactionException e) {
107             if (e.getMessage().equals(message))
108                 return;
109         }
110     }
111     public void validateResource(final Resource r)
112     throws DatabaseException {
113         session.syncRequest(new ReadRequest() {
114             @Override
115             public void run(ReadGraph g) throws DatabaseException {
116                 if (!g.hasStatement(r))
117                     throw new ValidationException("Failed to verify modifications (stm).");
118                 String t = g.getValue(r, Bindings.STRING);
119                 if (!t.equals(value))
120                     throw new ValidationException("Failed to verify modifications (val).");
121             }
122         });
123     }
124 }