You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by eb...@apache.org on 2007/05/22 12:49:26 UTC

svn commit: r540529 - in /jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration: FileURLStreamHandler.java TestPropertiesConfiguration.java

Author: ebourg
Date: Tue May 22 03:49:26 2007
New Revision: 540529

URL: http://svn.apache.org/viewvc?view=rev&rev=540529
Log:
Added a test case for loading and saving configurations to non standard URLs using a custom URLStreamHandler (CONFIGURATION-249)

Added:
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/FileURLStreamHandler.java
Modified:
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java

Added: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/FileURLStreamHandler.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/FileURLStreamHandler.java?view=auto&rev=540529
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/FileURLStreamHandler.java (added)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/FileURLStreamHandler.java Tue May 22 03:49:26 2007
@@ -0,0 +1,60 @@
+/*
+ * 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.configuration;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+/**
+ * A custom URLStreamHandler to test loading and saving configurations to non
+ * standard URLs. This handler acts like a file handler with write support.
+ *
+ * @author Emmanuel Bourg
+ * @version $Revision$, $Date$
+ */
+public class FileURLStreamHandler extends URLStreamHandler
+{
+    protected URLConnection openConnection(URL u) throws IOException
+    {
+        final File file = new File(u.getFile());
+
+        return new URLConnection(u) {
+
+            public void connect() throws IOException
+            {
+            }
+
+            public InputStream getInputStream() throws IOException
+            {
+                return new FileInputStream(file);
+            }
+
+            public OutputStream getOutputStream() throws IOException
+            {
+                return new FileOutputStream(file);
+            }
+        };
+    }
+}

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java?view=diff&rev=540529&r1=540528&r2=540529
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java Tue May 22 03:49:26 2007
@@ -24,6 +24,7 @@
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -158,6 +159,17 @@
 
         // Save it again, verifing a save with a filename works.
         checkConfig.save();
+    }
+
+    public void testSaveToCustomURL() throws Exception
+    {
+        // save the configuration to a custom URL
+        URL url = new URL("foo", "", 0, "./target/testsave-custom-url.properties", new FileURLStreamHandler());
+        conf.save(url);
+
+        // reload the configuration
+        Configuration config2 = new PropertiesConfiguration(url);
+        assertEquals("true", config2.getString("configuration.loaded"));        
     }
 
     public void testInMemoryCreatedSave() throws Exception



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org