|
@@ -16,6 +16,7 @@ import java.nio.file.Paths;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.function.Predicate;
|
|
|
import java.util.stream.Stream;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
import org.junit.jupiter.api.extension.ExtensionContext;
|
|
@@ -27,26 +28,39 @@ import org.mockito.Mock;
|
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
|
|
|
@ExtendWith(MockitoExtension.class)
|
|
|
+// todo split JarFileDetectorTest and JarServiceNameDetectorTest
|
|
|
class JarServiceNameDetectorTest {
|
|
|
|
|
|
@Mock ConfigProperties config;
|
|
|
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ JarPathFinder.resetForTest();
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
void createResource_empty() {
|
|
|
- JarServiceNameDetector serviceNameProvider =
|
|
|
- new JarServiceNameDetector(
|
|
|
- () -> new String[0], prop -> null, JarServiceNameDetectorTest::failPath);
|
|
|
+ String[] processArgs = new String[0];
|
|
|
+ Function<String, String> getProperty = prop -> null;
|
|
|
+ Predicate<Path> fileExists = JarServiceNameDetectorTest::failPath;
|
|
|
+ JarServiceNameDetector serviceNameProvider = getDetector(processArgs, getProperty, fileExists);
|
|
|
|
|
|
Resource resource = serviceNameProvider.createResource(config);
|
|
|
|
|
|
assertThat(resource.getAttributes()).isEmpty();
|
|
|
}
|
|
|
|
|
|
+ private static JarServiceNameDetector getDetector(
|
|
|
+ String[] processArgs, Function<String, String> getProperty, Predicate<Path> fileExists) {
|
|
|
+ return new JarServiceNameDetector(
|
|
|
+ new JarPathFinder(() -> processArgs, getProperty, fileExists));
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
void createResource_noJarFileInArgs() {
|
|
|
String[] args = new String[] {"-Dtest=42", "-Xmx666m", "-jar"};
|
|
|
JarServiceNameDetector serviceNameProvider =
|
|
|
- new JarServiceNameDetector(() -> args, prop -> null, JarServiceNameDetectorTest::failPath);
|
|
|
+ getDetector(args, prop -> null, JarServiceNameDetectorTest::failPath);
|
|
|
|
|
|
Resource resource = serviceNameProvider.createResource(config);
|
|
|
|
|
@@ -55,10 +69,8 @@ class JarServiceNameDetectorTest {
|
|
|
|
|
|
@Test
|
|
|
void createResource_processHandleJar() {
|
|
|
- String path = Paths.get("path", "to", "app", "my-service.jar").toString();
|
|
|
- String[] args = new String[] {"-Dtest=42", "-Xmx666m", "-jar", path, "abc", "def"};
|
|
|
JarServiceNameDetector serviceNameProvider =
|
|
|
- new JarServiceNameDetector(() -> args, prop -> null, JarServiceNameDetectorTest::failPath);
|
|
|
+ getDetector(getArgs("my-service.jar"), prop -> null, JarServiceNameDetectorTest::failPath);
|
|
|
|
|
|
Resource resource = serviceNameProvider.createResource(config);
|
|
|
|
|
@@ -69,10 +81,8 @@ class JarServiceNameDetectorTest {
|
|
|
|
|
|
@Test
|
|
|
void createResource_processHandleJarWithoutExtension() {
|
|
|
- String path = Paths.get("path", "to", "app", "my-service.jar").toString();
|
|
|
- String[] args = new String[] {"-Dtest=42", "-Xmx666m", "-jar", path};
|
|
|
JarServiceNameDetector serviceNameProvider =
|
|
|
- new JarServiceNameDetector(() -> args, prop -> null, JarServiceNameDetectorTest::failPath);
|
|
|
+ getDetector(getArgs("my-service"), prop -> null, JarServiceNameDetectorTest::failPath);
|
|
|
|
|
|
Resource resource = serviceNameProvider.createResource(config);
|
|
|
|
|
@@ -81,6 +91,11 @@ class JarServiceNameDetectorTest {
|
|
|
.containsEntry(ResourceAttributes.SERVICE_NAME, "my-service");
|
|
|
}
|
|
|
|
|
|
+ static String[] getArgs(String jarName) {
|
|
|
+ String path = Paths.get("path", "to", "app", jarName).toString();
|
|
|
+ return new String[] {"-Dtest=42", "-Xmx666m", "-jar", path, "abc", "def"};
|
|
|
+ }
|
|
|
+
|
|
|
@ParameterizedTest
|
|
|
@ArgumentsSource(SunCommandLineProvider.class)
|
|
|
void createResource_sunCommandLine(String commandLine, Path jarPath) {
|
|
@@ -89,7 +104,7 @@ class JarServiceNameDetectorTest {
|
|
|
Predicate<Path> fileExists = jarPath::equals;
|
|
|
|
|
|
JarServiceNameDetector serviceNameProvider =
|
|
|
- new JarServiceNameDetector(() -> new String[0], getProperty, fileExists);
|
|
|
+ getDetector(new String[0], getProperty, fileExists);
|
|
|
|
|
|
Resource resource = serviceNameProvider.createResource(config);
|
|
|
|
|
@@ -107,7 +122,7 @@ class JarServiceNameDetectorTest {
|
|
|
Predicate<Path> fileExists = path -> false;
|
|
|
|
|
|
JarServiceNameDetector serviceNameProvider =
|
|
|
- new JarServiceNameDetector(() -> new String[0], getProperty, fileExists);
|
|
|
+ getDetector(new String[0], getProperty, fileExists);
|
|
|
|
|
|
Resource resource = serviceNameProvider.createResource(config);
|
|
|
|
|
@@ -128,7 +143,7 @@ class JarServiceNameDetectorTest {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static boolean failPath(Path file) {
|
|
|
+ static boolean failPath(Path file) {
|
|
|
throw new AssertionError("Unexpected call to Files.isRegularFile()");
|
|
|
}
|
|
|
}
|