]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/client/TransactionTest3.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/client/TransactionTest3.java
new file mode 100644 (file)
index 0000000..4a8f68f
--- /dev/null
@@ -0,0 +1,143 @@
+/*******************************************************************************
+ * 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 java.util.Collection;
+import java.util.Vector;
+
+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.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 TransactionTest3 extends ExistingDatabaseTest {
+       @Test
+       public void testTransaction3()
+       throws DatabaseException {
+           addResources();
+       }
+       void addResources() throws DatabaseException {
+           TransactionTest3Client client = new TransactionTest3Client(this);
+           Vector<Resource> resources = new Vector<Resource>();
+        try {
+            Resource r = client.createResource();
+            client.validateResource(r);
+            resources.add(r);
+            for (int i=0; i<10; ++i) {
+                Resource r2 = client.addStatement(r);
+                client.validateResource(r2);
+                resources.add(r2);
+            }
+            client.validateResource(resources);
+        } finally {
+            if (null != client)
+                client.close();
+        }
+       }
+}
+
+class TransactionTest3Client extends TransctionCommon {
+    private final String message = "Transaction was cancelled.";
+//    private final String value = "value";
+    private final String newValue = "new value";
+    TransactionTest3Client(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);
+                return r;
+            }
+        });
+    }
+    public Resource addStatement(final Resource r)
+    throws DatabaseException {
+        return session.syncRequest(new WriteResultRequest<Resource>() {
+            @Override
+            public Resource perform(WriteGraph g) throws DatabaseException {
+                Layer0 b = Layer0.getInstance(g);
+                Resource r2 = g.newResource();
+                g.claim(r2, b.InstanceOf, b.Entity);
+                g.claim(r, b.ConsistsOf, r2);
+                return r2;
+            }
+        });
+    }
+    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 DatabaseException(message);
+                }
+            });
+        } catch (DatabaseException 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 {
+                Layer0 l0 = Layer0.getInstance(g);
+                if (!g.isInstanceOf(r, l0.Entity))
+                    throw new ValidationException("Failed to verify resource " + r + ".");
+            }
+        });
+    }
+    public void validateResource(final Collection<Resource> resources)
+    throws DatabaseException {
+        session.syncRequest(new ReadRequest() {
+            @Override
+            public void run(ReadGraph g) throws DatabaseException {
+                Layer0 l0 = Layer0.getInstance(g);
+                for (Resource r : resources)
+                    if (!g.isInstanceOf(r, l0.Entity))
+                        throw new ValidationException("Failed to verify resource " + r + ".");
+            }
+        });
+    }
+}