]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/client/TransactionTest3.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / client / TransactionTest3.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 java.util.Collection;
15 import java.util.Vector;
16
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;
30
31 public class TransactionTest3 extends ExistingDatabaseTest {
32         @Test
33         public void testTransaction3()
34         throws DatabaseException {
35             addResources();
36         }
37         void addResources() throws DatabaseException {
38             TransactionTest3Client client = new TransactionTest3Client(this);
39             Vector<Resource> resources = new Vector<Resource>();
40         try {
41             Resource r = client.createResource();
42             client.validateResource(r);
43             resources.add(r);
44             for (int i=0; i<10; ++i) {
45                 Resource r2 = client.addStatement(r);
46                 client.validateResource(r2);
47                 resources.add(r2);
48             }
49             client.validateResource(resources);
50         } finally {
51             if (null != client)
52                 client.close();
53         }
54         }
55 }
56
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 {
63         super(testCommon);
64     }
65     long serializeResource(Resource r)
66     throws DatabaseException {
67         SerialisationSupport ss = session.getService(SerialisationSupport.class);
68         return ss.getRandomAccessId(r);
69     }
70     Resource deserializeResource(long rid)
71     throws DatabaseException {
72         SerialisationSupport ss = session.getService(SerialisationSupport.class);
73         return ss.getResource(rid);
74     }
75     public Resource createResource()
76     throws DatabaseException {
77         return session.syncRequest(new WriteResultRequest<Resource>() {
78             @Override
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);
83                 return r;
84             }
85         });
86     }
87     public Resource addStatement(final Resource r)
88     throws DatabaseException {
89         return session.syncRequest(new WriteResultRequest<Resource>() {
90             @Override
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);
96                 return r2;
97             }
98         });
99     }
100     public void modifyAndCancel(final Resource r)
101     throws DatabaseException {
102         try {
103             session.syncRequest(new WriteRequest() {
104                 
105                 @Override
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.");
111                     g.flushCluster(r);
112                     throw new DatabaseException(message);
113                 }
114             });
115         } catch (DatabaseException e) {
116             if (e.getMessage().equals(message))
117                 return;
118         }
119     }
120     public void validateResource(final Resource r)
121     throws DatabaseException {
122         session.syncRequest(new ReadRequest() {
123             @Override
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 + ".");
128             }
129         });
130     }
131     public void validateResource(final Collection<Resource> resources)
132     throws DatabaseException {
133         session.syncRequest(new ReadRequest() {
134             @Override
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 + ".");
140             }
141         });
142     }
143 }