]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph/tests/org/simantics/graph/tests/TestTGResourceUtil.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graph / tests / org / simantics / graph / tests / TestTGResourceUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * 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.graph.tests;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.junit.Test;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.databoard.annotations.Union;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.databoard.binding.RecordBinding;
22 import org.simantics.graph.tests._RVI._RVIPart;
23 import org.simantics.graph.tests._RVI._ResourceRVIPart;
24 import org.simantics.graph.tests._RVI._Role;
25 import org.simantics.graph.tests._RVI._StringRVIPart;
26 import org.simantics.graph.utils.TGResourceUtil;
27
28 public class TestTGResourceUtil {
29         
30         
31         public @Test void test() throws Exception {
32                 
33                 _ResourceRVIPart part1 = new _ResourceRVIPart();
34                 part1.resource = 5;
35                 part1.role = _Role.CHILD;
36                 
37                 _StringRVIPart part2 = new _StringRVIPart();
38                 part2.string = "Value";
39                 part2.role = _Role.PROPERTY;
40                 
41                 _RVI rvi = new _RVI();
42                 rvi.parts = new _RVIPart[] { part1, part2 };
43                 rvi.variant = 3L;
44                 
45                 Binding b1 = Bindings.getBinding( _RVI.class );
46                 RecordBinding b2 = Bindings.getBinding( _ResourceRVIPart.class );
47                 b2.componentBindings[1].type().metadata.put("resource", "true");
48                 
49                 TGResourceUtil util = new TGResourceUtil();
50                 util.addType( b1.type() );
51                 
52                 List<Long> resources = new ArrayList<Long>();
53                 util.findResources(b1, rvi, resources);
54                 for (Long l : resources) System.out.println("Found resource: "+l);
55                 
56                 // adapt resources
57                 TGResourceUtil.LongAdapter la = new TGResourceUtil.LongAdapter() {
58                         @Override
59                         public long adapt(long in) {
60                                 return in;
61                         }
62                 };
63                 util.adaptValue(b1, rvi, la);
64                 
65                 System.out.println("rvi = "+b1.toString(rvi));          
66         }
67         
68         
69
70 }
71
72 class _RVI {
73         
74         static enum _Role { CHILD, PROPERTY }   
75         public _RVIPart[] parts;
76         @Union({_ResourceRVIPart.class, _StringRVIPart.class})
77         interface _RVIPart {}
78         static class _ResourceRVIPart implements _RVIPart {
79                 public _Role role;
80                 public long resource;
81         }
82         static class _StringRVIPart implements _RVIPart {
83                 public _Role role;
84                 public String string;
85         }       
86         public Object variant;
87
88 }