Skip to content
Closed
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
11 changes: 3 additions & 8 deletions src/main/java/org/apache/maven/shared/utils/PropertyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,7 @@ public static Properties loadProperties(@Nullable InputStream is) {
try {
Properties result = new Properties();
if (is != null) {
try (InputStream in = is) {
result.load(in);
} catch (IOException e) {
// ignore
}
result.load(is);
}
return result;
} catch (Exception e) {
Expand Down Expand Up @@ -168,9 +164,8 @@ public static Properties loadOptionalProperties(final @Nullable InputStream inpu
Properties properties = new Properties();

if (inputStream != null) {
try (InputStream in = inputStream) // reassign inputStream to autoclose
{
properties.load(in);
try {
properties.load(inputStream);
} catch (IllegalArgumentException | IOException ex) {
// ignore and return empty properties
}
Expand Down
27 changes: 20 additions & 7 deletions src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Properties;

Expand All @@ -50,7 +51,6 @@ public class PropertyUtilsTest {

@Test
@SuppressWarnings("deprecation")
// @ReproducesPlexusBug( "Should return null on error like url and file do" )
public void loadNullInputStream() {
assertEquals(new Properties(), PropertyUtils.loadProperties((InputStream) null));
}
Expand Down Expand Up @@ -110,13 +110,11 @@ public void loadEmptyFile() throws Exception {
public void loadEmptyURL() throws Exception {
assertEquals(
new Properties(),
PropertyUtils.loadProperties(
newFile(tempFolder, "empty").toURI().toURL()));
PropertyUtils.loadProperties(newFile(tempFolder, "empty").toURI().toURL()));

assertEquals(
new Properties(),
PropertyUtils.loadOptionalProperties(
newFile(tempFolder, "optional").toURI().toURL()));
PropertyUtils.loadOptionalProperties(newFile(tempFolder, "optional").toURI().toURL()));
}

@Test
Expand Down Expand Up @@ -154,11 +152,26 @@ public void loadValidURL() throws IOException {
try (OutputStream out = Files.newOutputStream(valid.toPath())) {
value.store(out, "a test");
assertEquals(value, PropertyUtils.loadProperties(valid.toURI().toURL()));
assertEquals(
value, PropertyUtils.loadOptionalProperties(valid.toURI().toURL()));
assertEquals(value, PropertyUtils.loadOptionalProperties(valid.toURI().toURL()));
}
}

@Test
@SuppressWarnings("deprecation")
public void streamIsNotClosedByLoadProperties() throws IOException {
ByteArrayInputStream stream = new ByteArrayInputStream("a=b".getBytes(StandardCharsets.ISO_8859_1));
PropertyUtils.loadProperties(stream);
assertEquals(0, stream.available());
}

@Test
@SuppressWarnings("deprecation")
public void streamIsNotClosedByLoadOptionalProperties() throws IOException {
ByteArrayInputStream stream = new ByteArrayInputStream("a=b".getBytes(StandardCharsets.ISO_8859_1));
PropertyUtils.loadOptionalProperties(stream);
assertEquals(0, stream.available());
}

private static File newFile(File parent, String child) throws IOException {
File result = new File(parent, child);
result.createNewFile();
Expand Down
Loading