]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/virtualGraphSupport/VirtualGraphTest3.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / support / virtualGraphSupport / VirtualGraphTest3.java
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 (file)
index 0000000..ffa76a1
--- /dev/null
@@ -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<String> original = new HashSet<String>();
+                           final HashSet<String> strings = new HashSet<String>();
+
+                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 ");
+               }
+               
+       }
+
+}