--- /dev/null
+package org.simantics.db.tests.regression.bugs;
+
+/*******************************************************************************
+ * 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
+ *******************************************************************************/
+
+import org.junit.Test;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ImmutableException;
+import org.simantics.db.service.ClusterControl;
+import org.simantics.db.service.XSupport;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.db.testing.common.TestBase;
+
+ /**
+ * Tests that setting cluster to immutable after cluster is changed to big works.
+ * Init request adds immutable cluster to database.
+ * Modify request checks that it is immutable.
+ * The tests requires that small cluster is converted to big and that the cluster is fetched from the server.
+ *
+ * There was a bug in ProCoreServer which caused changes in immutable state to be discarded when small cluster was converted to big before setting immutable state.
+ * Refs #4688 (https://www.simantics.org/redmine/issues/4688). Fixes https://www.simulationsite.net/redmine/issues/8866.
+ */
+public class Issue4688Test1 extends ExistingDatabaseTest {
+ private final String URI_NAME = "ImmutabaleTest" + getRandomString();
+ private final String URI_PATH = TestBase.ROOT_LIBRARY_URI + "/" + URI_NAME;
+ @Test
+ public void test() throws DatabaseException {
+ Session session = getSession();
+ session.syncRequest(new Init());
+ session.syncRequest(new Modify());
+ }
+ class Init extends WriteRequest {
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ ClusterControl cc = (ClusterControl)g.getService(ClusterControl.class);
+ cc.collectClusters(Integer.MAX_VALUE);
+ int nClusters = cc.flushClusters();
+ g.flushCluster(); // Starts new cluster.
+ Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
+ Resource testRoot = g.getPossibleResource(URI_PATH);
+ assertTrue(URI_PATH + " must not be defined.", null == testRoot);
+ testRoot = g.newResource();
+ g.claim(testRoot, L0.InstanceOf, L0.Library);
+ g.addLiteral(testRoot, L0.HasName, L0.NameOf, L0.String, URI_NAME, Bindings.STRING);
+ g.claim(rl, L0.ConsistsOf, testRoot);
+ XSupport xs = (XSupport)g.getService(XSupport.class);
+ assertFalse("GetImmutable did not work.", xs.getImmutable(testRoot));
+ int size = (1<<14) - 1;
+ for (int i=0; i<1024; ++i) {
+ Resource r = g.newResource();
+ g.claim(testRoot, L0.Relation, r);
+ g.claimValue(r, bytes(size), Bindings.BYTE_ARRAY);
+ }
+ xs.setImmutable(testRoot, true);
+ assertTrue("SetImmutable did not work.", xs.getImmutable(testRoot));
+ cc.collectClusters(Integer.MAX_VALUE);
+ int nClusters2 = cc.flushClusters();
+ assertTrue("Could not relase clusters.", nClusters + 1 == nClusters2);
+ }
+ byte[] bytes(int size) {
+ byte[] result = new byte[size];
+ for(int i=0;i<size;i++) result[i] = (byte)i;
+ return result;
+ }
+ }
+ class Modify extends WriteRequest {
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ Resource testRoot = g.getResource(URI_PATH);
+ XSupport xs = (XSupport)g.getService(XSupport.class);
+ assertTrue("SetImmutable did not work.", xs.getImmutable(testRoot));
+ try {
+ g.claim(testRoot, L0.InstanceOf, L0.Relation);
+ } catch (ImmutableException e) {
+ return;
+ }
+ fail("Immutable check did not work.");
+ }
+ }
+}