X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2Fsupport%2FvirtualGraphSupport%2FVirtualGraphTest3.java;fp=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2Fsupport%2FvirtualGraphSupport%2FVirtualGraphTest3.java;h=ffa76a1d3c9a46aa09bb7a116ee2f8d9bd6edb0a;hb=67fd62f9c742337ec80eef658192db198a0efaac;hp=0000000000000000000000000000000000000000;hpb=cde82ba81327d5515fdca362f7f4c70f5103ae80;p=simantics%2Fplatform.git diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/virtualGraphSupport/VirtualGraphTest3.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/virtualGraphSupport/VirtualGraphTest3.java new file mode 100644 index 000000000..ffa76a1d3 --- /dev/null +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/virtualGraphSupport/VirtualGraphTest3.java @@ -0,0 +1,124 @@ +/******************************************************************************* + * 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.api.support.virtualGraphSupport; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.UUID; + +import org.junit.Test; +import org.simantics.databoard.Bindings; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.VirtualGraph; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.service.VirtualGraphSupport; +import org.simantics.db.testing.base.TestCleanDb; +import org.simantics.layer0.Layer0; + +public class VirtualGraphTest3 extends TestCleanDb { + + private Error exception; + + @Test + public void test() throws DatabaseException { + + Session session = getSession(); + final VirtualGraphSupport support = session.getService(VirtualGraphSupport.class); + final VirtualGraph virtual = support.getMemoryPersistent(UUID.randomUUID().toString()); + + session.syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph g) throws DatabaseException { + + Layer0 b = Layer0.getInstance(g); + + final HashSet original = new HashSet(); + final HashSet strings = new HashSet(); + + for(Resource r : g.getObjects(g.getRootLibrary() , b.ConsistsOf)) { + String name = g.getPossibleRelatedValue(r, b.HasName); + if (name != null) { + original.add(name); + strings.add(name); + } else if (DEBUG) + System.out.println("Resource " + r + " has no name."); + } + + g.syncRequest(new WriteRequest(virtual) { + + @Override + public void perform(WriteGraph g) throws DatabaseException { + + Layer0 b = Layer0.getInstance(g); + for(int i=0;i<10;i++) { + Resource r = g.newResource(); + String name = "Virtual" + i; + g.claimLiteral(r, b.HasName, name, Bindings.STRING); + strings.add(name); + g.claim(g.getRootLibrary(), b.ConsistsOf, r); + if (DEBUG) + System.out.println("added " + name + " resource " + r); + } + + } + + }); + + for(Resource r : g.getObjects(g.getRootLibrary() , b.ConsistsOf)) { + String name = g.getPossibleRelatedValue(r, b.HasName); + if (DEBUG) + System.out.println("found " + name); + boolean success = strings.remove(name); + if(!success) { + exception = new AssertionError("encountered unknown string " + name); + throw exception; + } + } + + if(!strings.isEmpty()) { + exception = new AssertionError("some strings were left over " + Arrays.toString(strings.toArray())); + throw exception; + } + + support.discard(virtual); + + for(Resource r : g.getObjects(g.getRootLibrary() , b.ConsistsOf)) { + String name = g.getPossibleRelatedValue(r, b.HasName); + if (DEBUG) + System.out.println("found " + name); + boolean success = original.remove(name); + if(!success) { + exception = new AssertionError("encountered unknown string " + name + " resource " + r); + throw exception; + } + } + + if(!original.isEmpty()) { + exception = new AssertionError("some strings were left over " + Arrays.toString(strings.toArray())); + throw exception; + } + + } + + }); + + if(exception != null) { + fail("Write transaction threw and exception (" + exception.getMessage() + ") which was not passed through "); + } + + } + +}