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.AsyncReadGraph;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.Session;
27 import org.simantics.db.WriteGraph;
28 import org.simantics.db.WriteOnlyGraph;
29 import org.simantics.db.common.request.WriteOnlyRequest;
30 import org.simantics.db.common.utils.Logger;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.exception.ServiceNotFoundException;
33 import org.simantics.db.management.SessionContext;
34 import org.simantics.db.procedure.AsyncProcedure;
35 import org.simantics.db.request.AsyncRead;
36 import org.simantics.db.request.Read;
37 import org.simantics.db.service.LifecycleSupport;
38 import org.simantics.db.testing.impl.Configuration;
39 import org.simantics.layer0.Layer0;
40 import org.simantics.utils.FileUtils;
43 * Base class for Simantics Test Cases. Assumes that ProCore is already running.
45 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
48 abstract public class TestBase /*extends TestCase*/ {
49 public static final boolean DEBUG = Configuration.get().debug;
50 public static final String ROOT_LIBRARY_URI = "http:/";
51 private NoExitSecurityManager noExitSecurityManager;
52 private SecurityManager securityManager;
53 protected DatabaseState state;
54 public Throwable exception2;
56 static boolean printStart = true;
57 public static final ArrayList<String> initialWorkspaceFiles = FileUtils.createFileFilter(Platform.getLocation().toFile(), null);
58 public static void printStart(Object t) {
59 if(printStart) System.out.println("Test is " + t.getClass().getName() /*+ "." + t.getName()*/);
61 protected static void setPrintStart(boolean value) {
65 public Resource getProjectResource() {
66 return SimanticsPlatform.INSTANCE.projectResource;
70 public void setUp() throws Exception {
72 securityManager = System.getSecurityManager();
73 noExitSecurityManager = new NoExitSecurityManager(state.getSession());
74 System.setSecurityManager(noExitSecurityManager);
75 Session session = state.getSession();
76 L0 = Layer0.getInstance(session);
79 public void tearDown() throws Exception {
80 if (noExitSecurityManager != null) {
81 System.setSecurityManager(securityManager);
82 noExitSecurityManager.dispose();
83 noExitSecurityManager = null;
88 securityManager = null;
91 public static void commonTearDown() {
92 Runtime rt = Runtime.getRuntime();
97 System.out.println("Max=" + rt.maxMemory()
98 + " tot=" + rt.totalMemory()
99 + " fre=" + rt.freeMemory());
103 public static String getRandomString() {
104 return UUID.randomUUID().toString();
107 protected Session getSession() throws DatabaseException {
108 return state.getSession();
110 protected SessionContext getSessionContext() {
111 return state.getSessionContext();
114 // protected Resource getRootLibrary() throws DatabaseException {
115 // return getRootLibrary(getSession());
118 // protected Resource getRootLibrary(Session session) throws DatabaseException {
119 // return session.syncRequest(new ReadQuery<Resource>() {
121 // public void run(ReadGraph g) throws DatabaseException {
122 // result = g.getResource(ROOT_LIBRARY_URI);
123 // assertTrue(result.getResourceId() != SessionManagerSource.NullSubjectId);
128 protected abstract class ReadQuery<Result> implements Read<Result> {
129 protected Result result = null;
131 public abstract void run(ReadGraph graph) throws Throwable;
134 public Result perform(ReadGraph graph) {
138 } catch(Throwable t) {
140 new Exception().printStackTrace();
143 if (null == exception2)
151 protected abstract class AsyncReadQuery<Result> implements AsyncRead<Result> {
152 protected Result result = null;
154 public abstract void run(AsyncReadGraph graph) throws Throwable;
157 public void perform(AsyncReadGraph graph, AsyncProcedure<Result> procedure) {
160 } catch(Throwable t) {
162 new Exception().printStackTrace();
165 if (null == exception2)
172 protected abstract class TestReadRequest extends ReadQuery<Object> {
175 protected abstract class TestAsyncReadRequest extends AsyncReadQuery<Object> {
178 public int getFlags() {
179 // TODO Auto-generated method stub
184 public int threadHash() {
185 // TODO Auto-generated method stub
191 protected abstract class WriteOnlyQuery
192 extends WriteOnlyRequest
195 public abstract void run(WriteOnlyGraph g) throws Throwable;
198 * Since some SimpleGraphRequest can only handle Exceptions, we need to wrap other Throwables inside Exceptions
201 public final void perform(WriteOnlyGraph g) {
204 } catch(Throwable t) {
205 new Exception().printStackTrace();
207 if (null == exception2)
209 throw new RuntimeException("Wrapping thrown non exception to exception.", t);
212 public void run(WriteGraph g) throws Throwable {
213 run((WriteOnlyGraph)g);
218 protected void checkException() throws DatabaseException {
219 if (exception2 != null)
220 if (exception2 instanceof DatabaseException)
221 throw (DatabaseException)exception2;
223 throw new DatabaseException(exception2);
226 protected boolean hasException() {
227 return null != exception2;
230 protected Throwable getException() {
234 protected static class ExitException extends SecurityException {
235 private static final long serialVersionUID = -1982617086752946683L;
236 public final int status;
238 public ExitException(int status) {
239 super("There is no escape!");
240 this.status = status;
244 private static class NoExitSecurityManager extends SecurityManager {
247 NoExitSecurityManager(Session session) {
248 this.session = session;
249 // this.thread = Thread.currentThread();
252 public void dispose() {
257 public void checkPermission(Permission perm) {
262 public void checkPermission(Permission perm, Object context) {
267 public void checkExit(int status) {
268 super.checkExit(status);
269 if(session != null) {
271 session.getService(LifecycleSupport.class).close(0, true);
272 } catch (ServiceNotFoundException e) {
273 Logger.defaultLogError(e);
274 } catch (DatabaseException e) {
275 Logger.defaultLogError(e);
278 // if (!Thread.currentThread().equals(thread)) {
279 // ThreadUtil.interruptThreadGroup("Query Thread Group");
280 // ThreadUtil.interruptThreadGroup("Session Thread Group");
281 // ThreadUtil.interruptThreadGroup("Connection Thread Group");
282 // thread.interrupt();
283 // throw new ExitException(status);
288 protected String getName() {
289 return getClass().getSimpleName();
292 protected static void fail() {
293 throw new AssertionError();
296 protected void fail(String cause) {
297 throw new AssertionError(cause);
300 protected void fail(String cause, Object a) {
301 if (a instanceof Throwable) {
302 Throwable t = (Throwable)a;
303 Throwable c = t.getCause();
305 throw new AssertionError(new Error(t.getMessage(), c));
307 throw new AssertionError(cause + " " + a);
310 protected void assertEquals(Object a, Object b) {
312 throw new AssertionError();
315 protected void assertEquals(int a, int b) {
317 throw new AssertionError();
320 protected void assertEquals(double a, double b, double tolerance) {
321 if(Math.abs(a - b) > tolerance)
322 throw new AssertionError();
325 protected void assertLess(double a, double b) {
327 throw new AssertionError(a + " is not less than " + b);
330 protected void assertLess(double a, double b, String info) {
332 System.err.println("assertion info:\n" + info + "\n");
333 throw new AssertionError(a + " is not less than " + b);
337 protected void assertEquals(String a, String b) {
339 throw new AssertionError();
342 protected void assertEquals(String message, int a, int b) {
344 throw new AssertionError(message);
347 protected void assertEquals(String message, boolean a, boolean b) {
349 throw new AssertionError(message);
352 protected void assertNotNull(Object a) {
354 throw new AssertionError();
357 protected void assertNotNull(String message, Object a) {
359 throw new AssertionError(message);
362 protected void assertTrue(String message, boolean a) {
364 throw new AssertionError(message);
367 protected void assertTrue(boolean a) {
369 throw new AssertionError();
372 protected void assertFalse(boolean a) {
374 throw new AssertionError();
377 protected void assertFalse(String message, boolean a) {
379 throw new AssertionError(message);