1 package org.simantics.db.tests.regression.bugs;
4 import org.simantics.db.Resource;
5 import org.simantics.db.Session;
6 import org.simantics.db.WriteGraph;
7 import org.simantics.db.WriteOnlyGraph;
8 import org.simantics.db.common.request.WriteOnlyResultRequest;
9 import org.simantics.db.common.request.WriteRequest;
10 import org.simantics.db.exception.AssumptionException;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.exception.InvalidResourceReferenceException;
13 import org.simantics.db.impl.query.QuerySupport;
14 import org.simantics.db.service.SerialisationSupport;
15 import org.simantics.db.service.XSupport;
16 import org.simantics.db.testing.cases.FreshDatabaseTest;
18 public class Issue3108Test1 extends FreshDatabaseTest {
19 private static final boolean DEBUG = false;
20 static class TestGetResourceId extends WriteRequest {
22 public TestGetResourceId(int clusterIndex, int resourceIndex) {
23 this.key = (clusterIndex << 16) + resourceIndex;
25 @SuppressWarnings("deprecation") // Using createRandomAccessId to simulate error while loading non existing cluster.
27 public void perform(WriteGraph graph) throws DatabaseException {
28 SerialisationSupport ss = graph.getService(SerialisationSupport.class);
29 QuerySupport gs = graph.getService(QuerySupport.class);
30 Resource r = gs.getResource(key);
32 // This used to throw RuntimeDatabaseException when loading illegal cluster failed
33 // but now the implementation has changed and this does not try to load cluster.
34 // long id = r.getResourceId();
36 // throw new AssumptionException("Illegal resource returned zero as expected.");
38 System.out.println("DEBBUG: resource key=" + key + " r="+ r);
39 ss.getResourceSerializer().createRandomAccessId(r);
40 } catch (InvalidResourceReferenceException e) {
42 System.out.println("Catched InvalidResourceReferenceException:" + e.getMessage());
43 throw new AssumptionException("Illegal resource threw runtime error.", e);
48 public void testIssue3108Test1() throws Exception {
49 final Session session = getSession();
50 // This simulates the situation in Apros bug 4210.
51 // We have an illegal entry in cluster table and we want to handle it
53 boolean exceptionThrown = false;
55 XSupport xs = (XSupport)session.getService(XSupport.class);
56 long illegalClusterId1 = 666666;
57 int clusterIndex = xs.corruptClusterTable(illegalClusterId1);
58 session.syncRequest(new TestGetResourceId(clusterIndex, 666));
59 } catch (Exception e) {
60 exceptionThrown = true;
62 System.out.println("Catched Exception as excepected:" + e.getMessage());
63 } catch (Throwable e) {
64 fail("Write transaction threw an unknown exception " + e);
66 assertTrue("Failed to throw exception as expected.", exceptionThrown);
67 // Here we have an illegal resource id without proxy in cluster table.
68 exceptionThrown = false;
70 session.syncRequest(new TestGetResourceId(666, 666));
71 } catch (AssumptionException e) {
72 exceptionThrown = true;
74 System.out.println("Catched AssumptionException as excepected:" + e.getMessage());
75 } catch (Throwable e) {
76 fail("Write transaction threw an unknown exception " + e);
78 assertTrue("Failed to throw exception as expected.", exceptionThrown);
79 // Here we have an illegal resource index with valid cluster proxy.
80 // Note that in this case the invalid resource index is not reported.
82 session.syncRequest(new TestGetResourceId(1, 6666));
83 } catch (Throwable e) {
84 fail("Write transaction threw an unknown exception " + e);
87 static class TestWriteOnly extends WriteOnlyResultRequest<Long> {
91 public Long perform(WriteOnlyGraph graph) throws DatabaseException {
93 XSupport xs = (XSupport)graph.getService(XSupport.class);
94 xs.setClusterStreamOff(true);
95 Resource r = graph.newResource();
96 id = r.getResourceId();
97 SerialisationSupport ss = graph.getService(SerialisationSupport.class);
98 key = ss.getTransientId(id);
99 xs.setClusterStreamOff(false);