]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/readGraph/adapt/AdaptionFailureTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / readGraph / adapt / AdaptionFailureTest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.api.readGraph.adapt;
13
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.request.ResourceAsyncRead;
22 import org.simantics.db.common.request.WriteRequest;
23 import org.simantics.db.exception.AdaptionException;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.procedure.AsyncProcedure;
26 import org.simantics.db.request.Read;
27 import org.simantics.db.services.GlobalServiceInitializer;
28 import org.simantics.db.testing.base.ExistingDatabaseTest;
29 import org.simantics.layer0.Layer0;
30
31 /**
32  * Checks that {@link ReadGraph#adapt(org.simantics.db.Resource, Class)},
33  * {@link ReadGraph#adaptUnique(org.simantics.db.Resource, Class)},
34  * <code>AsyncReadGraph.forAdapted</code> fail as expected.
35  * 
36  * <p>
37  * Regression test for https://www.simulationsite.net/trac/simantics/ticket/617
38  * 
39  * @author Tuukka Lehtonen
40  */
41 public class AdaptionFailureTest extends ExistingDatabaseTest {
42
43     @Override
44     @Before
45     public void setUp() throws Exception {
46         super.setUp();
47         new GlobalServiceInitializer().initialize(getSession());
48     }
49
50     private Resource writeTypeless(Session session) throws DatabaseException {
51         final Resource[] typeless = { null };
52         session.syncRequest(new WriteRequest() {
53             @Override
54             public void perform(WriteGraph graph) throws DatabaseException {
55                 Layer0 l0 = Layer0.getInstance(graph);
56                 Resource tless = graph.newResource();
57                 Resource name = graph.newResource();
58                 graph.claim(name, l0.InstanceOf, null, l0.String);
59                 graph.claimValue(name, "A Typeless Resource");
60                 graph.claim(tless, l0.HasName, name);
61                 typeless[0] = tless;
62             }
63         });
64         return typeless[0];
65     }
66
67     static abstract class Adapter {
68         String name;
69
70         public Adapter(String name) {
71             this.name = name;
72         }
73
74         public abstract <T> T adapt(ReadGraph graph, Resource r, Class<T> clazz) throws DatabaseException;
75
76         @Override
77         public String toString() {
78             return name;
79         }
80     }
81
82     private void adaptTester(final Adapter adapter) throws DatabaseException {
83         final Session session = getSession();
84         final Resource typeless = writeTypeless(session);
85         try {
86             String s = session.syncRequest(new Read<String>() {
87                 @Override
88                 public String perform(ReadGraph graph) throws DatabaseException {
89                     try {
90                         return adapter.adapt(graph, typeless, String.class);
91                     } catch (AdaptionException e) {
92                         throw e;
93                     } catch (DatabaseException e) {
94                         throw new DatabaseException("AdaptionException expected, got " + e, e);
95                     }
96                 }
97             });
98
99             if (s == null)
100                 fail(adapter + " must never return null");
101         } catch (AdaptionException e) {
102             // Failure with AdaptionException is expected
103             if (DEBUG)
104                 System.out.println("Expected failure message: " + e.getMessage());
105         }
106     }
107
108     @Test
109     public void testAdapt() throws DatabaseException{
110         adaptTester(new Adapter("ReadGraph.adapt") {
111             @Override
112             public <T> T adapt(ReadGraph graph, Resource resource, Class<T> clazz) throws DatabaseException {
113                 return graph.adapt(resource, clazz);
114             }
115         });
116     }
117
118     @Test
119     public void testAdaptUnique() throws DatabaseException{
120         adaptTester(new Adapter("ReadGraph.adaptUnique") {
121             @Override
122             public <T> T adapt(ReadGraph graph, Resource resource, Class<T> clazz) throws DatabaseException {
123                 return graph.adaptUnique(resource, clazz);
124             }
125         });
126     }
127
128     @Test
129     public void testforAdapted() throws DatabaseException{
130         adaptTester(new Adapter("AsyncReadGraph.forAdapted") {
131             @Override
132             public <T> T adapt(ReadGraph graph, Resource resource, final Class<T> clazz) throws DatabaseException {
133                 return graph.syncRequest(new ResourceAsyncRead<T>(resource) {
134                     @Override
135                     public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
136                         graph.forAdapted(resource, clazz, procedure);
137                     }
138                 });
139             }
140         });
141     }
142
143     @Test
144     public void testforUniqueAdapted() throws DatabaseException{
145         adaptTester(new Adapter("AsyncReadGraph.forUniqueAdapted") {
146             @Override
147             public <T> T adapt(ReadGraph graph, Resource resource, final Class<T> clazz) throws DatabaseException {
148                 return graph.syncRequest(new ResourceAsyncRead<T>(resource) {
149                     @Override
150                     public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
151                         graph.forUniqueAdapted(resource, clazz, procedure);
152                     }
153                 });
154             }
155         });
156     }
157
158 }