1 package org.simantics.db.testing.common;
2 /*******************************************************************************
3 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
11 * VTT Technical Research Centre of Finland - initial API and implementation
12 *******************************************************************************/
15 import java.security.Permission;
16 import java.util.ArrayList;
17 import java.util.UUID;
19 import org.eclipse.core.runtime.Platform;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.simantics.SimanticsPlatform;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.Session;
26 import org.simantics.db.WriteGraph;
27 import org.simantics.db.WriteOnlyGraph;
28 import org.simantics.db.common.request.WriteOnlyRequest;
29 import org.simantics.db.common.utils.Logger;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.exception.ServiceNotFoundException;
32 import org.simantics.db.management.SessionContext;
33 import org.simantics.db.request.Read;
34 import org.simantics.db.service.LifecycleSupport;
35 import org.simantics.db.testing.impl.Configuration;
36 import org.simantics.layer0.Layer0;
37 import org.simantics.utils.FileUtils;
40 * Base class for Simantics Test Cases. Assumes that ProCore is already running.
42 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
45 abstract public class TestBase /*extends TestCase*/ {
46 public static final boolean DEBUG = Configuration.get().debug;
47 public static final String ROOT_LIBRARY_URI = "http:/";
48 private NoExitSecurityManager noExitSecurityManager;
49 private SecurityManager securityManager;
50 protected DatabaseState state;
51 public Throwable exception2;
53 static boolean printStart = true;
54 public static final ArrayList<String> initialWorkspaceFiles = FileUtils.createFileFilter(Platform.getLocation().toFile(), null);
55 public static void printStart(Object t) {
56 if(printStart) System.out.println("Test is " + t.getClass().getName() /*+ "." + t.getName()*/);
58 protected static void setPrintStart(boolean value) {
62 public Resource getProjectResource() {
63 return SimanticsPlatform.INSTANCE.projectResource;
67 public void setUp() throws Exception {
69 securityManager = System.getSecurityManager();
70 noExitSecurityManager = new NoExitSecurityManager(state.getSession());
71 System.setSecurityManager(noExitSecurityManager);
72 Session session = state.getSession();
73 L0 = Layer0.getInstance(session);
76 public void tearDown() throws Exception {
77 if (noExitSecurityManager != null) {
78 System.setSecurityManager(securityManager);
79 noExitSecurityManager.dispose();
80 noExitSecurityManager = null;
85 securityManager = null;
88 public static void commonTearDown() {
89 Runtime rt = Runtime.getRuntime();
94 System.out.println("Max=" + rt.maxMemory()
95 + " tot=" + rt.totalMemory()
96 + " fre=" + rt.freeMemory());
100 public static String getRandomString() {
101 return UUID.randomUUID().toString();
104 protected Session getSession() throws DatabaseException {
105 return state.getSession();
107 protected SessionContext getSessionContext() {
108 return state.getSessionContext();
111 // protected Resource getRootLibrary() throws DatabaseException {
112 // return getRootLibrary(getSession());
115 // protected Resource getRootLibrary(Session session) throws DatabaseException {
116 // return session.syncRequest(new ReadQuery<Resource>() {
118 // public void run(ReadGraph g) throws DatabaseException {
119 // result = g.getResource(ROOT_LIBRARY_URI);
120 // assertTrue(result.getResourceId() != SessionManagerSource.NullSubjectId);
125 protected abstract class ReadQuery<Result> implements Read<Result> {
126 protected Result result = null;
128 public abstract void run(ReadGraph graph) throws Throwable;
131 public Result perform(ReadGraph graph) {
135 } catch(Throwable t) {
137 new Exception().printStackTrace();
140 if (null == exception2)
148 protected abstract class TestReadRequest extends ReadQuery<Object> {
151 protected abstract class WriteOnlyQuery
152 extends WriteOnlyRequest
155 public abstract void run(WriteOnlyGraph g) throws Throwable;
158 * Since some SimpleGraphRequest can only handle Exceptions, we need to wrap other Throwables inside Exceptions
161 public final void perform(WriteOnlyGraph g) {
164 } catch(Throwable t) {
165 new Exception().printStackTrace();
167 if (null == exception2)
169 throw new RuntimeException("Wrapping thrown non exception to exception.", t);
172 public void run(WriteGraph g) throws Throwable {
173 run((WriteOnlyGraph)g);
178 protected void checkException() throws DatabaseException {
179 if (exception2 != null)
180 if (exception2 instanceof DatabaseException)
181 throw (DatabaseException)exception2;
183 throw new DatabaseException(exception2);
186 protected boolean hasException() {
187 return null != exception2;
190 protected Throwable getException() {
194 protected static class ExitException extends SecurityException {
195 private static final long serialVersionUID = -1982617086752946683L;
196 public final int status;
198 public ExitException(int status) {
199 super("There is no escape!");
200 this.status = status;
204 private static class NoExitSecurityManager extends SecurityManager {
207 NoExitSecurityManager(Session session) {
208 this.session = session;
209 // this.thread = Thread.currentThread();
212 public void dispose() {
217 public void checkPermission(Permission perm) {
222 public void checkPermission(Permission perm, Object context) {
227 public void checkExit(int status) {
228 super.checkExit(status);
229 if(session != null) {
231 session.getService(LifecycleSupport.class).close(0, true);
232 } catch (ServiceNotFoundException e) {
233 Logger.defaultLogError(e);
234 } catch (DatabaseException e) {
235 Logger.defaultLogError(e);
238 // if (!Thread.currentThread().equals(thread)) {
239 // ThreadUtil.interruptThreadGroup("Query Thread Group");
240 // ThreadUtil.interruptThreadGroup("Session Thread Group");
241 // ThreadUtil.interruptThreadGroup("Connection Thread Group");
242 // thread.interrupt();
243 // throw new ExitException(status);
248 protected String getName() {
249 return getClass().getSimpleName();
252 protected static void fail() {
253 throw new AssertionError();
256 protected void fail(String cause) {
257 throw new AssertionError(cause);
260 protected void fail(String cause, Object a) {
261 if (a instanceof Throwable) {
262 Throwable t = (Throwable)a;
263 Throwable c = t.getCause();
265 throw new AssertionError(new Error(t.getMessage(), c));
267 throw new AssertionError(cause + " " + a);
270 protected void assertEquals(Object a, Object b) {
272 throw new AssertionError();
275 protected void assertEquals(int a, int b) {
277 throw new AssertionError();
280 protected void assertEquals(double a, double b, double tolerance) {
281 if(Math.abs(a - b) > tolerance)
282 throw new AssertionError();
285 protected void assertLess(double a, double b) {
287 throw new AssertionError(a + " is not less than " + b);
290 protected void assertLess(double a, double b, String info) {
292 System.err.println("assertion info:\n" + info + "\n");
293 throw new AssertionError(a + " is not less than " + b);
297 protected void assertEquals(String a, String b) {
299 throw new AssertionError();
302 protected void assertEquals(String message, int a, int b) {
304 throw new AssertionError(message);
307 protected void assertEquals(String message, boolean a, boolean b) {
309 throw new AssertionError(message);
312 protected void assertNotNull(Object a) {
314 throw new AssertionError();
317 protected void assertNotNull(String message, Object a) {
319 throw new AssertionError(message);
322 protected void assertTrue(String message, boolean a) {
324 throw new AssertionError(message);
327 protected void assertTrue(boolean a) {
329 throw new AssertionError();
332 protected void assertFalse(boolean a) {
334 throw new AssertionError();
337 protected void assertFalse(String message, boolean a) {
339 throw new AssertionError(message);