/******************************************************************************* * 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 VirtualGraphTest2 extends TestCleanDb { private Error exception; @Test public void test() throws DatabaseException { Session session = getSession(); 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 strings = new HashSet(); for(Resource r : g.getObjects(g.getRootLibrary() , b.ConsistsOf)) { String name = g.getPossibleRelatedValue(r, b.HasName); strings.add(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); } } }); 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; } } }); if(exception != null) { fail("Write transaction threw and exception (" + exception.getMessage() + ") which was not passed through "); } } }