X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fclient%2FTransactionTest3.java;fp=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fclient%2FTransactionTest3.java;h=4a8f68fd690d3e2ad49f6f824fd172fd682d8c94;hb=67fd62f9c742337ec80eef658192db198a0efaac;hp=0000000000000000000000000000000000000000;hpb=cde82ba81327d5515fdca362f7f4c70f5103ae80;p=simantics%2Fplatform.git 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 index 000000000..4a8f68fd6 --- /dev/null +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/client/TransactionTest3.java @@ -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 resources = new Vector(); + 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() { + @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() { + @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 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 + "."); + } + }); + } +}