--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.tests.client;
+
+import org.junit.Test;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.request.WriteResultRequest;
+import org.simantics.db.exception.CancelTransactionException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ValidationException;
+import org.simantics.db.service.SerialisationSupport;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.layer0.Layer0;
+
+public class TransactionTest2 extends ExistingDatabaseTest {
+ @Test
+ public void testTransaction2()
+ throws DatabaseException {
+ cancelTest1();
+ }
+ void cancelTest1() throws DatabaseException {
+ TransactionClientCancel client = new TransactionClientCancel(this);
+ TransactionClientCancel client2 = new TransactionClientCancel(this);
+ TransactionClientCancel client3 = null;
+ try {
+ Resource r = client.createResource();
+ client.validateResource(r);
+ client.modifyAndCancel(r);
+ client.validateResource(r);
+ long rid = client.serializeResource(r);
+ Resource r2 = client2.deserializeResource(rid);
+ client2.validateResource(r2);
+ client3 = new TransactionClientCancel(this);
+ Resource r3 = client3.deserializeResource(rid);
+ client3.validateResource(r3);
+ } finally {
+ if (null != client)
+ client.close();
+ if (null != client2)
+ client2.close();
+ if (null != client3)
+ client3.close();
+ }
+ }
+}
+
+class TransactionClientCancel extends TransctionCommon {
+ private final String message = "Transaction was cancelled.";
+ private final String value = "value";
+ private final String newValue = "new value";
+ TransactionClientCancel(ExistingDatabaseTest testCommon)
+ throws DatabaseException {
+ super(testCommon);
+ }
+ long serializeResource(Resource r)
+ throws DatabaseException {
+ SerialisationSupport ss = session.getService(SerialisationSupport.class);
+ return ss.getRandomAccessId(r);
+ }
+ Resource deserializeResource(long rid)
+ throws DatabaseException {
+ SerialisationSupport ss = session.getService(SerialisationSupport.class);
+ return ss.getResource(rid);
+ }
+ public Resource createResource()
+ throws DatabaseException {
+ return session.syncRequest(new WriteResultRequest<Resource>() {
+ @Override
+ public Resource perform(WriteGraph g) throws DatabaseException {
+ Layer0 b = Layer0.getInstance(g);
+ Resource r = g.newResource();
+ g.claim(r, b.InstanceOf, b.Entity);
+ g.claimValue(r, value);
+ return r;
+ }
+ });
+ }
+ public void modifyAndCancel(final Resource r)
+ throws DatabaseException {
+ try {
+ session.syncRequest(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ g.claimValue(r, newValue);
+ String t = g.getValue(r, Bindings.STRING);
+ if (!t.equals(newValue))
+ throw new ValidationException("Failed to modify value.");
+ g.flushCluster(r);
+ throw new CancelTransactionException(message);
+ }
+ });
+ } catch (CancelTransactionException e) {
+ if (e.getMessage().equals(message))
+ return;
+ }
+ }
+ public void validateResource(final Resource r)
+ throws DatabaseException {
+ session.syncRequest(new ReadRequest() {
+ @Override
+ public void run(ReadGraph g) throws DatabaseException {
+ if (!g.hasStatement(r))
+ throw new ValidationException("Failed to verify modifications (stm).");
+ String t = g.getValue(r, Bindings.STRING);
+ if (!t.equals(value))
+ throw new ValidationException("Failed to verify modifications (val).");
+ }
+ });
+ }
+}