]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/transferableGraphSupport/TransferableGraphSupportTest2.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / support / transferableGraphSupport / TransferableGraphSupportTest2.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.support.transferableGraphSupport;
13
14 import java.io.IOException;
15 import java.util.UUID;
16
17 import org.junit.Test;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.databoard.binding.Binding;
20 import org.simantics.databoard.serialization.SerializationException;
21 import org.simantics.databoard.serialization.Serializer;
22 import org.simantics.databoard.serialization.SerializerConstructionException;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.Session;
26 import org.simantics.db.WriteGraph;
27 import org.simantics.db.common.request.ReadRequest;
28 import org.simantics.db.common.request.WriteRequest;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.service.TransferableGraphSupport;
31 import org.simantics.db.testing.base.ExistingDatabaseTest;
32 import org.simantics.layer0.Layer0;
33 import org.simantics.utils.DataContainer;
34
35 public class TransferableGraphSupportTest2 extends ExistingDatabaseTest {
36
37         @Test
38     public void test() throws Exception {
39
40         Session session = getSession();
41
42         final String value = UUID.randomUUID().toString();
43         final TransferableGraphSupport support = session.getService(TransferableGraphSupport.class);
44         final DataContainer<Resource> resource = new DataContainer<Resource>();
45
46         try {
47             session.syncRequest(new WriteRequest() {
48                 @Override
49                 public void perform(WriteGraph graph) throws DatabaseException {
50
51                     Layer0 b = Layer0.getInstance(graph);
52                                 
53                     Resource r = graph.newResource();
54                     resource.set(r);
55                     graph.claim(r, b.InstanceOf, null, b.String);
56                     
57                                         try {
58                             Binding binding = Bindings.STRING;
59                                                 Serializer serializer = Bindings.getSerializer( binding );
60                                         byte[] raw = serializer.serialize(value);
61                                         support.setValue(graph, r, null, raw);
62                                         } catch (SerializerConstructionException e) {
63                                                 throw new Error(e);
64                                         } catch (SerializationException e) {
65                                                 throw new Error(e);
66                                         } catch (IOException e) {
67                                                 throw new Error(e);
68                                         }
69                 }
70             });
71         } catch (Throwable e) {
72                 e.printStackTrace();
73             fail("Write transaction threw an unknown exception " + e);
74         }
75         
76         
77         try {
78             session.syncRequest(new ReadRequest() {
79                 @Override
80                 public void run(ReadGraph graph) throws DatabaseException {
81                     String v = graph.getValue(resource.get());
82                     assert(v.equals(value));
83                 }
84             });
85         } catch (Throwable e) {
86             fail("Write transaction threw an unknown exception " + e);
87         }
88         
89     }
90
91 }