Native version test

This commit is contained in:
Peter Palaga
2020-07-29 20:25:16 +02:00
parent 8fd2234f57
commit 5d46afa55c
3 changed files with 96 additions and 53 deletions

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.fuse.mvnd.it;
import java.io.IOException;
import javax.inject.Inject;
import org.assertj.core.api.Assertions;
import org.jboss.fuse.mvnd.assertj.MatchInOrderAmongOthers;
import org.jboss.fuse.mvnd.client.Client;
import org.jboss.fuse.mvnd.client.ClientLayout;
import org.jboss.fuse.mvnd.client.ClientOutput;
import org.jboss.fuse.mvnd.junit.MvndNativeTest;
import org.jboss.fuse.mvnd.junit.MvndTestExtension;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
@MvndNativeTest(projectDir = "src/test/projects/single-module")
public class VersionNativeIT {
@Inject
Client client;
@Inject
ClientLayout layout;
@Test
void version() throws IOException, InterruptedException {
final ClientOutput output = Mockito.mock(ClientOutput.class);
client.execute(output, "-v").assertSuccess();
final ArgumentCaptor<String> logMessage = ArgumentCaptor.forClass(String.class);
Mockito.verify(output, Mockito.atLeast(1)).accept(logMessage.capture());
Assertions.assertThat(logMessage.getAllValues())
.is(new MatchInOrderAmongOthers<>(
"\\QMaven Daemon " + System.getProperty("project.version") + "\\E",
"\\QMaven home: " + layout.mavenHome() + "\\E"));
}
}

View File

@@ -15,39 +15,10 @@
*/
package org.jboss.fuse.mvnd.it;
import java.io.IOException;
import javax.inject.Inject;
import org.assertj.core.api.Assertions;
import org.jboss.fuse.mvnd.assertj.MatchInOrderAmongOthers;
import org.jboss.fuse.mvnd.client.Client;
import org.jboss.fuse.mvnd.client.ClientLayout;
import org.jboss.fuse.mvnd.client.ClientOutput;
import org.jboss.fuse.mvnd.junit.MvndTest;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.jboss.fuse.mvnd.junit.MvndTestExtension;
@MvndTest(projectDir = "src/test/projects/single-module")
public class VersionTest {
public class VersionTest extends VersionNativeIT {
@Inject
Client client;
@Inject
ClientLayout layout;
@Test
void version() throws IOException, InterruptedException {
final ClientOutput output = Mockito.mock(ClientOutput.class);
client.execute(output, "-v").assertSuccess();
final ArgumentCaptor<String> logMessage = ArgumentCaptor.forClass(String.class);
Mockito.verify(output, Mockito.atLeast(1)).accept(logMessage.capture());
Assertions.assertThat(logMessage.getAllValues())
.is(new MatchInOrderAmongOthers<>(
"\\QMaven Daemon " + System.getProperty("project.version") + "\\E",
"\\QMaven home: " + layout.mavenHome() + "\\E"));
}
}

View File

@@ -40,6 +40,8 @@ import org.junit.jupiter.api.extension.ExtensionContext.Store;
public class MvndTestExtension implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback {
/** A placeholder to replace with a temporary directory outside of the current source tree */
public static final String TEMP_EXTERNAL = "${temp.external}";
private volatile Exception bootException;
public MvndTestExtension() {
@@ -54,11 +56,18 @@ public class MvndTestExtension implements BeforeAllCallback, BeforeEachCallback,
final MvndTest mnvdTest = testClass.getAnnotation(MvndTest.class);
if (mnvdTest != null) {
store.put(MvndResource.class.getName(),
MvndResource.create(context.getRequiredTestClass().getSimpleName(), mnvdTest.projectDir(), false, -1L));
MvndResource.create(
context.getRequiredTestClass().getSimpleName(),
mnvdTest.projectDir(),
false,
-1L));
} else {
final MvndNativeTest mvndNativeTest = testClass.getAnnotation(MvndNativeTest.class);
store.put(MvndResource.class.getName(),
MvndResource.create(context.getRequiredTestClass().getSimpleName(), mvndNativeTest.projectDir(), true,
MvndResource.create(
context.getRequiredTestClass().getSimpleName(),
mvndNativeTest.projectDir(),
true,
mvndNativeTest.timeoutSec() * 1000L));
}
} catch (Exception e) {
@@ -126,28 +135,37 @@ public class MvndTestExtension implements BeforeAllCallback, BeforeEachCallback,
if (rawProjectDir == null) {
throw new IllegalStateException("rawProjectDir of @MvndTest must be set");
}
final Path mvndTestSrcDir = Paths.get(rawProjectDir).toAbsolutePath().normalize();
if (!Files.exists(mvndTestSrcDir)) {
throw new IllegalStateException("@MvndTest(projectDir = \"" + rawProjectDir
+ "\") points at a path that does not exist: " + mvndTestSrcDir);
}
final Path testDir = Paths.get("target/mvnd-tests/" + className).toAbsolutePath();
final Path testExecutionDir = testDir.resolve("project");
try (Stream<Path> files = Files.walk(mvndTestSrcDir)) {
files.forEach(source -> {
final Path dest = testExecutionDir.resolve(mvndTestSrcDir.relativize(source));
try {
if (Files.isDirectory(source)) {
Files.createDirectories(dest);
} else {
Files.createDirectories(dest.getParent());
Files.copy(source, dest);
Files.createDirectories(testDir);
final Path testExecutionDir;
if (TEMP_EXTERNAL.equals(rawProjectDir)) {
try {
testExecutionDir = Files.createTempDirectory(MvndTestExtension.class.getSimpleName());
} catch (IOException e) {
throw new RuntimeException("Could not create temporary directory", e);
}
} else {
final Path mvndTestSrcDir = Paths.get(rawProjectDir).toAbsolutePath().normalize();
if (!Files.exists(mvndTestSrcDir)) {
throw new IllegalStateException("@MvndTest(projectDir = \"" + mvndTestSrcDir
+ "\") points at a path that does not exist: " + mvndTestSrcDir);
}
testExecutionDir = testDir.resolve("project");
try (Stream<Path> files = Files.walk(mvndTestSrcDir)) {
files.forEach(source -> {
final Path dest = testExecutionDir.resolve(mvndTestSrcDir.relativize(source));
try {
if (Files.isDirectory(source)) {
Files.createDirectories(dest);
} else {
Files.createDirectories(dest.getParent());
Files.copy(source, dest);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
});
}
}
final Path mvndHome = Paths