[eclipse/xtext#1851] More tests for the tests

see eclipse/xtext#1851
This commit is contained in:
Sebastian Zarnekow 2020-09-22 13:11:37 +02:00 committed by Sebastian Zarnekow
parent d6426978c6
commit e04efb9af6
4 changed files with 74 additions and 20 deletions

View file

@ -57,18 +57,26 @@ public class InjectionExtension implements BeforeEachCallback, AfterEachCallback
* @since 2.24
*/
protected static class RegistryReset implements CloseableResource {
protected IRegistryConfigurator resetter;
protected final IRegistryConfigurator resetter;
protected boolean didSetup = false;
public RegistryReset(IRegistryConfigurator resetter) {
this.resetter = resetter;
resetter.setupRegistry();
}
public void setup() {
if (!didSetup) {
didSetup = true;
resetter.setupRegistry();
}
}
@Override
public void close() throws Throwable {
if (resetter != null) {
if (didSetup) {
resetter.restoreRegistry();
resetter = null;
didSetup = false;
}
}
}
@ -94,7 +102,7 @@ public class InjectionExtension implements BeforeEachCallback, AfterEachCallback
protected void setupRegistry(IInjectorProvider injectorProvider, ExtensionContext context) {
if (injectorProvider instanceof IRegistryConfigurator) {
IRegistryConfigurator registryConfigurator = (IRegistryConfigurator) injectorProvider;
context.getStore(Namespace.create(this)).getOrComputeIfAbsent(registryConfigurator, RegistryReset::new, RegistryReset.class);
context.getStore(Namespace.create(this)).getOrComputeIfAbsent(registryConfigurator, RegistryReset::new, RegistryReset.class).setup();
}
}

View file

@ -17,7 +17,8 @@ import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import com.google.inject.Binder;
import com.google.inject.Guice;
@ -59,13 +60,18 @@ public abstract class AbstractJUnitIntegrationTest {
@Override
public void setupRegistry() {
registrySaved = true;
assertFalse(injectorCreated);
if (!registryRestored) {
assertFalse(injectorCreated);
} else {
registryRestored = false;
}
}
@Override
public void restoreRegistry() {
assertTrue(registrySaved);
registryRestored = true;
registrySaved = false;
}
}
@ -78,19 +84,18 @@ public abstract class AbstractJUnitIntegrationTest {
}
@Before
public void beforeShouldBeExecutedAfterTheRegistriesAreInitialized(){
@BeforeEach
public final void beforeShouldBeExecutedAfterTheRegistriesAreInitialized(){
assertTrue(registrySaved);
assertTrue(injectorCreated);
assertTrue(fieldsInjected);
}
@Test
public void shouldSaveRegistriesBeforeCreatingAnInjector() {
// tests are performed in MyInjectorProvider
}
public abstract void shouldSaveRegistriesBeforeCreatingAnInjector();
@After
public void afterShouldBeExecutedBeforeTheRegistriesAreRestored(){
@AfterEach
public final void afterShouldBeExecutedBeforeTheRegistriesAreRestored(){
assertFalse(registryRestored);
}
}

View file

@ -9,12 +9,17 @@
package org.eclipse.xtext.testing.tests;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.XtextRunner;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runners.model.Statement;
import com.google.inject.Inject;
import com.google.inject.Injector;
@ -28,13 +33,27 @@ public class XtextRunnerTest extends AbstractJUnitIntegrationTest {
@Inject
private Injector injector;
/**
* Need to override, otherwise no tests are found in Gradle build
*/
@Test
@Override
public void shouldSaveRegistriesBeforeCreatingAnInjector() {
super.shouldSaveRegistriesBeforeCreatingAnInjector();
@Inject
@Rule
public SomeRule someRule;
static class SomeRule implements TestRule {
@Inject Injector injector;
boolean didApplyRule;
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
assertNotNull(injector);
didApplyRule = true;
base.evaluate();
didApplyRule = false;
}
};
}
}
@Before
@ -42,4 +61,18 @@ public class XtextRunnerTest extends AbstractJUnitIntegrationTest {
assertNotNull(injector);
}
/**
* Need to override to add proper {@code @Test}, otherwise no tests are found in Gradle build
*/
@Test
@Override
public void shouldSaveRegistriesBeforeCreatingAnInjector() {
// tests are performed in MyInjectorProvider
}
@Test
public void didUseTestRule() {
assertNotNull(someRule);
assertTrue(someRule.didApplyRule);
}
}

View file

@ -54,11 +54,19 @@ public class InjectionExtensionTest extends AbstractJUnitIntegrationTest {
Assertions.assertNotNull(injector);
}
/**
* Need to override to add proper {@code @Test}, otherwise no tests are found in Gradle build
*/
@Test
@Override
public void shouldSaveRegistriesBeforeCreatingAnInjector() {
// tests are performed in MyInjectorProvider
}
@Test
public void didUseRegisteredExtension() {
Assertions.assertNotNull(registeredExtension);
Assertions.assertTrue(registeredExtension.didCallBeforeEach);
}
}