Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHARED-866] don't throw a java.lang.Error when reading invalid or corrupt properties files #25

Merged
merged 1 commit into from
Apr 4, 2020
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
145 changes: 58 additions & 87 deletions src/main/java/org/apache/maven/shared/utils/PropertyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ public PropertyUtils()

/**
* @param url The URL which should be used to load the properties.
*
* @return The loaded properties.
*
* @deprecated As of 3.1.0, please use method {@link #loadOptionalProperties(java.net.URL)}. This method should not
* be used as it suppresses exceptions silently when loading properties fails and returns {@code null} instead of an
* empty {@code Properties} instance when the given {@code URL} is {@code null}.
* be used as it suppresses exceptions silently when loading properties fails and returns {@code null}
* instead of an empty {@code Properties} instance when the given {@code URL} is {@code null}.
*/
@Deprecated
public static java.util.Properties loadProperties( @Nonnull URL url )
Expand All @@ -70,12 +68,10 @@ public static java.util.Properties loadProperties( @Nonnull URL url )

/**
* @param file The file from which the properties will be loaded.
*
* @return The loaded properties.
*
* @deprecated As of 3.1.0, please use method {@link #loadOptionalProperties(java.io.File)}. This method should not
* be used as it suppresses exceptions silently when loading properties fails and returns {@code null} instead of an
* empty {@code Properties} instance when the given {@code File} is {@code null}.
* be used as it suppresses exceptions silently when loading properties fails and returns {@code null}
* instead of an empty {@code Properties} instance when the given {@code File} is {@code null}.
*/
@Deprecated
public static Properties loadProperties( @Nonnull File file )
Expand All @@ -93,11 +89,9 @@ public static Properties loadProperties( @Nonnull File file )

/**
* @param is {@link InputStream}
*
* @return The loaded properties.
*
* @deprecated As of 3.1.0, please use method {@link #loadOptionalProperties(java.io.InputStream)}. This method
* should not be used as it suppresses exceptions silently when loading properties fails.
* should not be used as it suppresses exceptions silently when loading properties fails.
*/
@Deprecated
public static Properties loadProperties( @Nullable InputStream is )
Expand Down Expand Up @@ -133,121 +127,98 @@ public static Properties loadProperties( @Nullable InputStream is )
/**
* Loads {@code Properties} from a given {@code URL}.
* <p>
* If the given {@code URL} is not {@code null}, it is asserted to represent a valid and loadable properties
* resource.
* If the given {@code URL} is {@code null} or the properties can't be read, an empty properties object is returned.
* </p>
*
* @param url The {@code URL} of the properties resource to load or {@code null}.
*
* @return The loaded properties or an empty {@code Properties} instance if {@code url} is {@code null}.
*
* @param url the {@code URL} of the properties resource to load or {@code null}
* @return the loaded properties or an empty {@code Properties} instance if properties fail to load
* @since 3.1.0
*/
@Nonnull public static Properties loadOptionalProperties( final @Nullable URL url )
@Nonnull
public static Properties loadOptionalProperties( final @Nullable URL url )
{
InputStream in = null;
try
{
final Properties properties = new Properties();

if ( url != null )
Properties properties = new Properties();
if ( url != null )
{
try ( InputStream in = url.openStream() )
{
in = url.openStream();
properties.load( in );
in.close();
in = null;
}

return properties;
}
catch ( final IOException e )
{
throw new AssertionError( e );
}
finally
{
IOUtil.close( in );
catch ( IllegalArgumentException | IOException ex )
{
// ignore and return empty properties
}
}
return properties;
}

/**
* Loads {@code Properties} from a given {@code File}.
* <p>
* If the given {@code File} is not {@code null}, it is asserted to represent a valid and loadable properties
* resource.
* If the given {@code File} is {@code null} or the properties file can't be read, an empty properties object is
* returned.
* </p>
*
* @param file The {@code File} of the properties resource to load or {@code null}.
*
* @return The loaded properties or an empty {@code Properties} instance if {@code file} is {@code null}.
*
* @param file the {@code File} of the properties resource to load or {@code null}
* @return the loaded properties or an empty {@code Properties} instance if properties fail to load
* @since 3.1.0
*/
@Nonnull public static Properties loadOptionalProperties( final @Nullable File file )
@Nonnull
public static Properties loadOptionalProperties( final @Nullable File file )
{
InputStream in = null;
try
Properties properties = new Properties();
if ( file != null )
{
final Properties properties = new Properties();

if ( file != null )
try ( InputStream in = new FileInputStream( file ) )
{
in = new FileInputStream( file );
properties.load( in );
in.close();
in = null;
}

return properties;
}
catch ( final IOException e )
{
throw new AssertionError( e );
}
finally
{
IOUtil.close( in );
catch ( IllegalArgumentException | IOException ex )
{
// ignore and return empty properties
}
}

return properties;

}

/**
* Loads {@code Properties} from a given {@code InputStream}.
* <p>
* If the given {@code InputStream} is not {@code null}, it is asserted to represent a valid and loadable properties
* resource.
* If the given {@code InputStream} is {@code null} or the properties can't be read, an empty properties object is
* returned.
* </p>
*
* @param inputStream The {@code InputStream} of the properties resource to load or {@code null}.
*
* @return The loaded properties or an empty {@code Properties} instance if {@code inputStream} is {@code null}.
*
* @param inputStream the properties resource to load or {@code null}
* @return the loaded properties or an empty {@code Properties} instance if properties fail to load
* @since 3.1.0
*/
@Nonnull public static Properties loadOptionalProperties( final @Nullable InputStream inputStream )
@Nonnull
public static Properties loadOptionalProperties( final @Nullable InputStream inputStream )
{
InputStream in = null;
try
{
final Properties properties = new Properties();

if ( inputStream != null )
{
in = inputStream;
properties.load( in );
in.close();
in = null;
}
Properties properties = new Properties();

return properties;
}
catch ( final IOException e )
if ( inputStream != null )
{
throw new AssertionError( e );
}
finally
{
IOUtil.close( in );
try
{
properties.load( inputStream );
}
catch ( IllegalArgumentException | IOException ex )
{
// ignore and return empty properties
}
finally
{
IOUtil.close( inputStream );
}
}

return properties;

}

}
22 changes: 15 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 @@ -22,8 +22,10 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -38,7 +40,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

public class PropertyUtilsTest
{
Expand Down Expand Up @@ -67,6 +69,15 @@ public void loadOptionalNullInputStream()
{
assertThat( PropertyUtils.loadOptionalProperties( (InputStream) null ), is( new Properties() ) );
}


@Test
public void loadOptionalProperties_ioException()
throws Exception
{
URL url = new URL( "https://nonesuch12344.foo.bar.com" );
assertThat( PropertyUtils.loadOptionalProperties( url ), is( new Properties() ) );
}

@Test
@SuppressWarnings( "deprecation" )
Expand Down Expand Up @@ -137,8 +148,7 @@ public void loadEmptyURL()

@Test
@SuppressWarnings( "deprecation" )
public void loadValidInputStream()
throws Exception
public void loadValidInputStream() throws UnsupportedEncodingException
{
Properties value = new Properties();
value.setProperty( "a", "b" );
Expand All @@ -154,8 +164,7 @@ public void loadValidInputStream()
@Test
@NeedsTemporaryFolder
@SuppressWarnings( "deprecation" )
public void loadValidFile()
throws Exception
public void loadValidFile() throws IOException
{
OutputStream out = null;
try
Expand All @@ -179,8 +188,7 @@ public void loadValidFile()
@Test
@NeedsTemporaryFolder
@SuppressWarnings( "deprecation" )
public void loadValidURL()
throws Exception
public void loadValidURL() throws IOException
{
OutputStream out = null;
try
Expand Down