Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor mock setup in maven plugin tests #2911

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"baseDir": "base",
"classPath": ["build/test-classes"],
"classPath": ["build/classes","build/test-classes"],
"generator": {
"plugins": {
"use": [
Expand Down