[jbosscache-commits] JBoss Cache SVN: r6677 - in core/trunk/src: test/java/org/jboss/cache/config/parsing and 1 other directory.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Sep 2 09:44:35 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-09-02 09:44:35 -0400 (Tue, 02 Sep 2008)
New Revision: 6677

Added:
   core/trunk/src/test/java/org/jboss/cache/config/parsing/CacheConfigsTest.java
Modified:
   core/trunk/src/main/java/org/jboss/cache/config/parsing/CacheConfigsXmlParser.java
   core/trunk/src/main/java/org/jboss/cache/config/parsing/RootElementBuilder.java
   core/trunk/src/main/java/org/jboss/cache/config/parsing/XmlConfigurationParser.java
   core/trunk/src/test/java/org/jboss/cache/config/parsing/SampleConfigFilesCorrectnessTest.java
Log:
Added support for legacy config files.

Modified: core/trunk/src/main/java/org/jboss/cache/config/parsing/CacheConfigsXmlParser.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/config/parsing/CacheConfigsXmlParser.java	2008-09-02 13:03:30 UTC (rev 6676)
+++ core/trunk/src/main/java/org/jboss/cache/config/parsing/CacheConfigsXmlParser.java	2008-09-02 13:44:35 UTC (rev 6677)
@@ -90,7 +90,8 @@
    {
       // loop through all elements in XML.
       Element root = getDocumentRoot(stream);
-      NodeList list = root.getElementsByTagNameNS(RootElementBuilder.JBOSSCACHE_REPO_NS, CONFIG_ROOT);
+
+      NodeList list = root.getElementsByTagName(CONFIG_ROOT);
       if (list == null || list.getLength() == 0)
          throw new ConfigurationException("Can't find " + CONFIG_ROOT + " tag");
 
@@ -108,9 +109,21 @@
          String name = element.getAttribute(CONFIG_NAME);
          if (name == null || name.trim().length() == 0)
             throw new ConfigurationException("Element " + element + " has no name attribute");
+
          XmlConfigurationParser parser = new XmlConfigurationParser();
-         // FIXME - This should be using a valid schema!!!
-         Configuration c = parser.parseElementIgnoringRoot(element);
+         Configuration c;
+         if (parser.isValidElementRoot(element))
+         {
+            // FIXME - This should be using a valid schema!!!
+            c = parser.parseElementIgnoringRoot(element);
+         }
+         else
+         {
+            log.warn("Detected legacy configuration file format when parsing configuration file.  Migrating to the new (3.x) file format is recommended.  See FAQs for details.");
+            XmlConfigurationParser2x oldParser = new XmlConfigurationParser2x();
+            c = oldParser.parseConfiguration(element);
+         }
+
          // Prove that we can successfully clone it
          c = c.clone();
          result.put(name.trim(), c);
@@ -121,7 +134,7 @@
 
    private Element getDocumentRoot(InputStream stream)
    {
-      RootElementBuilder rootElementBuilder = new RootElementBuilder();
+      RootElementBuilder rootElementBuilder = new RootElementBuilder(false);
       return rootElementBuilder.readRoot(stream);
    }
 }
\ No newline at end of file

Modified: core/trunk/src/main/java/org/jboss/cache/config/parsing/RootElementBuilder.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/config/parsing/RootElementBuilder.java	2008-09-02 13:03:30 UTC (rev 6676)
+++ core/trunk/src/main/java/org/jboss/cache/config/parsing/RootElementBuilder.java	2008-09-02 13:44:35 UTC (rev 6677)
@@ -21,18 +21,18 @@
  */
 package org.jboss.cache.config.parsing;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jboss.cache.config.ConfigurationException;
+import org.jboss.util.xml.JBossEntityResolver;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
-import org.jboss.cache.config.ConfigurationException;
-import org.jboss.util.xml.JBossEntityResolver;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
-import org.xml.sax.SAXException;
 
+import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.DocumentBuilder;
 import java.io.InputStream;
 
 /**
@@ -79,6 +79,11 @@
       this(new FailureErrorHandler());
    }
 
+   public RootElementBuilder(boolean validating)
+   {
+      this(new FailureErrorHandler(), validating);
+   }
+
    public Element readRoot(InputStream config)
    {
       try

Modified: core/trunk/src/main/java/org/jboss/cache/config/parsing/XmlConfigurationParser.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/config/parsing/XmlConfigurationParser.java	2008-09-02 13:03:30 UTC (rev 6676)
+++ core/trunk/src/main/java/org/jboss/cache/config/parsing/XmlConfigurationParser.java	2008-09-02 13:44:35 UTC (rev 6677)
@@ -23,7 +23,11 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.jboss.cache.config.*;
+import org.jboss.cache.config.BuddyReplicationConfig;
+import org.jboss.cache.config.CacheLoaderConfig;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.config.ConfigurationException;
+import org.jboss.cache.config.CustomInterceptorConfig;
 import org.jboss.cache.config.parsing.element.BuddyElementParser;
 import org.jboss.cache.config.parsing.element.CustomInterceptorsElementParser;
 import org.jboss.cache.config.parsing.element.EvictionElementParser;
@@ -31,6 +35,7 @@
 import org.jboss.cache.lock.IsolationLevel;
 import org.jboss.cache.util.FileLookup;
 import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
 import org.xml.sax.ErrorHandler;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
@@ -386,6 +391,19 @@
    }
 
    /**
+    * Tests whether the element passed in is a modern (3.0) config element rather than a legacy one.
+    *
+    * @param element element to test
+    * @return true of the element is a modern one and can be parsed using the current parser.
+    */
+   public boolean isValidElementRoot(Element element)
+   {
+      // simply test for the "jbosscache" element.
+      NodeList elements = element.getElementsByTagName("jbosscache");
+      return elements != null && elements.getLength() > 0;
+   }
+
+   /**
     * Default schema validation error handler, that throws an exception on validation errors.
     */
    private static class FailureErrorHandler implements ErrorHandler

Added: core/trunk/src/test/java/org/jboss/cache/config/parsing/CacheConfigsTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/config/parsing/CacheConfigsTest.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/config/parsing/CacheConfigsTest.java	2008-09-02 13:44:35 UTC (rev 6677)
@@ -0,0 +1,117 @@
+package org.jboss.cache.config.parsing;
+
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.lock.IsolationLevel;
+import org.testng.annotations.Test;
+
+import java.io.ByteArrayInputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Tests the {@link org.jboss.cache.config.parsing.CacheConfigsXmlParser}.
+ *
+ * @author Manik Surtani (<a href="mailto:manik at jboss.org">manik at jboss.org</a>)
+ * @since 3.0
+ */
+ at Test(groups = "unit")
+public class CacheConfigsTest
+{
+   public void testNewFormat() throws CloneNotSupportedException
+   {
+      String xml = "<cache-configs>\n" +
+            "   <cache-config name=\"A\">\n" +
+            "      <jbosscache  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
+            "            xmlns=\"urn:jboss:jbosscache-core:config:3.0\">\n" +
+            "         <locking isolationLevel=\"REPEATABLE_READ\" lockAcquisitionTimeout=\"15000\"/>\n" +
+            "         <transaction transactionManagerLookupClass=\"org.jboss.cache.transaction.GenericTransactionManagerLookup\"/>\n" +
+            "         <stateRetrieval timeout=\"20000\"/>\n" +
+            "      </jbosscache>\n" +
+            "   </cache-config>\n" +
+            "\n" +
+            "   <cache-config name=\"B\">\n" +
+            "      <jbosscache  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
+            "            xmlns=\"urn:jboss:jbosscache-core:config:3.0\">\n" +
+            "         <locking isolationLevel=\"READ_COMMITTED\" lockAcquisitionTimeout=\"15000\"/>\n" +
+            "         <transaction transactionManagerLookupClass=\"org.jboss.cache.transaction.GenericTransactionManagerLookup\"/>\n" +
+            "         <stateRetrieval timeout=\"20000\"/>\n" +
+            "      </jbosscache>\n" +
+            "   </cache-config>\n" +
+            "\n" +
+            "   <cache-config name=\"C\">\n" +
+            "      <jbosscache  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
+            "            xmlns=\"urn:jboss:jbosscache-core:config:3.0\">\n" +
+            "         <locking isolationLevel=\"READ_COMMITTED\" lockAcquisitionTimeout=\"100\"/>\n" +
+            "         <stateRetrieval timeout=\"100\"/>\n" +
+            "      </jbosscache>\n" +
+            "   </cache-config>\n" +
+            "</cache-configs>";
+
+      ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
+
+      CacheConfigsXmlParser ccxp = new CacheConfigsXmlParser();
+      Map<String, Configuration> map = ccxp.parseConfigs(bais);
+      Map toExpect = buildExpectedValues();
+
+      assert map.equals(toExpect);
+   }
+
+   public void testLegacyFormat() throws CloneNotSupportedException
+   {
+      String xml = "<cache-configs>\n" +
+            "   <cache-config name=\"A\">\n" +
+            "      <attribute name=\"IsolationLevel\">REPEATABLE_READ</attribute>\n" +
+            "      <attribute name=\"LockAcquisitionTimeout\">15000</attribute>\n" +
+            "      <attribute name=\"TransactionManagerLookupClass\">org.jboss.cache.transaction.GenericTransactionManagerLookup</attribute>\n" +
+            "      <attribute name=\"StateRetrievalTimeout\">20000</attribute>\n" +
+            "   </cache-config>\n" +
+            "\n" +
+            "   <cache-config name=\"B\">\n" +
+            "         <attribute name=\"IsolationLevel\">READ_COMMITTED</attribute>\n" +
+            "         <attribute name=\"LockAcquisitionTimeout\">15000</attribute>\n" +
+            "         <attribute name=\"TransactionManagerLookupClass\">org.jboss.cache.transaction.GenericTransactionManagerLookup</attribute>\n" +
+            "         <attribute name=\"StateRetrievalTimeout\">20000</attribute>\n" +
+            "   </cache-config>\n" +
+            "\n" +
+            "   <cache-config name=\"C\">\n" +
+            "         <attribute name=\"IsolationLevel\">READ_COMMITTED</attribute>\n" +
+            "         <attribute name=\"LockAcquisitionTimeout\">100</attribute>\n" +
+            "         <attribute name=\"StateRetrievalTimeout\">100</attribute>\n" +
+            "   </cache-config>\n" +
+            "</cache-configs>";
+
+      ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
+
+      CacheConfigsXmlParser ccxp = new CacheConfigsXmlParser();
+      Map<String, Configuration> map = ccxp.parseConfigs(bais);
+      Map toExpect = buildExpectedValues();
+
+      assert map.equals(toExpect);
+   }
+
+   private Map<String, Configuration> buildExpectedValues()
+   {
+      Map<String, Configuration> map = new HashMap<String, Configuration>(3);
+      Configuration cfg = new Configuration();
+      map.put("A", cfg);
+      cfg.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
+      cfg.setLockAcquisitionTimeout(15000);
+      cfg.setTransactionManagerLookupClass("org.jboss.cache.transaction.GenericTransactionManagerLookup");
+      cfg.setStateRetrievalTimeout(20000);
+
+      cfg = new Configuration();
+      map.put("B", cfg);
+      cfg.setIsolationLevel(IsolationLevel.READ_COMMITTED);
+      cfg.setLockAcquisitionTimeout(15000);
+      cfg.setTransactionManagerLookupClass("org.jboss.cache.transaction.GenericTransactionManagerLookup");
+      cfg.setStateRetrievalTimeout(20000);
+
+      cfg = new Configuration();
+      map.put("C", cfg);
+      cfg.setIsolationLevel(IsolationLevel.READ_COMMITTED);
+      cfg.setLockAcquisitionTimeout(100);
+      cfg.setStateRetrievalTimeout(100);
+
+      return map;
+   }
+}

Modified: core/trunk/src/test/java/org/jboss/cache/config/parsing/SampleConfigFilesCorrectnessTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/config/parsing/SampleConfigFilesCorrectnessTest.java	2008-09-02 13:03:30 UTC (rev 6676)
+++ core/trunk/src/test/java/org/jboss/cache/config/parsing/SampleConfigFilesCorrectnessTest.java	2008-09-02 13:44:35 UTC (rev 6677)
@@ -27,8 +27,8 @@
 import org.apache.log4j.spi.LoggingEvent;
 import org.jboss.cache.Cache;
 import org.jboss.cache.DefaultCacheFactory;
-import org.testng.annotations.AfterTest;
-import org.testng.annotations.BeforeTest;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import java.io.File;
@@ -51,11 +51,7 @@
    private InMemoryAppender appender;
    private Level oldLevel;
 
-
-   /**
-    * Will be called only once, before all test methods run.
-    */
-   @BeforeTest
+   @BeforeMethod
    public void setUpTest()
    {
       Logger log4jLogger = Logger.getRootLogger();
@@ -67,15 +63,13 @@
       System.out.println("f = " + f.getAbsolutePath());
    }
 
-   /**
-    * Will be called only once, after all test methods run.
-    */
-   @AfterTest
+   @AfterMethod
    public void tearDownTest()
    {
       Logger log4jLogger = Logger.getRootLogger();
       log4jLogger.setLevel(oldLevel);
       log4jLogger.removeAppender(appender);
+      appender.close();
    }
 
    public void testSchemaValidity()




More information about the jbosscache-commits mailing list