You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tiles.apache.org by ap...@apache.org on 2008/06/15 17:00:54 UTC

svn commit: r667964 - in /tiles/framework/trunk/tiles-core/src: main/java/org/apache/tiles/definition/dao/ main/java/org/apache/tiles/util/ test/java/org/apache/tiles/definition/ test/java/org/apache/tiles/definition/dao/ test/java/org/apache/tiles/util/

Author: apetrelli
Date: Sun Jun 15 08:00:54 2008
New Revision: 667964

URL: http://svn.apache.org/viewvc?rev=667964&view=rev
Log:
TILES-256
Created CachingLocaleUrlDefinitionDao.
Refactored LocaleUrlDefinitionDao to an abstract class to allow an easier inheritance.

Added:
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/BaseLocaleUrlDefinitionDAO.java   (with props)
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAO.java   (with props)
    tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAOTest.java   (with props)
    tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/util/LocaleUtilTest.java   (with props)
Modified:
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/LocaleUrlDefinitionDAO.java
    tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/util/LocaleUtil.java
    tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/TestUrlDefinitionsFactory.java
    tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/LocaleUrlDefinitionDAOTest.java

Added: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/BaseLocaleUrlDefinitionDAO.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/BaseLocaleUrlDefinitionDAO.java?rev=667964&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/BaseLocaleUrlDefinitionDAO.java (added)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/BaseLocaleUrlDefinitionDAO.java Sun Jun 15 08:00:54 2008
@@ -0,0 +1,271 @@
+/*
+ * $Id$
+ *
+ * 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.tiles.definition.dao;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.tiles.Definition;
+import org.apache.tiles.Initializable;
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.awareness.TilesApplicationContextAware;
+import org.apache.tiles.definition.DefinitionsFactory;
+import org.apache.tiles.definition.DefinitionsFactoryException;
+import org.apache.tiles.definition.DefinitionsReader;
+import org.apache.tiles.definition.RefreshMonitor;
+import org.apache.tiles.definition.digester.DigesterDefinitionsReader;
+import org.apache.tiles.impl.BasicTilesContainer;
+import org.apache.tiles.util.ClassUtil;
+
+/**
+ * Base abstract class for a DAO that is based on URLs and locale as a
+ * customization key.
+ *
+ * @version $Rev$ $Date$
+ * @since 2.1.0
+ */
+public abstract class BaseLocaleUrlDefinitionDAO implements
+        DefinitionDAO<Locale>, Initializable, TilesApplicationContextAware,
+        RefreshMonitor, URLReader {
+
+    /**
+     * The logging object.
+     */
+    private static final Log LOG = LogFactory
+            .getLog(BaseLocaleUrlDefinitionDAO.class);
+
+    /**
+     * Compatibility constant.
+     *
+     * @deprecated use {@link DEFINITIONS_CONFIG} to avoid namespace collisions.
+     */
+    private static final String LEGACY_DEFINITIONS_CONFIG = "definitions-config";
+
+    /**
+     * Contains the URL objects identifying where configuration data is found.
+     *
+     * @since 2.1.0
+     */
+    protected List<URL> sourceURLs;
+
+    /**
+     * Contains the dates that the URL sources were last modified.
+     *
+     * @since 2.1.0
+     */
+    protected Map<String, Long> lastModifiedDates;
+
+    /**
+     * The application context.
+     *
+     * @since 2.1.0
+     */
+    protected TilesApplicationContext applicationContext;
+
+    /**
+     * Reader used to get definitions from the sources.
+     *
+     * @since 2.1.0
+     */
+    protected DefinitionsReader reader;
+
+    /**
+     * Constructor.
+     */
+    public BaseLocaleUrlDefinitionDAO() {
+        sourceURLs = new ArrayList<URL>();
+        lastModifiedDates = new HashMap<String, Long>();
+    }
+
+    /**  {@inheritDoc}*/
+    public void setSourceURLs(List<URL> sourceURLs) {
+        this.sourceURLs = sourceURLs;
+    }
+
+    /**  {@inheritDoc}*/
+    public void setReader(DefinitionsReader reader) {
+        this.reader = reader;
+    }
+
+    /**  {@inheritDoc}*/
+    public void addSourceURL(URL sourceURL) {
+        if (sourceURLs == null) {
+            sourceURLs = new ArrayList<URL>();
+        }
+        sourceURLs.add(sourceURL);
+    }
+
+    /** {@inheritDoc} */
+    public void setApplicationContext(TilesApplicationContext applicationContext) {
+        this.applicationContext = applicationContext;
+    }
+
+    /** {@inheritDoc} */
+    public void init(Map<String, String> params) {
+        identifySources(params);
+        String readerClassName = params
+                .get(DefinitionsFactory.READER_IMPL_PROPERTY);
+
+        if (readerClassName != null) {
+            reader = (DefinitionsReader) ClassUtil.instantiate(readerClassName);
+        } else {
+            reader = new DigesterDefinitionsReader();
+        }
+        reader.init(params);
+    }
+
+    /** {@inheritDoc} */
+    public boolean refreshRequired() {
+        boolean status = false;
+
+        Set<String> urls = lastModifiedDates.keySet();
+
+        try {
+            for (String urlPath : urls) {
+                Long lastModifiedDate = lastModifiedDates.get(urlPath);
+                URL url = new URL(urlPath);
+                URLConnection connection = url.openConnection();
+                connection.connect();
+                long newModDate = connection.getLastModified();
+                if (newModDate != lastModifiedDate) {
+                    status = true;
+                    break;
+                }
+            }
+        } catch (Exception e) {
+            LOG.warn("Exception while monitoring update times.", e);
+            return true;
+        }
+        return status;
+    }
+
+    /**
+     * Detects the sources to load.
+     *
+     * @param initParameters The initialization parameters.
+     * @since 2.1.0
+     */
+    protected void identifySources(Map<String, String> initParameters) {
+        if (applicationContext == null) {
+            throw new IllegalStateException(
+                    "The TilesApplicationContext cannot be null");
+        }
+
+        String resourceString = getResourceString(initParameters);
+        String[] resources = getResourceNames(resourceString);
+
+        try {
+            for (int i = 0; i < resources.length; i++) {
+                URL resourceUrl = applicationContext.getResource(resources[i]);
+                if (resourceUrl != null) {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Adding resource '" + resourceUrl
+                                + "' to definitions factory.");
+                    }
+                    sourceURLs.add(resourceUrl);
+                } else {
+                    LOG.warn("Unable to find configured definition '"
+                            + resources[i] + "'");
+                }
+            }
+        } catch (IOException e) {
+            throw new DefinitionsFactoryException(
+                    "Unable to parse definitions from " + resourceString, e);
+        }
+    }
+
+    /**
+     * Derive the resource string from the initialization parameters. If no
+     * parameter {@link DefinitionsFactory#DEFINITIONS_CONFIG} is available,
+     * attempts to retrieve {@link BasicTilesContainer#DEFINITIONS_CONFIG} and
+     * {@link #LEGACY_DEFINITIONS_CONFIG}. If neither are available, returns
+     * "/WEB-INF/tiles.xml".
+     *
+     * @param parms The initialization parameters.
+     * @return resource string to be parsed.
+     */
+    @SuppressWarnings("deprecation")
+    protected String getResourceString(Map<String, String> parms) {
+        String resourceStr = parms.get(DefinitionsFactory.DEFINITIONS_CONFIG);
+        if (resourceStr == null) {
+            resourceStr = parms.get(BasicTilesContainer.DEFINITIONS_CONFIG);
+        }
+        if (resourceStr == null) {
+            resourceStr = parms.get(LEGACY_DEFINITIONS_CONFIG);
+        }
+        if (resourceStr == null) {
+            resourceStr = "/WEB-INF/tiles.xml";
+        }
+        return resourceStr;
+    }
+
+    /**
+     * Parse the resourceString into a list of resource paths which can be
+     * loaded by the application context.
+     *
+     * @param resourceString comma separated resources
+     * @return parsed resources
+     */
+    protected String[] getResourceNames(String resourceString) {
+        return resourceString.split(",");
+    }
+
+    /**
+     * Loads definitions from an URL without loading from "parent" URLs.
+     *
+     * @param url The URL to read.
+     * @return The definition map that has been read.
+     */
+    protected Map<String, Definition> loadDefinitionsFromURL(URL url) {
+        Map<String, Definition> defsMap = null;
+        try {
+            URLConnection connection = url.openConnection();
+            connection.connect();
+            lastModifiedDates.put(url.toExternalForm(), connection
+                    .getLastModified());
+
+            // Definition must be collected, starting from the base
+            // source up to the last localized file.
+            defsMap = reader.read(connection.getInputStream());
+        } catch (FileNotFoundException e) {
+            // File not found. continue.
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("File " + null + " not found, continue");
+            }
+        } catch (IOException e) {
+            throw new DefinitionsFactoryException(
+                    "I/O error processing configuration.", e);
+        }
+
+        return defsMap;
+    }
+}

Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/BaseLocaleUrlDefinitionDAO.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/BaseLocaleUrlDefinitionDAO.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAO.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAO.java?rev=667964&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAO.java (added)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAO.java Sun Jun 15 08:00:54 2008
@@ -0,0 +1,140 @@
+/*
+ * $Id$
+ *
+ * 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.tiles.definition.dao;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.tiles.Definition;
+import org.apache.tiles.definition.DefinitionsFactoryException;
+import org.apache.tiles.util.LocaleUtil;
+
+/**
+ * A definitions DAO (loading URLs and using Locale as a customization key) that
+ * caches definitions that have been loaded in a raw way (i.e. with inheritance
+ * that is not resolved).
+ *
+ * @version $Rev$ $Date$
+ * @since 2.1.0
+ */
+public class CachingLocaleUrlDefinitionDAO extends BaseLocaleUrlDefinitionDAO {
+
+    /**
+     * The locale-specific set of definitions objects.
+     */
+    private Map<Locale, Map<String, Definition>> locale2definitionMap;
+
+    /**
+     * Constructor.
+     *
+     * @since 2.1.0
+     */
+    public CachingLocaleUrlDefinitionDAO() {
+        locale2definitionMap = new HashMap<Locale, Map<String, Definition>>();
+    }
+
+    /** {@inheritDoc} */
+    public Definition getDefinition(String name, Locale customizationKey) {
+        Definition retValue = null;
+        Map<String, Definition> definitions = getDefinitions(customizationKey);
+        if (definitions != null) {
+            retValue = definitions.get(name);
+        }
+
+        return retValue;
+    }
+
+    /** {@inheritDoc} */
+    public Map<String, Definition> getDefinitions(Locale customizationKey) {
+        if (customizationKey == null) {
+            customizationKey = LocaleUtil.NULL_LOCALE;
+        }
+        Map<String, Definition> retValue = locale2definitionMap
+                .get(customizationKey);
+        if (retValue == null || refreshRequired()) {
+            retValue = checkAndloadDefinitions(customizationKey);
+        }
+        return retValue;
+    }
+
+    /**
+     * Checks if URLs have changed. If yes, it clears the cache. Then continues
+     * loading definitions.
+     *
+     * @param customizationKey The locale to use when loading URLs.
+     * @return The loaded definitions.
+     * @since 2.1.0
+     */
+    protected synchronized Map<String, Definition> checkAndloadDefinitions(
+            Locale customizationKey) {
+        if (refreshRequired()) {
+            locale2definitionMap.clear();
+        }
+        return loadDefinitions(customizationKey);
+    }
+
+    /**
+     * Loads definitions from the URLs..
+     *
+     * @param customizationKey The locale to use when loading URLs.
+     * @return The loaded definitions.
+     * @since 2.1.0
+     */
+    protected Map<String, Definition> loadDefinitions(Locale customizationKey) {
+        Map<String, Definition> localeDefsMap = locale2definitionMap
+                .get(customizationKey);
+        if (localeDefsMap != null) {
+            return localeDefsMap;
+        }
+
+        String postfix = LocaleUtil.calculatePostfix(customizationKey);
+        Locale parentLocale = LocaleUtil.getParentLocale(customizationKey);
+        localeDefsMap = new HashMap<String, Definition>();
+        if (parentLocale != null) {
+            Map<String, Definition> parentDefs = loadDefinitions(parentLocale);
+            if (parentDefs != null) {
+                localeDefsMap.putAll(parentDefs);
+            }
+        }
+        // For each source, the URL must be loaded.
+        for (URL url : sourceURLs) {
+            String path = url.toExternalForm();
+
+            String newPath = LocaleUtil.concatPostfix(path, postfix);
+            try {
+                URL newUrl = new URL(newPath);
+                Map<String, Definition> defsMap = loadDefinitionsFromURL(newUrl);
+                if (defsMap != null) {
+                    localeDefsMap.putAll(defsMap);
+                }
+            } catch (MalformedURLException e) {
+                throw new DefinitionsFactoryException("Error parsing URL "
+                        + newPath, e);
+            }
+        }
+        locale2definitionMap.put(customizationKey, localeDefsMap);
+        return localeDefsMap;
+    }
+}

Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAO.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAO.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/LocaleUrlDefinitionDAO.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/LocaleUrlDefinitionDAO.java?rev=667964&r1=667963&r2=667964&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/LocaleUrlDefinitionDAO.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/LocaleUrlDefinitionDAO.java Sun Jun 15 08:00:54 2008
@@ -20,30 +20,16 @@
  */
 package org.apache.tiles.definition.dao;
 
-import java.io.FileNotFoundException;
-import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URL;
-import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
-import java.util.Set;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.tiles.Definition;
-import org.apache.tiles.Initializable;
-import org.apache.tiles.TilesApplicationContext;
-import org.apache.tiles.awareness.TilesApplicationContextAware;
-import org.apache.tiles.definition.DefinitionsFactory;
 import org.apache.tiles.definition.DefinitionsFactoryException;
-import org.apache.tiles.definition.DefinitionsReader;
-import org.apache.tiles.definition.RefreshMonitor;
-import org.apache.tiles.definition.digester.DigesterDefinitionsReader;
-import org.apache.tiles.impl.BasicTilesContainer;
-import org.apache.tiles.util.ClassUtil;
 import org.apache.tiles.util.LocaleUtil;
 
 /**
@@ -53,45 +39,7 @@
  * @version $Rev$ $Date$
  * @since 2.1.0
  */
-public class LocaleUrlDefinitionDAO implements DefinitionDAO<Locale>,
-        Initializable, TilesApplicationContextAware, RefreshMonitor, URLReader {
-
-    /**
-     * The logging object.
-     */
-    private static final Log LOG = LogFactory
-            .getLog(LocaleUrlDefinitionDAO.class);
-
-    /**
-     * Compatibility constant.
-     *
-     * @deprecated use {@link DEFINITIONS_CONFIG} to avoid namespace collisions.
-     */
-    private static final String LEGACY_DEFINITIONS_CONFIG = "definitions-config";
-
-    /**
-     * Contains the URL objects identifying where configuration data is found.
-     *
-     * @since 2.1.0
-     */
-    protected List<URL> sourceURLs;
-
-    /**
-     * Contains the dates that the URL sources were last modified.
-     */
-    protected Map<String, Long> lastModifiedDates;
-
-    /**
-     * The application context.
-     *
-     * @since 2.1.0
-     */
-    protected TilesApplicationContext applicationContext;
-
-    /**
-     * Reader used to get definitions from the sources.
-     */
-    protected DefinitionsReader reader;
+public class LocaleUrlDefinitionDAO extends BaseLocaleUrlDefinitionDAO {
 
     /**
      * Constructor.
@@ -132,163 +80,17 @@
                         (String) postfix);
                 try {
                     URL newUrl = new URL(newPath);
-                    URLConnection connection = newUrl.openConnection();
-                    connection.connect();
-                    lastModifiedDates.put(newUrl.toExternalForm(), connection
-                            .getLastModified());
-
-                    // Definition must be collected, starting from the base
-                    // source up to the last localized file.
-                    Map<String, Definition> defsMap = reader.read(connection
-                            .getInputStream());
+                    Map<String, Definition> defsMap = loadDefinitionsFromURL(newUrl);
                     if (defsMap != null) {
                         localeDefsMap.putAll(defsMap);
                     }
-                } catch (FileNotFoundException e) {
-                    // File not found. continue.
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("File " + newPath + " not found, continue");
-                    }
-                } catch (IOException e) {
-                    throw new DefinitionsFactoryException(
-                            "I/O error processing configuration.", e);
+                } catch (MalformedURLException e) {
+                    throw new DefinitionsFactoryException("Error parsing URL "
+                        + newPath, e);
                 }
             }
         }
 
         return localeDefsMap;
     }
-
-    /**  {@inheritDoc}*/
-    public void setSourceURLs(List<URL> sourceURLs) {
-        this.sourceURLs = sourceURLs;
-    }
-
-    /**  {@inheritDoc}*/
-    public void setReader(DefinitionsReader reader) {
-        this.reader = reader;
-    }
-
-    /**  {@inheritDoc}*/
-    public void addSourceURL(URL sourceURL) {
-        if (sourceURLs == null) {
-            sourceURLs = new ArrayList<URL>();
-        }
-        sourceURLs.add(sourceURL);
-    }
-
-    /** {@inheritDoc} */
-    public void setApplicationContext(TilesApplicationContext applicationContext) {
-        this.applicationContext = applicationContext;
-    }
-
-    /** {@inheritDoc} */
-    public void init(Map<String, String> params) {
-        identifySources(params);
-        String readerClassName = params
-                .get(DefinitionsFactory.READER_IMPL_PROPERTY);
-
-        if (readerClassName != null) {
-            reader = (DefinitionsReader) ClassUtil.instantiate(readerClassName);
-        } else {
-            reader = new DigesterDefinitionsReader();
-        }
-        reader.init(params);
-    }
-
-    /** {@inheritDoc} */
-    public boolean refreshRequired() {
-        boolean status = false;
-
-        Set<String> urls = lastModifiedDates.keySet();
-
-        try {
-            for (String urlPath : urls) {
-                Long lastModifiedDate = lastModifiedDates.get(urlPath);
-                URL url = new URL(urlPath);
-                URLConnection connection = url.openConnection();
-                connection.connect();
-                long newModDate = connection.getLastModified();
-                if (newModDate != lastModifiedDate) {
-                    status = true;
-                    break;
-                }
-            }
-        } catch (Exception e) {
-            LOG.warn("Exception while monitoring update times.", e);
-            return true;
-        }
-        return status;
-    }
-
-    /**
-     * Detects the sources to load.
-     *
-     * @param initParameters The initialization parameters.
-     * @since 2.1.0
-     */
-    protected void identifySources(Map<String, String> initParameters) {
-        if (applicationContext == null) {
-            throw new IllegalStateException(
-                    "The TilesApplicationContext cannot be null");
-        }
-
-        String resourceString = getResourceString(initParameters);
-        String[] resources = getResourceNames(resourceString);
-
-        try {
-            for (int i = 0; i < resources.length; i++) {
-                URL resourceUrl = applicationContext.getResource(resources[i]);
-                if (resourceUrl != null) {
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Adding resource '" + resourceUrl
-                                + "' to definitions factory.");
-                    }
-                    sourceURLs.add(resourceUrl);
-                } else {
-                    LOG.warn("Unable to find configured definition '"
-                            + resources[i] + "'");
-                }
-            }
-        } catch (IOException e) {
-            throw new DefinitionsFactoryException(
-                    "Unable to parse definitions from " + resourceString, e);
-        }
-    }
-
-    /**
-     * Derive the resource string from the initialization parameters. If no
-     * parameter {@link DefinitionsFactory#DEFINITIONS_CONFIG} is available,
-     * attempts to retrieve {@link BasicTilesContainer#DEFINITIONS_CONFIG} and
-     * {@link #LEGACY_DEFINITIONS_CONFIG}. If neither are available, returns
-     * "/WEB-INF/tiles.xml".
-     *
-     * @param parms The initialization parameters.
-     * @return resource string to be parsed.
-     */
-    @SuppressWarnings("deprecation")
-    protected String getResourceString(Map<String, String> parms) {
-        String resourceStr = parms.get(DefinitionsFactory.DEFINITIONS_CONFIG);
-        if (resourceStr == null) {
-            resourceStr = parms.get(BasicTilesContainer.DEFINITIONS_CONFIG);
-        }
-        if (resourceStr == null) {
-            resourceStr = parms.get(LEGACY_DEFINITIONS_CONFIG);
-        }
-        if (resourceStr == null) {
-            resourceStr = "/WEB-INF/tiles.xml";
-        }
-        return resourceStr;
-    }
-
-    /**
-     * Parse the resourceString into a list of resource paths which can be
-     * loaded by the application context.
-     *
-     * @param resourceString comma separated resources
-     * @return parsed resources
-     */
-    protected String[] getResourceNames(String resourceString) {
-        return resourceString.split(",");
-    }
 }

Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/util/LocaleUtil.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/util/LocaleUtil.java?rev=667964&r1=667963&r2=667964&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/util/LocaleUtil.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/util/LocaleUtil.java Sun Jun 15 08:00:54 2008
@@ -33,6 +33,13 @@
 public final class LocaleUtil {
 
     /**
+     * The "null" Locale, i.e. a Locale that points to no real locale.
+     *
+     * @since 2.1.0
+     */
+    public static final Locale NULL_LOCALE = new Locale("");
+
+    /**
      * Private constructor to avoid instantiation.
      */
     private LocaleUtil() {
@@ -99,6 +106,38 @@
     }
 
     /**
+     * Calculate the postfix to append to a filename to load the correct single
+     * filename for that Locale.
+     *
+     * @param locale The locale.
+     * @return The postfix to append to the filename.
+     * @since 2.1.0
+     */
+    public static String calculatePostfix(Locale locale) {
+        if (locale == null) {
+            return "";
+        }
+
+        StringBuilder builder = new StringBuilder();
+        String language = locale.getLanguage();
+        String country = locale.getCountry();
+        String variant = locale.getVariant();
+        if (!"".equals(language)) {
+            builder.append("_");
+            builder.append(language);
+            if (!"".equals(country)) {
+                builder.append("_");
+                builder.append(country);
+                if (!"".equals(variant)) {
+                    builder.append("_");
+                    builder.append(variant);
+                }
+            }
+        }
+        return builder.toString();
+    }
+
+    /**
      * Concat postfix to the name. Take care of existing filename extension.
      * Transform the given name "name.ext" to have "name" + "postfix" + "ext".
      * If there is no ext, return "name" + "postfix".
@@ -109,7 +148,7 @@
      * @since 2.1.0
      */
     public static String concatPostfix(String name, String postfix) {
-        if (postfix == null) {
+        if (postfix == null || "".equals(postfix)) {
             return name;
         }
 
@@ -125,4 +164,36 @@
         name = name.substring(0, dotIndex);
         return name + postfix + ext;
     }
+
+    /**
+     * <p>
+     * Returns the "parent" locale of a given locale.
+     * </p>
+     * <p>
+     * If the original locale is only language-based, the {@link #NULL_LOCALE}
+     * object is returned.
+     * </p>
+     * <p>
+     * If the original locale is {@link #NULL_LOCALE}, then <code>null</code>
+     * is returned.
+     * </p>
+     *
+     * @param locale The original locale.
+     * @return The parent locale.
+     */
+    public static Locale getParentLocale(Locale locale) {
+        Locale retValue = null;
+        String language = locale.getLanguage();
+        String country = locale.getCountry();
+        String variant = locale.getVariant();
+        if (!"".equals(variant)) {
+            retValue = new Locale(language, country);
+        } else if (!"".equals(country)) {
+            retValue = new Locale(language);
+        } else if (!"".equals(language)) {
+            retValue = NULL_LOCALE;
+        }
+
+        return retValue;
+    }
 }

Modified: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/TestUrlDefinitionsFactory.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/TestUrlDefinitionsFactory.java?rev=667964&r1=667963&r2=667964&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/TestUrlDefinitionsFactory.java (original)
+++ tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/TestUrlDefinitionsFactory.java Sun Jun 15 08:00:54 2008
@@ -23,7 +23,6 @@
 
 import java.net.URL;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
@@ -33,7 +32,6 @@
 
 import org.apache.tiles.TilesApplicationContext;
 import org.apache.tiles.context.TilesRequestContext;
-import org.apache.tiles.util.LocaleUtil;
 import org.easymock.EasyMock;
 
 /**
@@ -44,11 +42,6 @@
 public class TestUrlDefinitionsFactory extends TestCase {
 
     /**
-     * The number of foreseen URLs with postfixes.
-     */
-    private static final int POSTFIX_COUNT = 3;
-
-    /**
      * The definitions factory.
      */
     private UrlDefinitionsFactory factory;
@@ -367,32 +360,4 @@
                 instanceCount + 1,
                 MockDefinitionsReader.getInstanceCount());
     }
-
-    /**
-     * Tests the calculatePostfixes method.
-     */
-    public void testCalculatePostfixes() {
-        Locale locale = Locale.US;
-
-        List<String> posts = LocaleUtil.calculatePostfixes(locale);
-        assertEquals(POSTFIX_COUNT, posts.size());
-        assertTrue(posts.contains("_en_US"));
-        assertTrue(posts.contains("_en"));
-
-        locale = Locale.ENGLISH;
-        posts = LocaleUtil.calculatePostfixes(locale);
-        assertEquals(2, posts.size());
-        assertTrue(posts.contains("_en"));
-    }
-
-    /**
-     * Tests the concatPostfix method.
-     */
-    public void testConcatPostfix() {
-        String postfix = "_en_US";
-        assertEquals("a_en_US", LocaleUtil.concatPostfix("a", postfix));
-        assertEquals("a_en_US.jsp", LocaleUtil.concatPostfix("a.jsp", postfix));
-        assertEquals("file_en_US.jsp", LocaleUtil.concatPostfix("file.jsp", postfix));
-        assertEquals("./path/file_en_US.jsp", LocaleUtil.concatPostfix("./path/file.jsp", postfix));
-    }
 }

Added: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAOTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAOTest.java?rev=667964&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAOTest.java (added)
+++ tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAOTest.java Sun Jun 15 08:00:54 2008
@@ -0,0 +1,586 @@
+/*
+ * $Id$
+ *
+ * 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.tiles.definition.dao;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.tiles.Definition;
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.awareness.TilesApplicationContextAware;
+import org.apache.tiles.context.TilesRequestContext;
+import org.apache.tiles.definition.DefinitionsFactory;
+import org.apache.tiles.definition.DefinitionsReader;
+import org.apache.tiles.definition.MockDefinitionsReader;
+import org.apache.tiles.definition.RefreshMonitor;
+import org.apache.tiles.definition.digester.DigesterDefinitionsReader;
+import org.easymock.EasyMock;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests {@link CachingLocaleUrlDefinitionDAO}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class CachingLocaleUrlDefinitionDAOTest extends TestCase {
+
+    /**
+     * The time (in milliseconds) to wait to be sure that the system updates the
+     * modify date of a file.
+     */
+    private static final int SLEEP_MILLIS = 30000;
+
+    /**
+     * The object to test.
+     */
+    private CachingLocaleUrlDefinitionDAO definitionDao;
+
+    /** {@inheritDoc} */
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        definitionDao = new CachingLocaleUrlDefinitionDAO();
+    }
+
+    /**
+     * Tests {@link LocaleUrlDefinitionDAO#getDefinition(String, Locale)}.
+     *
+     * @throws IOException If something goes wrong.
+     */
+    public void testGetDefinition() throws IOException {
+        // Set up multiple data sources.
+        URL url1 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs1.xml");
+        assertNotNull("Could not load defs1 file.", url1);
+        URL url2 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs2.xml");
+        assertNotNull("Could not load defs2 file.", url2);
+        URL url3 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs3.xml");
+        assertNotNull("Could not load defs3 file.", url3);
+
+        TilesApplicationContext applicationContext = EasyMock
+                .createMock(TilesApplicationContext.class);
+        EasyMock.expect(
+                applicationContext
+                        .getResource("org/apache/tiles/config/defs1.xml"))
+                .andReturn(url1);
+        EasyMock.expect(
+                applicationContext
+                        .getResource("org/apache/tiles/config/defs2.xml"))
+                .andReturn(url2);
+        EasyMock.expect(
+                applicationContext
+                        .getResource("org/apache/tiles/config/defs3.xml"))
+                .andReturn(url3);
+        EasyMock.replay(applicationContext);
+        definitionDao.setApplicationContext(applicationContext);
+
+        Map<String, String> params = new HashMap<String, String>();
+        params.put(DefinitionsFactory.DEFINITIONS_CONFIG,
+                "org/apache/tiles/config/defs1.xml,org/apache/tiles/config/defs2.xml,"
+                        + "org/apache/tiles/config/defs3.xml");
+        definitionDao.init(params);
+
+        assertNotNull("test.def1 definition not found.", definitionDao
+                .getDefinition("test.def1", null));
+        assertNotNull("test.def2 definition not found.", definitionDao
+                .getDefinition("test.def2", null));
+        assertNotNull("test.def3 definition not found.", definitionDao
+                .getDefinition("test.def3", null));
+        assertNotNull("test.common definition not found.", definitionDao
+                .getDefinition("test.common", null));
+        assertNotNull("test.common definition in US locale not found.",
+                definitionDao.getDefinition("test.common", Locale.US));
+        assertNotNull("test.common definition in FRENCH locale not found.",
+                definitionDao.getDefinition("test.common", Locale.FRENCH));
+        assertNotNull("test.common definition in CHINA locale not found.",
+                definitionDao.getDefinition("test.common", Locale.CHINA));
+        assertNotNull(
+                "test.common.french definition in FRENCH locale not found.",
+                definitionDao.getDefinition("test.common.french",
+                        Locale.FRENCH));
+        assertNotNull(
+                "test.common.french definition in CANADA_FRENCH locale not found.",
+                definitionDao.getDefinition("test.common.french",
+                        Locale.CANADA_FRENCH));
+        assertNotNull("test.def.toextend definition not found.", definitionDao
+                .getDefinition("test.def.toextend", null));
+        assertNotNull("test.def.overridden definition not found.",
+                definitionDao.getDefinition("test.def.overridden", null));
+        assertNotNull(
+                "test.def.overridden definition in FRENCH locale not found.",
+                definitionDao.getDefinition("test.def.overridden",
+                        Locale.FRENCH));
+
+        assertEquals("Incorrect default country value", "default",
+                definitionDao.getDefinition("test.def1", null).getAttribute(
+                        "country").getValue());
+        assertEquals("Incorrect US country value", "US", definitionDao
+                .getDefinition("test.def1", Locale.US).getAttribute("country")
+                .getValue());
+        assertEquals("Incorrect France country value", "France", definitionDao
+                .getDefinition("test.def1", Locale.FRENCH).getAttribute(
+                        "country").getValue());
+        assertEquals("Incorrect Chinese country value (should be default)",
+                "default", definitionDao.getDefinition("test.def1",
+                        Locale.CHINA).getAttribute("country").getValue());
+        assertEquals("Incorrect default country value", "default",
+                definitionDao.getDefinition("test.def.overridden", null)
+                        .getAttribute("country").getValue());
+        assertEquals("Incorrect default title value",
+                "Definition to be overridden", definitionDao.getDefinition(
+                        "test.def.overridden", null).getAttribute("title")
+                        .getValue());
+        assertEquals("Incorrect France country value", "France", definitionDao
+                .getDefinition("test.def.overridden", Locale.FRENCH)
+                .getAttribute("country").getValue());
+        assertNull("Definition in French not found", definitionDao
+                .getDefinition("test.def.overridden", Locale.FRENCH)
+                .getAttribute("title"));
+    }
+
+    /**
+     * Tests {@link LocaleUrlDefinitionDAO#getDefinitions(Locale)}.
+     *
+     * @throws IOException If something goes wrong.
+     */
+    public void testGetDefinitions() throws IOException {
+        // Set up multiple data sources.
+        URL url1 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs1.xml");
+        assertNotNull("Could not load defs1 file.", url1);
+        URL url2 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs2.xml");
+        assertNotNull("Could not load defs2 file.", url2);
+        URL url3 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs3.xml");
+        assertNotNull("Could not load defs3 file.", url3);
+
+        TilesApplicationContext applicationContext = EasyMock
+                .createMock(TilesApplicationContext.class);
+        EasyMock.expect(
+                applicationContext
+                        .getResource("org/apache/tiles/config/defs1.xml"))
+                .andReturn(url1);
+        EasyMock.expect(
+                applicationContext
+                        .getResource("org/apache/tiles/config/defs2.xml"))
+                .andReturn(url2);
+        EasyMock.expect(
+                applicationContext
+                        .getResource("org/apache/tiles/config/defs3.xml"))
+                .andReturn(url3);
+        EasyMock.replay(applicationContext);
+        definitionDao.setApplicationContext(applicationContext);
+
+        Map<String, String> params = new HashMap<String, String>();
+        params.put(DefinitionsFactory.DEFINITIONS_CONFIG,
+                "org/apache/tiles/config/defs1.xml,org/apache/tiles/config/defs2.xml,"
+                        + "org/apache/tiles/config/defs3.xml");
+        definitionDao.init(params);
+
+        Map<String, Definition> defaultDefinitions = definitionDao
+                .getDefinitions(null);
+        Map<String, Definition> usDefinitions = definitionDao
+                .getDefinitions(Locale.US);
+        Map<String, Definition> frenchDefinitions = definitionDao
+                .getDefinitions(Locale.FRENCH);
+        Map<String, Definition> chinaDefinitions = definitionDao
+                .getDefinitions(Locale.CHINA);
+        Map<String, Definition> canadaFrendDefinitions = definitionDao
+                .getDefinitions(Locale.CANADA_FRENCH);
+
+        assertNotNull("test.def1 definition not found.", defaultDefinitions
+                .get("test.def1"));
+        assertNotNull("test.def2 definition not found.", defaultDefinitions
+                .get("test.def2"));
+        assertNotNull("test.def3 definition not found.", defaultDefinitions
+                .get("test.def3"));
+        assertNotNull("test.common definition not found.", defaultDefinitions
+                .get("test.common"));
+        assertNotNull("test.common definition in US locale not found.",
+                usDefinitions.get("test.common"));
+        assertNotNull("test.common definition in FRENCH locale not found.",
+                frenchDefinitions.get("test.common"));
+        assertNotNull("test.common definition in CHINA locale not found.",
+                chinaDefinitions.get("test.common"));
+        assertNotNull(
+                "test.common.french definition in FRENCH locale not found.",
+                canadaFrendDefinitions.get("test.common.french"));
+        assertNotNull(
+                "test.common.french definition in CANADA_FRENCH locale not found.",
+                canadaFrendDefinitions.get("test.common.french"));
+        assertNotNull("test.def.toextend definition not found.",
+                defaultDefinitions.get("test.def.toextend"));
+        assertNotNull("test.def.overridden definition not found.",
+                defaultDefinitions.get("test.def.overridden"));
+        assertNotNull(
+                "test.def.overridden definition in FRENCH locale not found.",
+                frenchDefinitions.get("test.def.overridden"));
+
+        assertEquals("Incorrect default country value", "default",
+                defaultDefinitions.get("test.def1").getAttribute("country")
+                        .getValue());
+        assertEquals("Incorrect US country value", "US", usDefinitions.get(
+                "test.def1").getAttribute("country").getValue());
+        assertEquals("Incorrect France country value", "France",
+                frenchDefinitions.get("test.def1").getAttribute("country")
+                        .getValue());
+        assertEquals("Incorrect Chinese country value (should be default)",
+                "default", chinaDefinitions.get("test.def1").getAttribute(
+                        "country").getValue());
+        assertEquals("Incorrect default country value", "default",
+                defaultDefinitions.get("test.def.overridden").getAttribute(
+                        "country").getValue());
+        assertEquals("Incorrect default title value",
+                "Definition to be overridden", defaultDefinitions.get(
+                        "test.def.overridden").getAttribute("title").getValue());
+        assertEquals("Incorrect France country value", "France",
+                frenchDefinitions.get("test.def.overridden").getAttribute(
+                        "country").getValue());
+        assertNull("Definition in French not found", frenchDefinitions.get(
+                "test.def.overridden").getAttribute("title"));
+    }
+
+    /**
+     * Tests {@link LocaleUrlDefinitionDAO#setSourceURLs(List)}.
+     */
+    public void testSetSourceURLs() {
+        // Set up multiple data sources.
+        URL url1 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs1.xml");
+        assertNotNull("Could not load defs1 file.", url1);
+        URL url2 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs2.xml");
+        assertNotNull("Could not load defs2 file.", url2);
+        URL url3 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs3.xml");
+        assertNotNull("Could not load defs3 file.", url3);
+        List<URL> sourceURLs = new ArrayList<URL>();
+        sourceURLs.add(url1);
+        sourceURLs.add(url2);
+        sourceURLs.add(url3);
+        definitionDao.setSourceURLs(sourceURLs);
+        assertEquals("The source URLs are not set correctly", sourceURLs,
+                definitionDao.sourceURLs);
+    }
+
+    /**
+     * Tests {@link LocaleUrlDefinitionDAO#setReader(DefinitionsReader)}.
+     */
+    public void testSetReader() {
+        DefinitionsReader reader = EasyMock.createMock(DefinitionsReader.class);
+        definitionDao.setReader(reader);
+        assertEquals("There reader has not been set correctly", reader,
+                definitionDao.reader);
+    }
+
+    /**
+     * Tests {@link LocaleUrlDefinitionDAO#addSourceURL(URL)}.
+     */
+    public void testAddSourceURL() {
+        // Set up multiple data sources.
+        URL url1 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs1.xml");
+        assertNotNull("Could not load defs1 file.", url1);
+        URL url2 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs2.xml");
+        assertTrue("The source URLs is not empty", definitionDao.sourceURLs
+                .isEmpty());
+        definitionDao.addSourceURL(url1);
+        assertEquals("The size of the source URLs is not correct", 1,
+                definitionDao.sourceURLs.size());
+        assertEquals("The URL is not correct", url1, definitionDao.sourceURLs
+                .get(0));
+        List<URL> sourceURLs = new ArrayList<URL>();
+        sourceURLs.add(url2);
+        definitionDao.setSourceURLs(sourceURLs);
+        definitionDao.addSourceURL(url1);
+        assertEquals("The size of the source URLs is not correct", 2,
+                definitionDao.sourceURLs.size());
+        assertEquals("The URL is not correct", url2, definitionDao.sourceURLs
+                .get(0));
+        assertEquals("The URL is not correct", url1, definitionDao.sourceURLs
+                .get(1));
+    }
+
+    /**
+     * Tests {@link LocaleUrlDefinitionDAO#setApplicationContext(TilesApplicationContext)}.
+     */
+    public void testSetApplicationContext() {
+        TilesApplicationContext applicationContext = EasyMock
+                .createMock(TilesApplicationContext.class);
+        definitionDao.setApplicationContext(applicationContext);
+        assertEquals("The application context has not been set",
+                applicationContext, definitionDao.applicationContext);
+    }
+
+    /**
+     * Tests {@link LocaleUrlDefinitionDAO#init(Map)}.
+     *
+     * @throws IOException If something goes wrong.
+     */
+    public void testInit() throws IOException {
+        URL url1 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs1.xml");
+        URL url2 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs2.xml");
+        URL url3 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs3.xml");
+        TilesApplicationContext applicationContext = EasyMock
+                .createMock(TilesApplicationContext.class);
+        EasyMock.expect(applicationContext.getResource("/WEB-INF/tiles.xml"))
+                .andReturn(url1);
+        EasyMock.replay(applicationContext);
+        Map<String, String> params = new HashMap<String, String>();
+        definitionDao.setApplicationContext(applicationContext);
+        definitionDao.init(params);
+        assertEquals("The reader is not of the correct class",
+                DigesterDefinitionsReader.class, definitionDao.reader
+                        .getClass());
+        List<URL> sourceURLs = new ArrayList<URL>();
+        sourceURLs.add(url1);
+        assertEquals("The source URLs are not correct", sourceURLs,
+                definitionDao.sourceURLs);
+        EasyMock.reset(applicationContext);
+
+        applicationContext = EasyMock.createMock(TilesApplicationContext.class);
+        EasyMock.expect(
+                applicationContext
+                        .getResource("org/apache/tiles/config/defs1.xml"))
+                .andReturn(url1);
+        EasyMock.expect(
+                applicationContext
+                        .getResource("org/apache/tiles/config/defs2.xml"))
+                .andReturn(url2);
+        EasyMock.expect(
+                applicationContext
+                        .getResource("org/apache/tiles/config/defs3.xml"))
+                .andReturn(url3);
+        EasyMock.replay(applicationContext);
+        params.clear();
+        params.put(DefinitionsFactory.READER_IMPL_PROPERTY,
+                MockDefinitionsReader.class.getName());
+        params.put(DefinitionsFactory.DEFINITIONS_CONFIG,
+                "org/apache/tiles/config/defs1.xml,"
+                        + "org/apache/tiles/config/defs2.xml,"
+                        + "org/apache/tiles/config/defs3.xml");
+        definitionDao.setApplicationContext(applicationContext);
+        definitionDao.setSourceURLs(new ArrayList<URL>());
+        definitionDao.init(params);
+        assertEquals("The reader is not of the correct class",
+                MockDefinitionsReader.class, definitionDao.reader.getClass());
+        sourceURLs = new ArrayList<URL>();
+        sourceURLs.add(url1);
+        sourceURLs.add(url2);
+        sourceURLs.add(url3);
+        assertEquals("The source URLs are not correct", sourceURLs,
+                definitionDao.sourceURLs);
+    }
+
+    /**
+     * Tests {@link LocaleUrlDefinitionDAO#identifySources(Map)}.
+     *
+     * @throws IOException If something goes wrong.
+     */
+    public void testIdentifySources() throws IOException {
+        URL url1 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs1.xml");
+        URL url2 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs2.xml");
+        URL url3 = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/defs3.xml");
+        TilesApplicationContext applicationContext = EasyMock
+                .createMock(TilesApplicationContext.class);
+        EasyMock.expect(
+                applicationContext
+                        .getResource("org/apache/tiles/config/defs1.xml"))
+                .andReturn(url1);
+        EasyMock.expect(
+                applicationContext
+                        .getResource("org/apache/tiles/config/defs2.xml"))
+                .andReturn(url2);
+        EasyMock.expect(
+                applicationContext
+                        .getResource("org/apache/tiles/config/defs3.xml"))
+                .andReturn(url3);
+        EasyMock.replay(applicationContext);
+        Map<String, String> params = new HashMap<String, String>();
+        params.put(DefinitionsFactory.DEFINITIONS_CONFIG,
+                "org/apache/tiles/config/defs1.xml,"
+                        + "org/apache/tiles/config/defs2.xml,"
+                        + "org/apache/tiles/config/defs3.xml");
+        definitionDao.setApplicationContext(applicationContext);
+        definitionDao.setSourceURLs(new ArrayList<URL>());
+        definitionDao.identifySources(params);
+        List<URL> sourceURLs = new ArrayList<URL>();
+        sourceURLs.add(url1);
+        sourceURLs.add(url2);
+        sourceURLs.add(url3);
+        assertEquals("The source URLs are not correct", sourceURLs,
+                definitionDao.sourceURLs);
+    }
+
+    /**
+     * Tests {@link LocaleUrlDefinitionDAO#getResourceString(Map)}.
+     */
+    public void testGetResourceString() {
+        Map<String, String> params = new HashMap<String, String>();
+        params.put(DefinitionsFactory.DEFINITIONS_CONFIG, "The string value");
+        assertEquals("The resource string has not been got correctly",
+                "The string value", definitionDao.getResourceString(params));
+    }
+
+    /**
+     * Tests {@link LocaleUrlDefinitionDAO#getResourceNames(String)}.
+     */
+    public void testGetResourceNames() {
+        String toSplit = "This,will,be,split";
+        String[] splitted = toSplit.split(",");
+        String[] result = definitionDao.getResourceNames(toSplit);
+        for (int i = 0; i < splitted.length; i++) {
+            assertEquals("The string has not been split correctly", splitted[i],
+                    result[i]);
+        }
+    }
+
+    /**
+     * Tests {@link LocaleUrlDefinitionDAO#refreshRequired()}.
+     *
+     * @throws IOException If something goes wrong during I/O.
+     * @throws InterruptedException If the "sleep" instruction fails.
+     * @throws URISyntaxException If the URIs are not correct.
+     */
+    public void testRefreshRequired() throws IOException, InterruptedException,
+            URISyntaxException {
+        // Set up multiple data sources.
+        URL url = this.getClass().getClassLoader().getResource(
+                "org/apache/tiles/config/temp-defs.xml");
+
+        URI uri = null;
+        String urlPath = null;
+
+        // The following madness is necessary b/c of the way Windows hanndles
+        // URLs.
+        // We must add a slash to the protocol if Windows does not. But we
+        // cannot
+        // add a slash to Unix paths b/c they already have one.
+        if (url.getPath().startsWith("/")) {
+            urlPath = "file:" + url.getPath();
+        } else {
+            urlPath = "file:/" + url.getPath();
+        }
+
+        TilesApplicationContext applicationContext = EasyMock
+                .createMock(TilesApplicationContext.class);
+        EasyMock.expect(applicationContext.getResource(urlPath)).andReturn(url);
+        EasyMock.replay(applicationContext);
+        ((TilesApplicationContextAware) definitionDao)
+                .setApplicationContext(applicationContext);
+
+        // The following second madness is necessary b/c sometimes spaces
+        // are encoded as '%20', sometimes they are not. For example in
+        // Windows 2000 under Eclipse they are encoded, under the prompt of
+        // Windows 2000 they are not.
+        // It seems to be in the different behaviour of
+        // sun.misc.Launcher$AppClassLoader (called under Eclipse) and
+        // java.net.URLClassLoader (under maven).
+        // And an URL accepts spaces while URIs need '%20'.
+        try {
+            uri = new URI(urlPath);
+        } catch (URISyntaxException e) {
+            uri = new URI(urlPath.replaceAll(" ", "%20"));
+        }
+
+        String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"
+                + "<!DOCTYPE tiles-definitions PUBLIC "
+                + "\"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN\" "
+                + "\"http://tiles.apache.org/dtds/tiles-config_2_0.dtd\">\n\n"
+                + "<tiles-definitions>"
+                + "<definition name=\"rewrite.test\" template=\"/test.jsp\">"
+                + "<put-attribute name=\"testparm\" value=\"testval\"/>"
+                + "</definition>" + "</tiles-definitions>";
+
+        File file = new File(uri);
+        FileOutputStream fileOut = new FileOutputStream(file);
+        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
+                fileOut));
+        writer.write(xml);
+        writer.close();
+
+        Map<String, String> params = new HashMap<String, String>();
+        params.put(DefinitionsFactory.DEFINITIONS_CONFIG, urlPath);
+        definitionDao.init(params);
+        TilesRequestContext context = EasyMock
+                .createMock(TilesRequestContext.class);
+        EasyMock.expect(context.getSessionScope()).andReturn(
+                new HashMap<String, Object>()).anyTimes();
+        EasyMock.expect(context.getRequestLocale()).andReturn(null).anyTimes();
+        EasyMock.replay(context);
+
+        Definition definition = definitionDao.getDefinition("rewrite.test",
+                null);
+        assertNotNull("rewrite.test definition not found.", definition);
+        assertEquals("Incorrect initial template value", "/test.jsp",
+                definition.getTemplate());
+
+        RefreshMonitor reloadable = (RefreshMonitor) definitionDao;
+        assertEquals("Factory should be fresh.", false, reloadable
+                .refreshRequired());
+
+        // Make sure the system actually updates the timestamp.
+        Thread.sleep(SLEEP_MILLIS);
+
+        // Set up multiple data sources.
+        xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"
+                + "<!DOCTYPE tiles-definitions PUBLIC "
+                + "\"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN\" "
+                + "\"http://tiles.apache.org/dtds/tiles-config_2_0.dtd\">\n\n"
+                + "<tiles-definitions>"
+                + "<definition name=\"rewrite.test\" template=\"/newtest.jsp\">"
+                + "<put-attribute name=\"testparm\" value=\"testval\"/>"
+                + "</definition>" + "</tiles-definitions>";
+
+        file = new File(uri);
+        fileOut = new FileOutputStream(file);
+        writer = new BufferedWriter(new OutputStreamWriter(fileOut));
+        writer.write(xml);
+        writer.close();
+        file = new File(uri);
+
+        assertEquals("Factory should be stale.", true, reloadable
+                .refreshRequired());
+    }
+}

Propchange: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAOTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAOTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/LocaleUrlDefinitionDAOTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/LocaleUrlDefinitionDAOTest.java?rev=667964&r1=667963&r2=667964&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/LocaleUrlDefinitionDAOTest.java (original)
+++ tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/LocaleUrlDefinitionDAOTest.java Sun Jun 15 08:00:54 2008
@@ -130,7 +130,7 @@
         assertNotNull(
                 "test.common.french definition in FRENCH locale not found.",
                 definitionDao.getDefinition("test.common.french",
-                        Locale.CANADA_FRENCH));
+                        Locale.FRENCH));
         assertNotNull(
                 "test.common.french definition in CANADA_FRENCH locale not found.",
                 definitionDao.getDefinition("test.common.french",
@@ -219,7 +219,7 @@
                 .getDefinitions(Locale.FRENCH);
         Map<String, Definition> chinaDefinitions = definitionDao
                 .getDefinitions(Locale.CHINA);
-        Map<String, Definition> canadaFrendDefinitions = definitionDao
+        Map<String, Definition> canadaFrenchDefinitions = definitionDao
                 .getDefinitions(Locale.CANADA_FRENCH);
 
         assertNotNull("test.def1 definition not found.", defaultDefinitions
@@ -238,10 +238,10 @@
                 chinaDefinitions.get("test.common"));
         assertNotNull(
                 "test.common.french definition in FRENCH locale not found.",
-                canadaFrendDefinitions.get("test.common.french"));
+                frenchDefinitions.get("test.common.french"));
         assertNotNull(
                 "test.common.french definition in CANADA_FRENCH locale not found.",
-                canadaFrendDefinitions.get("test.common.french"));
+                canadaFrenchDefinitions.get("test.common.french"));
         assertNotNull("test.def.toextend definition not found.",
                 defaultDefinitions.get("test.def.toextend"));
         assertNotNull("test.def.overridden definition not found.",

Added: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/util/LocaleUtilTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/util/LocaleUtilTest.java?rev=667964&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/util/LocaleUtilTest.java (added)
+++ tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/util/LocaleUtilTest.java Sun Jun 15 08:00:54 2008
@@ -0,0 +1,98 @@
+/*
+ * $Id$
+ *
+ * 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.tiles.util;
+
+import java.util.List;
+import java.util.Locale;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests {@link LocaleUtil}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class LocaleUtilTest extends TestCase {
+
+    /**
+     * The number of foreseen URLs with postfixes.
+     */
+    private static final int POSTFIX_COUNT = 3;
+
+    /**
+     * Test method for {@link LocaleUtil#calculatePostfixes(java.util.Locale)}.
+     */
+    public void testCalculatePostfixes() {
+        Locale locale = Locale.US;
+
+        List<String> posts = LocaleUtil.calculatePostfixes(locale);
+        assertEquals(POSTFIX_COUNT, posts.size());
+        assertTrue(posts.contains("_en_US"));
+        assertTrue(posts.contains("_en"));
+
+        locale = Locale.ENGLISH;
+        posts = LocaleUtil.calculatePostfixes(locale);
+        assertEquals(2, posts.size());
+        assertTrue(posts.contains("_en"));
+    }
+
+    /**
+     * Test method for {@link LocaleUtil#calculatePostfix(java.util.Locale)}.
+     */
+    public void testCalculatePostfix() {
+        assertEquals("The English locale is not correct", "_en", LocaleUtil
+                .calculatePostfix(Locale.ENGLISH));
+        assertEquals("The US locale is not correct", "_en_US", LocaleUtil
+                .calculatePostfix(Locale.US));
+        Locale locale = new Locale("es", "ES", "Traditional_WIN");
+        assertEquals("The Spain Traditional_WIN locale is not correct",
+                "_es_ES_Traditional_WIN", LocaleUtil.calculatePostfix(locale));
+    }
+
+    /**
+     * Test method for {@link LocaleUtil#concatPostfix(java.lang.String, java.lang.String)}.
+     */
+    public void testConcatPostfix() {
+        String postfix = "_en_US";
+        assertEquals("a_en_US", LocaleUtil.concatPostfix("a", postfix));
+        assertEquals("a_en_US.jsp", LocaleUtil.concatPostfix("a.jsp", postfix));
+        assertEquals("file_en_US.jsp", LocaleUtil.concatPostfix("file.jsp", postfix));
+        assertEquals("./path/file_en_US.jsp", LocaleUtil.concatPostfix("./path/file.jsp", postfix));
+    }
+
+    /**
+     * Test method for {@link LocaleUtil#getParentLocale(Locale)}.
+     */
+    public void testGetParentLocale() {
+        assertNull("The parent locale of NULL_LOCALE is not correct",
+                LocaleUtil.getParentLocale(LocaleUtil.NULL_LOCALE));
+        assertEquals("The parent locale of 'en' is not correct",
+                LocaleUtil.NULL_LOCALE, LocaleUtil
+                        .getParentLocale(Locale.ENGLISH));
+        assertEquals("The parent locale of 'en_US' is not correct",
+                Locale.ENGLISH, LocaleUtil.getParentLocale(Locale.US));
+        Locale locale = new Locale("es", "ES", "Traditional_WIN");
+        Locale parentLocale = new Locale("es", "ES");
+        assertEquals("The parent locale of 'es_ES_Traditional_WIN' is not correct",
+                parentLocale, LocaleUtil.getParentLocale(locale));
+    }
+}

Propchange: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/util/LocaleUtilTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/util/LocaleUtilTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL