Skip to content
Snippets Groups Projects
Commit 8b611a4e authored by Alberto Debiasi's avatar Alberto Debiasi
Browse files

Merge remote-tracking branch...

Merge remote-tracking branch 'Polarsys/562302-Implement-unit-tests-for-existing-features' into neon-orig
parents b021cd64 c76d2572
No related branches found
No related tags found
No related merge requests found
Showing
with 1629 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
/bin/
/target/
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.papyrus.junit.framework</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.api.tools.apiAnalysisBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.oomph.version.VersionNature</nature>
<nature>org.eclipse.pde.api.tools.apiAnalysisNature</nature>
</natures>
</projectDescription>
Manifest-Version: 1.0
Export-Package: org.eclipse.papyrus.junit.framework.classification,
org.eclipse.papyrus.junit.framework.classification.rules,
org.eclipse.papyrus.junit.framework.classification.tests,
org.eclipse.papyrus.junit.framework.runner
Require-Bundle: org.junit;bundle-version="[4.12.0,5.0.0)";visibility:=reexport,
org.eclipse.papyrus.infra.tools;bundle-version="[2.0.0,3.0.0)",
org.eclipse.emf.edit;bundle-version="[2.12.0,3.0.0)",
org.eclipse.ui;bundle-version="[3.107.0,4.0.0)"
Bundle-Vendor: Eclipse Modeling Project
Bundle-Version: 1.2.0.qualifier
Bundle-Name: Conditional Junit Annotations
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.papyrus.junit.framework
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>About</title>
</head>
<body lang="EN-US">
<h2>About This Content</h2>
<p>November 14, 2008</p>
<h3>License</h3>
<p>The Eclipse Foundation makes available all content in this plug-in (&quot;Content&quot;). Unless otherwise
indicated below, the Content is provided to you under the terms and conditions of the
Eclipse Public License Version 1.0 (&quot;EPL&quot;). A copy of the EPL is available
at <a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>.
For purposes of the EPL, &quot;Program&quot; will mean the Content.</p>
<p>If you did not receive this Content directly from the Eclipse Foundation, the Content is
being redistributed by another party (&quot;Redistributor&quot;) and different terms and conditions may
apply to your use of any object code in the Content. Check the Redistributor's license that was
provided with the Content. If no such license exists, contact the Redistributor. Unless otherwise
indicated below, the terms and conditions of the EPL still apply to any source code in the Content
and such source code may be obtained at <a href="http://www.eclipse.org">http://www.eclipse.org</a>.</p>
</body>
</html>
\ No newline at end of file
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
about.html
src.includes = about.html
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.polarsys.chess</groupId>
<artifactId>org.polarsys.chess.parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../org.polarsys.chess.parent/pom.xml</relativePath>
</parent>
<artifactId>org.eclipse.papyrus.junit.framework</artifactId>
<version>1.2.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
</project>
/*****************************************************************************
* Copyright (c) 2014, 2016 CEA LIST, Christian W. Damus, and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
* Christian W. Damus - bug 451230
* Christian W. Damus - bug 464647
* Christian W. Damus - bug 480812
* Christian W. Damus - bug 485156
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import static org.eclipse.papyrus.junit.framework.classification.TestCategory.ExpensiveTest;
import static org.eclipse.papyrus.junit.framework.classification.TestCategory.FailingTest;
import static org.eclipse.papyrus.junit.framework.classification.TestCategory.GeneratedTest;
import static org.eclipse.papyrus.junit.framework.classification.TestCategory.InteractiveTest;
import static org.eclipse.papyrus.junit.framework.classification.TestCategory.InvalidTest;
import static org.eclipse.papyrus.junit.framework.classification.TestCategory.NotImplemented;
import static org.eclipse.papyrus.junit.framework.classification.TestCategory.Standard;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.eclipse.core.runtime.Platform;
import org.eclipse.papyrus.infra.tools.util.ListHelper;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Suite;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
/**
* <p>
* This class is used to configure the Classification Annotations which should be excluded from a test job. A classification configuration is an immutable set of {@linkplain TestCategory test categories} that are included in the test configuration.
* </p>
* <p>
* Usage:
* </p>
*
* <pre>
* ClassificationConfig.setExcludedTestCategories(TestCategory.InvalidTest, TestCategory.NotImplemented);
*
* // or
*
* ClassificationConfig.setIncludedTestCategories(TestCategory.Standard, TestCategory.NotImplemented);
*
* // or
*
* ClassificationConfig.setExcludedTestCategories(ClassificationConfig.FAILING_TESTS_CONFIG);
*
* // or
*
* ClassificationConfig.setIncludedTestCategories(ClassificationConfig.FULL_CI_TESTS_CONFIG);
*
* // or
*
* ClassificationConfig.setTestsConfiguration(ClassificationConfig.CI_TESTS_CONFIG);
* </pre>
*
* @author Camille Letavernier
*
* @see {@link TestCategory}
* @see {@link ClassificationRunner}
*
*/
public enum ClassificationConfig implements Set<TestCategory> {
/**
* Default tests configuration for Continuous Integration (Hudson): Excludes the tests which are
* identified as failing, as well as the interactive tests
*
* This configuration may require a couple of hours to run
*/
CI_TESTS_CONFIG(NotImplemented, InvalidTest, FailingTest, InteractiveTest, GeneratedTest),
/**
* This tests configuration runs all tests which are already identified as failing.
* If one test from this configuration passes, this might mean that the annotation should be removed
*
* InteractiveTests are excluded as well, as this configuration is supposed to be executed on Hudson
*/
FAILING_TESTS_CONFIG(Standard, InteractiveTest, GeneratedTest, ExpensiveTest),
/**
* This tests configuration is expected to run in ~15 minutes. This is useful for quick testing,
* and validation through Gerrit
*/
LIGTHWEIGHT_TESTS_CONFIG(InteractiveTest, NotImplemented, FailingTest, InvalidTest, ExpensiveTest, GeneratedTest),
/**
* This test configuration runs all generated diagram tests only.
*
* @since 1.2
*/
GENERATED_TESTS_CONFIG(InteractiveTest, NotImplemented, FailingTest, InvalidTest, ExpensiveTest, Standard),
/**
* This tests configuration is meant to execute all tests in an automated environment
* It includes all tests which are known to be failing, but excludes tests which
* require a user interaction
*/
FULL_CI_TESTS_CONFIG(InteractiveTest),
/**
* Executes all tests
*/
FULL_TESTS_CONFIG();
public static final Set<TestCategory> excludedTestCategories = new HashSet<TestCategory>();
private final Set<TestCategory> categories;
static {
// Default on Hudson: exclude everything which is already identified as an issue (i.e. is not a (new) regression)
setTestsConfiguration(CI_TESTS_CONFIG);
// Check whether identified regressions are still failing
// setIncludedTestCategories(FailingTest);
for (String arg : Platform.getApplicationArgs()) {
if (arg.contains("-testConfig=")) {
String configName = arg.substring("-testConfig=".length());
Set<TestCategory> testsConfig = ClassificationConfig.valueOf(configName);
if (testsConfig != null) {
setTestsConfiguration(testsConfig);
}
break;
}
}
}
private ClassificationConfig(TestCategory... exclusions) {
Set<TestCategory> categories = Sets.newHashSet(TestCategory.values());
categories.removeAll(Arrays.asList(exclusions));
this.categories = ImmutableSet.copyOf(categories);
}
// Same as setExcludedTestsCategories, but renamed for clarity (To be used with predefined configurations)
public static void setTestsConfiguration(Set<TestCategory> predefinedConfiguration) {
setIncludedTestCategories(predefinedConfiguration);
}
/**
* Define the test categories which should be excluded from the TestSuite
*
* @param categoriesToInclude
*/
public static void setExcludedTestCategories(TestCategory... categories) {
setExcludedTestCategories(Arrays.asList(categories));
}
/**
* Define the test categories which should be included in the TestSuite
*
* @param categoriesToInclude
*/
public static void setIncludedTestCategories(TestCategory... categories) {
setIncludedTestCategories(Arrays.asList(categories));
}
/**
* Define the test categories which should be excluded from the TestSuite
*
* @param categoriesToInclude
*/
public static void setExcludedTestCategories(Collection<TestCategory> categoriesToExclude) {
excludedTestCategories.clear();
excludedTestCategories.addAll(categoriesToExclude);
}
/**
* Define the test categories which should be included in the TestSuite
*
* @param categoriesToInclude
*/
public static void setIncludedTestCategories(Collection<TestCategory> categoriesToInclude) {
excludedTestCategories.clear();
List<TestCategory> categoriesToExclude = ListHelper.asList(TestCategory.values());
categoriesToExclude.removeAll(categoriesToInclude);
excludedTestCategories.addAll(categoriesToExclude);
}
/**
* Tests whether a method containing the given set of Annotations should be executed
*
* @param annotations
* The annotations applied to the Method
* @return
* True if the test method should be executed, false if it should be ignored
*/
public static boolean shouldRun(Annotation[] annotations) {
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationClass = annotation.annotationType();
// Support the usual @Ignore annotation, too, of course
if ((annotationClass == Ignore.class) || isExcluded(annotationClass)) {
return false;
}
}
if (excludedTestCategories.contains(TestCategory.Standard)) {
// If it's just a test suite, then let the execution of its children be filtered
for (Annotation annotation : annotations) {
if (RunWith.class.isInstance(annotation)) {
RunWith runWith = (RunWith) annotation;
Class<?> runner = runWith.value();
if (Suite.class.isAssignableFrom(runner) && !Parameterized.class.isAssignableFrom(runner)) {
return true;
}
}
}
for (TestCategory testCategory : TestCategory.values()) {
if (testCategory == TestCategory.Standard) {
continue;
}
for (Annotation annotation : annotations) {
if (testCategory.match(annotation.annotationType())) {
return true;
}
}
}
return false;
}
return true;
}
/**
* Tests whether the given Annotation should be excluded
*
* @param annotationClass
* @return
*/
public static boolean isExcluded(Class<? extends Annotation> annotationClass) {
for (TestCategory testCategory : excludedTestCategories) {
if (testCategory.match(annotationClass)) {
return true;
}
}
return false;
}
@Override
public int size() {
return categories.size();
}
@Override
public boolean isEmpty() {
return categories.isEmpty();
}
@Override
public boolean contains(Object o) {
return categories.contains(o);
}
@Override
public boolean containsAll(Collection<?> c) {
return categories.containsAll(c);
}
@Override
public Iterator<TestCategory> iterator() {
return categories.iterator();
}
@Override
public Object[] toArray() {
return categories.toArray();
}
@Override
public <T> T[] toArray(T[] a) {
return categories.toArray(a);
}
@Override
public boolean add(TestCategory e) {
throw new UnsupportedOperationException("add"); //$NON-NLS-1$
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException("remove"); //$NON-NLS-1$
}
@Override
public boolean addAll(Collection<? extends TestCategory> c) {
throw new UnsupportedOperationException("addAll"); //$NON-NLS-1$
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException("retainAll"); //$NON-NLS-1$
}
@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException("removeAll"); //$NON-NLS-1$
}
@Override
public void clear() {
throw new UnsupportedOperationException("clear"); //$NON-NLS-1$
}
}
/*****************************************************************************
* Copyright (c) 2014, 2016 CEA LIST, Christian W. Damus, and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
* Christian W. Damus (CEA) - add support for conditional tests
* Christian W. Damus (CEA) - bug 432813
* Christian W. Damus (CEA) - bug 434993
* Christian W. Damus (CEA) - bug 436047
* Christian W. Damus - bug 485156
*
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import java.util.List;
import org.eclipse.papyrus.junit.framework.classification.rules.Conditional;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
/**
* A Test Runner which is aware of Classification-related annotations and {@link Conditional @Conditional} tests.
*
* It ignores the test methods according to their annotations, and the policy defined
* in {@link ClassificationConfig}
*
*
* @see {@link ClassificationConfig}
* @see {@link TestCategory}
* @see Conditional
*
* @author Camille Letavernier
*
*/
public class ClassificationRunner extends BlockJUnit4ClassRunner {
private final ClassificationRunnerImpl impl;
public ClassificationRunner(Class<?> klass) throws InitializationError {
super(klass);
this.impl = new ClassificationRunnerImpl(new ClassificationRunnerImpl.Delegate() {
@Override
public void runChild(FrameworkMethod method, RunNotifier notifier) {
ClassificationRunner.super.runChild(method, notifier);
}
@Override
public Description describeChild(FrameworkMethod method) {
return ClassificationRunner.super.describeChild(method);
}
@Override
public Object createTest() throws Exception {
return ClassificationRunner.super.createTest();
}
@Override
public List<TestRule> getTestRules(Object target) {
return ClassificationRunner.super.getTestRules(target);
}
@Override
public Statement classBlock(RunNotifier notifier) {
return ClassificationRunner.super.classBlock(notifier);
}
});
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
impl.runChild(method, notifier);
}
@Override
protected Object createTest() throws Exception {
return impl.createTest();
}
@Override
protected List<TestRule> getTestRules(Object target) {
return impl.getTestRules(target);
}
@Override
protected Statement classBlock(RunNotifier notifier) {
return impl.classBlock(notifier);
}
}
/*****************************************************************************
* Copyright (c) 2014, 2016 CEA LIST, Christian W. Damus, and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
* Christian W. Damus (CEA) - add support for conditional tests
* Christian W. Damus (CEA) - bug 432813
* Christian W. Damus (CEA) - bug 434993
* Christian W. Damus (CEA) - bug 436047
* Christian W. Damus - bug 485156
*
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.eclipse.core.commands.operations.DefaultOperationHistory;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.BaseLabelProvider;
import org.eclipse.jface.viewers.IBaseLabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.LabelProviderChangedEvent;
import org.eclipse.papyrus.infra.tools.util.ListHelper;
import org.eclipse.papyrus.junit.framework.classification.rules.ConditionRule;
import org.eclipse.papyrus.junit.framework.classification.rules.Conditional;
import org.eclipse.papyrus.junit.framework.classification.rules.MemoryLeakRule;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
/**
* Internal implementation of the common classification-sensitive behaviour
* of the {@link ClassificationRunner} and {@link ClassificationRunnerWithParameters}
* test runners.
*
* @author Camille Letavernier
*/
class ClassificationRunnerImpl {
private final static long EVENT_LOOP_TIMEOUT = 2L * 60L * 1000L; // 2 minutes in millis
private final static long ONE_MB = 1024L * 1024L; // a megabyte, in bytes
private static final Supplier<TestRule> uiFlusherRuleSupplier = createUIFlusherRuleSupplier();
private final ThreadLocal<Object> preparedTest = new ThreadLocal<Object>();
private final Delegate delegate;
ClassificationRunnerImpl(Delegate delegate) throws InitializationError {
super();
this.delegate = delegate;
}
final void runChild(FrameworkMethod method, RunNotifier notifier) {
List<Annotation> allAnnotations = ListHelper.asList(method.getAnnotations());
allAnnotations.addAll(Arrays.asList(method.getMethod().getDeclaringClass().getAnnotations()));
if (ClassificationConfig.shouldRun(allAnnotations.toArray(new Annotation[allAnnotations.size()])) && conditionSatisfied(method)) {
delegate.runChild(method, notifier);
} else {
Description description = delegate.describeChild(method);
notifier.fireTestIgnored(description);
}
}
final Object createTest() throws Exception {
// Look for a prepared test instance
Object result = preparedTest.get();
if (result != null) {
// We won't need this test instance again
clearPreparedTest();
} else {
result = delegate.createTest();
}
return result;
}
final Object prepareTest() throws Exception {
// Prepare the test instance and stash it to return on the next invocation
Object result = delegate.createTest();
preparedTest.set(result);
return result;
}
final void clearPreparedTest() {
preparedTest.remove();
}
private boolean conditionSatisfied(FrameworkMethod method) {
boolean result = true;
// Does this test declare some precondition?
Conditional conditional = method.getAnnotation(Conditional.class);
if (conditional != null) {
try {
// We need the test instance to invoke the condition on it, so prepare it now
Object test = prepareTest();
result = ConditionRule.testCondition(method.getMethod().getDeclaringClass(), conditional, test);
} catch (Throwable t) {
// If we couldn't create the test, then we should just ignore it
result = false;
} finally {
if (!result) {
// We won't be running the test, so forget the prepared instance (if any)
clearPreparedTest();
}
}
}
return result;
}
List<TestRule> getTestRules(Object target) {
// MemoryLeakRules must be the outer-most rules, because leak assertions must only happen after all possible tear-down actions have run
return reorderForMemoryLeakRules(delegate.getTestRules(target));
}
private List<TestRule> reorderForMemoryLeakRules(List<TestRule> rules) {
// Quick scan for memory rules
if (!rules.isEmpty()) {
int memoryRuleCount = Iterables.size(Iterables.filter(rules, Predicates.instanceOf(MemoryLeakRule.class)));
if (memoryRuleCount > 0) {
// Bubble the memory rules to the end
int limit = rules.size() - memoryRuleCount;
for (int i = 0; i < limit; i++) {
if (rules.get(i) instanceof MemoryLeakRule) {
// Move the rule to the end and take a step back to get the next element
rules.add(rules.remove(i--));
}
}
}
}
return rules;
}
Statement classBlock(RunNotifier notifier) {
Statement result = delegate.classBlock(notifier);
// Wrap the class suite in a rule that flushes the UI thread to release memory referenced by UI runnables
TestRule uiFlusher = uiFlusherRuleSupplier.get();
if (uiFlusher != null) {
// This rule doesn't need any actual test description
result = uiFlusher.apply(result, Description.EMPTY);
}
return result;
}
private static Supplier<TestRule> createUIFlusherRuleSupplier() {
Supplier<TestRule> result = Suppliers.ofInstance(null);
try {
if (PlatformUI.isWorkbenchRunning()) {
result = Suppliers.memoize(new Supplier<TestRule>() {
@Override
public TestRule get() {
if (Display.getCurrent() != null) {
return new TestWatcher() {
@Override
protected void finished(Description description) {
final Display display = Display.getCurrent();
if (display == null) {
// Can't do UI manipulations and history listener hacking except on the UI thread
return;
}
flushUIEventQueue(display);
purgeZombieHistoryListeners();
clearDecorationScheduler();
}
};
}
return null;
}
});
}
} catch (LinkageError e) {
// Not running in Eclipse UI context. Fine
}
return result;
}
private static void flushUIEventQueue(Display display) {
long base = System.currentTimeMillis();
long timeout = EVENT_LOOP_TIMEOUT;
// Flush the UI thread's pending events
while (!display.isDisposed()) {
try {
if (!display.readAndDispatch()) {
break;
}
} catch (Exception e) {
// Ignore it
}
long now = System.currentTimeMillis();
if ((now - base) > timeout) {
// This seems to be taking a really long time. What's up?
base = now;
timeout = timeout * 3L / 2L; // Exponential back-off to avoid over-reporting
int freeMB = (int) (Runtime.getRuntime().freeMemory() / ONE_MB);
System.err.printf("========%nUI event queue clean-up seems to be running long.%nCurrent free memory: %d MB%n========%n%n", freeMB);
}
}
}
private static void purgeZombieHistoryListeners() {
// If there are no editors open any longer, then all of the action handlers currently
// listening to the operation history are leaked, so remove them. This ensures that we
// do not end up wasting time in notifying thousands of dead/broken/useless listeners
// every time a test case executes an operation on the history (which happens *a lot*)
IWorkbench bench = PlatformUI.getWorkbench();
IWorkbenchWindow window = (bench == null) ? null : bench.getActiveWorkbenchWindow();
if ((window == null) && (bench != null) && (bench.getWorkbenchWindowCount() > 0)) {
window = bench.getWorkbenchWindows()[0];
}
if (window != null && window.getActivePage().getEditorReferences().length == 0) {
final ListenerList historyListeners = OperationHistoryHelper.getOperationHistoryListeners();
final Object[] listeners = historyListeners.getListeners();
for (int i = 0; i < listeners.length; i++) {
if (OperationHistoryHelper.shouldRemoveHistoryListener(listeners[i])) {
historyListeners.remove(listeners[i]);
}
}
}
}
private static void clearDecorationScheduler() {
IWorkbench bench = PlatformUI.getWorkbench();
if (bench != null) {
IBaseLabelProvider bogusProvider = new BaseLabelProvider();
try {
// The DecoratorManager is a label-provider listener and
// it clears the scheduler on label-provider change events
((ILabelProviderListener) bench.getDecoratorManager()).labelProviderChanged(new LabelProviderChangedEvent(bogusProvider));
} finally {
bogusProvider.dispose();
}
}
}
//
// Nested types
//
static class OperationHistoryHelper {
static final Field listenersField;
static final Set<Class<?>> historyListenerClasses;
static {
try {
listenersField = DefaultOperationHistory.class.getDeclaredField("listeners");
listenersField.setAccessible(true);
historyListenerClasses = Sets.<Class<?>> newHashSet( //
Platform.getBundle("org.eclipse.gmf.runtime.diagram.ui.actions").loadClass("org.eclipse.gmf.runtime.diagram.ui.actions.internal.PropertyChangeContributionItem"), //
Platform.getBundle("org.eclipse.ui.workbench").loadClass("org.eclipse.ui.operations.OperationHistoryActionHandler$HistoryListener"));
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
static ListenerList getOperationHistoryListeners() {
try {
return (ListenerList) listenersField.get(PlatformUI.getWorkbench().getOperationSupport().getOperationHistory());
} catch (Exception e) {
org.junit.Assert.fail(e.getLocalizedMessage());
return null; // Unreachable
}
}
static boolean shouldRemoveHistoryListener(Object listener) {
boolean result = historyListenerClasses.contains(listener.getClass().getName());
if (!result) {
// Maybe it's a subclass
for (Class<?> next : historyListenerClasses) {
if (next.isInstance(listener)) {
// Remember this
historyListenerClasses.add(listener.getClass());
result = true;
break;
}
}
}
return result;
}
}
/**
* Protocol for a delegate that provides the default test framework behaviour
* for the classification runner. These methods are as specified by the
* corresponding APIs of the {@link BlockJUnit4ClassRunner} class.
*/
interface Delegate {
void runChild(FrameworkMethod method, RunNotifier notifier);
Description describeChild(FrameworkMethod method);
Object createTest() throws Exception;
List<TestRule> getTestRules(Object target);
Statement classBlock(RunNotifier notifier);
}
}
/*****************************************************************************
* Copyright (c) 2016 Christian W. Damus and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christian W. Damus - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import java.util.List;
import org.eclipse.papyrus.junit.framework.classification.rules.Conditional;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.UseParametersRunnerFactory;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters;
import org.junit.runners.parameterized.TestWithParameters;
/**
* A Test Runner which is aware of Classification-related annotations and {@link Conditional @Conditional} tests,
* for use with test {@link Parameters}.
*
* It ignores the test methods according to their annotations, and the policy defined
* in {@link ClassificationConfig}.
*
* @see Parameterized
* @see UseParametersRunnerFactory
* @see ClassificationRunnerWithParametersFactory
* @see ClassificationConfig
* @see TestCategory
* @see Conditional
*
*/
public class ClassificationRunnerWithParameters extends BlockJUnit4ClassRunnerWithParameters {
private final ClassificationRunnerImpl impl;
public ClassificationRunnerWithParameters(TestWithParameters test) throws InitializationError {
super(test);
this.impl = new ClassificationRunnerImpl(new ClassificationRunnerImpl.Delegate() {
@Override
public void runChild(FrameworkMethod method, RunNotifier notifier) {
ClassificationRunnerWithParameters.super.runChild(method, notifier);
}
@Override
public Description describeChild(FrameworkMethod method) {
return ClassificationRunnerWithParameters.super.describeChild(method);
}
@Override
public Object createTest() throws Exception {
return ClassificationRunnerWithParameters.super.createTest();
}
@Override
public List<TestRule> getTestRules(Object target) {
return ClassificationRunnerWithParameters.super.getTestRules(target);
}
@Override
public Statement classBlock(RunNotifier notifier) {
return ClassificationRunnerWithParameters.super.classBlock(notifier);
}
});
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
impl.runChild(method, notifier);
}
@Override
public Object createTest() throws Exception {
return impl.createTest();
}
@Override
protected List<TestRule> getTestRules(Object target) {
return impl.getTestRules(target);
}
@Override
protected Statement classBlock(RunNotifier notifier) {
return impl.classBlock(notifier);
}
}
/*****************************************************************************
* Copyright (c) 2016 Christian W. Damus and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christian W. Damus - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.UseParametersRunnerFactory;
import org.junit.runners.model.InitializationError;
import org.junit.runners.parameterized.ParametersRunnerFactory;
import org.junit.runners.parameterized.TestWithParameters;
/**
* Factory for classification-sensitive parameterized test suites.
* Specify this factory in the {@literal @}{@link UseParametersRunnerFactory}
* annotation on your <tt>{@literal @}{@link RunWith}({@link Parameterized}.class)</tt>
* test class to support the classfication and condition annotations of the Papyrus
* test framework.
*
* @see Parameterized
* @see UseParametersRunnerFactory
* @since 1.2
*/
public class ClassificationRunnerWithParametersFactory implements ParametersRunnerFactory {
public ClassificationRunnerWithParametersFactory() {
super();
}
@Override
public Runner createRunnerForTestWithParameters(TestWithParameters test) throws InitializationError {
return new ClassificationRunnerWithParameters(test);
}
}
/*****************************************************************************
* Copyright (c) 2015 Christian W. Damus and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christian W. Damus - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import org.junit.runners.model.Statement;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.ObjectArrays;
/**
* A specialized Test {@linkplain Suite Suite} runner that recognizes the Papyrus-specific
* {@link ClassificationConfig classification} annotations on the suite as a whole. It also
* supports a dynamic specification of test suites to include via the {@literal @}{@link DynamicClasses}
* annotation, especially useful for generated tests that may or may not have been generated at
* the time of test execution (or at least of compilation of the test suite class).
*/
public class ClassificationSuite extends Suite {
/**
* Mapping of whether any tests at all in a suite tree are statically enabled, according to
* their annotations.
*/
private static final LoadingCache<Description, Boolean> enabledSuites = CacheBuilder.newBuilder().build(
CacheLoader.from(ClassificationSuite::isEnabled));
private Description description;
public ClassificationSuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {
this(builder, klass, getAnnotatedClasses(klass));
}
public ClassificationSuite(Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
super(klass, withDynamicSuites(klass, suiteClasses));
}
public ClassificationSuite(RunnerBuilder builder, Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
super(builder, klass, withDynamicSuites(klass, suiteClasses));
}
private static Class<?>[] getAnnotatedClasses(Class<?> class_) throws InitializationError {
SuiteClasses annotation = class_.getAnnotation(SuiteClasses.class);
if (annotation == null) {
throw new InitializationError(String.format("class '%s' must have a SuiteClasses annotation", class_.getName()));
}
return annotation.value();
}
private static Class<?>[] withDynamicSuites(Class<?> suiteClass, Class<?>[] staticSuites) {
Class<?>[] result = staticSuites;
Class<?>[] dynamicSuites = getDynamicSuites(suiteClass);
if (dynamicSuites.length > 0) {
result = ObjectArrays.concat(staticSuites, dynamicSuites, Class.class);
}
return result;
}
private static Class<?>[] getDynamicSuites(Class<?> suiteClass) {
List<Class<?>> result;
DynamicClasses dynclasses = suiteClass.getAnnotation(DynamicClasses.class);
if (dynclasses == null) {
result = Collections.emptyList();
} else {
result = Lists.newArrayListWithCapacity(dynclasses.value().length);
for (String classname : dynclasses.value()) {
try {
result.add(suiteClass.getClassLoader().loadClass(classname));
} catch (Exception e) {
// OK. It's not there, so we just ignore it
}
}
}
return Iterables.toArray(result, Class.class);
}
@Override
protected List<Runner> getChildren() {
// If I don't match the current configuration, none of my tests will run, so don't even provide them
// because they just inflate the size of the progress meter
if (ClassificationConfig.shouldRun(getRunnerAnnotations())) {
return super.getChildren();
} else {
return Collections.emptyList();
}
}
@Override
public void run(RunNotifier notifier) {
// If I don't match the current configuration, none of my tests should be run
if (ClassificationConfig.shouldRun(getRunnerAnnotations())) {
super.run(notifier);
} else {
Description description = getDescription();
notifier.fireTestIgnored(description);
}
}
@Override
protected Statement classBlock(final RunNotifier notifier) {
// We never throw in these lazy calculations
return enabledSuites.getUnchecked(getDescription())
// Include the @BeforeClass, @AfterClass, and @ClassRule steps
? super.classBlock(notifier)
// Just the bare bones to record everything that is skipped
: skipAll(notifier);
}
@Override
public Description getDescription() {
if (description == null) {
description = super.getDescription();
}
return description;
}
/**
* Creates a statement that simply skips all of the tests in my suite, recursively.
*
* @param notifier
* tracks the execution (or, rather, skipping) of the tests
*
* @return the all-skipping statement
*/
protected Statement skipAll(RunNotifier notifier) {
return new Statement() {
@Override
public void evaluate() {
skipAll(getDescription(), notifier);
}
};
}
/**
* Skips all of the tests in the given {@code suite}, recursively.
*
* @param suite
* a test suite to skip
* @param notifier
* tracks the execution (or, rather, skipping) of the tests
*/
protected void skipAll(Description suite, RunNotifier notifier) {
for (Description next : suite.getChildren()) {
if (next.isSuite()) {
skipAll(next, notifier);
} else {
notifier.fireTestIgnored(next);
}
}
notifier.fireTestIgnored(suite);
}
/**
* Queries whether a given {@code suite} will run any tests at all.
*
* @param suite
* a test suite
* @return whether it has any leaf tests that will not be ignored for some reason
*/
static boolean isEnabled(Description suite) {
boolean result = false;
for (Description leaf : getLeafTests(suite)) {
Iterable<Annotation> annotations = Iterables.concat(
leaf.getAnnotations(),
Arrays.asList(leaf.getTestClass().getAnnotations()));
if (ClassificationConfig.shouldRun(Iterables.toArray(annotations, Annotation.class))) {
result = true;
break;
}
}
return result;
}
/**
* Obtains an iteration of all of the leaf-level tests in a {@code suite}, in depth-first order.
*
* @param suite
* a test suite to iterate
*
* @return all of its leaf-level tests
*/
static Iterable<Description> getLeafTests(Description suite) {
return new Iterable<Description>() {
@Override
public Iterator<Description> iterator() {
return new AbstractIterator<Description>() {
Deque<Iterator<Description>> stack = new ArrayDeque<>();
Iterator<Description> current = suite.getChildren().iterator();
// Advance the tractor
Iterator<Description> feed() {
while (!current.hasNext()) {
current = stack.pollLast();
if (current == null) {
current = Collections.emptyIterator();
break;
}
}
return current;
}
void push(Description suite) {
stack.addLast(current);
current = suite.getChildren().iterator();
}
@Override
protected Description computeNext() {
Description result = null;
while ((result == null) && feed().hasNext()) {
Description next = feed().next();
if (next.isTest() && (next.getTestClass() != null)) {
result = next;
} else if (next.isSuite()) {
// Push this suite onto the stack
push(next);
} else {
// Otherwise it's a weird test without a class?!?
System.err.println("Leaf test without a class: " + next);
}
}
if (result == null) {
result = endOfData();
}
return result;
}
};
}
};
}
//
// Nested types
//
/**
* An annotation like the {@literal @}{@link SuiteClasses} that specifies, by name, test suite classes
* to find and include dynamically. Any that are not available are simply ignored.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface DynamicClasses {
/**
* Names of test suite classes to optionally include in the test suite.
*/
String[] value();
}
}
/*****************************************************************************
* Copyright (c) 2014 CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that a test is expensive, and should be excluded from a Lightweight test job (e.g. Gerrit)
*
*
* This annotation must be used with the {@link ClassificationRule}
*
* @author Camille Letavernier
*
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface ExpensiveTest {
/**
* A description
*
* @return
*/
String value() default "";
}
/*****************************************************************************
* Copyright (c) 2014 CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that a test currently fails. It can be used to sort new (unidentified) regressions
* from known (reported) issues.
*
* This annotation must be used with the {@link ClassificationRule}
*
* @author Camille Letavernier
*
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface FailingTest {
/**
* A specific message explaining why this test is failing
*
* @return
*/
String value() default "";
}
/*****************************************************************************
* Copyright (c) 2014 CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that this test has been generated with the Papyrus Test Generation Framework
* (see https://wiki.eclipse.org/Papyrus_Developer_Guide/Automatic_Test_Generation_for_Diagram_Editors)
*
*
* This annotation must be used with the {@link ClassificationRule}
*
* @author Camille Letavernier
* @since 1.2
*
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface GeneratedTest {
/**
* A description
*
* @return
*/
String value() default "";
}
/*****************************************************************************
* Copyright (c) 2014 CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that a test is interactive, e.g. it opens a user dialog. Such tests cannot be run
* automatically and should be disabled on Hudson
*
* This annotation must be used with the {@link ClassificationRule}
*
* @author Camille Letavernier
*
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface InteractiveTest {
/**
* A specific message explaining why this test is failing
*
* @return
*/
String value() default "";
}
/*****************************************************************************
* Copyright (c) 2014 CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that a test may fail because it is not properly implemented
* (e.g. not compatible with Hudson, because it opens some blocking dialogs)
*
* This annotation must be used with the {@link ClassificationRule}
*
* @author Camille Letavernier
*
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface InvalidTest {
/**
* A specific message explaining why this test may be invalid
*
* @return
*/
String value() default "";
}
/*****************************************************************************
* Copyright (c) 2014 CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that a test may fail because the tested feature
* is not yet implemented (Test-driven development)
*
* This annotation must be used with the {@link ClassificationRule}
*
* @author Camille Letavernier
*
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface NotImplemented {
/**
* A specific message explaining what needs to be implemented
* for this test to run
*
* @return
*/
String value() default "";
}
/*****************************************************************************
* Copyright (c) 2014 CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
*****************************************************************************/
package org.eclipse.papyrus.junit.framework.classification;
import java.lang.annotation.Annotation;
/**
* Enumerates all available TestCategories
*
* @author Camille Letavernier
*
*/
public enum TestCategory {
/**
* Test methods annotated with {@link NotImplemented}
*/
NotImplemented(NotImplemented.class),
/**
* Test methods annotated with {@link InvalidTest}
*/
InvalidTest(InvalidTest.class),
/**
* Test methods annotated with {@link FailingTest}
*/
FailingTest(FailingTest.class),
/**
* Test methods annotated with {@link InteractiveTest}
*/
InteractiveTest(InteractiveTest.class),
/**
* Test methods annotated with {@link GeneratedTest}
*/
GeneratedTest(GeneratedTest.class),
/**
* Test methods annotated with {@link ExpensiveTest}
*/
ExpensiveTest(ExpensiveTest.class),
/**
* Test methods without any classification-related annotation
*/
Standard();
private Class<? extends Annotation> annotationClass;
private TestCategory() {
//Empty constructor for Standard
}
private TestCategory(Class<? extends Annotation> annotationClass) {
this.annotationClass = annotationClass;
}
public boolean match(Class<? extends Annotation> annotationClass) {
return annotationClass == this.annotationClass;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment