]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.testing/src/org/simantics/db/testing/common/Tests.java
Fixing problems in the database unit testing environment with Acorn
[simantics/platform.git] / bundles / org.simantics.db.testing / src / org / simantics / db / testing / common / Tests.java
1 package org.simantics.db.testing.common;
2
3
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.InputStream;
7 import java.util.ArrayList;
8 import java.util.Collection;
9
10 import org.eclipse.core.runtime.Platform;
11 import org.simantics.DefaultChoiceUserAgent;
12 import org.simantics.Simantics;
13 import org.simantics.SimanticsPlatform;
14 import org.simantics.SimanticsPlatform.OntologyRecoveryPolicy;
15 import org.simantics.SimanticsPlatform.RecoveryPolicy;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.databoard.Files;
18 import org.simantics.databoard.binding.Binding;
19 import org.simantics.databoard.binding.error.BindingConstructionException;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.Statement;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.management.ISessionContextProvider;
25 import org.simantics.db.management.ISessionContextProviderSource;
26 import org.simantics.db.management.SessionContext;
27 import org.simantics.db.management.SessionContextProvider;
28 import org.simantics.db.management.SingleSessionContextProviderSource;
29 import org.simantics.db.service.LifecycleSupport;
30 import org.simantics.db.testing.impl.Configuration;
31 import org.simantics.graph.db.TransferableGraphs;
32 import org.simantics.graph.representation.TransferableGraph1;
33 import org.simantics.scl.osgi.SCLOsgi;
34
35 public class Tests {
36
37     private static AcornTestHandler testHandler;
38
39     public static boolean contains(Collection<Statement> stms, Resource predicate, Resource object) {
40         for(Statement stm : stms) {
41             if(stm.getPredicate().equals(predicate) && stm.getObject().equals(object)) return true;
42         }
43         return false;
44     }
45
46     public static boolean contains(Collection<Statement> stms, Resource subject, Resource predicate, Resource object) {
47         for(Statement stm : stms) {
48             if(stm.getSubject().equals(subject) && stm.getPredicate().equals(predicate) && stm.getObject().equals(object)) return true;
49         }
50         return false;
51     }
52
53     public static DatabaseState freshWorkspace(String CORE_DIR, ArrayList<String> fileFilter) throws Exception {
54         SimanticsPlatform.INSTANCE.resetWorkspace(null, fileFilter);
55         return newSimanticsWorkspace(null, CORE_DIR);
56     }
57
58     public static void freshDatabase() throws Exception {
59         DatabaseState state = freshWorkspace(Configuration.get().coreDir, null);
60         shutdown(state);
61     }
62
63     public static DatabaseState existingDatabase() throws Exception {
64         return oldSimanticsWorkspace(null, Configuration.get().coreDir);
65     }
66
67     private static void initSimanticsStuff() {
68         // Set session context provider.
69         final ISessionContextProvider provider = new SessionContextProvider(null);
70         ISessionContextProviderSource source = new SingleSessionContextProviderSource(provider);
71         Simantics.setSessionContextProviderSource(source);
72         org.simantics.db.layer0.internal.SimanticsInternal.setSessionContextProviderSource(source);
73     }
74
75     public static DatabaseState newSimanticsWorkspace(TestSettings testSettings, String address) throws Exception {
76         AcornTestHandler testHandler = getTestHandler(testSettings, address);
77         testHandler.initNew();
78         initSimanticsStuff();
79         SessionContext sessionContext = SimanticsPlatform.INSTANCE.startUp(Simantics.getDefaultDatabaseDriver(), null, RecoveryPolicy.FixError, OntologyRecoveryPolicy.Merge, true, new DefaultChoiceUserAgent());
80         return new DatabaseState(address, sessionContext);
81     }
82
83     public static DatabaseState oldSimanticsWorkspace(TestSettings testSettings, String address) throws Exception {
84         getTestHandler(testSettings, address);
85         initSimanticsStuff();
86         SessionContext sessionContext = SimanticsPlatform.INSTANCE.startUp(Simantics.getDefaultDatabaseDriver(), null, RecoveryPolicy.FixError, OntologyRecoveryPolicy.Merge, false, null);
87         return new DatabaseState(address, sessionContext);
88     }
89     public static void shutdown(DatabaseState state) throws Exception {
90         if (SCLOsgi.MODULE_REPOSITORY != null)
91             SCLOsgi.MODULE_REPOSITORY.flush();
92         SCLOsgi.SOURCE_REPOSITORY = null;
93         SCLOsgi.MODULE_REPOSITORY = null;
94         SCLOsgi.TEST_REPOSITORY = null;
95
96         if (Platform.isRunning()) {
97             SimanticsPlatform.INSTANCE.shutdown(null);
98             return;
99         } // Cleanup without platform code.
100         if (null == state)
101             return;
102         String address = state.getAddress();
103         try {
104             if (null != address)
105                 getTestHandler(null, address).getManagement().shutdown();
106         } catch (Throwable t) {
107             System.out.println(t);
108         }
109     }
110
111     public static void initOntology(Session session, String relative) throws DatabaseException {
112         // Open transferable graph file for layer0.
113         Binding binding;
114         try {
115             binding = Bindings.getBinding(TransferableGraph1.class);
116         } catch (BindingConstructionException e) {
117             throw new DatabaseException(e);
118         }
119         File t = new File(TestSettings.getInstance().getWorkspace(), relative);
120         if (!t.exists())
121             throw new DatabaseException("File does not exist. File=" + t + ".");
122
123         TransferableGraph1 layer0;
124         try {
125             InputStream is = new FileInputStream(t);
126             layer0 = (TransferableGraph1) Files.readFile(is, binding);
127             is.close();
128         } catch (Exception e) {
129             e.printStackTrace();
130             throw new DatabaseException(e);
131         }
132         try {
133             TransferableGraphs.importGraph(session, layer0);
134         } catch (Throwable e) {
135             e.printStackTrace();
136             throw new DatabaseException("Failed to init db.", e);
137         }
138     }
139 //    public static TestHandler newTestHandler(TestSettings testSettings, String dbFolderName) throws DatabaseException {
140 //        return new TestHandler(testSettings, dbFolderName);
141 //    }
142
143     public static AcornTestHandler getTestHandler() throws DatabaseException {
144         return getTestHandler(null, null);
145     }
146
147     public static AcornTestHandler getTestHandler(TestSettings testSettings, String address) throws DatabaseException {
148         if (null == testHandler)
149             testHandler = new AcornTestHandler(testSettings, address);
150         return testHandler;
151     }
152
153     public static void closeSession(Session session) throws DatabaseException {
154         try {
155             LifecycleSupport support = session.getService(LifecycleSupport.class);
156             support.close(-1, false);
157             session = null;
158         } catch (Exception e) {
159             session = null;
160             throw new DatabaseException("Session did not close cleanly.");
161         }
162     }
163
164     public static void killCore() throws DatabaseException {
165     }
166
167 }