From cbe2b2238ff721bdf3b01a4a5b531548129b908c Mon Sep 17 00:00:00 2001 From: Vaadin Bot Date: Wed, 20 Nov 2024 10:42:29 +0100 Subject: [PATCH] test: refactor mock setup in maven plugin tests (#2911) (CP: 24.4) (#2913) test: refactor mock setup in maven plugin tests (#2911) Refactoring required by changes in vaadin/flow#20465. Co-authored-by: Marco Collovati Co-authored-by: Zhe Sun <31067185+ZheSun88@users.noreply.github.com> Co-authored-by: Soroosh Taefi --- .../vaadin/hilla/maven/AbstractMojoTest.java | 50 +++++++++++-------- .../hilla/maven/EngineGenerateMojoTest.java | 8 ++- .../maven/hilla-engine-configuration.json | 2 +- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/packages/java/maven-plugin/src/test/java/com/vaadin/hilla/maven/AbstractMojoTest.java b/packages/java/maven-plugin/src/test/java/com/vaadin/hilla/maven/AbstractMojoTest.java index 69e14cb728..2d95f77a97 100644 --- a/packages/java/maven-plugin/src/test/java/com/vaadin/hilla/maven/AbstractMojoTest.java +++ b/packages/java/maven-plugin/src/test/java/com/vaadin/hilla/maven/AbstractMojoTest.java @@ -1,25 +1,25 @@ package com.vaadin.hilla.maven; -import static org.junit.jupiter.api.Assertions.assertNotNull; - import java.io.File; import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; -import java.util.List; import java.util.Objects; +import java.util.Set; +import com.vaadin.hilla.engine.EngineConfiguration; +import com.vaadin.hilla.parser.testutils.TestEngineConfigurationPathResolver; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.model.Build; import org.apache.maven.plugin.Mojo; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.project.MavenProject; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.mockito.Mockito; -import com.vaadin.hilla.engine.EngineConfiguration; -import com.vaadin.hilla.parser.testutils.TestEngineConfigurationPathResolver; +import static org.junit.jupiter.api.Assertions.assertNotNull; /** * Base class for Engine Maven plugin tests. Delegates to @@ -48,22 +48,9 @@ public void setUpMojoTest() throws Exception { buildDirectory.resolve("test-classes/com/vaadin/hilla")); Files.createFile(buildDirectory.resolve( "test-classes/com/vaadin/hilla/EndpointController.class")); + // Maven project is not initialized on the mojo, setup a mock manually - project = Mockito.mock(MavenProject.class); - // Using Path.of here to have correct separators at Windows & Unix - var classPathElements = List - .of(buildDirectory.resolve("test-classes").toString()); - Mockito.doReturn(classPathElements).when(project) - .getCompileClasspathElements(); - Mockito.doReturn(classPathElements).when(project) - .getRuntimeClasspathElements(); - Mockito.doReturn(getTemporaryDirectory().toFile()).when(project) - .getBasedir(); - var mockBuild = Mockito.mock(Build.class); - Mockito.doReturn("build").when(mockBuild).getDirectory(); - Mockito.doReturn(Path.of("build/classes").toString()).when(mockBuild) - .getOutputDirectory(); - Mockito.doReturn(mockBuild).when(project).getBuild(); + project = createMavenProject(); var configFilePath = getBuildDirectory() .resolve(EngineConfiguration.DEFAULT_CONFIG_FILE_NAME); @@ -154,4 +141,25 @@ protected void tearDown() throws Exception { super.tearDown(); } } + + MavenProject createMavenProject() { + MavenProject project = new MavenProject(); + project.setGroupId("com.vaadin.testing"); + project.setArtifactId("my-application"); + project.setFile(temporaryDirectory.resolve("pom.xml").toFile()); + project.setBuild(new Build()); + project.getBuild().setFinalName("finalName"); + project.getBuild().setDirectory("build"); + project.getBuild().setOutputDirectory("build/classes"); + + DefaultArtifactHandler artifactHandler = new DefaultArtifactHandler(); + artifactHandler.setAddedToClasspath(true); + DefaultArtifact artifact = new DefaultArtifact( + "com.vaadin.hilla.testing", "mock-hilla", "1.0", "compile", + "jar", null, artifactHandler); + artifact.setFile(buildDirectory.resolve("test-classes").toFile()); + project.setArtifacts(Set.of(artifact)); + return project; + } + } diff --git a/packages/java/maven-plugin/src/test/java/com/vaadin/hilla/maven/EngineGenerateMojoTest.java b/packages/java/maven-plugin/src/test/java/com/vaadin/hilla/maven/EngineGenerateMojoTest.java index 9ee90cfae1..28a5f0f397 100644 --- a/packages/java/maven-plugin/src/test/java/com/vaadin/hilla/maven/EngineGenerateMojoTest.java +++ b/packages/java/maven-plugin/src/test/java/com/vaadin/hilla/maven/EngineGenerateMojoTest.java @@ -36,8 +36,12 @@ public void should_RunParserAndGenerator() throws Exception { assertInstanceOf(URLClassLoader.class, classLoader); assertEquals(classLoader.getParent(), EngineGenerateMojo.class.getClassLoader()); - assertArrayEquals(new URL[] { getTemporaryDirectory() - .resolve("build/test-classes").toUri().toURL() }, + assertArrayEquals( + new URL[] { getTemporaryDirectory() + .resolve("build/classes").toUri().toURL(), + getTemporaryDirectory() + .resolve("build/test-classes") + .toUri().toURL() }, ((URLClassLoader) classLoader).getURLs()); }); var mockedConstructionGenerator = Mockito.mockConstruction( diff --git a/packages/java/maven-plugin/src/test/resources/com/vaadin/hilla/maven/hilla-engine-configuration.json b/packages/java/maven-plugin/src/test/resources/com/vaadin/hilla/maven/hilla-engine-configuration.json index 984e52e6d8..0cf376d7dc 100644 --- a/packages/java/maven-plugin/src/test/resources/com/vaadin/hilla/maven/hilla-engine-configuration.json +++ b/packages/java/maven-plugin/src/test/resources/com/vaadin/hilla/maven/hilla-engine-configuration.json @@ -1,6 +1,6 @@ { "baseDir": "base", - "classPath": ["build/test-classes"], + "classPath": ["build/classes","build/test-classes"], "generator": { "plugins": { "use": [