]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/request/WriteMetadataTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / write / request / WriteMetadataTest.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.write.request;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.TreeMap;
18
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.simantics.databoard.Databoard;
22 import org.simantics.databoard.annotations.Arguments;
23 import org.simantics.databoard.annotations.Union;
24 import org.simantics.databoard.binding.Binding;
25 import org.simantics.databoard.serialization.Serializer;
26 import org.simantics.db.ChangeSetIdentifier;
27 import org.simantics.db.Metadata;
28 import org.simantics.db.Resource;
29 import org.simantics.db.Session;
30 import org.simantics.db.WriteGraph;
31 import org.simantics.db.common.request.WriteRequest;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.exception.RuntimeDatabaseException;
34 import org.simantics.db.service.ManagementSupport;
35 import org.simantics.db.testing.base.ExistingDatabaseTest;
36
37 /**
38  * @author Tuukka Lehtonen
39  */
40 public class WriteMetadataTest extends ExistingDatabaseTest {
41     boolean DEBUG = false;
42     @Test
43     public void test() throws Exception{
44
45         final Resource root = getSession().getRootLibrary();
46
47         Collection<ChangeSetIdentifier> ids = getSession().getService(ManagementSupport.class).getChangeSetIdentifiers(Long.MIN_VALUE, Long.MAX_VALUE);
48         if (DEBUG)
49             for (ChangeSetIdentifier id : ids) {
50                 System.out.println("CSID: " + id.getId());
51                 System.out.println("    METADATA: " + id.getMetadata());
52             }
53
54         getSession().syncRequest(new WriteRequest() {
55             @Override
56             public void perform(WriteGraph graph) throws DatabaseException {
57                 Resource r = graph.newResource();
58                 graph.claim(root, L0.InstanceOf, L0.Library);
59                 graph.claim(root, L0.ConsistsOf, r);
60
61                 TreeMap<Resource, StructuralChanges.Change[]> map = new TreeMap<Resource, StructuralChanges.Change[]>();
62                 map.put(r, new StructuralChanges.Change[] { new StructuralChanges.ComponentAddition(r) });
63                 StructuralChanges ch = new StructuralChanges(map);
64                 graph.addMetadata(ch);
65             }
66         });
67         int count = ids.size();
68         ids = getSession().getService(ManagementSupport.class).getChangeSetIdentifiers(Long.MIN_VALUE, Long.MAX_VALUE);
69         Assert.assertTrue("Metadata not updated.", count < ids.size());
70         if (DEBUG)
71             for (ChangeSetIdentifier id : ids) {
72                 System.out.println("CSID: " + id.getId());
73                 System.out.println("    METADATA: " + id.getMetadata());
74             }
75         if (DEBUG) {
76             Thread.sleep(5000);
77
78             ids = getSession().getService(ManagementSupport.class).getChangeSetIdentifiers(Long.MIN_VALUE, Long.MAX_VALUE);
79             for (ChangeSetIdentifier id : ids) {
80                 System.out.println("CSID: " + id.getId());
81                 System.out.println("    METADATA: " + id.getMetadata());
82             }
83         }
84     }
85
86 }
87
88 class StructuralChanges implements Metadata {
89
90     public final @Arguments({Resource.class, Change[].class}) TreeMap<Resource, Change[]> modelChanges;
91
92     public StructuralChanges(TreeMap<Resource, Change[]> modelChanges) {
93         this.modelChanges = modelChanges;
94     }
95
96     public StructuralChanges(Map<Resource, ArrayList<Change>> _modelChanges) {
97         this(new TreeMap<Resource, Change[]>());
98         for(Map.Entry<Resource, ArrayList<Change>> entry : _modelChanges.entrySet()) {
99             ArrayList<Change> value = entry.getValue();
100             modelChanges.put(entry.getKey(), value.toArray(new Change[value.size()]));
101         }
102     }
103
104     public static enum ChangeType {
105         COMPONENT_ADDITION, COMPONENT_REMOVAL,
106         COMPONENT_MODIFICATION, CONNECTION_CHANGE
107     }
108
109     @Union({ComponentAddition.class,
110         ComponentRemoval.class,
111         ComponentModification.class,
112         ConnectionChange.class})
113     public static interface Change {
114         ChangeType getType();
115     }
116
117     public static class ComponentAddition implements Change {
118         public final Resource component;
119         public ComponentAddition(Resource component) {
120             this.component = component;
121         }
122         @Override
123         public ChangeType getType() {
124             return ChangeType.COMPONENT_ADDITION;
125         }
126     }
127
128     public static class ComponentRemoval implements Change {
129         public final Resource component;
130         public final Resource parent;
131         public ComponentRemoval(Resource component, Resource parent) {
132             this.component = component;
133             this.parent = parent;
134         }
135         @Override
136         public ChangeType getType() {
137             return ChangeType.COMPONENT_REMOVAL;
138         }
139     }
140
141     public static class ComponentModification implements Change {
142         public final Resource component;
143         public ComponentModification(Resource component) {
144             this.component = component;
145         }
146         @Override
147         public ChangeType getType() {
148             return ChangeType.COMPONENT_MODIFICATION;
149         }
150     }
151
152     public static class ConnectionChange implements Change {
153         public final Resource connection;
154         public ConnectionChange(Resource connection) {
155             this.connection = connection;
156         }
157         @Override
158         public ChangeType getType() {
159             return ChangeType.CONNECTION_CHANGE;
160         }
161     }
162
163     public Change[] get(Resource model) {
164         return modelChanges.get(model);
165     }
166
167     @Override
168     public byte[] serialise(Session session) {
169         try {
170             Databoard databoard = session.getService( Databoard.class );
171             Binding binding = databoard.getBinding( StructuralChanges.class );
172             Serializer serializer = databoard.getSerializer( binding );
173             return serializer.serialize(this);
174         } catch (Exception e) {
175             e.printStackTrace();
176             if (e instanceof RuntimeDatabaseException)
177                 throw (RuntimeDatabaseException)e;
178             else
179                 throw new RuntimeDatabaseException("Serialization failed.", e);
180         }
181     }
182
183     public static StructuralChanges deserialise(Session session, byte[] input) {
184         try {
185             Databoard databoard = session.getService( Databoard.class );
186             Binding binding = databoard.getBinding( StructuralChanges.class );
187             Serializer serializer = databoard.getSerializer( binding );
188             return (StructuralChanges) serializer.deserialize(input);
189         } catch (Exception e) {
190             e.printStackTrace();
191             if (e instanceof RuntimeDatabaseException)
192                 throw (RuntimeDatabaseException)e;
193             else
194                 throw new RuntimeDatabaseException("Deserialization failed.", e);
195         }
196     }
197
198 }