Author: oheger
Date: Thu Dec  1 20:54:45 2016
New Revision: 1772267

URL: http://svn.apache.org/viewvc?rev=1772267&view=rev
Log:
[CONFIGURATION-641] Improved exception when loading PropertiesConfiguration.

It is now checked whether the locator has been correctly initialized.
If not, an exception with a helpful message is thrown.

Modified:
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java
    
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java?rev=1772267&r1=1772266&r2=1772267&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java
 Thu Dec  1 20:54:45 2016
@@ -1421,7 +1421,13 @@ public class PropertiesConfiguration ext
      */
     private void loadIncludeFile(String fileName) throws ConfigurationException
     {
-        assert locator != null : "Locator has not been set!";
+        if (locator == null)
+        {
+            throw new ConfigurationException("Load operation not properly "
+                    + "initialized! Do not call read(InputStream) directly,"
+                    + " but use a FileHandler to load a configuration.");
+        }
+
         URL url = locateIncludeFile(locator.getBasePath(), fileName);
         if (url == null)
         {

Modified: 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java?rev=1772267&r1=1772266&r2=1772267&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java
 (original)
+++ 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java
 Thu Dec  1 20:54:45 2016
@@ -17,29 +17,7 @@
 
 package org.apache.commons.configuration2;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
-import java.io.BufferedReader;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Reader;
-import java.io.StringReader;
-import java.io.StringWriter;
-import java.io.UnsupportedEncodingException;
-import java.io.Writer;
+import java.io.*;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
@@ -71,6 +49,9 @@ import org.junit.Test;
 import org.junit.Rule;
 import org.junit.rules.TemporaryFolder;
 
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.*;
+
 /**
  * Test for loading and saving properties files.
  *
@@ -1190,6 +1171,31 @@ public class TestPropertiesConfiguration
     }
 
     /**
+     * Tests a direct invocation of the read() method. This is not allowed
+     * because certain initializations have not been done. This test is
+     * related to CONFIGURATION-641.
+     */
+    @Test
+    public void testReadCalledDirectly() throws IOException
+    {
+        conf = new PropertiesConfiguration();
+        Reader in = new 
FileReader(ConfigurationAssert.getTestFile("test.properties"));
+        try
+        {
+            conf.read(in);
+            fail("No exception thrown!");
+        }
+        catch (ConfigurationException e)
+        {
+            assertThat(e.getMessage(), containsString("FileHandler"));
+        }
+        finally
+        {
+            in.close();
+        }
+    }
+
+    /**
      * Helper method for testing the content of a list with elements that
      * contain backslashes.
      *


Reply via email to