You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/10/13 00:24:38 UTC
[commons-configuration] branch master updated: [CONFIGURATION-794]
Unclosed file handle when reading config from JAR file URL.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git
The following commit(s) were added to refs/heads/master by this push:
new e0888d8 [CONFIGURATION-794] Unclosed file handle when reading config from JAR file URL.
e0888d8 is described below
commit e0888d8afc1598a0e2b801d34023ba81ede32ee3
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Oct 12 20:24:34 2020 -0400
[CONFIGURATION-794] Unclosed file handle when reading config from JAR
file URL.
- Add and use URLConnectionOptions.
---
src/changes/changes.xml | 6 +-
.../builder/FileBasedBuilderParametersImpl.java | 8 +
.../builder/FileBasedBuilderProperties.java | 14 ++
.../configuration2/io/DefaultFileSystem.java | 13 +-
.../commons/configuration2/io/FileHandler.java | 21 ++-
.../commons/configuration2/io/FileLocator.java | 109 ++++++++------
.../commons/configuration2/io/FileSystem.java | 70 +++++----
.../configuration2/io/URLConnectionOptions.java | 167 +++++++++++++++++++++
.../builder/TestFileBasedConfigurationBuilder.java | 73 ++++-----
9 files changed, 353 insertions(+), 128 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8a8ad13..04d6e0a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -24,10 +24,14 @@
<body>
<release version="2.8.0" date="2020-MM-DD"
description="Minor release with new features and updated dependencies.">
- <!-- ADD -->
+ <!-- ADD -->
<action type="update" dev="ggregory" due-to="SethiPandi">
Implement Iterable in ImmutableNode #74.
</action>
+ <action issues="CONFIGURATION-794" type="update" dev="ggregory" due-to="Robin Jansohn, Gary Gregory, Rob Spoor">
+ Unclosed file handle when reading config from JAR file URL.
+ Add and use FileBasedBuilderProperties.setURL(URL, URLConnectionOptions).
+ </action>
<!-- UPDATES -->
<action type="update" dev="ggregory" issue="CONFIGURATION-787" due-to="Gary Gregory">
Update from Apache Commons Lang 3.9 to 3.11.
diff --git a/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderParametersImpl.java b/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderParametersImpl.java
index 7b52431..b4c117b 100644
--- a/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderParametersImpl.java
+++ b/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderParametersImpl.java
@@ -23,6 +23,7 @@ import java.util.Map;
import org.apache.commons.configuration2.io.FileHandler;
import org.apache.commons.configuration2.io.FileLocationStrategy;
import org.apache.commons.configuration2.io.FileSystem;
+import org.apache.commons.configuration2.io.URLConnectionOptions;
/**
* <p>
@@ -327,4 +328,11 @@ public class FileBasedBuilderParametersImpl extends BasicBuilderParameters
getFileHandler().setURL(url);
return this;
}
+
+ @Override
+ public FileBasedBuilderParametersImpl setURL(final URL url, final URLConnectionOptions urlConnectionOptions)
+ {
+ getFileHandler().setURL(url, urlConnectionOptions);
+ return this;
+ }
}
diff --git a/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderProperties.java b/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderProperties.java
index 8cfc64b..e87d76c 100644
--- a/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderProperties.java
+++ b/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderProperties.java
@@ -21,6 +21,7 @@ import java.net.URL;
import org.apache.commons.configuration2.io.FileLocationStrategy;
import org.apache.commons.configuration2.io.FileSystem;
+import org.apache.commons.configuration2.io.URLConnectionOptions;
/**
* <p>
@@ -120,4 +121,17 @@ public interface FileBasedBuilderProperties<T>
* @return a reference to this object for method chaining
*/
T setURL(URL url);
+
+ /**
+ * Sets the location of the associated {@code FileHandler} as a {@code URL}
+ * object.
+ *
+ * @param url the {@code URL} location
+ * @param urlConnectionOptions options
+ * @return a reference to this object for method chaining
+ * @since 2.8.0
+ */
+ default T setURL(final URL url, final URLConnectionOptions urlConnectionOptions) {
+ return setURL(url);
+ }
}
diff --git a/src/main/java/org/apache/commons/configuration2/io/DefaultFileSystem.java b/src/main/java/org/apache/commons/configuration2/io/DefaultFileSystem.java
index 2201ba6..c8fa13c 100644
--- a/src/main/java/org/apache/commons/configuration2/io/DefaultFileSystem.java
+++ b/src/main/java/org/apache/commons/configuration2/io/DefaultFileSystem.java
@@ -30,14 +30,22 @@ import java.net.URLConnection;
import org.apache.commons.configuration2.ex.ConfigurationException;
/**
- * FileSystem that uses java.io.File or HttpClient
+ * FileSystem that uses java.io.File or HttpClient.
+ *
* @since 1.7
*/
public class DefaultFileSystem extends FileSystem
{
+
@Override
public InputStream getInputStream(final URL url) throws ConfigurationException
{
+ return getInputStream(url, null);
+ }
+
+ @Override
+ public InputStream getInputStream(final URL url, final URLConnectionOptions urlConnectionOptions) throws ConfigurationException
+ {
// throw an exception if the target URL is a directory
final File file = FileLocatorUtils.fileFromURL(url);
if (file != null && file.isDirectory())
@@ -47,7 +55,8 @@ public class DefaultFileSystem extends FileSystem
try
{
- return url.openStream();
+ return urlConnectionOptions == null ? url.openStream()
+ : urlConnectionOptions.openConnection(url).getInputStream();
}
catch (final Exception e)
{
diff --git a/src/main/java/org/apache/commons/configuration2/io/FileHandler.java b/src/main/java/org/apache/commons/configuration2/io/FileHandler.java
index 3c39d5f..4ffc787 100644
--- a/src/main/java/org/apache/commons/configuration2/io/FileHandler.java
+++ b/src/main/java/org/apache/commons/configuration2/io/FileHandler.java
@@ -869,7 +869,10 @@ public class FileHandler
try
{
- in = FileLocatorUtils.obtainFileSystem(locator).getInputStream(url);
+ final FileSystem obtainFileSystem = FileLocatorUtils.obtainFileSystem(locator);
+ final URLConnectionOptions urlConnectionOptions = locator.getURLConnectionOptions();
+ in = urlConnectionOptions == null ? obtainFileSystem.getInputStream(url)
+ : obtainFileSystem.getInputStream(url, urlConnectionOptions);
loadFromStream(in, locator.getEncoding(), url);
}
catch (final ConfigurationException e)
@@ -1558,12 +1561,28 @@ public class FileHandler
*/
public void setURL(final URL url)
{
+ setURL(url, URLConnectionOptions.DEFAULT);
+ }
+
+ /**
+ * Sets the location of the associated file as a URL. For loading this can
+ * be an arbitrary URL with a supported protocol. If the file is to be
+ * saved, too, a URL with the "file" protocol should be provided.
+ * This method sets the file name and the base path to <b>null</b>.
+ * They have to be determined anew based on the new URL.
+ *
+ * @param url the location of the file as URL
+ * @param urlConnectionOptions URL connection options
+ * @since 2.8.0
+ */
+ public void setURL(final URL url, final URLConnectionOptions urlConnectionOptions) {
new Updater()
{
@Override
protected void updateBuilder(final FileLocatorBuilder builder)
{
builder.sourceURL(url);
+ builder.urlConnectionOptions(urlConnectionOptions);
builder.basePath(null).fileName(null);
}
}
diff --git a/src/main/java/org/apache/commons/configuration2/io/FileLocator.java b/src/main/java/org/apache/commons/configuration2/io/FileLocator.java
index bb7932c..2257a97 100644
--- a/src/main/java/org/apache/commons/configuration2/io/FileLocator.java
+++ b/src/main/java/org/apache/commons/configuration2/io/FileLocator.java
@@ -17,10 +17,9 @@
package org.apache.commons.configuration2.io;
import java.net.URL;
+import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.builder.EqualsBuilder;
-import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
/**
@@ -66,24 +65,27 @@ public final class FileLocator
*/
public static final class FileLocatorBuilder
{
- /** The file name. */
- private String fileName;
-
/** The base path. */
private String basePath;
- /** The source URL. */
- private URL sourceURL;
-
/** The encoding. */
private String encoding;
+ /** The file name. */
+ private String fileName;
+
/** The file system. */
private FileSystem fileSystem;
/** The location strategy. */
private FileLocationStrategy locationStrategy;
+ /** The URL. */
+ private URL sourceURL;
+
+ /** The URL connection options. */
+ private URLConnectionOptions urlConnectionOptions;
+
/**
* Creates a new instance of {@code FileLocatorBuilder} and initializes
* the builder's properties from the passed in {@code FileLocator}
@@ -169,6 +171,7 @@ public final class FileLocator
basePath = src.getBasePath();
fileName = src.getFileName();
sourceURL = src.getSourceURL();
+ urlConnectionOptions = src.getURLConnectionOptions();
encoding = src.getEncoding();
fileSystem = src.getFileSystem();
locationStrategy = src.getLocationStrategy();
@@ -195,29 +198,44 @@ public final class FileLocator
*/
public FileLocatorBuilder sourceURL(final URL url)
{
- sourceURL = url;
+ this.sourceURL = url;
return this;
}
- }
- /** The file name. */
- private final String fileName;
+ /**
+ * Specifies the source URL connection options of the new {@code FileLocator}.
+ *
+ * @param urlConnectionOptions the source URL connection options.
+ * @return a reference to this builder for method chaining
+ */
+ public FileLocatorBuilder urlConnectionOptions(URLConnectionOptions urlConnectionOptions) {
+ this.urlConnectionOptions = urlConnectionOptions;
+ return this;
+
+ }
+ }
/** The base path. */
private final String basePath;
- /** The source URL. */
- private final URL sourceURL;
-
/** The encoding. */
private final String encoding;
+ /** The file name. */
+ private final String fileName;
+
/** The file system. */
private final FileSystem fileSystem;
/** The file location strategy. */
private final FileLocationStrategy locationStrategy;
+ /** The source URL. */
+ private final URL sourceURL;
+
+ /** The source URL connection options. */
+ private final URLConnectionOptions urlConnectionOptions;
+
/**
* Creates a new instance of {@code FileLocatorImpl} and initializes it from
* the given builder instance
@@ -229,6 +247,7 @@ public final class FileLocator
fileName = builder.fileName;
basePath = builder.basePath;
sourceURL = builder.sourceURL;
+ urlConnectionOptions = builder.urlConnectionOptions;
encoding = builder.encoding;
fileSystem = builder.fileSystem;
locationStrategy = builder.locationStrategy;
@@ -243,25 +262,18 @@ public final class FileLocator
* @return a flag whether these objects are equal
*/
@Override
- public boolean equals(final Object obj)
- {
- if (this == obj)
- {
+ public boolean equals(Object obj) {
+ if (this == obj) {
return true;
}
- if (!(obj instanceof FileLocator))
- {
+ if (!(obj instanceof FileLocator)) {
return false;
}
-
- final FileLocator c = (FileLocator) obj;
- return new EqualsBuilder().append(getFileName(), c.getFileName())
- .append(getBasePath(), c.getBasePath())
- .append(sourceURLAsString(), c.sourceURLAsString())
- .append(getEncoding(), c.getEncoding())
- .append(getFileSystem(), c.getFileSystem())
- .append(getLocationStrategy(), c.getLocationStrategy())
- .isEquals();
+ FileLocator other = (FileLocator) obj;
+ return Objects.equals(basePath, other.basePath) && Objects.equals(encoding, other.encoding)
+ && Objects.equals(fileName, other.fileName) && Objects.equals(fileSystem, other.fileSystem)
+ && Objects.equals(locationStrategy, other.locationStrategy) && Objects.equals(sourceURL, other.sourceURL)
+ && Objects.equals(urlConnectionOptions, other.urlConnectionOptions);
}
/**
@@ -333,17 +345,24 @@ public final class FileLocator
}
/**
+ * Returns the URLConnectionOptions
+ *
+ * @return the URLConnectionOptions
+ */
+ public URLConnectionOptions getURLConnectionOptions()
+ {
+ return urlConnectionOptions;
+ }
+
+ /**
* Returns a hash code for this object.
*
* @return a hash code for this object
*/
@Override
- public int hashCode()
- {
- return new HashCodeBuilder().append(getFileName())
- .append(getBasePath()).append(sourceURLAsString())
- .append(getEncoding()).append(getFileSystem())
- .append(getLocationStrategy()).toHashCode();
+ public int hashCode() {
+ return Objects.hash(basePath, encoding, fileName, fileSystem, locationStrategy, sourceURL,
+ urlConnectionOptions);
}
/**
@@ -358,20 +377,10 @@ public final class FileLocator
: StringUtils.EMPTY;
}
- /**
- * Returns a string representation of this object. This string contains the
- * values of all properties.
- *
- * @return a string for this object
- */
@Override
- public String toString()
- {
- return new ToStringBuilder(this).append("fileName", getFileName())
- .append("basePath", getBasePath())
- .append("sourceURL", sourceURLAsString())
- .append("encoding", getEncoding())
- .append("fileSystem", getFileSystem())
- .append("locationStrategy", getLocationStrategy()).toString();
+ public String toString() {
+ return "FileLocator [basePath=" + basePath + ", encoding=" + encoding + ", fileName=" + fileName
+ + ", fileSystem=" + fileSystem + ", locationStrategy=" + locationStrategy + ", sourceURL=" + sourceURL
+ + ", urlConnectionOptions=" + urlConnectionOptions + "]";
}
}
diff --git a/src/main/java/org/apache/commons/configuration2/io/FileSystem.java b/src/main/java/org/apache/commons/configuration2/io/FileSystem.java
index 8aa998b..2a9637a 100644
--- a/src/main/java/org/apache/commons/configuration2/io/FileSystem.java
+++ b/src/main/java/org/apache/commons/configuration2/io/FileSystem.java
@@ -39,6 +39,27 @@ public abstract class FileSystem
/** FileSystem options provider */
private volatile FileOptionsProvider optionsProvider;
+ public abstract String getBasePath(String path);
+
+ public abstract String getFileName(String path);
+
+ public FileOptionsProvider getFileOptionsProvider()
+ {
+ return this.optionsProvider;
+ }
+
+ public abstract InputStream getInputStream(URL url) throws ConfigurationException;
+
+ /**
+ * Not abstract for binary compatibility.
+ *
+ * @since 2.8.0
+ */
+ public InputStream getInputStream(final URL url, final URLConnectionOptions urlConnectionOptions)
+ throws ConfigurationException {
+ return getInputStream(url);
+ }
+
/**
* Returns the logger used by this FileSystem.
*
@@ -50,6 +71,25 @@ public abstract class FileSystem
return result != null ? result : DEFAULT_LOG;
}
+ public abstract OutputStream getOutputStream(File file) throws ConfigurationException;
+
+ public abstract OutputStream getOutputStream(URL url) throws ConfigurationException;
+
+ public abstract String getPath(File file, URL url, String basePath, String fileName);
+
+ public abstract URL getURL(String basePath, String fileName) throws MalformedURLException;
+
+ public abstract URL locateFromURL(String basePath, String fileName);
+
+ /**
+ * Set the FileOptionsProvider
+ * @param provider The FileOptionsProvider
+ */
+ public void setFileOptionsProvider(final FileOptionsProvider provider)
+ {
+ this.optionsProvider = provider;
+ }
+
/**
* Allows setting the logger to be used by this FileSystem. This
* method makes it possible for clients to exactly control logging behavior.
@@ -64,34 +104,4 @@ public abstract class FileSystem
{
this.log = log;
}
-
- /**
- * Set the FileOptionsProvider
- * @param provider The FileOptionsProvider
- */
- public void setFileOptionsProvider(final FileOptionsProvider provider)
- {
- this.optionsProvider = provider;
- }
-
- public FileOptionsProvider getFileOptionsProvider()
- {
- return this.optionsProvider;
- }
-
- public abstract InputStream getInputStream(URL url) throws ConfigurationException;
-
- public abstract OutputStream getOutputStream(URL url) throws ConfigurationException;
-
- public abstract OutputStream getOutputStream(File file) throws ConfigurationException;
-
- public abstract String getPath(File file, URL url, String basePath, String fileName);
-
- public abstract String getBasePath(String path);
-
- public abstract String getFileName(String path);
-
- public abstract URL locateFromURL(String basePath, String fileName);
-
- public abstract URL getURL(String basePath, String fileName) throws MalformedURLException;
}
diff --git a/src/main/java/org/apache/commons/configuration2/io/URLConnectionOptions.java b/src/main/java/org/apache/commons/configuration2/io/URLConnectionOptions.java
new file mode 100644
index 0000000..d62265a
--- /dev/null
+++ b/src/main/java/org/apache/commons/configuration2/io/URLConnectionOptions.java
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.configuration2.io;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Objects;
+
+/**
+ * Carries options and operates on {@link URLConnection}.
+ *
+ * @since 2.8.0
+ */
+public final class URLConnectionOptions {
+
+ /**
+ * Default options.
+ */
+ public static final URLConnectionOptions DEFAULT = new URLConnectionOptions();
+
+ private boolean allowUserInteraction;
+
+ private int connectTimeoutMillis;
+
+ private int readTimeoutMillis;
+
+ private boolean useCaches = true;
+
+ /**
+ * Constructs a new default instance.
+ */
+ public URLConnectionOptions() {
+ // Defaults initialized in declarations.
+ }
+
+ /**
+ * Constructs an instance with values from the given URLConnectionOptions.
+ *
+ * @param urlConnectionOptions the source
+ */
+ public URLConnectionOptions(final URLConnectionOptions urlConnectionOptions) {
+ this.allowUserInteraction = urlConnectionOptions.getAllowUserInteraction();
+ this.useCaches = urlConnectionOptions.getUseCaches();
+ this.connectTimeoutMillis = urlConnectionOptions.getConnectTimeoutMillis();
+ this.readTimeoutMillis = urlConnectionOptions.getReadTimeoutMillis();
+ }
+
+ /**
+ * Applies the options to the given connection.
+ *
+ * @param urlConnection the target connection.
+ * @return the given connection.
+ */
+ public URLConnection apply(final URLConnection urlConnection) {
+ urlConnection.setUseCaches(useCaches);
+ urlConnection.setConnectTimeout(connectTimeoutMillis);
+ urlConnection.setReadTimeout(readTimeoutMillis);
+ return urlConnection;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof URLConnectionOptions)) {
+ return false;
+ }
+ final URLConnectionOptions other = (URLConnectionOptions) obj;
+ return allowUserInteraction == other.allowUserInteraction && connectTimeoutMillis == other.connectTimeoutMillis
+ && readTimeoutMillis == other.readTimeoutMillis && useCaches == other.useCaches;
+ }
+
+ /**
+ * Gets whether to allow user interaction.
+ *
+ * @return whether to allow user interaction.
+ */
+ public boolean getAllowUserInteraction() {
+ return allowUserInteraction;
+ }
+
+ /**
+ * Gets the connect timeout.
+ *
+ * @return the connect timeout.
+ */
+ public int getConnectTimeoutMillis() {
+ return connectTimeoutMillis;
+ }
+
+ /**
+ * Gets the read timeout.
+ *
+ * @return the read timeout.
+ */
+ public int getReadTimeoutMillis() {
+ return readTimeoutMillis;
+ }
+
+ /**
+ * Whether to cache.
+ *
+ * @return Whether to cache.
+ */
+ public boolean getUseCaches() {
+ return useCaches;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(allowUserInteraction, connectTimeoutMillis, readTimeoutMillis, useCaches);
+ }
+
+ /**
+ * Opens a connection for the given URL with our options.
+ *
+ * @param url the URL to open
+ * @return A new connection
+ * @throws IOException if an I/O exception occurs.
+ */
+ public URLConnection openConnection(final URL url) throws IOException {
+ return apply(url.openConnection());
+ }
+
+ public URLConnectionOptions setAllowUserInteraction(final boolean allowUserInteraction) {
+ this.allowUserInteraction = allowUserInteraction;
+ return this;
+ }
+
+ public URLConnectionOptions setConnectTimeoutMillis(final int connectTimeoutMillis) {
+ this.connectTimeoutMillis = connectTimeoutMillis;
+ return this;
+ }
+
+ public URLConnectionOptions setReadTimeoutMillis(final int readTimeoutMillis) {
+ this.readTimeoutMillis = readTimeoutMillis;
+ return this;
+ }
+
+ public URLConnectionOptions setUseCaches(final boolean useCaches) {
+ this.useCaches = useCaches;
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return "URLConnectionOptions [allowUserInteraction=" + allowUserInteraction + ", connectTimeoutMillis="
+ + connectTimeoutMillis + ", readTimeoutMillis=" + readTimeoutMillis + ", useCaches=" + useCaches + "]";
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/commons/configuration2/builder/TestFileBasedConfigurationBuilder.java b/src/test/java/org/apache/commons/configuration2/builder/TestFileBasedConfigurationBuilder.java
index 31a6a77..5257f2d 100644
--- a/src/test/java/org/apache/commons/configuration2/builder/TestFileBasedConfigurationBuilder.java
+++ b/src/test/java/org/apache/commons/configuration2/builder/TestFileBasedConfigurationBuilder.java
@@ -51,6 +51,7 @@ import org.apache.commons.configuration2.io.FileHandler;
import org.apache.commons.configuration2.io.FileLocator;
import org.apache.commons.configuration2.io.FileLocatorUtils;
import org.apache.commons.configuration2.io.HomeDirectoryLocationStrategy;
+import org.apache.commons.configuration2.io.URLConnectionOptions;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
@@ -60,8 +61,7 @@ import org.junit.rules.TemporaryFolder;
* Test class for {@code FileBasedConfigurationBuilder}.
*
*/
-public class TestFileBasedConfigurationBuilder
-{
+public class TestFileBasedConfigurationBuilder {
/** Constant for a test property name. */
private static final String PROP = "testProperty";
@@ -75,31 +75,21 @@ public class TestFileBasedConfigurationBuilder
* @param value the value for the test property
* @return the File object pointing to the test file
*/
- private File createTestFile(final int value)
- {
+ private File createTestFile(final int value) {
Writer out = null;
File file;
- try
- {
+ try {
file = folder.newFile();
out = new FileWriter(file);
out.write(String.format("%s=%d", PROP, value));
- }
- catch (final IOException ioex)
- {
+ } catch (final IOException ioex) {
fail("Could not create test file: " + ioex);
return null; // cannot happen
- }
- finally
- {
- if (out != null)
- {
- try
- {
+ } finally {
+ if (out != null) {
+ try {
out.close();
- }
- catch (final IOException ioex)
- {
+ } catch (final IOException ioex) {
// ignore
}
}
@@ -111,47 +101,35 @@ public class TestFileBasedConfigurationBuilder
* Tests whether a configuration can be created if no location is set.
*/
@Test
- public void testGetConfigurationNoLocation() throws ConfigurationException
- {
+ public void testGetConfigurationNoLocation() throws ConfigurationException {
final Map<String, Object> params = new HashMap<>();
params.put("throwExceptionOnMissing", Boolean.TRUE);
- final FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
- new FileBasedConfigurationBuilder<>(
- PropertiesConfiguration.class, params);
+ final FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<>(
+ PropertiesConfiguration.class, params);
final PropertiesConfiguration conf = builder.getConfiguration();
assertTrue("Property not set", conf.isThrowExceptionOnMissing());
assertTrue("Not empty", conf.isEmpty());
}
/**
- * Tests whether a configuration is loaded from file if a location is
- * provided.
+ * Tests whether a configuration is loaded from file if a location is provided.
*/
@Test
- public void testGetConfigurationLoadFromFile()
- throws ConfigurationException
- {
+ public void testGetConfigurationLoadFromFile() throws ConfigurationException {
final File file = createTestFile(1);
- final FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
- new FileBasedConfigurationBuilder<>(
- PropertiesConfiguration.class)
- .configure(new FileBasedBuilderParametersImpl()
- .setFile(file));
+ final FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<>(
+ PropertiesConfiguration.class).configure(new FileBasedBuilderParametersImpl().setFile(file));
final PropertiesConfiguration config = builder.getConfiguration();
assertEquals("Not read from file", 1, config.getInt(PROP));
- assertSame("FileHandler not initialized", config, builder
- .getFileHandler().getContent());
+ assertSame("FileHandler not initialized", config, builder.getFileHandler().getContent());
}
/**
- * Tests whether a configuration is loaded from a JAR file if a location is
- * provided.
+ * Tests whether a configuration is loaded from a JAR file if a location is provided.
+ * CONFIGURATION-794: Unclosed file handle when reading config from JAR file URL.
*/
@Test
- @Ignore
- public void testGetConfigurationLoadFromJarFile()
- throws ConfigurationException, IOException
- {
+ public void testGetConfigurationLoadFromJarFile() throws ConfigurationException, IOException {
URL jarResourceUrl = getClass().getClassLoader().getResource("org/apache/commons/configuration2/test.jar");
assertNotNull(jarResourceUrl);
final Path testJar = Paths.get(folder.getRoot().getAbsolutePath(), "test.jar");
@@ -160,8 +138,14 @@ public class TestFileBasedConfigurationBuilder
}
URL url = new URL("jar:" + testJar.toUri() + "!/configuration.properties");
- final FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<>(
- PropertiesConfiguration.class).configure(new FileBasedBuilderParametersImpl().setURL(url));
+ //@formatter:off
+ final FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
+ new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
+ .configure(new FileBasedBuilderParametersImpl()
+ .setURL(url, new URLConnectionOptions().setUseCaches(false)));
+ //@formatter:off
+
+// CONFIGURATION-794
// the next line causes:
// java.lang.AssertionError: Unable to clean up temporary folder C:\Users\ggregory\AppData\Local\Temp\junit7789840233804508643
// at org.junit.Assert.fail(Assert.java:89)
@@ -187,6 +171,7 @@ public class TestFileBasedConfigurationBuilder
// at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
// at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
+ // builder contains the current FileHandler which loads the file.
final PropertiesConfiguration config = builder.getConfiguration();
assertEquals("Not read from file", 1, config.getInt(PROP));
assertSame("FileHandler not initialized", config, builder.getFileHandler().getContent());