1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.api.story.misc;
14 import java.util.Collection;
15 import java.util.HashMap;
18 import org.junit.Test;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.testing.base.ExistingDatabaseTest;
24 import org.simantics.db.testing.common.WriteQuery;
25 import org.simantics.layer0.Layer0;
29 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
32 public class DataModelTest extends ExistingDatabaseTest {
35 * Creates new instance and then tries to find it.
39 public void testCreateInstance() throws Exception{
40 Session session = getSession();
41 final Resource rootLib = getSession().getRootLibrary();
42 final String newInstanceName = "New Instance " + getRandomString();
44 session.syncRequest(new WriteQuery(this) {
46 public void run(WriteGraph g) throws Throwable {
47 Layer0 b = Layer0.getInstance(g);
48 Resource newResource = g.newResource();
49 g.claim(newResource, b.InstanceOf, null, b.Type);
50 g.claimLiteral(newResource, b.HasName, newInstanceName);
51 g.claim(rootLib, b.ConsistsOf, newResource);
52 // comment(g, newResource, "DataModelTest.testCreateInstance");
58 session.syncRequest(new WriteQuery(this) {
60 public void run(WriteGraph g) throws Throwable {
61 Layer0 b = Layer0.getInstance(g);
62 Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
63 Resource newResource = null;
64 for (Resource r : resources) {
65 String value = g.getPossibleRelatedValue(r, b.HasName);
66 if (null != value && value.equals(newInstanceName)) {
71 if (newResource == null) {
72 throw new Exception("Could not find created resource");
74 if (!g.isInstanceOf(newResource, b.Type))
75 throw new Exception("Created resource is not an instance of Type");
84 * Creates new type and new instance of it. Then tries to find the instance and the type, and verifies that inheritance is correct.
88 public void testCreateInstance2() throws Exception{
89 Session session = getSession();
90 final Resource rootLib = getSession().getRootLibrary();
91 final String random = getRandomString();
92 final String newTypeName = "New Type " + random;
93 final String newInstanceName = "New Instance " + random;
95 session.syncRequest(new WriteQuery(this) {
97 public void run(WriteGraph g) throws Throwable {
98 Layer0 b = Layer0.getInstance(g);
99 Resource newType = g.newResource();
100 g.claim(newType, b.InstanceOf, null, b.Type);
101 g.claim(newType, b.Inherits, b.SupertypeOf, b.Type);
102 g.claimLiteral(newType, b.HasName, newTypeName);
103 g.claim(rootLib, b.ConsistsOf, newType);
104 Resource newResource = g.newResource();
105 g.claim(newResource, b.InstanceOf, null, newType);
106 g.claimLiteral(newResource, b.HasName, newInstanceName);
107 g.claim(rootLib, b.ConsistsOf, newResource);
108 Map<String, String> md = new HashMap<String, String>();
109 md.put("DataModelTest2", "DataModelTest2");
117 session.syncRequest(new TestReadRequest() {
119 public void run(ReadGraph g) throws Throwable {
120 Layer0 b = Layer0.getInstance(g);
121 Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
122 Resource newType = null;
123 Resource newInstance = null;
124 for (Resource r : resources) {
125 String value = g.getPossibleRelatedValue(r, b.HasName);
128 if (value.equals(newTypeName)) {
130 } else if (value.equals(newInstanceName)) {
135 throw new Exception("Could not find created type");
136 if (newInstance == null)
137 throw new Exception("Could not find created instance");
139 if (!g.isInstanceOf(newInstance, b.Type))
140 throw new Exception("Created resource is not an instance of Type");
141 if (!g.isInstanceOf(newInstance, newType))
142 throw new Exception("Created resource is not an instance of created type");