commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1225022 - /commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestVFSConfigurationBuilder.java
Date Tue, 27 Dec 2011 21:36:24 GMT
Author: oheger
Date: Tue Dec 27 21:36:23 2011
New Revision: 1225022

URL: http://svn.apache.org/viewvc?rev=1225022&view=rev
Log:
Converted tests to JUnit 4, fixed warnings.

Modified:
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestVFSConfigurationBuilder.java

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestVFSConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestVFSConfigurationBuilder.java?rev=1225022&r1=1225021&r2=1225022&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestVFSConfigurationBuilder.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestVFSConfigurationBuilder.java
Tue Dec 27 21:36:23 2011
@@ -16,6 +16,14 @@
  */
 package org.apache.commons.configuration;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+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.File;
 import java.io.FileReader;
 import java.io.FileWriter;
@@ -28,20 +36,23 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
-import junit.framework.TestCase;
-
 import org.apache.commons.configuration.beanutils.BeanHelper;
 import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
 import org.apache.commons.configuration.tree.DefaultConfigurationNode;
 import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
 
 /**
  * Test class for VFSConfigurationBuilder.
  *
- * @author Oliver Heger
+ * @author <a
+ * href="http://commons.apache.org/configuration/team-list.html">Commons
+ * Configuration team</a>
  * @version $Id$
  */
-public class TestVFSConfigurationBuilder extends TestCase
+public class TestVFSConfigurationBuilder
 {
     /** Test configuration definition file. */
     private static final File TEST_FILE = ConfigurationAssert
@@ -104,9 +115,9 @@ public class TestVFSConfigurationBuilder
     /** Stores the object to be tested. */
     DefaultConfigurationBuilder factory;
 
-    protected void setUp() throws Exception
+    @Before
+    public void setUp() throws Exception
     {
-        super.setUp();
         System
                 .setProperty("java.naming.factory.initial",
                         "org.apache.commons.configuration.MockInitialContextFactory");
@@ -118,15 +129,16 @@ public class TestVFSConfigurationBuilder
         factory.clearErrorListeners();  // avoid exception messages
     }
 
-    protected void tearDown() throws Exception
+    @After
+    public void tearDown() throws Exception
     {
         FileSystem.resetDefaultFileSystem();
-        super.tearDown();
     }
 
     /**
      * Tests the isReservedNode() method of ConfigurationDeclaration.
      */
+    @Test
     public void testConfigurationDeclarationIsReserved()
     {
         DefaultConfigurationBuilder.ConfigurationDeclaration decl = new DefaultConfigurationBuilder.ConfigurationDeclaration(
@@ -153,6 +165,7 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests if the at attribute is correctly detected as reserved attribute.
      */
+    @Test
     public void testConfigurationDeclarationIsReservedAt()
     {
         checkOldReservedAttribute("at");
@@ -162,6 +175,7 @@ public class TestVFSConfigurationBuilder
      * Tests if the optional attribute is correctly detected as reserved
      * attribute.
      */
+    @Test
     public void testConfigurationDeclarationIsReservedOptional()
     {
         checkOldReservedAttribute("optional");
@@ -197,6 +211,7 @@ public class TestVFSConfigurationBuilder
      * Tests access to certain reserved attributes of a
      * ConfigurationDeclaration.
      */
+    @Test
     public void testConfigurationDeclarationGetAttributes()
     {
         factory.addProperty("xml.fileName", "test.xml");
@@ -217,21 +232,25 @@ public class TestVFSConfigurationBuilder
         factory.clearProperty("xml[@config-optional]");
         factory.setProperty("xml[@optional]", Boolean.TRUE);
         assertTrue("Old optional attribute not detected", decl.isOptional());
+    }
+
+    /**
+     * Tests whether an invalid value of an optional attribute is detected.
+     */
+    @Test(expected = ConfigurationRuntimeException.class)
+    public void testConfigurationDeclarationOptionalAttributeInvalid()
+    {
+        factory.addProperty("xml.fileName", "test.xml");
+        DefaultConfigurationBuilder.ConfigurationDeclaration decl = new DefaultConfigurationBuilder.ConfigurationDeclaration(
+                factory, factory.configurationAt("xml"));
         factory.setProperty("xml[@optional]", "invalid value");
-        try
-        {
-            decl.isOptional();
-            fail("Invalid optional attribute was not detected!");
-        }
-        catch (ConfigurationRuntimeException crex)
-        {
-            // ok
-        }
+        decl.isOptional();
     }
 
     /**
      * Tests adding a new configuration provider.
      */
+    @Test
     public void testAddConfigurationProvider()
     {
         DefaultConfigurationBuilder.ConfigurationProvider provider = new DefaultConfigurationBuilder.ConfigurationProvider();
@@ -246,40 +265,27 @@ public class TestVFSConfigurationBuilder
      * Tries to register a null configuration provider. This should cause an
      * exception.
      */
+    @Test(expected = IllegalArgumentException.class)
     public void testAddConfigurationProviderNull()
     {
-        try
-        {
-            factory.addConfigurationProvider("test", null);
-            fail("Could register null provider");
-        }
-        catch (IllegalArgumentException iex)
-        {
-            // ok
-        }
+        factory.addConfigurationProvider("test", null);
     }
 
     /**
      * Tries to register a configuration provider for a null tag. This should
      * cause an exception to be thrown.
      */
+    @Test(expected = IllegalArgumentException.class)
     public void testAddConfigurationProviderNullTag()
     {
-        try
-        {
-            factory.addConfigurationProvider(null,
-                    new DefaultConfigurationBuilder.ConfigurationProvider());
-            fail("Could register provider for null tag!");
-        }
-        catch (IllegalArgumentException iex)
-        {
-            // ok
-        }
+        factory.addConfigurationProvider(null,
+               new DefaultConfigurationBuilder.ConfigurationProvider());
     }
 
     /**
      * Tests removing configuration providers.
      */
+    @Test
     public void testRemoveConfigurationProvider()
     {
         assertNull("Removing unknown provider", factory
@@ -296,6 +302,7 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests creating a configuration object from a configuration declaration.
      */
+    @Test
     public void testConfigurationBeanFactoryCreateBean()
     {
         factory.addConfigurationProvider("test",
@@ -314,25 +321,19 @@ public class TestVFSConfigurationBuilder
      * Tests creating a configuration object from an unknown tag. This should
      * cause an exception.
      */
+    @Test(expected = ConfigurationRuntimeException.class)
     public void testConfigurationBeanFactoryCreateUnknownTag()
     {
         factory.addProperty("test[@throwExceptionOnMissing]", "true");
         DefaultConfigurationBuilder.ConfigurationDeclaration decl = new DefaultConfigurationBuilder.ConfigurationDeclaration(
                 factory, factory.configurationAt("test"));
-        try
-        {
-            BeanHelper.createBean(decl);
-            fail("Could create configuration from unknown tag!");
-        }
-        catch (ConfigurationRuntimeException crex)
-        {
-            // ok
-        }
+        BeanHelper.createBean(decl);
     }
 
     /**
      * Tests loading a simple configuration definition file.
      */
+    @Test
     public void testLoadConfiguration() throws ConfigurationException
     {
         factory.setFile(TEST_FILE);
@@ -342,6 +343,7 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests the file constructor.
      */
+    @Test
     public void testLoadConfigurationFromFile() throws ConfigurationException
     {
         factory = new DefaultConfigurationBuilder(TEST_FILE);
@@ -351,6 +353,7 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests the file name constructor.
      */
+    @Test
     public void testLoadConfigurationFromFileName()
             throws ConfigurationException
     {
@@ -361,9 +364,10 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests the URL constructor.
      */
+    @Test
     public void testLoadConfigurationFromURL() throws Exception
     {
-        factory = new DefaultConfigurationBuilder(TEST_FILE.toURL());
+        factory = new DefaultConfigurationBuilder(TEST_FILE.toURI().toURL());
         checkConfiguration();
     }
 
@@ -412,6 +416,7 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests loading a configuration definition file with an additional section.
      */
+    @Test
     public void testLoadAdditional() throws ConfigurationException
     {
         factory.setFile(ADDITIONAL_FILE);
@@ -423,7 +428,7 @@ public class TestVFSConfigurationBuilder
         // Test if union was constructed correctly
         Object prop = compositeConfiguration.getProperty("tables.table.name");
         assertTrue(prop instanceof Collection);
-        assertEquals(3, ((Collection) prop).size());
+        assertEquals(3, ((Collection<?>) prop).size());
         assertEquals("users", compositeConfiguration
                 .getProperty("tables.table(0).name"));
         assertEquals("documents", compositeConfiguration
@@ -434,20 +439,20 @@ public class TestVFSConfigurationBuilder
         prop = compositeConfiguration
                 .getProperty("tables.table.fields.field.name");
         assertTrue(prop instanceof Collection);
-        assertEquals(17, ((Collection) prop).size());
+        assertEquals(17, ((Collection<?>) prop).size());
 
         assertEquals("smtp.mydomain.org", compositeConfiguration
                 .getString("mail.host.smtp"));
         assertEquals("pop3.mydomain.org", compositeConfiguration
                 .getString("mail.host.pop"));
 
-        // This was overriden
+        // This was overridden
         assertEquals("masterOfPost", compositeConfiguration
                 .getString("mail.account.user"));
         assertEquals("topsecret", compositeConfiguration
                 .getString("mail.account.psswd"));
 
-        // This was overriden, too, but not in additional section
+        // This was overridden, too, but not in additional section
         assertEquals("enhanced factory", compositeConfiguration
                 .getString("test.configuration"));
     }
@@ -456,6 +461,7 @@ public class TestVFSConfigurationBuilder
      * Tests whether a default log error listener is registered at the builder
      * instance.
      */
+    @Test
     public void testLogErrorListener()
     {
         assertEquals("No default error listener registered", 1,
@@ -465,9 +471,10 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests loading a definition file that contains optional configurations.
      */
+    @Test
     public void testLoadOptional() throws Exception
     {
-        factory.setURL(OPTIONAL_FILE.toURL());
+        factory.setURL(OPTIONAL_FILE.toURI().toURL());
         Configuration config = factory.getConfiguration();
         assertTrue(config.getBoolean("test.boolean"));
         assertEquals("value", config.getProperty("element"));
@@ -477,6 +484,7 @@ public class TestVFSConfigurationBuilder
      * Tests whether loading a failing optional configuration causes an error
      * event.
      */
+    @Test
     public void testLoadOptionalErrorEvent() throws Exception
     {
         factory.clearErrorListeners();
@@ -492,18 +500,11 @@ public class TestVFSConfigurationBuilder
      * configuration sources. One non optional does not exist, so this should
      * cause an exception.
      */
-    public void testLoadOptionalWithException()
+    @Test(expected = ConfigurationException.class)
+    public void testLoadOptionalWithException() throws ConfigurationException
     {
         factory.setFile(OPTIONALEX_FILE);
-        try
-        {
-            factory.getConfiguration();
-            fail("Non existing source did not cause an exception!");
-        }
-        catch (ConfigurationException cex)
-        {
-            // ok
-        }
+        factory.getConfiguration();
     }
 
     /**
@@ -511,6 +512,7 @@ public class TestVFSConfigurationBuilder
      * configuration. The optional attribute should work for other configuration
      * classes, too.
      */
+    @Test
     public void testLoadOptionalNonFileBased() throws ConfigurationException
     {
         CombinedConfiguration config = prepareOptionalTest("configuration", false);
@@ -524,6 +526,7 @@ public class TestVFSConfigurationBuilder
      * attribute. This configuration should be added to the resulting
      * configuration.
      */
+    @Test
     public void testLoadOptionalForceCreate() throws ConfigurationException
     {
         factory.setBasePath(TEST_FILE.getParent());
@@ -542,6 +545,7 @@ public class TestVFSConfigurationBuilder
      * Tests loading an embedded optional configuration builder with the force
      * create attribute.
      */
+    @Test
     public void testLoadOptionalBuilderForceCreate()
             throws ConfigurationException
     {
@@ -559,6 +563,7 @@ public class TestVFSConfigurationBuilder
      * set. The provider will always throw an exception. In this case the
      * configuration will not be added to the resulting combined configuration.
      */
+    @Test
     public void testLoadOptionalForceCreateWithException()
             throws ConfigurationException
     {
@@ -566,6 +571,7 @@ public class TestVFSConfigurationBuilder
                 new DefaultConfigurationBuilder.ConfigurationBuilderProvider()
                 {
                     // Throw an exception here, too
+                    @Override
                     public AbstractConfiguration getEmptyConfiguration(
                             DefaultConfigurationBuilder.ConfigurationDeclaration decl) throws
Exception
                     {
@@ -603,6 +609,7 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests loading a definition file with multiple different sources.
      */
+    @Test
     public void testLoadDifferentSources() throws ConfigurationException
     {
         factory.setFile(MULTI_FILE);
@@ -637,6 +644,7 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests if the base path is correctly evaluated.
      */
+    @Test
     public void testSetConfigurationBasePath() throws ConfigurationException
     {
         factory.addProperty("properties[@fileName]", "test.properties");
@@ -652,6 +660,7 @@ public class TestVFSConfigurationBuilder
      * Tests reading a configuration definition file that contains complex
      * initialization of properties of the declared configuration sources.
      */
+    @Test
     public void testComplexInitialization() throws ConfigurationException
     {
         factory.setFile(INIT_FILE);
@@ -686,6 +695,7 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests if the returned combined configuration has the expected structure.
      */
+    @Test
     public void testCombinedConfiguration() throws ConfigurationException
     {
         factory.setFile(INIT_FILE);
@@ -700,7 +710,7 @@ public class TestVFSConfigurationBuilder
         CombinedConfiguration cc2 = (CombinedConfiguration) cc
                 .getConfiguration(DefaultConfigurationBuilder.ADDITIONAL_NAME);
         assertNotNull("No additional configuration found", cc2);
-        Set names = cc2.getConfigurationNames();
+        Set<String> names = cc2.getConfigurationNames();
         assertEquals("Wrong number of contained additional configs", 2, names
                 .size());
         assertTrue("Config 1 not contained", names.contains("combiner1"));
@@ -711,6 +721,7 @@ public class TestVFSConfigurationBuilder
      * Tests the structure of the returned combined configuration if there is no
      * additional section.
      */
+    @Test
     public void testCombinedConfigurationNoAdditional()
             throws ConfigurationException
     {
@@ -723,12 +734,13 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests whether the list node definition was correctly processed.
      */
+    @Test
     public void testCombinedConfigurationListNodes()
             throws ConfigurationException
     {
         factory.setFile(INIT_FILE);
         CombinedConfiguration cc = factory.getConfiguration(true);
-        Set listNodes = cc.getNodeCombiner().getListNodes();
+        Set<String> listNodes = cc.getNodeCombiner().getListNodes();
         assertEquals("Wrong number of list nodes", 2, listNodes.size());
         assertTrue("table node not a list node", listNodes.contains("table"));
         assertTrue("list node not a list node", listNodes.contains("list"));
@@ -744,6 +756,7 @@ public class TestVFSConfigurationBuilder
      * Tests whether a configuration builder can itself be declared in a
      * configuration definition file.
      */
+    @Test
     public void testConfigurationBuilderProvider()
             throws ConfigurationException
     {
@@ -758,6 +771,7 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests whether XML settings can be inherited.
      */
+    @Test
     public void testLoadXMLWithSettings() throws ConfigurationException,
             IOException
     {
@@ -798,6 +812,7 @@ public class TestVFSConfigurationBuilder
      * Tests loading a configuration definition file that defines a custom
      * result class.
      */
+    @Test
     public void testExtendedClass() throws ConfigurationException
     {
         factory.setFile(CLASS_FILE);
@@ -811,6 +826,7 @@ public class TestVFSConfigurationBuilder
     /**
      * Tests loading a configuration definition file that defines new providers.
      */
+    @Test
     public void testConfigurationProvider() throws ConfigurationException
     {
         factory.setFile(PROVIDER_FILE);
@@ -820,9 +836,10 @@ public class TestVFSConfigurationBuilder
         assertNotNull("Provider 'test' not registered", provider);
     }
 
-        /**
+    /**
      * Tests loading a configuration definition file that defines new providers.
      */
+    @Test
     public void testExtendedXMLConfigurationProvider() throws ConfigurationException
     {
         factory.setFile(EXTENDED_PROVIDER_FILE);
@@ -837,6 +854,7 @@ public class TestVFSConfigurationBuilder
                 config instanceof TestDefaultConfigurationBuilder.ExtendedXMLConfiguration);
     }
 
+    @Test
     public void testGlobalLookup() throws Exception
     {
         factory.setFile(GLOBAL_LOOKUP_FILE);
@@ -846,6 +864,7 @@ public class TestVFSConfigurationBuilder
         assertEquals("Incorrect value retrieved","test.value",value);
     }
 
+    @Test
     public void testSystemProperties() throws Exception
     {
         factory.setFile(SYSTEM_PROPS_FILE);
@@ -855,7 +874,7 @@ public class TestVFSConfigurationBuilder
         assertEquals("Incorrect value retrieved","value1",value);
     }
 
-
+    @Test
     public void testValidation() throws Exception
     {
         factory.setFile(VALIDATION_FILE);
@@ -865,6 +884,7 @@ public class TestVFSConfigurationBuilder
         assertEquals("Incorrect value retrieved","value1",value);
     }
 
+    @Test
     public void testValidation2() throws Exception
     {
         factory.setFile(VALIDATION2_FILE);
@@ -874,6 +894,7 @@ public class TestVFSConfigurationBuilder
         assertEquals("Incorrect value retrieved","value1",value);
     }
 
+    @Test
     public void testMultiTenentConfiguration() throws Exception
     {
         factory.setFile(MULTI_TENENT_FILE);
@@ -890,6 +911,7 @@ public class TestVFSConfigurationBuilder
         verify("1005", config, 50);
     }
 
+    @Test
     public void testMultiTenentConfiguration2() throws Exception
     {
         factory.setFile(MULTI_TENENT_FILE);
@@ -905,6 +927,7 @@ public class TestVFSConfigurationBuilder
         verify("1005", config, 50);
     }
 
+    @Test
     public void testMultiTenentConfiguration3() throws Exception
     {
         factory.setFile(MULTI_TENENT_FILE);
@@ -920,6 +943,7 @@ public class TestVFSConfigurationBuilder
         verify("1005", config, 50);
     }
 
+    @Test
     public void testSetFileSystem() throws Exception
     {
         factory.setFile(PROVIDER_FILE);
@@ -929,12 +953,12 @@ public class TestVFSConfigurationBuilder
         System.getProperties().remove("Id");
 
         CombinedConfiguration config = factory.getConfiguration(true);
-        List list = config.getConfigurations();
+        List<AbstractConfiguration> list = config.getConfigurations();
         assertTrue("Incorrect number of configurations - " + list.size(), list.size() ==
4);
-        Iterator iter = list.iterator();
+        Iterator<AbstractConfiguration> iter = list.iterator();
         while (iter.hasNext())
         {
-            Configuration conf = (Configuration)iter.next();
+            Configuration conf = iter.next();
             if (conf instanceof FileSystemBased)
             {
                 assertTrue("Incorrect file system for Configuration " + conf,
@@ -942,10 +966,10 @@ public class TestVFSConfigurationBuilder
             }
             else if (conf instanceof CombinedConfiguration)
             {
-                Iterator it = ((CombinedConfiguration)conf).getConfigurations().iterator();
+                Iterator<AbstractConfiguration> it = ((CombinedConfiguration)conf).getConfigurations().iterator();
                 while (it.hasNext())
                 {
-                    conf = (Configuration)it.next();
+                    conf = it.next();
                     if (conf instanceof FileSystemBased)
                     {
                         assertTrue("Incorrect file system for Configuration " + conf,
@@ -956,6 +980,7 @@ public class TestVFSConfigurationBuilder
         }
     }
 
+    @Test
     public void testConfiguredFileSystem() throws Exception
     {
         factory.setFile(FILESYSTEM_FILE);
@@ -966,12 +991,12 @@ public class TestVFSConfigurationBuilder
         FileSystem fs = factory.getFileSystem();
         assertNotNull("No File System",fs);
         assertTrue("Incorrect File System", fs instanceof VFSFileSystem);
-        List list = config.getConfigurations();
+        List<AbstractConfiguration> list = config.getConfigurations();
         assertTrue("Incorrect number of configurations - " + list.size(), list.size() ==
4);
-        Iterator iter = list.iterator();
+        Iterator<AbstractConfiguration> iter = list.iterator();
         while (iter.hasNext())
         {
-            Configuration conf = (Configuration)iter.next();
+            Configuration conf = iter.next();
             if (conf instanceof FileSystemBased)
             {
                 assertTrue("Incorrect file system for Configuration " + conf,
@@ -979,10 +1004,10 @@ public class TestVFSConfigurationBuilder
             }
             else if (conf instanceof CombinedConfiguration)
             {
-                Iterator it = ((CombinedConfiguration)conf).getConfigurations().iterator();
+                Iterator<AbstractConfiguration> it = ((CombinedConfiguration)conf).getConfigurations().iterator();
                 while (it.hasNext())
                 {
-                    conf = (Configuration)it.next();
+                    conf = it.next();
                     if (conf instanceof FileSystemBased)
                     {
                         assertTrue("Incorrect file system for Configuration " + conf,
@@ -993,6 +1018,7 @@ public class TestVFSConfigurationBuilder
         }
     }
 
+    @Test
     public void testFileReload1() throws Exception
     {
         // create a new configuration
@@ -1019,6 +1045,7 @@ public class TestVFSConfigurationBuilder
         output.delete();
     }
 
+    @Test
     public void testFileReload2() throws Exception
     {
         // create a new configuration
@@ -1042,6 +1069,7 @@ public class TestVFSConfigurationBuilder
         output.delete();
     }
 
+    @Test
     public void testFileReload3() throws Exception
     {
         // create a new configuration
@@ -1070,6 +1098,7 @@ public class TestVFSConfigurationBuilder
         output.delete();
     }
 
+    @Test
     public void testReloadDefault() throws Exception
     {
         // create a new configuration
@@ -1102,7 +1131,7 @@ public class TestVFSConfigurationBuilder
         assertNull("Property was not cleared by reload", test);
     }
 
-
+    @Test
     public void testFileReloadSchemaValidationError() throws Exception
     {
         System.getProperties().remove("Id");
@@ -1142,7 +1171,6 @@ public class TestVFSConfigurationBuilder
         output.delete();
     }
 
-
     private void copyFile(File input, File output) throws IOException
     {
         Reader reader = new FileReader(input);
@@ -1157,7 +1185,6 @@ public class TestVFSConfigurationBuilder
         writer.close();
     }
 
-
     private void verify(String key, CombinedConfiguration config, int rows)
     {
         if (key == null)



Mime
View raw message