From cadb87b21ad01a5ea28dea34c0162d1ad7ee882f Mon Sep 17 00:00:00 2001 From: U004458 Date: Mon, 10 Nov 2025 20:49:50 +0800 Subject: [PATCH] feat: Migrate com.avaloq.tools.ddk.xtext.test to Junit5 --- .../test/AbstractCheckGenerationTestCase.java | 5 + .../IssueCodeToLabelMapGenerationTest.xtend | 11 +- .../xtext/test/jupiter/AbstractUtilTest.java | 75 +++ .../META-INF/MANIFEST.MF | 4 +- .../tools/ddk/xtext/XtextTestSuite.java | 14 +- .../xtext/builder/XtextBuildTriggerTest.java | 39 +- .../builder/XtextBuilderInjectorProvider.java | 39 ++ .../formatter/AbstractFormatterTest.java | 76 +++ .../jupiter/formatter/FormatterTest.java | 507 ++++++++++++++++++ .../jupiter/formatter/FormatterTestUtil.java | 46 ++ .../linking/AbstractFragmentProviderTest.java | 11 +- .../linking/ShortFragmentProviderTest.java | 29 +- .../naming/QualifiedNamePatternTest.java | 27 +- .../QualifiedNameSegmentTreeLookupTest.java | 6 +- .../AbstractSelectorFragmentProviderTest.java | 56 +- .../AbstractXtextTestsInjectorProvider.java | 36 ++ .../tools/ddk/xtext/resource/BugAig1084.java | 2 +- .../ResourceDescriptionDeltaTest.java | 10 +- .../xtext/util/RuntimeProjectUtilTest.java | 26 +- 19 files changed, 906 insertions(+), 113 deletions(-) create mode 100644 com.avaloq.tools.ddk.xtext.test.core/src/com/avaloq/tools/ddk/xtext/test/jupiter/AbstractUtilTest.java create mode 100644 com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/builder/XtextBuilderInjectorProvider.java create mode 100644 com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/jupiter/formatter/AbstractFormatterTest.java create mode 100644 com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/jupiter/formatter/FormatterTest.java create mode 100644 com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/jupiter/formatter/FormatterTestUtil.java create mode 100644 com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/AbstractXtextTestsInjectorProvider.java diff --git a/com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/AbstractCheckGenerationTestCase.java b/com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/AbstractCheckGenerationTestCase.java index aca59bac33..cbd192f421 100644 --- a/com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/AbstractCheckGenerationTestCase.java +++ b/com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/AbstractCheckGenerationTestCase.java @@ -124,4 +124,9 @@ public List generateAndCompile(final InputStream sourceStream) { } } + @Override + protected Injector getInjector() { + return injector; + } + } \ No newline at end of file diff --git a/com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/IssueCodeToLabelMapGenerationTest.xtend b/com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/IssueCodeToLabelMapGenerationTest.xtend index b0c7826fdf..064a7b8ceb 100644 --- a/com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/IssueCodeToLabelMapGenerationTest.xtend +++ b/com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/IssueCodeToLabelMapGenerationTest.xtend @@ -13,19 +13,20 @@ package com.avaloq.tools.ddk.check.core.test import com.avaloq.tools.ddk.check.CheckInjectorProvider import java.io.ByteArrayInputStream -import org.eclipse.xtext.testing.InjectWith import java.util.List -import org.eclipse.xtext.xbase.testing.JavaSource -import org.junit.jupiter.api.^extension.ExtendWith +import org.eclipse.xtext.testing.InjectWith import org.eclipse.xtext.testing.extensions.InjectionExtension +import org.eclipse.xtext.xbase.testing.JavaSource import org.junit.jupiter.api.Test +import org.junit.jupiter.api.^extension.ExtendWith + import static org.junit.jupiter.api.Assertions.* /** - * Unit test for autogeneration of check issue code to label map. + * Unit test for auto generation of check issue code to label map. */ @InjectWith(CheckInjectorProvider) -@ExtendWith(typeof(InjectionExtension)) +@ExtendWith(InjectionExtension) class IssueCodeToLabelMapGenerationTest extends AbstractCheckGenerationTestCase { static final String PACKAGE_NAME = "mypackage" diff --git a/com.avaloq.tools.ddk.xtext.test.core/src/com/avaloq/tools/ddk/xtext/test/jupiter/AbstractUtilTest.java b/com.avaloq.tools.ddk.xtext.test.core/src/com/avaloq/tools/ddk/xtext/test/jupiter/AbstractUtilTest.java new file mode 100644 index 0000000000..3ee301be93 --- /dev/null +++ b/com.avaloq.tools.ddk.xtext.test.core/src/com/avaloq/tools/ddk/xtext/test/jupiter/AbstractUtilTest.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * Copyright (c) 2025 Avaloq Group AG 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: + * Avaloq Group AG - initial API and implementation + *******************************************************************************/ +package com.avaloq.tools.ddk.xtext.test.jupiter; + +import static java.util.Collections.singleton; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IStorage; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.Path; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.xtext.resource.IResourceDescription; +import org.eclipse.xtext.resource.IResourceDescription.Delta; +import org.eclipse.xtext.ui.resource.IStorage2UriMapper; +import org.eclipse.xtext.ui.resource.Storage2UriMapperImpl; +import org.eclipse.xtext.util.Pair; +import org.eclipse.xtext.util.Tuples; + + +/** + * A base class for util test classes, which prepares common required mocks. + */ +@SuppressWarnings("nls") +public abstract class AbstractUtilTest extends AbstractXtextTest { + + public static final String TEST_PROJECT_NAME = "TestProjectName"; + private static final String DUMMY_PATH = TEST_PROJECT_NAME + "/TEST/"; + + // CHECKSTYLE:CHECK-OFF VisibilityModifierCheck + protected static Delta delta; + protected static IResourceDescription oldDesc; + protected static IResourceDescription newDesc; + protected static URI uriCorrect; + protected static Resource resource; + protected static IStorage2UriMapper mapperCorrect; + protected static IFile file; + + // CHECKSTYLE:CHECK-ON VisibilityModifierCheck + + /** + * Prepare mocks for all tests. + */ + public static void prepareMocksBase() { + oldDesc = mock(IResourceDescription.class); + newDesc = mock(IResourceDescription.class); + delta = mock(Delta.class); + resource = mock(Resource.class); + uriCorrect = mock(URI.class); + when(uriCorrect.isPlatformResource()).thenReturn(true); + when(uriCorrect.isFile()).thenReturn(true); + when(uriCorrect.toFileString()).thenReturn(DUMMY_PATH); + when(uriCorrect.toPlatformString(true)).thenReturn(DUMMY_PATH); + when(delta.getNew()).thenReturn(newDesc); + when(delta.getOld()).thenReturn(oldDesc); + when(delta.getUri()).thenReturn(uriCorrect); + when(resource.getURI()).thenReturn(uriCorrect); + file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uriCorrect.toPlatformString(true))); + Iterable> storages = singleton(Tuples. create(file, file.getProject())); + mapperCorrect = mock(Storage2UriMapperImpl.class); + when(mapperCorrect.getStorages(uriCorrect)).thenReturn(storages); + } + +} diff --git a/com.avaloq.tools.ddk.xtext.test/META-INF/MANIFEST.MF b/com.avaloq.tools.ddk.xtext.test/META-INF/MANIFEST.MF index 327ef000bf..38f21ffd49 100644 --- a/com.avaloq.tools.ddk.xtext.test/META-INF/MANIFEST.MF +++ b/com.avaloq.tools.ddk.xtext.test/META-INF/MANIFEST.MF @@ -30,7 +30,9 @@ Require-Bundle: com.avaloq.tools.ddk.xtext, junit-jupiter-api, junit-jupiter-engine, junit-vintage-engine, - junit-platform-suite-api + junit-platform-suite-api, + org.eclipse.xtext.testing, + org.opentest4j Import-Package: org.slf4j, org.apache.log4j, com.avaloq.tools.ddk.check.runtime.test.core, com.avaloq.tools.ddk.check.test.core, diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/XtextTestSuite.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/XtextTestSuite.java index dd2a231434..4bc9db01cc 100644 --- a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/XtextTestSuite.java +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/XtextTestSuite.java @@ -10,27 +10,27 @@ *******************************************************************************/ package com.avaloq.tools.ddk.xtext; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; +import org.junit.platform.suite.api.SelectClasses; +import org.junit.platform.suite.api.Suite; import com.avaloq.tools.ddk.xtext.builder.XtextBuildTriggerTest; -import com.avaloq.tools.ddk.xtext.formatter.FormatterTest; +import com.avaloq.tools.ddk.xtext.jupiter.formatter.FormatterTest; import com.avaloq.tools.ddk.xtext.linking.AbstractFragmentProviderTest; import com.avaloq.tools.ddk.xtext.linking.ShortFragmentProviderTest; import com.avaloq.tools.ddk.xtext.naming.QualifiedNamePatternTest; import com.avaloq.tools.ddk.xtext.naming.QualifiedNameSegmentTreeLookupTest; import com.avaloq.tools.ddk.xtext.resource.AbstractSelectorFragmentProviderTest; -import com.avaloq.tools.ddk.xtext.resource.ResourceDescriptionDeltaTest; import com.avaloq.tools.ddk.xtext.resource.BugAig1084; +import com.avaloq.tools.ddk.xtext.resource.ResourceDescriptionDeltaTest; import com.avaloq.tools.ddk.xtext.util.RuntimeProjectUtilTest; /** - * Empty class serving only as holder for JUnit4 annotations. + * Empty class serving only as holder for JUnit5 annotations. */ // @Format-Off -@RunWith(Suite.class) -@Suite.SuiteClasses({ +@Suite +@SelectClasses({ AbstractFragmentProviderTest.class, ShortFragmentProviderTest.class, AbstractSelectorFragmentProviderTest.class, diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/builder/XtextBuildTriggerTest.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/builder/XtextBuildTriggerTest.java index 21f8b1f119..47642e3ecc 100644 --- a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/builder/XtextBuildTriggerTest.java +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/builder/XtextBuildTriggerTest.java @@ -24,39 +24,38 @@ import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.xtext.builder.impl.BuildScheduler; import org.eclipse.xtext.builder.impl.IBuildFlag; -import org.eclipse.xtext.testing.AbstractXtextTests; -import org.junit.Before; -import org.junit.Test; +import org.eclipse.xtext.testing.InjectWith; +import org.eclipse.xtext.testing.extensions.InjectionExtension; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentMatchers; import com.avaloq.tools.ddk.xtext.builder.layered.XtextBuildTrigger; -import com.google.inject.AbstractModule; +import com.google.inject.Inject; +import com.google.inject.Injector; -@SuppressWarnings({"restriction", "deprecation", "removal"}) -public class XtextBuildTriggerTest extends AbstractXtextTests { +@SuppressWarnings({"restriction", "deprecation"}) +@InjectWith(XtextBuilderInjectorProvider.class) +@ExtendWith(InjectionExtension.class) +public class XtextBuildTriggerTest { + + @Inject + private Injector injector; private IWorkspace workspace; private BuildScheduler scheduler; - @Override - @Before - public void setUp() throws Exception { - super.setUp(); - workspace = mock(IWorkspace.class); - scheduler = mock(BuildScheduler.class); - with(new AbstractModule() { - @Override - protected void configure() { - bind(BuildScheduler.class).toInstance(scheduler); - bind(IWorkspace.class).toInstance(workspace); - } - }); + @BeforeEach + public void setUp() { + workspace = injector.getInstance(IWorkspace.class); + scheduler = injector.getInstance(BuildScheduler.class); } @Test public void testTriggerRespectsAutoBuilding() { - XtextBuildTrigger buildTrigger = get(XtextBuildTrigger.class); + XtextBuildTrigger buildTrigger = injector.getInstance(XtextBuildTrigger.class); // auto-build disabled when(workspace.isAutoBuilding()).thenReturn(false); diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/builder/XtextBuilderInjectorProvider.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/builder/XtextBuilderInjectorProvider.java new file mode 100644 index 0000000000..5ef2d3250c --- /dev/null +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/builder/XtextBuilderInjectorProvider.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2016 Avaloq Group AG 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: + * Avaloq Evolution AG - initial API and implementation + *******************************************************************************/ + +package com.avaloq.tools.ddk.xtext.builder; + +import static org.mockito.Mockito.mock; + +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.xtext.builder.impl.BuildScheduler; +import org.eclipse.xtext.testing.IInjectorProvider; + +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; + + +public class XtextBuilderInjectorProvider implements IInjectorProvider { + + @Override + public Injector getInjector() { + return Guice.createInjector(new AbstractModule() { + @SuppressWarnings({"deprecation", "restriction"}) + @Override + protected void configure() { + bind(BuildScheduler.class).toInstance(mock(BuildScheduler.class)); + bind(IWorkspace.class).toInstance(mock(IWorkspace.class)); + } + }); + } + +} diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/jupiter/formatter/AbstractFormatterTest.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/jupiter/formatter/AbstractFormatterTest.java new file mode 100644 index 0000000000..3898446b3f --- /dev/null +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/jupiter/formatter/AbstractFormatterTest.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2025 Avaloq Group AG 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: + * Avaloq Group AG - initial API and implementation + *******************************************************************************/ +package com.avaloq.tools.ddk.xtext.jupiter.formatter; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.xtext.formatting.INodeModelFormatter; +import org.eclipse.xtext.formatting.INodeModelFormatter.IFormattedRegion; +import org.eclipse.xtext.nodemodel.ICompositeNode; +import org.eclipse.xtext.nodemodel.util.NodeModelUtils; +import org.eclipse.xtext.resource.SaveOptions; +import org.eclipse.xtext.serializer.ISerializer; + +import com.avaloq.tools.ddk.xtext.test.jupiter.AbstractXtextTest; + + +/** + * Base class for formatting tests. + * The assertXyz methods are essentially copied from {@link com.avaloq.tools.ddk.xtext.test.formatting.AbstractAcfFormattingTest}. + */ +@SuppressWarnings("nls") +public abstract class AbstractFormatterTest extends AbstractXtextTest { + + /** + * Loads a model from a string representation of a source. + * + * @param input + * String representing a serialized model + * @return Loaded model + */ + private EObject getModel(final String input) throws IOException { + return getXtextTestUtil().getModel("mytestmodel." + getXtextTestUtil().getFileExtension(), input); + } + + /** + * Gets the Guice injected serializer. + * + * @return Serializer the DI serializer + */ + protected ISerializer getSerializer() { + return getXtextTestUtil().getSerializer(); + } + + // test formatting based on the ParseTreeConstructorin + protected void assertFormattedPTC(final String expected, final String model) throws IOException { + EObject m = getModel(model); + String res = getSerializer().serialize(m, SaveOptions.newBuilder().format().getOptions()); + assertEquals(expected, res, "Serialization not equal"); + } + + protected void assertPreserved(final String model) throws IOException { + EObject m = getModel(model); + String res = getSerializer().serialize(m, SaveOptions.newBuilder().getOptions()); + assertEquals(model, res, "Preserved node model"); + } + + // test formatting based on the NodeModel + protected void assertFormattedNM(final String expected, final String model, final int offset, final int length) throws IOException { + ICompositeNode node = NodeModelUtils.getNode(getModel(model)).getRootNode(); + IFormattedRegion r = getXtextTestUtil().get(INodeModelFormatter.class).format(node, offset, length); + String actual = model.substring(0, r.getOffset()) + r.getFormattedText() + model.substring(r.getLength() + r.getOffset()); + assertEquals(expected, actual, "Formatting based on the NodeModel"); + } + +} diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/jupiter/formatter/FormatterTest.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/jupiter/formatter/FormatterTest.java new file mode 100644 index 0000000000..7398c4366c --- /dev/null +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/jupiter/formatter/FormatterTest.java @@ -0,0 +1,507 @@ +/******************************************************************************* + * Copyright (c) 2025 Avaloq Group AG 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: + * Avaloq Group AG - initial API and implementation + *******************************************************************************/ +package com.avaloq.tools.ddk.xtext.jupiter.formatter; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; + +import org.eclipse.xtext.resource.SaveOptions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import com.avaloq.tools.ddk.xtext.formatter.formatterTestLanguage.Decl; +import com.avaloq.tools.ddk.xtext.formatter.formatterTestLanguage.FormatterTestLanguageFactory; +import com.avaloq.tools.ddk.xtext.formatter.formatterTestLanguage.TestLinewrapMinMax; + + +/** + * This class tests the Acs Formatter framework. The tests are basically a copy + * of the Xtext Formatter tests. + */ +@SuppressWarnings("nls") +public class FormatterTest extends AbstractFormatterTest { + @Override + protected FormatterTestUtil getXtextTestUtil() { + return FormatterTestUtil.getInstance(); + } + + /** + * This test class does not have a test source file. {@inheritDoc} + */ + @Override + protected String getTestSourceFileName() { + return null; + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void linewrap() throws IOException { + String model = "test linewrap float val; int x; double y;"; + String expected = "test linewrap\nfloat val;\nint x;\ndouble y;"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void keepComments() throws IOException { + // String model = "test linewrap float val; int x; double y;"; + String model = "// begincomment \ntest linewrap// comment1\n" + "float val;//comment2\n" + "int x;" + "double y; //yoyoyo!\n// endcomment."; + final String exp = "// begincomment \ntest linewrap// comment1\n" + "float val;//comment2\n" + "int x;\n" + "double y; //yoyoyo!\n// endcomment."; + assertFormattedPTC(exp, model); + assertFormattedNM(exp, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test verifies that {@link com.avaloq.tools.ddk.xtext.formatter.formatterTestLanguage.Line} elements are aligned at the specified column. + * + * @throws IOException + */ + @Test + public void column() throws IOException { + String model = "test column item int x;"; + String expected = "test\n column\n\titem int x;"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test verifies that elements can be aligned at column 0 and also that + * a minimum padding is always inserted. + * + * @throws IOException + */ + @Test + public void columnMinimumPadding() throws IOException { + String model = " test column name item int x;"; + String expected = "test\n column name\n\n\titem int x;"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model.trim()); + } + + /** + * This test verifies that {@link com.avaloq.tools.ddk.xtext.formatter.formatterTestLanguage.Line} elements are aligned with the specified offset. + * + * @throws IOException + */ + @Test + public void offset() throws IOException { + String model = "test offset value v pair p1 p2"; + String expected = "test\noffset\n\tvalue v\n\t\tpair p1 p2"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test verifies right padding does pad and that there is always minimum padding " ". + * + * @throws IOException + */ + @Test + public void rightPadding() throws IOException { + String model = "test padding long_name n2;"; + String expected = "test\npadding long_name n2 ;"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void indentation() throws IOException { + String model = "test indentation { float val; double y; indentation { int x; } }"; + String expected = "test indentation {\n float val;\n double y;\n indentation {\n int x;\n }\n}"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void association() throws IOException { + String model = "test indentation { var = [0,1,2,3,4]; }"; + String expected = "test indentation {\n var=[ 0, 1, 2, 3, 4 ];\n}"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void indentationAndComments() throws IOException { + String model = "test /* xxx */ indentation { float val; // some float\n double /* oo */ y; indentation { // some block\n int x; // xxx\n } } // final comment"; + String expected = "test /* xxx */ indentation {\n float val; // some float\n double /* oo */ y;\n indentation { // some block\n int x; // xxx\n }\n} // final comment"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * It has been adapted to + * + * @throws IOException + */ + @Test + public void indentationAndLineWrap() throws IOException { + String model = "test indentation { void func(x:int,y:int,s:javalangString, foo:javasqlDate, blupp:mylongtype, msads:adshdjkhsakdasdkslajdlsask, x:x, a:b, c:d ); }"; + String expected = "test indentation {\n void func(x:int,y:int,\n\t\ts:javalangString,\n\t\tfoo:javasqlDate,\n\t\tblupp:mylongtype,\n\t\tmsads:adshdjkhsakdasdkslajdlsask,\n\t\tx:x,a:b,c:d);\n}"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void between1() throws IOException { + String model = "test indentation { indentation { x x; }; }"; + String expected = "test indentation {\n indentation {\n x x;\n };\n}"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void between2() throws IOException { + String model = "test indentation { indentation { x x; } }"; + String expected = "test indentation {\n indentation {\n x x;\n }\n}"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void linewrapDatatypeRule() throws IOException { + String model = "test linewrap fqn ab; fqn xx.yy.zz;"; + String expected = "test linewrap\nfqn\nab;\nfqn\nxx.yy.zz;"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void linewrapDatatypeRulePartial1() throws IOException { + String model = "test linewrap fqn ab . xx .yy .zz;"; + String expected = "test linewrap fqn ab.xx.yy.zz;"; + assertFormattedNM(expected, model, 22, 2); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void linewrapDatatypeRulePartial2() throws IOException { + String model = "test linewrap fqn ab . xx .yy .zz;fqn xxx;"; + String expected = "test linewrap fqn\nab.xx.yy.zz;fqn xxx;"; + assertFormattedNM(expected, model, 15, 10); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void linewrapDatatypeRulePartial3() throws IOException { + String model = "test linewrap fqn ab . xx .yy .zz;fqn xxx;"; + String expected = "test linewrap fqn ab.xx.yy.zz;\nfqn xxx;"; + assertFormattedNM(expected, model, 25, 12); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void formatSegment1() throws IOException { + String model = "test\nindentation {\n indentation { x x ; } }"; + String expected = "test\nindentation {\n indentation {\n x x;\n } }"; + assertFormattedNM(expected, model, 30, 18); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void formatSegment2() throws IOException { + String model = "test indentation {\n indentation { x x ; } }"; + // String expected = + // "test\nindentation {\n indentation {\n x x;\n } }"; + assertFormattedNM(model, model, 7, 10); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void formatSegment3() throws IOException { + String model = " test indentation {\n indentation { x x ; } }"; + String expected = "test indentation {\n indentation {\n x x;\n }\n}"; + assertFormattedNM(expected, model, 0, model.length()); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void linewrapDatatypeRuleRef1() throws IOException { + String model = "test linewrap fqn ab .cd .ef; fqnref ab. cd. ef;"; + String expected = "test linewrap\nfqn\nab.cd.ef;\nfqnref\nab.cd.ef;"; + // assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void linewrapDatatypeRuleRef2() throws IOException { + String model = "test linewrap fqn ab.cd.ef; fqnref ab.cd.ef;"; + String expected = "test linewrap\nfqn\nab.cd.ef;\nfqnref\nab.cd.ef;"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest but it is modified + * because I think the expected behavior in the original test is wrong. + * + * @throws IOException + */ + @Test + public void linewrapDatatypeRuleComments() throws IOException { + String model = "test linewrap/* 1 */fqn/* 2 */ab.cd.ef/* 3 */;/* 4 */fqnref/* 5 */ab.cd.ef/* 6 */;/* 7 */"; + // The expected model string differs from Xtext's - + // Xtext does not expect a line wrap after the keyword "linewrap" + // Xtext does not expect a line wrap prior to fqnref assignment + // etc... + String expected = "test linewrap/* 1 */ fqn/* 2 */\nab.cd.ef/* 3 */;/* 4 */ fqnref\n/* 5 */ ab.cd.ef/* 6 */;/* 7 */"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void enumeration() throws IOException { + String model = "test linewrap enum lit1,lit2,lit3,lit1;"; + String expected = "test linewrap\nenum lit1 ,\nlit2,\nlit3,\nlit1;"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=312559 + @Test + public void suppressedWhitespace() throws IOException { + String model = "test linewrap `f%%a` post;"; + String expected = "test linewrap\n`f%< b >%a` post;"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + // TODO: investigate whether to include test or not - currently this test + // would fail + @Disabled + public void suppressedLinewrap() throws IOException { + String model = "test linewrap\n`foo%abcd%foo%< b\n>%abcd%foo%abcd%foo%abcd%" + "foo%abcd%foo%abcd%foo%abcd%foo%abcd%foo%abcd%foo%xx%foo%abcd%foo%abcd%" + + "foo%abcd%foo%<\nb >%foo%abcd` post;"; + assertFormattedPTC(model, model); + assertFormattedNM(model, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void linewrapMin() throws IOException { + String model = "test wrapminmax foo bar;"; + String expected = "test wrapminmax\n\nfoo bar;"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void linewrapMax() throws IOException { + String model = "test wrapminmax\n\n\n\n\n\n\n\n\n\n\n\n\nfoo bar;"; + String expected = "test wrapminmax\n\n\n\n\nfoo bar;"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void linewrapKeep() throws IOException { + String model = "test wrapminmax\n\n\n\nfoo bar;"; + assertFormattedPTC(model, model); + assertFormattedNM(model, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void linewrapDefault() { + FormatterTestLanguageFactory f = FormatterTestLanguageFactory.eINSTANCE; + TestLinewrapMinMax m = f.createTestLinewrapMinMax(); + Decl d = f.createDecl(); + d.getType().add("xxx"); + d.getName().add("yyy"); + m.getItems().add(d); + String actual = getSerializer().serialize(m, SaveOptions.newBuilder().format().getOptions()); + String expected = "test wrapminmax\n\n\nxxx yyy;"; + assertEquals(expected, actual, "Default Linewrap"); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void space() throws IOException { + String model = "test linewrap space foo;"; + String expected = "test linewrap\nspace foo;"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } + + /** + * This test is copied from + * org.eclipse.xtext.nodemodel.impl.formatter.FormatterTest. + * + * @throws IOException + */ + @Test + public void datatypeRules() throws IOException { + String model = "test linewrap datatypes abc kw1 bcd def kw3;"; + String expected = "test linewrap\ndatatypes abc\nkw1\nbcd\ndef\nkw3;"; + assertFormattedPTC(expected, model); + assertFormattedNM(expected, model, 0, model.length()); + assertPreserved(model); + } +} diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/jupiter/formatter/FormatterTestUtil.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/jupiter/formatter/FormatterTestUtil.java new file mode 100644 index 0000000000..1d63fb44b5 --- /dev/null +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/jupiter/formatter/FormatterTestUtil.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2025 Avaloq Group AG 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: + * Avaloq Group AG - initial API and implementation + *******************************************************************************/ +package com.avaloq.tools.ddk.xtext.jupiter.formatter; + +import com.avaloq.tools.ddk.xtext.formatter.FormatterTestLanguageStandaloneSetup; +import com.avaloq.tools.ddk.xtext.test.jupiter.AbstractXtextTestUtil; +import com.google.inject.Injector; + + +public final class FormatterTestUtil extends AbstractXtextTestUtil { + private final Injector injector = new FormatterTestLanguageStandaloneSetup().createInjectorAndDoEMFRegistration(); + + private FormatterTestUtil() { + // private constructor + } + + /** + * The singleton instance. + */ + private static final class InstanceHolder { + // Initialize-on-demand holder pattern. + private static final FormatterTestUtil INSTANCE = new FormatterTestUtil(); + + public static FormatterTestUtil get() { + return INSTANCE; + } + } + + public static FormatterTestUtil getInstance() { + return InstanceHolder.get(); + } + + @Override + protected Injector getInjector() { + return injector; + } + +} diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/linking/AbstractFragmentProviderTest.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/linking/AbstractFragmentProviderTest.java index 0d57f63daf..58f1f104d2 100644 --- a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/linking/AbstractFragmentProviderTest.java +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/linking/AbstractFragmentProviderTest.java @@ -10,9 +10,10 @@ *******************************************************************************/ package com.avaloq.tools.ddk.xtext.linking; +import static org.junit.jupiter.api.Assertions.assertEquals; + import org.eclipse.emf.ecore.EObject; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** @@ -56,12 +57,12 @@ public String unescape(final String text) { public void testEscape() { StringBuilder builder = new StringBuilder(); fragmentProvider.appendEscaped("foo/bar#\\", builder); - Assert.assertEquals("Fragment not properly scaped", builder.toString(), "foo\\/bar#\\\\"); + assertEquals(builder.toString(), "foo\\/bar#\\\\", "Fragment not properly scaped"); } @Test public void testUnescape() { - Assert.assertEquals("Fragment not properly unscaped", "foo//bar##\\", fragmentProvider.unescape("foo\\/\\/bar##\\\\")); + assertEquals("foo//bar##\\", fragmentProvider.unescape("foo\\/\\/bar##\\\\"), "Fragment not properly unscaped"); } @Test @@ -69,7 +70,7 @@ public void testUnescapeEscape() { for (String text : SPECIAL_ESCAPE_CASES) { StringBuilder builder = new StringBuilder(); fragmentProvider.appendEscaped(text, builder); - Assert.assertEquals("Escaped Characters must be equal", text, fragmentProvider.unescape(builder.toString())); + assertEquals(text, fragmentProvider.unescape(builder.toString()), "Escaped Characters must be equal"); } } diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/linking/ShortFragmentProviderTest.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/linking/ShortFragmentProviderTest.java index 57c29d5818..1bca473ece 100644 --- a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/linking/ShortFragmentProviderTest.java +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/linking/ShortFragmentProviderTest.java @@ -10,6 +10,8 @@ *******************************************************************************/ package com.avaloq.tools.ddk.xtext.linking; +import static org.junit.jupiter.api.Assertions.assertEquals; + import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EPackage; @@ -18,13 +20,13 @@ import org.eclipse.emf.ecore.resource.impl.ResourceImpl; import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.xtext.resource.IFragmentProvider; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; import com.avaloq.tools.ddk.test.core.BugTest; -import com.avaloq.tools.ddk.xtext.test.AbstractTest; import com.avaloq.tools.ddk.xtext.test.AbstractTestUtil; @@ -32,7 +34,8 @@ * Tests for {@code AbstractFragmentProvider}. */ @SuppressWarnings("nls") -public class ShortFragmentProviderTest extends AbstractTest { +@TestInstance(Lifecycle.PER_CLASS) +public class ShortFragmentProviderTest { private static final String FRAGMENT_MUST_BE_EQUAL = "Fragment must be equal"; private static AbstractTestUtil testUtil = new AbstractTestUtil() { @@ -59,7 +62,7 @@ public EObject getEObject(final String fragment) { private EReference testReference; private EReference testReference2; - @Before + @BeforeEach public void initialize() { EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE; @@ -81,7 +84,7 @@ public void initialize() { EPackage.Registry.INSTANCE.put(testPackage.getNsURI(), testPackage); } - @After + @AfterEach public void cleanup() { EPackage.Registry.INSTANCE.remove(testPackage.getNsURI()); } @@ -101,9 +104,9 @@ public void testLongFragment() { resource.getContents().add(root); String fragment = fragmentProvider.getFragment(parent, fragmentFallback); - Assert.assertEquals(FRAGMENT_MUST_BE_EQUAL, "/0*" + (reps + 1), fragment); + assertEquals("/0*" + (reps + 1), fragment, FRAGMENT_MUST_BE_EQUAL); - Assert.assertEquals(FRAGMENT_MUST_BE_EQUAL, parent, fragmentProvider.getEObject(resource, fragment, fragmentFallback)); + assertEquals(parent, fragmentProvider.getEObject(resource, fragment, fragmentFallback), FRAGMENT_MUST_BE_EQUAL); } @Test @@ -125,17 +128,15 @@ public void testLongFragment2() { resource.getContents().add(root); String fragment = fragmentProvider.getFragment(parent, fragmentFallback); - Assert.assertEquals(FRAGMENT_MUST_BE_EQUAL, "/0*" + (reps + 1) + "/1", fragment); + assertEquals("/0*" + (reps + 1) + "/1", fragment, FRAGMENT_MUST_BE_EQUAL); - Assert.assertEquals(FRAGMENT_MUST_BE_EQUAL, parent, fragmentProvider.getEObject(resource, fragment, fragmentFallback)); + assertEquals(parent, fragmentProvider.getEObject(resource, fragment, fragmentFallback), FRAGMENT_MUST_BE_EQUAL); } - @Override protected String getTestSourceFileName() { return null; } - @Override protected AbstractTestUtil getTestUtil() { return testUtil; } diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNamePatternTest.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNamePatternTest.java index 8a932cb484..56c20b5354 100644 --- a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNamePatternTest.java +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNamePatternTest.java @@ -10,22 +10,23 @@ *******************************************************************************/ package com.avaloq.tools.ddk.xtext.naming; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Comparator; import org.eclipse.xtext.naming.QualifiedName; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.eclipse.xtext.testing.extensions.InjectionExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import com.avaloq.tools.ddk.test.core.BugTest; -import com.avaloq.tools.ddk.xtext.test.junit.runners.XtextClassRunner; -@RunWith(XtextClassRunner.class) @SuppressWarnings({"PMD.JUnitAssertionsShouldIncludeMessage", "nls"}) +@ExtendWith(InjectionExtension.class) // CHECKSTYLE:CONSTANTS-OFF public class QualifiedNamePatternTest { @@ -73,19 +74,19 @@ public void testRecursiveWildcardPatternWithPrefix() { assertNoMatch(pattern, "foo2", "bar"); } - @Test(expected = IllegalArgumentException.class) + @Test public void testRecursiveWildcardPatternError() { - QualifiedNamePattern.create("foo", "bar**baz"); + assertThrows(IllegalArgumentException.class, () -> QualifiedNamePattern.create("foo", "bar**baz")); } - @Test(expected = IllegalArgumentException.class) + @Test public void testRecursiveWildcardPatternError2() { - QualifiedNamePattern.create("foo", "**", "bar"); + assertThrows(IllegalArgumentException.class, () -> QualifiedNamePattern.create("foo", "**", "bar")); } - @Test(expected = IllegalArgumentException.class) + @Test public void testRecursiveWildcardPatternError3() { - QualifiedNamePattern.create("foo*bar"); + assertThrows(IllegalArgumentException.class, () -> QualifiedNamePattern.create("foo*bar")); } @Test diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookupTest.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookupTest.java index 05968c87f8..55ae55dd2e 100644 --- a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookupTest.java +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookupTest.java @@ -10,15 +10,15 @@ *******************************************************************************/ package com.avaloq.tools.ddk.xtext.naming; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import java.util.Collection; import java.util.Collections; import org.eclipse.emf.common.util.URI; import org.eclipse.xtext.naming.QualifiedName; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.google.common.collect.ImmutableSet; diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/AbstractSelectorFragmentProviderTest.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/AbstractSelectorFragmentProviderTest.java index ec4fb770d5..625c3eb828 100644 --- a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/AbstractSelectorFragmentProviderTest.java +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/AbstractSelectorFragmentProviderTest.java @@ -10,6 +10,9 @@ *******************************************************************************/ package com.avaloq.tools.ddk.xtext.resource; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; + import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EPackage; @@ -20,56 +23,48 @@ import org.eclipse.xtext.Grammar; import org.eclipse.xtext.Keyword; import org.eclipse.xtext.XtextPackage; -import org.eclipse.xtext.XtextRuntimeModule; -import org.eclipse.xtext.resource.IFragmentProvider; -import org.eclipse.xtext.testing.AbstractXtextTests; -import org.eclipse.xtext.util.Modules2; -import org.junit.Before; -import org.junit.Test; +import org.eclipse.xtext.testing.InjectWith; +import org.eclipse.xtext.testing.extensions.InjectionExtension; +import org.eclipse.xtext.testing.util.ParseHelper; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; -import com.google.inject.AbstractModule; +import com.google.inject.Inject; /** * Tests for {@code AbstractSelectorFragmentProvider}. */ // CHECKSTYLE:OFF -@SuppressWarnings({"PMD.JUnitAssertionsShouldIncludeMessage", "deprecation", "removal", "nls"}) -public class AbstractSelectorFragmentProviderTest extends AbstractXtextTests { +@SuppressWarnings({"PMD.JUnitAssertionsShouldIncludeMessage", "nls"}) +@InjectWith(AbstractXtextTestsInjectorProvider.class) +@ExtendWith(InjectionExtension.class) +public class AbstractSelectorFragmentProviderTest { // CHECKSTYLE:ON - @Before - @Override - public void setUp() throws Exception { - super.setUp(); - with(Modules2.mixin(new XtextRuntimeModule(), new AbstractModule() { - @Override - protected void configure() { - bind(IFragmentProvider.class).to(TestSelectorFragmentProvider.class); - } - })); - } + @Inject + private ParseHelper parseHelper; @Test - public void testTopLevelObject() throws Exception { + public void testTopLevelObject() { Grammar grammar = (Grammar) getModel("grammar foo.Foo\n" + "generate foo 'http://www.foo.com/foo'\n" + "Foo: 'foo';"); assertFragmentMatchesAndResolves(grammar.eResource(), "/0", grammar); } @Test - public void testMultiValuedContainment() throws Exception { + public void testMultiValuedContainment() { Grammar grammar = (Grammar) getModel("grammar foo.Foo\n" + "generate foo 'http://www.foo.com/foo'\n" + "Foo: 'foo';"); assertFragmentMatchesAndResolves(grammar.eResource(), "/0/5(0='Foo')#0", grammar.getRules().get(0)); } @Test - public void testSingleValuedContainment() throws Exception { + public void testSingleValuedContainment() { Grammar grammar = (Grammar) getModel("grammar foo.Foo\n" + "generate foo 'http://www.foo.com/foo'\n" + "Foo: 'foo'+;"); assertFragmentMatchesAndResolves(grammar.eResource(), "/0/5(0='Foo')#0/2(3='foo')", grammar.getRules().get(0).getAlternatives()); } @Test - public void testNullSelectorValue() throws Exception { + public void testNullSelectorValue() { Grammar grammar = (Grammar) getModel("grammar foo.Foo\n" + "generate foo 'http://www.foo.com/foo'\n" + "Foo: 'selectCardinality';"); assertFragmentMatchesAndResolves(grammar.eResource(), "/0/5(0='Foo')#0/2(0=null)", grammar.getRules().get(0).getAlternatives()); @@ -78,7 +73,7 @@ public void testNullSelectorValue() throws Exception { } @Test - public void testEscapedSelectorValue() throws Exception { + public void testEscapedSelectorValue() { Grammar grammar = (Grammar) getModel("grammar foo.Foo\n" + "generate foo 'http://www.foo.com/foo'\n" + "Foo: 'foo.bar#';"); assertFragmentMatchesAndResolves(grammar.eResource(), "/0/5(0='Foo')#0/2(3='foo.bar#')", grammar.getRules().get(0).getAlternatives()); } @@ -89,7 +84,16 @@ private void assertFragmentMatchesAndResolves(final Resource res, final String e assertSame(obj, res.getEObject(fragment)); } - private static final class TestSelectorFragmentProvider extends AbstractSelectorFragmentProvider { + public EObject getModel(final String model) { + try { + return parseHelper.parse(model); + } catch (Exception e) { + // TODO Auto-generated catch block + throw new AssertionError(e); + } + } + + static final class TestSelectorFragmentProvider extends AbstractSelectorFragmentProvider { @Override public boolean appendFragmentSegment(final EObject object, final StringBuilder builder) { diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/AbstractXtextTestsInjectorProvider.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/AbstractXtextTestsInjectorProvider.java new file mode 100644 index 0000000000..b804cd5ad3 --- /dev/null +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/AbstractXtextTestsInjectorProvider.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2025 Avaloq Group AG 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: + * Avaloq Evolution AG - initial API and implementation + *******************************************************************************/ + +package com.avaloq.tools.ddk.xtext.resource; + +import org.eclipse.xtext.XtextRuntimeModule; +import org.eclipse.xtext.resource.IFragmentProvider; +import org.eclipse.xtext.testing.IInjectorProvider; +import org.eclipse.xtext.util.Modules2; + +import com.avaloq.tools.ddk.xtext.resource.AbstractSelectorFragmentProviderTest.TestSelectorFragmentProvider; +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; + + +public class AbstractXtextTestsInjectorProvider implements IInjectorProvider { + + @Override + public Injector getInjector() { + return Guice.createInjector(Modules2.mixin(new XtextRuntimeModule(), new AbstractModule() { + @Override + protected void configure() { + bind(IFragmentProvider.class).to(TestSelectorFragmentProvider.class); + } + })); + } +} diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/BugAig1084.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/BugAig1084.java index ec472473e8..a2d550034f 100644 --- a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/BugAig1084.java +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/BugAig1084.java @@ -19,7 +19,7 @@ import org.eclipse.xtext.resource.IEObjectDescription; import org.eclipse.xtext.util.IResourceScopeCache; import org.eclipse.xtext.util.OnChangeEvictingCache; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.google.common.collect.Lists; diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/ResourceDescriptionDeltaTest.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/ResourceDescriptionDeltaTest.java index 1538a10465..e60dca7862 100644 --- a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/ResourceDescriptionDeltaTest.java +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/resource/ResourceDescriptionDeltaTest.java @@ -10,10 +10,10 @@ *******************************************************************************/ package com.avaloq.tools.ddk.xtext.resource; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; @@ -26,7 +26,7 @@ import org.eclipse.xtext.resource.IReferenceDescription; import org.eclipse.xtext.resource.IResourceDescription; import org.eclipse.xtext.resource.impl.AbstractResourceDescription; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.google.common.base.Function; import com.google.common.collect.ImmutableSet; diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/util/RuntimeProjectUtilTest.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/util/RuntimeProjectUtilTest.java index 7a4b158afd..58d1374632 100644 --- a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/util/RuntimeProjectUtilTest.java +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/util/RuntimeProjectUtilTest.java @@ -10,9 +10,9 @@ *******************************************************************************/ package com.avaloq.tools.ddk.xtext.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assume.assumeNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -26,10 +26,10 @@ import org.eclipse.xtext.ui.resource.IStorage2UriMapper; import org.eclipse.xtext.ui.resource.Storage2UriMapperImpl; import org.eclipse.xtext.util.Pair; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import com.avaloq.tools.ddk.xtext.test.AbstractUtilTest; -import com.avaloq.tools.ddk.xtext.test.AbstractXtextTestUtil; +import com.avaloq.tools.ddk.xtext.test.jupiter.AbstractUtilTest; +import com.avaloq.tools.ddk.xtext.test.jupiter.AbstractXtextTestUtil; import com.avaloq.tools.ddk.xtext.ui.util.RuntimeProjectUtil; import com.google.common.collect.ImmutableList; import com.google.inject.Guice; @@ -85,8 +85,8 @@ protected void beforeAllTests() { */ @Test public void getPathProjectTest() { - assertEquals("Check if the correct project path has been returned", WORKSPACE_PATH + "/" - + TEST_PROJECT_NAME, RuntimeProjectUtil.getPathProject(resource, mapperCorrect)); + assertEquals(WORKSPACE_PATH + "/" + + TEST_PROJECT_NAME, RuntimeProjectUtil.getPathProject(resource, mapperCorrect), "Check if the correct project path has been returned"); } /** @@ -96,8 +96,8 @@ public void getPathProjectTest() { public void getProjectCorrectTest() { super.addSourceToWorkspace(SOURCE_NAMES.get(0)); IProject iproject = RuntimeProjectUtil.getProject(uriCorrect, mapperCorrect); - assumeNotNull(iproject); - assertEquals("Check if the correct project name has been returned", TEST_PROJECT_NAME, iproject.getName()); + assumeTrue(iproject != null); + assertEquals(TEST_PROJECT_NAME, iproject.getName(), "Check if the correct project name has been returned"); } /** @@ -107,7 +107,7 @@ public void getProjectCorrectTest() { public void getProjectInCorrectTest() { super.addSourceToWorkspace(SOURCE_NAMES.get(0)); IProject iproject = RuntimeProjectUtil.getProject(uriCorrect, mapperInCorrect); - assertNull("When passed IStorage2UriMapper is broken then null instead of project name expected", iproject); + assertNull(iproject, "When passed IStorage2UriMapper is broken then null instead of project name expected"); } @@ -117,7 +117,7 @@ public void getProjectInCorrectTest() { @Test public void findFileStorageCorrectTest() { super.addSourceToWorkspace(SOURCE_NAMES.get(0)); - assertEquals("Check if the correct file has been returned", RuntimeProjectUtil.findFileStorage(uriCorrect, mapperCorrect), file); + assertEquals(RuntimeProjectUtil.findFileStorage(uriCorrect, mapperCorrect), file, "Check if the correct file has been returned"); } /** @@ -125,7 +125,7 @@ public void findFileStorageCorrectTest() { */ @Test public void findFileStorageInCorrectTest() { - assertNull("When passed IStorage2UriMapper is broken then no file expected", RuntimeProjectUtil.findFileStorage(uriInCorrect, mapperInCorrect)); + assertNull(RuntimeProjectUtil.findFileStorage(uriInCorrect, mapperInCorrect), "When passed IStorage2UriMapper is broken then no file expected"); } }