1 package org.simantics.db.testing.common;
5 import java.io.FileInputStream;
6 import java.io.InputStream;
7 import java.util.ArrayList;
8 import java.util.Collection;
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;
37 private static AcornTestHandler testHandler;
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;
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;
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);
58 public static void freshDatabase() throws Exception {
59 DatabaseState state = freshWorkspace(Configuration.get().coreDir, null);
63 public static DatabaseState existingDatabase() throws Exception {
64 return oldSimanticsWorkspace(null, Configuration.get().coreDir);
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);
75 public static DatabaseState newSimanticsWorkspace(TestSettings testSettings, String address) throws Exception {
76 AcornTestHandler testHandler = getTestHandler(testSettings, address);
77 testHandler.initNew();
79 SessionContext sessionContext = SimanticsPlatform.INSTANCE.startUp(Simantics.getDefaultDatabaseDriver(), null, RecoveryPolicy.FixError, OntologyRecoveryPolicy.Merge, true, new DefaultChoiceUserAgent());
80 return new DatabaseState(address, sessionContext);
83 public static DatabaseState oldSimanticsWorkspace(TestSettings testSettings, String address) throws Exception {
84 getTestHandler(testSettings, address);
86 SessionContext sessionContext = SimanticsPlatform.INSTANCE.startUp(Simantics.getDefaultDatabaseDriver(), null, RecoveryPolicy.FixError, OntologyRecoveryPolicy.Merge, false, null);
87 return new DatabaseState(address, sessionContext);
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;
96 if (Platform.isRunning()) {
97 SimanticsPlatform.INSTANCE.shutdown(null);
99 } // Cleanup without platform code.
102 String address = state.getAddress();
105 getTestHandler(null, address).getManagement().shutdown();
106 } catch (Throwable t) {
107 System.out.println(t);
111 public static void initOntology(Session session, String relative) throws DatabaseException {
112 // Open transferable graph file for layer0.
115 binding = Bindings.getBinding(TransferableGraph1.class);
116 } catch (BindingConstructionException e) {
117 throw new DatabaseException(e);
119 File t = new File(TestSettings.getInstance().getWorkspace(), relative);
121 throw new DatabaseException("File does not exist. File=" + t + ".");
123 TransferableGraph1 layer0;
125 InputStream is = new FileInputStream(t);
126 layer0 = (TransferableGraph1) Files.readFile(is, binding);
128 } catch (Exception e) {
130 throw new DatabaseException(e);
133 TransferableGraphs.importGraph(session, layer0);
134 } catch (Throwable e) {
136 throw new DatabaseException("Failed to init db.", e);
139 // public static TestHandler newTestHandler(TestSettings testSettings, String dbFolderName) throws DatabaseException {
140 // return new TestHandler(testSettings, dbFolderName);
143 public static AcornTestHandler getTestHandler() throws DatabaseException {
144 return getTestHandler(null, null);
147 public static AcornTestHandler getTestHandler(TestSettings testSettings, String address) throws DatabaseException {
148 if (null == testHandler)
149 testHandler = new AcornTestHandler(testSettings, address);
153 public static void closeSession(Session session) throws DatabaseException {
155 LifecycleSupport support = session.getService(LifecycleSupport.class);
156 support.close(-1, false);
158 } catch (Exception e) {
160 throw new DatabaseException("Session did not close cleanly.");
164 public static void killCore() throws DatabaseException {