1 package org.simantics.db.tests.regression.bugs;
3 /*******************************************************************************
4 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
12 * VTT Technical Research Centre of Finland - initial API and implementation
13 *******************************************************************************/
15 import org.junit.Test;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.Resource;
18 import org.simantics.db.Session;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.request.WriteRequest;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.exception.ImmutableException;
23 import org.simantics.db.service.ClusterControl;
24 import org.simantics.db.service.XSupport;
25 import org.simantics.db.testing.base.ExistingDatabaseTest;
26 import org.simantics.db.testing.common.TestBase;
29 * Tests that setting cluster to immutable after cluster is changed to big works.
30 * Init request adds immutable cluster to database.
31 * Modify request checks that it is immutable.
32 * The tests requires that small cluster is converted to big and that the cluster is fetched from the server.
34 * 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.
35 * Refs #4688 (https://www.simantics.org/redmine/issues/4688). Fixes https://www.simulationsite.net/redmine/issues/8866.
37 public class Issue4688Test1 extends ExistingDatabaseTest {
38 private final String URI_NAME = "ImmutabaleTest" + getRandomString();
39 private final String URI_PATH = TestBase.ROOT_LIBRARY_URI + "/" + URI_NAME;
41 public void test() throws DatabaseException {
42 Session session = getSession();
43 session.syncRequest(new Init());
44 session.syncRequest(new Modify());
46 class Init extends WriteRequest {
48 public void perform(WriteGraph g) throws DatabaseException {
49 ClusterControl cc = (ClusterControl)g.getService(ClusterControl.class);
50 cc.collectClusters(Integer.MAX_VALUE);
51 int nClusters = cc.flushClusters();
52 g.flushCluster(); // Starts new cluster.
53 Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
54 Resource testRoot = g.getPossibleResource(URI_PATH);
55 assertTrue(URI_PATH + " must not be defined.", null == testRoot);
56 testRoot = g.newResource();
57 g.claim(testRoot, L0.InstanceOf, L0.Library);
58 g.addLiteral(testRoot, L0.HasName, L0.NameOf, L0.String, URI_NAME, Bindings.STRING);
59 g.claim(rl, L0.ConsistsOf, testRoot);
60 XSupport xs = (XSupport)g.getService(XSupport.class);
61 assertFalse("GetImmutable did not work.", xs.getImmutable(testRoot));
62 int size = (1<<14) - 1;
63 for (int i=0; i<1024; ++i) {
64 Resource r = g.newResource();
65 g.claim(testRoot, L0.Relation, r);
66 g.claimValue(r, bytes(size), Bindings.BYTE_ARRAY);
68 xs.setImmutable(testRoot, true);
69 assertTrue("SetImmutable did not work.", xs.getImmutable(testRoot));
70 cc.collectClusters(Integer.MAX_VALUE);
71 int nClusters2 = cc.flushClusters();
72 assertTrue("Could not relase clusters.", nClusters + 1 == nClusters2);
74 byte[] bytes(int size) {
75 byte[] result = new byte[size];
76 for(int i=0;i<size;i++) result[i] = (byte)i;
80 class Modify extends WriteRequest {
82 public void perform(WriteGraph g) throws DatabaseException {
83 Resource testRoot = g.getResource(URI_PATH);
84 XSupport xs = (XSupport)g.getService(XSupport.class);
85 assertTrue("SetImmutable did not work.", xs.getImmutable(testRoot));
87 g.claim(testRoot, L0.InstanceOf, L0.Relation);
88 } catch (ImmutableException e) {
91 fail("Immutable check did not work.");