/******************************************************************************* * 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.regression.bugs; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeSet; import java.util.Vector; import org.junit.Test; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.db.WriteGraph; import org.simantics.db.WriteOnlyGraph; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteOnlyRequest; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.ClusterSetExistException; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.ClusterTraitsBase; import org.simantics.db.service.ClusteringSupport; import org.simantics.db.testing.base.ExistingDatabaseTest; import org.simantics.layer0.Layer0; /** * Tests that cluster set can be used to explicitly control clustering. * Must be called more than once from runner to test that cluster set is persistent. */ public class Issue3420Test2 extends ExistingDatabaseTest { // final boolean DEBUG = true; final boolean VERBOSE = false; final int CLUSTER_FILL_SIZE = ClusterTraitsBase.getMaxNumberOfResources(); final Vector clusterSets = new Vector(); final Vector resources = new Vector(); @Test public void test() throws DatabaseException { Session session = getSession(); session.syncRequest(new InitClusterSet()); session.syncRequest(new Init()); session.syncRequest(new Modi()); session.syncRequest(new Check()); session.syncRequest(new ModiWO()); session.syncRequest(new Check()); } class InitClusterSet extends WriteRequest { @Override public void perform(WriteGraph g) throws DatabaseException { Layer0 l0 = Layer0.getInstance(g); String base = "http://Projects/Development%20Project"; String tail = "Issue3420Test2"; Resource pl = g.getResource(base); if (DEBUG) System.out.println("projects=" + pl); Resource il = g.getPossibleResource(base + "/" + tail); try { g.newClusterSet(pl); // New cluster set. assertTrue("Old cluster set data lost.", null == il); il = g.newResource(); g.claim(il, l0.InstanceOf, null, l0.Library); g.claim(il, l0.PartOf, pl); g.claimLiteral(il, l0.HasName, tail); } catch (ClusterSetExistException e) { // Old cluster set. assertTrue("Missing library resource.", null != il); } clusterSets.setSize(2); clusterSets.set(0, pl); clusterSets.set(1, il); } } class Init extends ReadRequest { @Override public void run(ReadGraph g) throws DatabaseException { resources.clear(); Layer0 l0 = Layer0.getInstance(g); for (Resource r : g.getObjects(clusterSets.get(1), l0.ConsistsOf)) { if (DEBUG) if (VERBOSE) System.out.println("Resource " + r); resources.add(r); } if (DEBUG) System.out.println("Resource count=" + resources.size()); } } class Modi extends WriteRequest { @Override public void perform(WriteGraph g) throws DatabaseException { for (int i=0; i clusters = new HashMap(); for (int i=0; i sortedKeys = new TreeSet(); for (Map.Entry i : clusters.entrySet()) sortedKeys.add(i.getKey()); long sum = 0; for (long key : sortedKeys) { int count = clusters.get(key); if (DEBUG) System.out.println("cluster=" + key + " count=" + count); sum += count; } if (resources.size() != sum) { String msg = "Resource count mismatch. expected=" + resources.size() + " realized=" + sum; if (DEBUG) System.out.println(msg); fail(msg); } int n = resources.size() / CLUSTER_FILL_SIZE + 1; if (n < clusters.size()) { String msg = "Cluster count mismatch. expected=" + n + " realized=" + clusters.size(); if (DEBUG) System.out.println(msg); fail(msg); } } } }