1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.client;
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;
29 public class TransactionTest2 extends ExistingDatabaseTest {
31 public void testTransaction2()
32 throws DatabaseException {
35 void cancelTest1() throws DatabaseException {
36 TransactionClientCancel client = new TransactionClientCancel(this);
37 TransactionClientCancel client2 = new TransactionClientCancel(this);
38 TransactionClientCancel client3 = null;
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);
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 {
69 long serializeResource(Resource r)
70 throws DatabaseException {
71 SerialisationSupport ss = session.getService(SerialisationSupport.class);
72 return ss.getRandomAccessId(r);
74 Resource deserializeResource(long rid)
75 throws DatabaseException {
76 SerialisationSupport ss = session.getService(SerialisationSupport.class);
77 return ss.getResource(rid);
79 public Resource createResource()
80 throws DatabaseException {
81 return session.syncRequest(new WriteResultRequest<Resource>() {
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);
92 public void modifyAndCancel(final Resource r)
93 throws DatabaseException {
95 session.syncRequest(new WriteRequest() {
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.");
103 throw new CancelTransactionException(message);
106 } catch (CancelTransactionException e) {
107 if (e.getMessage().equals(message))
111 public void validateResource(final Resource r)
112 throws DatabaseException {
113 session.syncRequest(new ReadRequest() {
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).");