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 java.util.Collection;
15 import java.util.Vector;
17 import org.junit.Test;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.ReadRequest;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.common.request.WriteResultRequest;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.exception.ValidationException;
27 import org.simantics.db.service.SerialisationSupport;
28 import org.simantics.db.testing.base.ExistingDatabaseTest;
29 import org.simantics.layer0.Layer0;
31 public class TransactionTest3 extends ExistingDatabaseTest {
33 public void testTransaction3()
34 throws DatabaseException {
37 void addResources() throws DatabaseException {
38 TransactionTest3Client client = new TransactionTest3Client(this);
39 Vector<Resource> resources = new Vector<Resource>();
41 Resource r = client.createResource();
42 client.validateResource(r);
44 for (int i=0; i<10; ++i) {
45 Resource r2 = client.addStatement(r);
46 client.validateResource(r2);
49 client.validateResource(resources);
57 class TransactionTest3Client extends TransctionCommon {
58 private final String message = "Transaction was cancelled.";
59 // private final String value = "value";
60 private final String newValue = "new value";
61 TransactionTest3Client(ExistingDatabaseTest testCommon)
62 throws DatabaseException {
65 long serializeResource(Resource r)
66 throws DatabaseException {
67 SerialisationSupport ss = session.getService(SerialisationSupport.class);
68 return ss.getRandomAccessId(r);
70 Resource deserializeResource(long rid)
71 throws DatabaseException {
72 SerialisationSupport ss = session.getService(SerialisationSupport.class);
73 return ss.getResource(rid);
75 public Resource createResource()
76 throws DatabaseException {
77 return session.syncRequest(new WriteResultRequest<Resource>() {
79 public Resource perform(WriteGraph g) throws DatabaseException {
80 Layer0 b = Layer0.getInstance(g);
81 Resource r = g.newResource();
82 g.claim(r, b.InstanceOf, b.Entity);
87 public Resource addStatement(final Resource r)
88 throws DatabaseException {
89 return session.syncRequest(new WriteResultRequest<Resource>() {
91 public Resource perform(WriteGraph g) throws DatabaseException {
92 Layer0 b = Layer0.getInstance(g);
93 Resource r2 = g.newResource();
94 g.claim(r2, b.InstanceOf, b.Entity);
95 g.claim(r, b.ConsistsOf, r2);
100 public void modifyAndCancel(final Resource r)
101 throws DatabaseException {
103 session.syncRequest(new WriteRequest() {
106 public void perform(WriteGraph g) throws DatabaseException {
107 g.claimValue(r, newValue);
108 String t = g.getValue(r, Bindings.STRING);
109 if (!t.equals(newValue))
110 throw new ValidationException("Failed to modify value.");
112 throw new DatabaseException(message);
115 } catch (DatabaseException e) {
116 if (e.getMessage().equals(message))
120 public void validateResource(final Resource r)
121 throws DatabaseException {
122 session.syncRequest(new ReadRequest() {
124 public void run(ReadGraph g) throws DatabaseException {
125 Layer0 l0 = Layer0.getInstance(g);
126 if (!g.isInstanceOf(r, l0.Entity))
127 throw new ValidationException("Failed to verify resource " + r + ".");
131 public void validateResource(final Collection<Resource> resources)
132 throws DatabaseException {
133 session.syncRequest(new ReadRequest() {
135 public void run(ReadGraph g) throws DatabaseException {
136 Layer0 l0 = Layer0.getInstance(g);
137 for (Resource r : resources)
138 if (!g.isInstanceOf(r, l0.Entity))
139 throw new ValidationException("Failed to verify resource " + r + ".");