diff --git a/java-reporter-core/pom.xml b/java-reporter-core/pom.xml index 3faaccc..bb604de 100644 --- a/java-reporter-core/pom.xml +++ b/java-reporter-core/pom.xml @@ -7,7 +7,7 @@ io.testomat java-reporter-core - 0.11.4 + 0.11.5 jar Testomat.io Reporter Core diff --git a/java-reporter-core/src/main/java/io/testomat/core/constants/CommonConstants.java b/java-reporter-core/src/main/java/io/testomat/core/constants/CommonConstants.java index ddd2b65..7738be8 100644 --- a/java-reporter-core/src/main/java/io/testomat/core/constants/CommonConstants.java +++ b/java-reporter-core/src/main/java/io/testomat/core/constants/CommonConstants.java @@ -1,7 +1,7 @@ package io.testomat.core.constants; public class CommonConstants { - public static final String REPORTER_VERSION = "0.11.4"; + public static final String REPORTER_VERSION = "0.11.5"; public static final String TESTS_STRING = "tests"; public static final String API_KEY_STRING = "api_key"; diff --git a/java-reporter-cucumber/pom.xml b/java-reporter-cucumber/pom.xml index 2af9d6c..3d8526c 100644 --- a/java-reporter-cucumber/pom.xml +++ b/java-reporter-cucumber/pom.xml @@ -51,7 +51,7 @@ io.testomat java-reporter-core - 0.11.4 + 0.11.5 org.slf4j diff --git a/java-reporter-junit/pom.xml b/java-reporter-junit/pom.xml index b1c23fe..095a5ab 100644 --- a/java-reporter-junit/pom.xml +++ b/java-reporter-junit/pom.xml @@ -51,7 +51,7 @@ io.testomat java-reporter-core - 0.11.4 + 0.11.5 org.slf4j diff --git a/java-reporter-karate/pom.xml b/java-reporter-karate/pom.xml index 25439c6..d7a8ef5 100644 --- a/java-reporter-karate/pom.xml +++ b/java-reporter-karate/pom.xml @@ -52,7 +52,7 @@ io.testomat java-reporter-core - 0.11.4 + 0.11.5 io.karatelabs diff --git a/java-reporter-testng/pom.xml b/java-reporter-testng/pom.xml index 964c42b..03672af 100644 --- a/java-reporter-testng/pom.xml +++ b/java-reporter-testng/pom.xml @@ -47,7 +47,7 @@ io.testomat java-reporter-core - 0.11.4 + 0.11.5 org.slf4j diff --git a/testomat-allure-adapter/pom.xml b/testomat-allure-adapter/pom.xml index 087e9a2..9cd95c4 100644 --- a/testomat-allure-adapter/pom.xml +++ b/testomat-allure-adapter/pom.xml @@ -66,7 +66,7 @@ io.testomat java-reporter-core - 0.11.4 + 0.11.5 io.qameta.allure diff --git a/testomat-allure-adapter/src/main/java/io/testomat/resolver/AttachmentFileResolverImpl.java b/testomat-allure-adapter/src/main/java/io/testomat/resolver/AttachmentFileResolverImpl.java index 62bbd09..0979d53 100644 --- a/testomat-allure-adapter/src/main/java/io/testomat/resolver/AttachmentFileResolverImpl.java +++ b/testomat-allure-adapter/src/main/java/io/testomat/resolver/AttachmentFileResolverImpl.java @@ -1,23 +1,25 @@ package io.testomat.resolver; import java.io.IOException; +import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Properties; import java.util.stream.Stream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Resolves attachment file path in Allure results directory by UUID. */ public class AttachmentFileResolverImpl implements AttachmentFileResolver { + private static final Logger log = LoggerFactory.getLogger(AttachmentFileResolverImpl.class); private final String resultsDir; public AttachmentFileResolverImpl() { - this.resultsDir = System.getProperty( - "allure.results.directory", - "allure-results" - ); + this.resultsDir = resolveAllurePath(); } public AttachmentFileResolverImpl(String resultsDir) { @@ -32,16 +34,49 @@ public AttachmentFileResolverImpl(String resultsDir) { */ @Override public String find(String uuid) throws IOException { - try (Stream paths = Files.list(Paths.get(resultsDir))) { + if (uuid == null || uuid.isBlank()) { + return null; + } + + Path dir = Paths.get(resultsDir); + + if (!Files.exists(dir)) { + return null; + } + try (Stream paths = Files.list(dir)) { return paths .filter(p -> - p.getFileName() - .toString() - .startsWith(uuid)) + p.getFileName().toString().startsWith(uuid) + && p.getFileName().toString().contains("-attachment")) .map(Path::toString) .findFirst() .orElse(null); } } + + private String resolveAllurePath() { + String systemProperty = System.getProperty("allure.results.directory"); + if (systemProperty != null && !systemProperty.isBlank()) { + return systemProperty; + } + + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + + try (InputStream inputStream = classLoader.getResourceAsStream("allure.properties")) { + if (inputStream != null) { + Properties props = new Properties(); + props.load(inputStream); + + String result = props.getProperty("allure.results.directory"); + if (result != null && !result.isBlank()) { + return result; + } + } + } catch (IOException e) { + log.trace("Failed to read allure.properties", e); + } + + return "allure-results"; + } } diff --git a/testomat-allure-adapter/src/test/java/resolver/AttachmentFileResolverImplTest.java b/testomat-allure-adapter/src/test/java/resolver/AttachmentFileResolverImplTest.java index 5b2d3b3..a8ff4cf 100644 --- a/testomat-allure-adapter/src/test/java/resolver/AttachmentFileResolverImplTest.java +++ b/testomat-allure-adapter/src/test/java/resolver/AttachmentFileResolverImplTest.java @@ -36,4 +36,104 @@ void shouldHandleEmptyDirectory() throws IOException { assertThat(result).isNull(); } + @Test + void shouldFindCorrectAttachmentAmongMultipleFiles() throws IOException { + String uuid = "123"; + + Files.createFile(tempDir.resolve(uuid + "-attachment.txt")); + Files.createFile(tempDir.resolve(uuid + "-other.txt")); // не должен попасть + Files.createFile(tempDir.resolve("999-attachment.txt")); // другой uuid + + AttachmentFileResolverImpl resolver = + new AttachmentFileResolverImpl(tempDir.toString()); + + String result = resolver.find(uuid); + + assertThat(result).contains(uuid + "-attachment.txt"); + } + + @Test + void shouldIgnoreFilesWithoutAttachmentSuffix() throws IOException { + String uuid = "123"; + + Files.createFile(tempDir.resolve(uuid + "-log.txt")); + + AttachmentFileResolverImpl resolver = + new AttachmentFileResolverImpl(tempDir.toString()); + + String result = resolver.find(uuid); + + assertThat(result).isNull(); + } + + @Test + void shouldNotMatchDifferentUuid() throws IOException { + Files.createFile(tempDir.resolve("999-attachment.txt")); + + AttachmentFileResolverImpl resolver = + new AttachmentFileResolverImpl(tempDir.toString()); + + String result = resolver.find("123"); + + assertThat(result).isNull(); + } + + @Test + void shouldReturnNullForNullUuid() throws IOException { + AttachmentFileResolverImpl resolver = + new AttachmentFileResolverImpl(tempDir.toString()); + + String result = resolver.find(null); + + assertThat(result).isNull(); + } + + @Test + void shouldReturnNullForBlankUuid() throws IOException { + AttachmentFileResolverImpl resolver = + new AttachmentFileResolverImpl(tempDir.toString()); + + String result = resolver.find(" "); + + assertThat(result).isNull(); + } + + @Test + void shouldReturnNullIfDirectoryDoesNotExist() throws IOException { + AttachmentFileResolverImpl resolver = + new AttachmentFileResolverImpl("non-existing-dir"); + + String result = resolver.find("123"); + + assertThat(result).isNull(); + } + + @Test + void shouldReturnOneOfMatchingFiles() throws IOException { + String uuid = "123"; + + Path file1 = Files.createFile(tempDir.resolve(uuid + "-attachment1.txt")); + Path file2 = Files.createFile(tempDir.resolve(uuid + "-attachment2.txt")); + + AttachmentFileResolverImpl resolver = + new AttachmentFileResolverImpl(tempDir.toString()); + + String result = resolver.find(uuid); + + assertThat(result).isIn(file1.toString(), file2.toString()); + } + + @Test + void shouldUseSystemProperty() { + System.setProperty("allure.results.directory", "custom-dir"); + + AttachmentFileResolverImpl resolver = new AttachmentFileResolverImpl(); + + assertThat(resolver) + .extracting("resultsDir") + .isEqualTo("custom-dir"); + + System.clearProperty("allure.results.directory"); + } + } \ No newline at end of file