]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/Issue3108Test1.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / regression / bugs / Issue3108Test1.java
1 package org.simantics.db.tests.regression.bugs;
2
3 import org.junit.Test;
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;
17
18 public class Issue3108Test1 extends FreshDatabaseTest {
19     private static final boolean DEBUG = false;
20     static class TestGetResourceId extends WriteRequest {
21         final int key;
22         public TestGetResourceId(int clusterIndex, int resourceIndex) {
23             this.key = (clusterIndex << 16)  + resourceIndex;
24         }
25         @SuppressWarnings("deprecation") // Using createRandomAccessId to simulate error while loading non existing cluster.
26         @Override
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);
31             try {
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();
35 //                if (0 == id)
36 //                    throw new AssumptionException("Illegal resource returned zero as expected.");
37                 if (DEBUG)
38                     System.out.println("DEBBUG: resource key=" + key + " r="+ r);
39                   ss.getResourceSerializer().createRandomAccessId(r);
40                 } catch (InvalidResourceReferenceException e) {
41                     if (DEBUG)
42                         System.out.println("Catched InvalidResourceReferenceException:" + e.getMessage());
43                     throw new AssumptionException("Illegal resource threw runtime error.", e);
44             }
45         }
46     }
47         @Test
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
52         // gracefully.
53         boolean exceptionThrown = false;
54         try {
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;
61             if (DEBUG)
62                 System.out.println("Catched Exception as excepected:" + e.getMessage());
63         } catch (Throwable e) {
64             fail("Write transaction threw an unknown exception " + e);
65         }
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;
69         try {
70             session.syncRequest(new TestGetResourceId(666, 666));
71         } catch (AssumptionException e) {
72             exceptionThrown = true;
73             if (DEBUG)
74                 System.out.println("Catched AssumptionException as excepected:" + e.getMessage());
75         } catch (Throwable e) {
76             fail("Write transaction threw an unknown exception " + e);
77         }
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.
81         try {
82             session.syncRequest(new TestGetResourceId(1, 6666));
83         } catch (Throwable e) {
84             fail("Write transaction threw an unknown exception " + e);
85         }
86     }
87     static class TestWriteOnly extends WriteOnlyResultRequest<Long> {
88         public long id = 0;
89         public int key = 0;
90         @Override
91         public Long perform(WriteOnlyGraph graph) throws DatabaseException {
92             graph.flushCluster();
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);
100             return id;
101         }
102     }
103 }