Skip to content
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
Expand Up @@ -47,7 +47,12 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackReader;
import java.math.BigInteger;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
Expand Down Expand Up @@ -311,7 +316,7 @@ private Step convert(final Path source,
//Copy test status details to each step set the same status
if (Objects.equals(status, testStatus)) {
current.setStatusMessage(message);
current.setStatusMessage(trace);
current.setStatusTrace(trace);
}
return current;
}
Expand Down Expand Up @@ -483,17 +488,17 @@ private Stream<TestSuiteResult> jsonFiles(final Path source) {
}

private Optional<TestSuiteResult> readXmlTestSuiteFile(final Path source) {
try (InputStream is = Files.newInputStream(source)) {
return Optional.of(xmlMapper.readValue(is, TestSuiteResult.class));
try (InputStream inputStream = Files.newInputStream(source)) {
return Optional.of(xmlMapper.readValue(inputStream, TestSuiteResult.class));
} catch (IOException e) {
LOGGER.error("Could not read xml result {}", source, e);
}
return Optional.empty();
}

private Optional<TestSuiteResult> readJsonTestSuiteFile(final Path source) {
try (InputStream is = Files.newInputStream(source)) {
return Optional.of(jsonMapper.readValue(is, TestSuiteResult.class));
try (InputStream inputStream = Files.newInputStream(source)) {
return Optional.of(jsonMapper.readValue(inputStream, TestSuiteResult.class));
} catch (IOException e) {
LOGGER.error("Could not read json result {}", source, e);
return Optional.empty();
Expand Down Expand Up @@ -537,20 +542,55 @@ private Map<String, String> processEnvironment(final Path directory) {
return environment;
}

private static Properties propertiesToMap(final Map<String, String> target) {
return new Properties() {
@Override
public Object put(final Object key, final Object value) {
return target.put((String) key, (String) value);
}
};
}

private Map<String, String> readEnvironmentPropertiesUtf8(final Path envPropsFile)
throws CharacterCodingException, IOException {
final Map<String, String> utf8Items = new LinkedHashMap<>();
final CharsetDecoder decoder = UTF_8.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);

try (InputStream envPropsStream = Files.newInputStream(envPropsFile);
InputStreamReader envPropsReader = new InputStreamReader(envPropsStream, decoder);
PushbackReader pushbackReader = new PushbackReader(envPropsReader, 1)) {

final int firstChar = pushbackReader.read();
if (firstChar != -1 && firstChar != '\uFEFF') {
pushbackReader.unread(firstChar);
}

propertiesToMap(utf8Items).load(pushbackReader);
return utf8Items;
}
}

private Map<String, String> processEnvironmentProperties(final Path directory) {
final Path envPropsFile = directory.resolve("environment.properties");
if (!Files.exists(envPropsFile)) {
return new LinkedHashMap<>();
}
try {
return readEnvironmentPropertiesUtf8(envPropsFile);
} catch (CharacterCodingException e) {
LOGGER.error("Failed to read {} as UTF-8, falling back to ISO-8859-1", envPropsFile, e);
} catch (IOException e) {
LOGGER.error("Could not read environment.properties file {}", envPropsFile, e);
return new LinkedHashMap<>();
}

final Map<String, String> items = new LinkedHashMap<>();
if (Files.exists(envPropsFile)) {
try (InputStream is = Files.newInputStream(envPropsFile)) {
new Properties() {
@Override
public Object put(final Object key, final Object value) {
return items.put((String) key, (String) value);
}
}.load(is);
} catch (IOException e) {
LOGGER.error("Could not read environments.properties file " + envPropsFile, e);
}
try (InputStream inputStream = Files.newInputStream(envPropsFile)) {
propertiesToMap(items).load(inputStream);
} catch (IOException e) {
LOGGER.error("Could not read environment.properties file {}", envPropsFile, e);
}
return items;
}
Expand All @@ -559,12 +599,15 @@ private Map<String, String> processEnvironmentXml(final Path directory) {
final Path envXmlFile = directory.resolve("environment.xml");
final Map<String, String> items = new LinkedHashMap<>();
if (Files.exists(envXmlFile)) {
try (InputStream fis = Files.newInputStream(envXmlFile)) {
xmlMapper.readValue(fis, ru.yandex.qatools.commons.model.Environment.class).getParameter().forEach(p ->
items.put(p.getKey(), p.getValue())
try (InputStream envXmlInputStream =
Files.newInputStream(envXmlFile)) {
xmlMapper
.readValue(envXmlInputStream, ru.yandex.qatools.commons.model.Environment.class)
.getParameter()
.forEach(p -> items.put(p.getKey(), p.getValue())
);
} catch (Exception e) {
LOGGER.error("Could not read environment.xml file " + envXmlFile.toAbsolutePath(), e);
LOGGER.error("Could not read environment.xml file {}", envXmlFile.toAbsolutePath(), e);
}
}
return items;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -355,6 +358,56 @@ void shouldPreserveContentTypeFromAttachment() throws IOException {
assertThat(attachments.get(0).getSource()).endsWith(".txt");
}

@SuppressWarnings("unchecked")
@Test
void shouldReadEnvironmentPropertiesUtf8() throws Exception {
writeBytes(
"test_executor=测试人员 A\n".getBytes(StandardCharsets.UTF_8));

final LaunchResults launchResults = process();
final Map<String, String> env = launchResults.getExtra(
Allure1Plugin.ENVIRONMENT_BLOCK_NAME,
(Supplier<Map<String, String>>) LinkedHashMap::new
);

assertThat(env).containsEntry("test_executor", "测试人员 A");
}

@SuppressWarnings("unchecked")
@Test
void shouldReadEnvironmentPropertiesUtf8WithBom() throws Exception {
final byte[] bom = new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
final byte[] content = "executor=测试人员 A\n".getBytes(StandardCharsets.UTF_8);
final byte[] bytes = new byte[bom.length + content.length];
System.arraycopy(bom, 0, bytes, 0, bom.length);
System.arraycopy(content, 0, bytes, bom.length, content.length);

writeBytes(bytes);

final LaunchResults launchResults = process();
final Map<String, String> env = launchResults.getExtra(
Allure1Plugin.ENVIRONMENT_BLOCK_NAME,
(Supplier<Map<String, String>>) LinkedHashMap::new
);

assertThat(env).containsEntry("executor", "测试人员 A");
assertThat(env).doesNotContainKey("\uFEFFexecutor");
}

@SuppressWarnings("unchecked")
@Test
void shouldReturnEmptyMapWhenUtf8DecodingFails() throws Exception {
writeBytes(
"name=café\n".getBytes(StandardCharsets.ISO_8859_1));

final LaunchResults launchResults = process();
final Map<String, String> env = launchResults.getExtra(
Allure1Plugin.ENVIRONMENT_BLOCK_NAME,
(Supplier<Map<String, String>>) LinkedHashMap::new
);

assertThat(env).isEmpty();
}

@Test
void shouldNotAllowInvalidCharactersInAttachmentSource() throws IOException {
Expand Down Expand Up @@ -424,6 +477,10 @@ private LaunchResults process(String... strings) throws IOException {
return resultsVisitor.getLaunchResults();
}

private void writeBytes(final byte[] bytes) throws IOException {
Files.write(directory.resolve("environment.properties"), bytes);
}

private void copyFile(Path dir, String resourceName, String fileName) throws IOException {
try (InputStream is = getClass().getClassLoader().getResourceAsStream(resourceName)) {
Files.copy(
Expand Down
Loading