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.ui.tester;
14 import java.util.Arrays;
16 import org.eclipse.core.expressions.PropertyTester;
17 import org.simantics.DatabaseJob;
18 import org.simantics.Simantics;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.request.UniqueRead;
22 import org.simantics.db.common.utils.NameUtils;
23 import org.simantics.db.common.utils.RequestUtil;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.exception.ResourceNotFoundException;
26 import org.simantics.db.management.ISessionContext;
27 import org.simantics.scl.reflection.OntologyVersions;
28 import org.simantics.ui.SimanticsUI;
29 import org.simantics.ui.utils.ResourceAdaptionUtils;
32 * @author Tuukka Lehtonen
34 public class CollectionResourcePropertyTester extends PropertyTester {
36 private static final boolean DEBUG = false;
39 * Tests if the received resource is an instance of any of the URIs
40 * listed in the arguments.
42 protected static final String RESOURCE_TYPES = "resourceTypes";
44 private static final OntologyVersions VERSIONS = OntologyVersions.getInstance();
47 public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
49 System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
51 final Resource[] resources = ResourceAdaptionUtils.toResources(receiver);
52 if (resources == null)
55 ISessionContext ctx = Simantics.getSessionContext();
59 if (DatabaseJob.inProgress())
63 return RequestUtil.trySyncRequest(
65 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
66 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
68 new UniqueRead<Boolean>() {
70 public Boolean perform(ReadGraph g) throws DatabaseException {
71 return Boolean.valueOf(doTest(g, resources, property, args, expectedValue));
74 } catch (DatabaseException | InterruptedException e) {
75 // Purposefully not logging these exceptions, there might be way too
76 // many even under normal circumstances.
77 // TODO: add debug tracing options controlling the printing of these exceptions
82 protected boolean doTest(ReadGraph g, Resource[] resources, String property, Object[] args, Object expectedValue) throws DatabaseException {
83 if (RESOURCE_TYPES.equals(property)) {
84 loop: for(Resource r : resources) {
86 System.out.println("** " + NameUtils.getSafeName(g, r));
88 for (int i = 0; i < args.length; i++) {
89 if (g.isInstanceOf(r, g.getResource(VERSIONS.currentVersion((String) args[i])))) {
91 System.out.println("IS INSTANCE OF " + args[i]);
95 } catch (ResourceNotFoundException e) {
96 /* This is a natural situation (database does not contain all resources
97 referred in plugins). No error reporting.
102 System.out.println("NOT AN INSTANCE OF ANY OF: " + Arrays.toString(args));