Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
avoid use of xtext.junit4 in xtext tests
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Dietrich <christian.dietrich@itemis.de>
  • Loading branch information
cdietrich committed Nov 30, 2022
1 parent 362dae7 commit dd56f7b
Show file tree
Hide file tree
Showing 55 changed files with 829 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Bundle-Version: 2.30.0.qualifier
Bundle-Vendor: %providerName
Bundle-RequiredExecutionEnvironment: JavaSE-11
Bundle-Localization: plugin
Require-Bundle: org.eclipse.xtext.junit4,
org.eclipse.xtext.util,
Require-Bundle: org.eclipse.xtext.util,
org.eclipse.xtext.common.types,
org.eclipse.emf.ecore;bundle-version="2.20.0",
org.eclipse.emf.mwe2.runtime,
Expand Down Expand Up @@ -71,6 +70,8 @@ Bundle-ActivationPolicy: lazy
Bundle-Activator: org.eclipse.xtext.common.types.tests.Activator
Import-Package: org.apache.log4j,
org.junit;version="4.12.0",
org.junit.rules;version="4.12.0",
org.junit.runner;version="4.12.0",
org.junit.runners;version="4.12.0"
org.junit.runners;version="4.12.0",
org.junit.runners.model;version="4.12.0"
Automatic-Module-Name: org.eclipse.xtext.common.types.eclipse.tests
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.eclipse.xtext.common.types.JvmMember;
import org.eclipse.xtext.common.types.access.IJvmTypeProvider;
import org.eclipse.xtext.common.types.access.jdt.MockJavaProjectProvider;
import org.eclipse.xtext.junit4.internal.StopwatchRule;
import org.eclipse.xtext.common.types.util.StopwatchRule;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*******************************************************************************
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.common.types.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.xtext.util.internal.Stopwatches;
import org.eclipse.xtext.util.internal.Stopwatches.NumbersForTask;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/**
* @author Sven Efftinge - Initial contribution and API
*/
public class StopwatchRule implements TestRule {

private boolean watchAll = false;

public StopwatchRule() {
}

/**
* @param watchAll <code>true</code> if all tests should be timed, <code>false</code>
* if only tests should be considered which are annotated with {@link Timed @Timed}
*/
public StopwatchRule(boolean watchAll) {
this.watchAll = watchAll;
}

public static Date DATE = new Date();

public void printStopwatchData(Description description, Map<String, Stopwatches.NumbersForTask> data, long timeSpend) {
String property = System.getProperty("stopwatch.file");
String isJson = System.getProperty("stopwatch.json");
PrintStream out = System.out;
FileOutputStream outputStream = null;
if (property != null) {
try {
outputStream = new FileOutputStream(new File(property), true);
out = new PrintStream(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if (isJson == null) {
out.println("-------------------------------------------------------------------------------------------------------------------------\n");
out.println("Test '" + description.getDisplayName() + "' :");
out.println(getStopwatchDataAsReadableString(data));
} else {
out.println(",{");
out.println(" \"name\" : \""+description.getDisplayName()+"\",");
out.println(" \"date\" : \""+DATE+"\",");
out.println(" \"time\" : \""+timeSpend+"\",");
out.println(" \"tasks\" : [");
Iterator<Entry<String, NumbersForTask>> iter = data.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, NumbersForTask> task = iter.next();
out.print(" {\"name\" : \""+task.getKey()+"\",\"time\" : "+task.getValue().getMilliseconds()+",\"measurements\" : "+task.getValue().getNumberOfMeasurements());
if (iter.hasNext()) {
out.println("},");
} else {
out.println("}");
}
}
out.println(" ]");
out.println("}");
}
} finally {
// out.flush();
if (outputStream != null)
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public String getStopwatchDataAsReadableString(Map<String, NumbersForTask> data) {
StringBuilder sb = new StringBuilder();
sb.append("-------------------------------------------------------------------------------------------------------------------------\n");
for (Entry<String, NumbersForTask> task : data.entrySet()) {
sb.append("Task '" + task.getKey() + "' took " + task.getValue().getMilliseconds() + "ms ("
+ task.getValue().getNumberOfMeasurements() + " measurements).\n");
}
sb.append("-------------------------------------------------------------------------------------------------------------------------\n");
return sb.toString();
}

@Override
public Statement apply(final Statement base, final Description description) {
if (!watchAll && description.getAnnotation(Timed.class) == null)
return base;
return new Statement() {
@Override
public void evaluate() throws Throwable {
long timeSpend = -1;
try {
Stopwatches.setEnabled(true);
Stopwatches.resetAll();
long before = System.currentTimeMillis();
base.evaluate();
timeSpend = System.currentTimeMillis()-before;
} finally {
printStopwatchData(description, Stopwatches.allNumbers(), timeSpend);
Stopwatches.resetAll();
Stopwatches.setEnabled(false);
}
}
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*******************************************************************************
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.common.types.util;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Sven Efftinge - Initial contribution and API
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface Timed {

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
import org.eclipse.xtext.common.types.access.jdt.MockJavaProjectProvider;
import org.eclipse.xtext.common.types.eclipse.tests.internal.TestsActivator;
import org.eclipse.xtext.common.types.xtext.ui.ui.ContentAssistTestLanguageUiModule;
import org.eclipse.xtext.junit4.AbstractXtextTests;
import org.eclipse.xtext.ui.shared.SharedStateModule;
import org.eclipse.xtext.ui.testing.AbstractXtextTests;
import org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder;
import org.eclipse.xtext.ui.testing.util.ResourceLoadHelper;
import org.eclipse.xtext.util.Modules2;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -33,7 +32,7 @@
/**
* @author dhuebner - Initial contribution and API
*/
public class ContentAssistTest extends AbstractXtextTests implements ResourceLoadHelper {
public class ContentAssistTest extends AbstractXtextTests {

@BeforeClass public static void createMockJavaProject() throws Exception {
MockJavaProjectProvider.setUp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
@RunWith(XtextRunner.class)
@InjectWith(XtextInjectorProvider.class)
@SuppressWarnings("deprecation")
@Deprecated(forRemoval = true)
public class Bug367679Test {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.junit.Assert;
import org.junit.Test;

@Deprecated(forRemoval = true)
public class DiagnosticTreeIterableTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* @author Sebastian Zarnekow
*/
@Deprecated
@Deprecated(forRemoval = true)
public class FlakyFailsFourTimes {
@Rule
public Flaky.Rule rule = new Flaky.Rule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* @author Sebastian Zarnekow
*/
@Deprecated
@Deprecated(forRemoval = true)
public class FlakyFailsTwoTimes {
@Rule
public Flaky.Rule rule = new Flaky.Rule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* @author Sebastian Zarnekow
*/
@Deprecated
@Deprecated(forRemoval = true)
public class IllegalFlakyConfigTest {

@Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* @author Sebastian Zarnekow
*/
@SuppressWarnings("deprecation")
@Deprecated(forRemoval = true)
public class SmokeTestScenarioTest {

@Test public void testSkipLastCharacters_01() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
@InjectWith(XtextRunnerTest.MyInjectorProvider.class)
@RunWith(XtextRunner.class)
@SuppressWarnings("deprecation")
@Deprecated(forRemoval = true)
public class XtextRunnerTest {

private static boolean injectorCreated = false;
Expand Down
Loading

0 comments on commit dd56f7b

Please sign in to comment.