Migrate yang-maven-plugin to JUnit5 40/104640/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 1 Mar 2023 09:33:12 +0000 (10:33 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 1 Mar 2023 09:33:12 +0000 (10:33 +0100)
A straightforward rewrite + customized cleanups.

Change-Id: Ibc92482edfa6f598f5d4ad446ffa9dd1cd2a03ba
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/AbstractCodeGeneratorTest.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/ContextHolderTest.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/FilenameResolutionTest.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/GenerateSourcesTest.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependencyTest.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesMojoTest.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessorTest.java

index a0c74cc7bd045583bd454afc8db086897206c154..d08ca5718f785f9b1da1ba1abeb8367cc82d6124 100644 (file)
@@ -26,16 +26,18 @@ import org.apache.maven.model.Plugin;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
-import org.junit.Before;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
-import org.mockito.MockedStatic;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
 import org.opendaylight.yangtools.plugin.generator.api.FileGenerator;
 import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException;
 import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory;
 
-@RunWith(MockitoJUnitRunner.Silent.class)
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
 public abstract class AbstractCodeGeneratorTest {
     @Mock
     MavenProject project;
@@ -46,7 +48,7 @@ public abstract class AbstractCodeGeneratorTest {
     @Mock
     private Plugin plugin;
 
-    @Before
+    @BeforeEach
     public void before() throws IOException {
         Files.deleteIfExists(YangToSourcesProcessor.stateFilePath("target/"));
         doReturn("target/").when(build).getDirectory();
@@ -62,11 +64,11 @@ public abstract class AbstractCodeGeneratorTest {
 
     static final void assertMojoExecution(final YangToSourcesProcessor processor, final Prepare prepare,
             final Verify verify) {
-        try (MockedStatic<?> staticLoader = mockStatic(ServiceLoader.class)) {
-            final FileGenerator generator = mock(FileGenerator.class);
+        try (var staticLoader = mockStatic(ServiceLoader.class)) {
+            final var generator = mock(FileGenerator.class);
             doCallRealMethod().when(generator).importResolutionMode();
 
-            final FileGeneratorFactory factory = mock(FileGeneratorFactory.class);
+            final var factory = mock(FileGeneratorFactory.class);
             doReturn("mockGenerator").when(factory).getIdentifier();
 
             try {
@@ -75,7 +77,7 @@ public abstract class AbstractCodeGeneratorTest {
                 throw new AssertionError(e);
             }
 
-            final ServiceLoader<?> loader = mock(ServiceLoader.class);
+            final var loader = mock(ServiceLoader.class);
             doReturn(Iterators.singletonIterator(factory)).when(loader).iterator();
             staticLoader.when(() -> ServiceLoader.load(FileGeneratorFactory.class)).thenReturn(loader);
 
index 5b2e922f2dc7d22316853c3c21d6c655d7095213..b09865f3f11ae4eb36404a1b892954df22e8b511 100644 (file)
@@ -7,16 +7,16 @@
  */
 package org.opendaylight.yangtools.yang2sources.plugin;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertSame;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
 
 import java.util.Set;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
-public class ContextHolderTest {
+class ContextHolderTest {
     @Test
-    public void contextHolderTest() {
+    void contextHolderTest() {
         final var context = YangParserTestUtils.parseYangResources(getClass(), "/test.yang", "/test2.yang");
         final var holder = new ContextHolder(context, Set.of(), Set.of());
         assertSame(context, holder.getContext());
index 9d0bf3ac2005d6effd613959bcd59b81ea9df078..5a3ae30dc05421772247975472ceeefd942db1d4 100644 (file)
@@ -7,7 +7,7 @@
  */
 package org.opendaylight.yangtools.yang2sources.plugin;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doAnswer;
 
@@ -15,25 +15,24 @@ import com.google.common.collect.ImmutableTable;
 import com.google.common.collect.Iterables;
 import com.google.common.io.Resources;
 import java.io.File;
-import java.io.IOException;
-import java.net.URISyntaxException;
 import java.util.List;
 import java.util.Optional;
-import org.apache.maven.plugin.AbstractMojoExecutionException;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.yangtools.plugin.generator.api.ModuleResourceResolver;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class FilenameResolutionTest extends AbstractCodeGeneratorTest {
+@ExtendWith(MockitoExtension.class)
+class FilenameResolutionTest extends AbstractCodeGeneratorTest {
     @Test
-    public void testResolveSubmoduleResource() throws URISyntaxException, AbstractMojoExecutionException, IOException {
+    void testResolveSubmoduleResource() throws Exception {
         assertMojoExecution(new YangToSourcesProcessor(
-            new File(Resources.getResource(FilenameResolutionTest.class, "/filename").toURI()), List.of(),
-            List.of(new FileGeneratorArg("mockGenerator")), project, false, yangProvider), mock -> {
+            new File(Resources.getResource(FilenameResolutionTest.class, "/filename").toURI()),
+            List.of(),
+            List.of(new FileGeneratorArg("mockGenerator")), project, false, yangProvider),
+            mock -> {
                 doAnswer(invocation -> {
                     final EffectiveModelContext context = invocation.getArgument(0);
                     final ModuleResourceResolver resolver = invocation.getArgument(2);
@@ -48,6 +47,8 @@ public class FilenameResolutionTest extends AbstractCodeGeneratorTest {
 
                     return ImmutableTable.of();
                 }).when(mock).generateFiles(any(), any(), any());
-            }, mock -> { });
+            }, mock -> {
+                // No-op
+            });
     }
 }
index 538b5116f890e077811861655c6e3cb2d06e10f2..a1c6f3eebcabfdef9cc2910af4833854fd7d48a9 100644 (file)
@@ -15,13 +15,14 @@ import com.google.common.collect.ImmutableTable;
 import com.google.common.io.Resources;
 import java.io.File;
 import java.util.List;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 
-public class GenerateSourcesTest extends AbstractCodeGeneratorTest {
+class GenerateSourcesTest extends AbstractCodeGeneratorTest {
     @Test
-    public void test() throws Exception {
+    void test() throws Exception {
         assertMojoExecution(new YangToSourcesProcessor(
-            new File(Resources.getResource(GenerateSourcesTest.class, "/yang").toURI()), List.of(),
+            new File(Resources.getResource(GenerateSourcesTest.class, "/yang").toURI()),
+            List.of(),
             List.of(new FileGeneratorArg("mockGenerator")), project, false, yangProvider),
             mock -> {
                 doReturn(ImmutableTable.of()).when(mock).generateFiles(any(), any(), any());
index 5702e08b8c666fce430a00e95d7c7650847bf15a..8ac765d594d6360ce191f8ec6ae16ea2cf40d389 100644 (file)
@@ -7,9 +7,9 @@
  */
 package org.opendaylight.yangtools.yang2sources.plugin;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
@@ -19,8 +19,6 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.util.Collection;
-import java.util.List;
 import java.util.Set;
 import java.util.jar.Attributes;
 import java.util.jar.JarEntry;
@@ -28,16 +26,22 @@ import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.project.MavenProject;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+class ScannedDependencyTest {
+    @Mock
+    private MavenProject project;
 
-public class ScannedDependencyTest {
     @Test
-    public void getClassPathTest() {
-        final MavenProject project = mock(MavenProject.class);
-        final File file = mock(File.class);
-        final File file2 = mock(File.class);
-        final Artifact artifact = mock(Artifact.class);
-        final Artifact artifact2 = mock(Artifact.class);
+    void getClassPathTest() {
+        final var file = mock(File.class);
+        final var file2 = mock(File.class);
+        final var artifact = mock(Artifact.class);
+        final var artifact2 = mock(Artifact.class);
 
         doReturn(Set.of(artifact, artifact2)).when(project).getArtifacts();
         doReturn(file).when(artifact).getFile();
@@ -46,44 +50,42 @@ public class ScannedDependencyTest {
         doReturn(file2).when(artifact2).getFile();
         doReturn(true).when(file2).isDirectory();
 
-        final List<File> files = ScannedDependency.getClassPath(project);
+        final var files = ScannedDependency.getClassPath(project);
         assertEquals(2, files.size());
         assertTrue(files.contains(file) && files.contains(file2));
     }
 
     @Test
-    public void findYangFilesInDependenciesAsStream() throws Exception {
-        final MavenProject project = mock(MavenProject.class);
+    void findYangFilesInDependenciesAsStream() throws Exception {
         prepareProject(project);
 
-        final Collection<ScannedDependency> yangzip = ScannedDependency.scanDependencies(project);
+        final var yangzip = ScannedDependency.scanDependencies(project);
         assertNotNull(yangzip);
         assertEquals(2, yangzip.size());
     }
 
     @Test
-    public void findYangFilesInDependencies() throws Exception {
-        final MavenProject project = mock(MavenProject.class);
+    void findYangFilesInDependencies() throws Exception {
         prepareProject(project);
 
-        final Collection<ScannedDependency> files = ScannedDependency.scanDependencies(project);
+        final var files = ScannedDependency.scanDependencies(project);
         assertNotNull(files);
         assertEquals(2, files.size());
     }
 
     private static void prepareProject(final MavenProject project) throws Exception {
-        final Manifest manifest = new Manifest();
+        final var manifest = new Manifest();
         manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
-        final File testFile2 = new File(ScannedDependencyTest.class.getResource("/").getPath(), "test.jar");
+        final var testFile2 = new File(ScannedDependencyTest.class.getResource("/").getPath(), "test.jar");
         try (var target = new JarOutputStream(new FileOutputStream(testFile2), manifest)) {
             addSourceFileToTargetJar(new File(ScannedDependencyTest.class.getResource("/tests/META-INF").getPath()),
                 target);
         }
 
-        final Artifact artifact = mock(Artifact.class);
+        final var artifact = mock(Artifact.class);
         doReturn(new File(ScannedDependencyTest.class.getResource("/tests").toURI())).when(artifact).getFile();
 
-        final Artifact artifact2 = mock(Artifact.class);
+        final var artifact2 = mock(Artifact.class);
         doReturn(testFile2).when(artifact2).getFile();
         doReturn(ImmutableSet.of(artifact, artifact2)).when(project).getArtifacts();
     }
@@ -95,18 +97,18 @@ public class ScannedDependencyTest {
                 if (!name.endsWith("/")) {
                     name += "/";
                 }
-                final JarEntry entry = new JarEntry(name);
+                final var entry = new JarEntry(name);
                 entry.setTime(source.lastModified());
                 target.putNextEntry(entry);
                 target.closeEntry();
             }
-            for (final File nestedFile : source.listFiles()) {
+            for (var nestedFile : source.listFiles()) {
                 addSourceFileToTargetJar(nestedFile, target);
             }
             return;
         }
 
-        final JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
+        final var entry = new JarEntry(source.getPath().replace("\\", "/"));
         entry.setTime(source.lastModified());
         target.putNextEntry(entry);
 
index 82b47cf05c9d5360e28123816e2e76d9e0270446..ce1d1fc0916e0db35efde97aeee59b05f39a6ce8 100644 (file)
@@ -21,13 +21,13 @@ import org.apache.maven.model.Dependency;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.repository.RepositorySystem;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
 
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class YangToSourcesMojoTest {
+@ExtendWith(MockitoExtension.class)
+class YangToSourcesMojoTest {
     @Mock
     private MavenProject project;
     @Mock
@@ -44,7 +44,7 @@ public class YangToSourcesMojoTest {
     private Dependency dep;
 
     @Test
-    public void checkClasspathTest() {
+    void checkClasspathTest() {
         final var artifacts = Set.of(artifact);
 
         doReturn(plugin).when(project).getPlugin(anyString());
index 47ba4eb7ff3f2bcdbbfa1c729802174c2565bd26..b9ee3f879bda23ef34349bcc19eb84bd400c7ee9 100644 (file)
@@ -7,7 +7,7 @@
  */
 package org.opendaylight.yangtools.yang2sources.plugin;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doAnswer;
 
@@ -15,41 +15,48 @@ import com.google.common.collect.ImmutableTable;
 import java.io.File;
 import java.util.List;
 import java.util.Set;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.yangtools.yang.model.api.Module;
 
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class YangToSourcesProcessorTest extends AbstractCodeGeneratorTest {
+@ExtendWith(MockitoExtension.class)
+class YangToSourcesProcessorTest extends AbstractCodeGeneratorTest {
     private final File file = new File(getClass().getResource("/yang").getFile());
 
     @Test
-    public void basicTest() {
-        assertMojoExecution(new YangToSourcesProcessor(file, List.of(),
-            List.of(new FileGeneratorArg("mockGenerator")), project, true,
-            YangProvider.getInstance()), mock -> {
+    void basicTest() {
+        assertMojoExecution(
+            new YangToSourcesProcessor(file, List.of(), List.of(new FileGeneratorArg("mockGenerator")), project, true,
+                YangProvider.getInstance()),
+            mock -> {
                 doAnswer(invocation -> {
                     final Set<Module> localModules = invocation.getArgument(1);
                     assertEquals(2, localModules.size());
                     return ImmutableTable.of();
                 }).when(mock).generateFiles(any(), any(), any());
-            }, mock -> { });
+            },
+            mock -> {
+                // No-op
+            });
     }
 
     @Test
-    public void excludeFilesTest() throws Exception {
-        final File excludedYang = new File(getClass().getResource("/yang/excluded-file.yang").getFile());
+    void excludeFilesTest() throws Exception {
+        final var excludedYang = new File(getClass().getResource("/yang/excluded-file.yang").getFile());
 
-        assertMojoExecution(new YangToSourcesProcessor(file, List.of(excludedYang),
-            List.of(new FileGeneratorArg("mockGenerator")), project, true,
-            YangProvider.getInstance()), mock -> {
+        assertMojoExecution(
+            new YangToSourcesProcessor(file, List.of(excludedYang), List.of(new FileGeneratorArg("mockGenerator")),
+                project, true, YangProvider.getInstance()),
+            mock -> {
                 doAnswer(invocation -> {
                     final Set<Module> localModules = invocation.getArgument(1);
                     assertEquals(1, localModules.size());
                     assertEquals("mock", localModules.iterator().next().getName());
                     return ImmutableTable.of();
                 }).when(mock).generateFiles(any(), any(), any());
-            }, mock -> { });
+            }, mock -> {
+                // No-op
+            });
     }
 }