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/27 21:12:30 UTC
svn commit: r672360 - in /tiles/framework/trunk/tiles-core/src:
main/java/org/apache/tiles/definition/
main/java/org/apache/tiles/definition/dao/
main/java/org/apache/tiles/factory/ test/java/org/apache/tiles/definition/
test/java/org/apache/tiles/defi...
Author: apetrelli
Date: Fri Jun 27 12:12:29 2008
New Revision: 672360
URL: http://svn.apache.org/viewvc?rev=672360&view=rev
Log:
TILES-280
Created ResolvingLocaleUrlDefinitionDao and used in UrlDefinitionsFactory.
Added:
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAO.java (with props)
tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAOTest.java (with props)
Modified:
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/UrlDefinitionsFactory.java
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAO.java
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/factory/BasicTilesContainerFactory.java
tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/TestUrlDefinitionsFactory.java
Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java?rev=672360&r1=672359&r2=672360&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/DefinitionsImpl.java Fri Jun 27 12:12:29 2008
@@ -36,7 +36,8 @@
* @version $Rev$ $Date$
*
* @deprecated This class is, in fact, part of the implementation of
- * {@link UrlDefinitionsFactory}.
+ * {@link UrlDefinitionsFactory} and
+ * {@link org.apache.tiles.definition.dao.ResolvingLocaleUrlDefinitionDAO}.
*/
@Deprecated
public class DefinitionsImpl implements Definitions {
Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/UrlDefinitionsFactory.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/UrlDefinitionsFactory.java?rev=672360&r1=672359&r2=672360&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/UrlDefinitionsFactory.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/UrlDefinitionsFactory.java Fri Jun 27 12:12:29 2008
@@ -23,23 +23,18 @@
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.awareness.TilesApplicationContextAware;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.definition.dao.DefinitionDAO;
-import org.apache.tiles.definition.dao.LocaleUrlDefinitionDAO;
+import org.apache.tiles.definition.dao.ResolvingLocaleUrlDefinitionDAO;
import org.apache.tiles.definition.dao.URLReader;
import org.apache.tiles.impl.BasicTilesContainer;
import org.apache.tiles.util.LocaleUtil;
import java.net.URL;
import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
-import java.util.Set;
import java.util.StringTokenizer;
/**
@@ -54,9 +49,8 @@
*
* @version $Rev$ $Date$
*/
-public class UrlDefinitionsFactory extends LocaleDefinitionsFactory implements DefinitionsFactory,
- Refreshable, TilesApplicationContextAware,
- Initializable {
+public class UrlDefinitionsFactory extends LocaleDefinitionsFactory implements
+ Refreshable {
/**
* Compatibility constant.
@@ -71,22 +65,6 @@
private static final Log LOG = LogFactory.getLog(UrlDefinitionsFactory.class);
/**
- * Contains a list of locales that have been processed.
- */
- private List<Locale> processedLocales;
-
-
- /**
- * The base set of Definition objects not discriminated by locale.
- */
- private Map<String, Definition> baseDefinitions;
-
- /**
- * The locale-specific set of definitions objects.
- */
- private Map<Locale, Map<String, Definition>> localeSpecificDefinitions;
-
- /**
* Contains the URL objects identifying where configuration data is found.
*
* @deprecated Use {@link #sourceURLs}.
@@ -108,29 +86,6 @@
protected Map<String, Long> lastModifiedDates;
/**
- * Creates a new instance of UrlDefinitionsFactory.
- */
- public UrlDefinitionsFactory() {
- definitionDao = new LocaleUrlDefinitionDAO();
- processedLocales = new ArrayList<Locale>();
- }
-
- /**
- * Initializes the DefinitionsFactory and its subcomponents.
- * <p/>
- * Implementations may support configuration properties to be passed in via
- * the params Map.
- *
- * @param params The Map of configuration properties.
- * @throws DefinitionsFactoryException if an initialization error occurs.
- */
- @SuppressWarnings("unchecked")
- public void init(Map<String, String> params) {
- super.init(params);
- loadDefinitions();
- }
-
- /**
* Returns a Definition object that matches the given name and
* Tiles context.
*
@@ -144,23 +99,17 @@
public Definition getDefinition(String name,
TilesRequestContext tilesContext) {
- Locale locale = null;
-
- if (tilesContext != null) {
- locale = localeResolver.resolveLocale(tilesContext);
- if (!isContextProcessed(tilesContext)) {
- addDefinitions(tilesContext);
- }
- }
+ Locale locale = localeResolver.resolveLocale(tilesContext);
- return getDefinition(name, locale);
+ return definitionDao.getDefinition(name, locale);
}
/** {@inheritDoc} */
public synchronized void refresh() {
LOG.debug("Updating Tiles definitions. . .");
- processedLocales.clear();
- loadDefinitions();
+ if (definitionDao instanceof Refreshable) {
+ ((Refreshable) definitionDao).refresh();
+ }
}
@@ -176,37 +125,6 @@
}
/**
- * Appends locale-specific {@link Definition} objects to existing
- * definitions set by reading locale-specific versions of the applied
- * sources.
- *
- * @param tilesContext The requested locale.
- * @throws DefinitionsFactoryException if an error occurs reading
- * definitions.
- * @since 2.1.0
- */
- protected synchronized void addDefinitions(TilesRequestContext tilesContext) {
-
- Locale locale = localeResolver.resolveLocale(tilesContext);
-
- if (isContextProcessed(tilesContext)) {
- return;
- }
-
- if (locale == null) {
- return;
- }
-
- processedLocales.add(locale);
-
- // At the end of definitions loading, they can be assigned to
- // Definitions implementation, to allow inheritance resolution.
- localeSpecificDefinitions.put(locale, definitionDao
- .getDefinitions(locale));
- resolveInheritances(locale);
- }
-
- /**
* Indicates whether a given context has been processed or not.
* <p/>
* This method can be used to avoid unnecessary synchronization of the
@@ -216,144 +134,11 @@
*
* @param tilesContext The Tiles context to check.
* @return true if the given context has been processed and false otherwise.
+ * @deprecated It always return <code>true</code>.
*/
+ @Deprecated
protected boolean isContextProcessed(TilesRequestContext tilesContext) {
- return processedLocales.contains(localeResolver
- .resolveLocale(tilesContext));
- }
-
- /**
- * Creates a base set by reading configuration data from the applied
- * sources.
- *
- * @throws DefinitionsFactoryException if an error occurs reading the
- * sources.
- * @since 2.1.0
- */
- protected synchronized void loadDefinitions() {
- reset();
-
- baseDefinitions.putAll(definitionDao.getDefinitions(null));
- }
-
- /**
- * Clears definitions.
- *
- * @since 2.1.0
- */
- protected void reset() {
- this.baseDefinitions = new HashMap<String, Definition>();
- this.localeSpecificDefinitions =
- new HashMap<Locale, Map<String, Definition>>();
- }
-
- /**
- * Resolve extended instances.
- *
- * @throws NoSuchDefinitionException If a parent definition is not found.
- * @since 2.1.0
- */
- protected void resolveInheritances() {
- Set<String> alreadyResolvedDefinitions = new HashSet<String>();
-
- for (Definition definition : baseDefinitions.values()) {
- resolveInheritance(definition, null, alreadyResolvedDefinitions);
- } // end loop
- }
-
- /**
- * Resolve locale-specific extended instances.
- *
- * @param locale The locale to use.
- * @throws NoSuchDefinitionException If a parent definition is not found.
- * @since 2.1.0
- */
- protected void resolveInheritances(Locale locale) {
- resolveInheritances();
-
- Map<String, Definition> map = localeSpecificDefinitions.get(locale);
- if (map != null) {
- Set<String> alreadyResolvedDefinitions = new HashSet<String>();
- for (Definition definition : map.values()) {
- resolveInheritance(definition, locale,
- alreadyResolvedDefinitions);
- } // end loop
- }
- }
-
- /**
- * Resolve locale-specific inheritance.
- * First, resolve parent's inheritance, then set template to the parent's
- * template.
- * Also copy attributes setted in parent, and not set in child
- * If instance doesn't extend anything, do nothing.
- *
- * @param definition The definition to resolve
- * @param locale The locale to use.
- * @param alreadyResolvedDefinitions The set of the definitions that have
- * been already resolved.
- * @throws NoSuchDefinitionException If an inheritance can not be solved.
- * @since 2.1.0
- */
- protected void resolveInheritance(Definition definition, Locale locale,
- Set<String> alreadyResolvedDefinitions) {
- // Already done, or not needed ?
- if (!definition.isExtending()
- || alreadyResolvedDefinitions.contains(definition.getName())) {
- return;
- }
-
- if (LOG.isDebugEnabled()) {
- LOG.debug("Resolve definition for child name='"
- + definition.getName()
- + "' extends='" + definition.getExtends() + "'.");
- }
-
- // Set as visited to avoid endless recurisvity.
- alreadyResolvedDefinitions.add(definition.getName());
-
- // Resolve parent before itself.
- Definition parent = getDefinition(definition.getExtends(),
- locale);
- if (parent == null) { // error
- String msg = "Error while resolving definition inheritance: child '"
- + definition.getName()
- + "' can't find its ancestor '"
- + definition.getExtends()
- + "'. Please check your description file.";
- // to do : find better exception
- throw new NoSuchDefinitionException(msg);
- }
-
- resolveInheritance(parent, locale, alreadyResolvedDefinitions);
-
- definition.inherit(parent);
- }
-
- /**
- * Returns a Definition object that matches the given name and locale.
- *
- * @param name The name of the Definition to return.
- * @param locale The locale to use to resolve the definition.
- * @return the Definition matching the given name or null if none is found.
- * @since 2.1.0
- */
- protected Definition getDefinition(String name, Locale locale) {
- Definition definition = null;
-
- if (locale != null) {
- Map<String, Definition> localeSpecificMap =
- localeSpecificDefinitions.get(locale);
- if (localeSpecificMap != null) {
- definition = localeSpecificMap.get(name);
- }
- }
-
- if (definition == null) {
- definition = baseDefinitions.get(name);
- }
-
- return definition;
+ return true;
}
/**
@@ -390,7 +175,7 @@
/** {@inheritDoc} */
@Override
protected DefinitionDAO<Locale> createDefaultDefinitionDAO() {
- return new LocaleUrlDefinitionDAO();
+ return new ResolvingLocaleUrlDefinitionDAO();
}
/**
@@ -404,8 +189,7 @@
*/
@Deprecated
public Definitions readDefinitions() {
- loadDefinitions();
- return new DefinitionsImpl(baseDefinitions, localeSpecificDefinitions);
+ return new CompatibilityDefinitionsImpl(definitionDao);
}
/**
@@ -416,7 +200,7 @@
*/
@Deprecated
protected Definitions getDefinitions() {
- return new DefinitionsImpl(baseDefinitions, localeSpecificDefinitions);
+ return new CompatibilityDefinitionsImpl(definitionDao);
}
/**
@@ -427,12 +211,18 @@
* @param definitions The Definitions object to append to.
* @param tilesContext The requested locale.
* @throws DefinitionsFactoryException if an error occurs reading definitions.
- * @deprecated Use {@link #addDefinitions(TilesRequestContext)}.
+ * @deprecated Let the definitions be loaded by a {@link DefinitionDAO}.
*/
@Deprecated
protected void addDefinitions(Definitions definitions,
TilesRequestContext tilesContext) {
- addDefinitions(tilesContext);
+ Locale locale = localeResolver.resolveLocale(tilesContext);
+ Map<String, Definition> defsMap = definitionDao.getDefinitions(locale);
+ if (defsMap == null) {
+ throw new NullPointerException(
+ "There are no definitions mapped to locale '"
+ + locale.toString() + "'");
+ }
}
/**
@@ -444,7 +234,7 @@
*/
@Deprecated
protected Definitions createDefinitions() {
- return new DefinitionsImpl();
+ return new CompatibilityDefinitionsImpl(definitionDao);
}
/**
@@ -517,4 +307,87 @@
}
return filenames;
}
+
+ /**
+ * {@link Definitions} implementation that uses a {@link DefinitionDAO}.
+ *
+ * @since 2.1.0
+ * @deprecated Here only for compatibility reasons.
+ */
+ @Deprecated
+ private static final class CompatibilityDefinitionsImpl implements Definitions {
+
+ /**
+ * The definition DAO to use.
+ *
+ * @since 2.1.0
+ */
+ private DefinitionDAO<Locale> definitionDao;
+
+ /**
+ * Constructor.
+ *
+ * @param definitionDao The definition DAO to use.
+ * @since 2.1.0
+ */
+ public CompatibilityDefinitionsImpl(DefinitionDAO<Locale> definitionDao) {
+ this.definitionDao = definitionDao;
+ }
+
+ /** {@inheritDoc} */
+ public void addDefinitions(Map<String, Definition> defsMap) {
+ Map<String, Definition> definitions = definitionDao
+ .getDefinitions(null);
+ if (definitions == null) {
+ throw new NullPointerException(
+ "No definitions loaded for default locale");
+ }
+ definitions.putAll(defsMap);
+ }
+
+ /** {@inheritDoc} */
+ public void addDefinitions(Map<String, Definition> defsMap,
+ Locale locale) {
+ Map<String, Definition> definitions = definitionDao
+ .getDefinitions(locale);
+ if (definitions == null) {
+ throw new NullPointerException(
+ "No definitions loaded for locale '"
+ + locale.toString() + "'");
+ }
+ definitions.putAll(defsMap);
+ }
+
+ /** {@inheritDoc} */
+ public Map<String, Definition> getBaseDefinitions() {
+ return definitionDao.getDefinitions(null);
+ }
+
+ /** {@inheritDoc} */
+ public Definition getDefinition(String name) {
+ return definitionDao.getDefinition(name, null);
+ }
+
+ /** {@inheritDoc} */
+ public Definition getDefinition(String name, Locale locale) {
+ return definitionDao.getDefinition(name, locale);
+ }
+
+ /** {@inheritDoc} */
+ public void reset() {
+ if (definitionDao instanceof Refreshable) {
+ ((Refreshable) definitionDao).refresh();
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void resolveInheritances() {
+ // Does nothing.
+ }
+
+ /** {@inheritDoc} */
+ public void resolveInheritances(Locale locale) {
+ // Does nothing.
+ }
+ }
}
Modified: 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=672360&r1=672359&r2=672360&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAO.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/CachingLocaleUrlDefinitionDAO.java Fri Jun 27 12:12:29 2008
@@ -67,7 +67,7 @@
*
* @since 2.1.0
*/
- private Map<Locale, Map<String, Definition>> locale2definitionMap;
+ protected Map<Locale, Map<String, Definition>> locale2definitionMap;
/**
* Flag that, when <code>true</code>, enables automatic checking of URLs
@@ -75,7 +75,7 @@
*
* @since 2.1.0
*/
- private boolean checkRefresh = false;
+ protected boolean checkRefresh = false;
/**
* An object that helps in resolving definitions with wildcards.
@@ -89,7 +89,7 @@
*
* @since 2.1.0
*/
- private Map<Locale, List<WildcardMapping>> localePatternPaths =
+ protected Map<Locale, List<WildcardMapping>> localePatternPaths =
new HashMap<Locale, List<WildcardMapping>>();
/**
@@ -189,7 +189,7 @@
}
/**
- * Loads definitions from the URLs..
+ * Tries to load definitions if necessary.
*
* @param customizationKey The locale to use when loading URLs.
* @return The loaded definitions.
@@ -202,11 +202,26 @@
return localeDefsMap;
}
+ localeDefsMap = loadDefinitionsFromURLs(customizationKey);
+ postDefinitionLoadOperations(localeDefsMap, customizationKey);
+ return localeDefsMap;
+ }
+
+ /**
+ * 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> loadDefinitionsFromURLs(Locale customizationKey) {
+ Map<String, Definition> 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);
+ Map<String, Definition> parentDefs = loadParentDefinitions(parentLocale);
if (parentDefs != null) {
localeDefsMap.putAll(parentDefs);
}
@@ -229,6 +244,30 @@
}
locale2definitionMap.put(customizationKey, localeDefsMap);
+ return localeDefsMap;
+ }
+
+ /**
+ * Loads parent definitions, i.e. definitions mapped to a parent locale.
+ *
+ * @param parentLocale The locale to use when loading URLs.
+ * @return The loaded parent definitions.
+ * @since 2.1.0
+ */
+ protected Map<String, Definition> loadParentDefinitions(Locale parentLocale) {
+ return loadDefinitions(parentLocale);
+ }
+
+ /**
+ * Tries to load definitions if necessary.
+ *
+ * @param localeDefsMap The loaded definitions.
+ * @param customizationKey The locale to use when loading URLs.
+ * @since 2.1.0
+ */
+ protected void postDefinitionLoadOperations(
+ Map<String, Definition> localeDefsMap, Locale customizationKey) {
+
List<WildcardMapping> lpaths = localePatternPaths
.get(customizationKey);
if (lpaths == null) {
@@ -237,7 +276,6 @@
}
addWildcardPaths(lpaths, localeDefsMap);
- return localeDefsMap;
}
/**
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=672360&r1=672359&r2=672360&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 Fri Jun 27 12:12:29 2008
@@ -33,7 +33,7 @@
/**
* A definition DAO that uses {@link Locale} as a customization key and loads
- * definitions from URLs.
+ * definitions from URLs. It does not cache definitions in any way.
*
* @version $Rev$ $Date$
* @since 2.1.0
Added: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAO.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAO.java?rev=672360&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAO.java (added)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAO.java Fri Jun 27 12:12:29 2008
@@ -0,0 +1,142 @@
+/*
+ * $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.util.HashSet;
+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.definition.NoSuchDefinitionException;
+
+/**
+ * <p>
+ * A definitions DAO (loading URLs and using Locale as a customization key) that
+ * caches definitions that have been loaded and resolves inheritances.
+ * </p>
+ * <p>
+ * It can check if the URLs change, but by default this feature is turned off.
+ * </p>
+ *
+ * @version $Rev$ $Date$
+ * @since 2.1.0
+ */
+public class ResolvingLocaleUrlDefinitionDAO extends
+ CachingLocaleUrlDefinitionDAO {
+
+ /**
+ * The logging object.
+ */
+ private static final Log LOG = LogFactory.getLog(ResolvingLocaleUrlDefinitionDAO.class);
+
+ /** {@inheritDoc} */
+ @Override
+ protected Map<String, Definition> loadDefinitions(Locale customizationKey) {
+ return super.loadDefinitions(customizationKey);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ protected Map<String, Definition> loadParentDefinitions(Locale parentLocale) {
+ return loadDefinitionsFromURLs(parentLocale);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ protected void postDefinitionLoadOperations(
+ Map<String, Definition> localeDefsMap, Locale customizationKey) {
+ resolveInheritances(localeDefsMap, customizationKey);
+ super.postDefinitionLoadOperations(localeDefsMap, customizationKey);
+ }
+
+ /**
+ * Resolve locale-specific extended instances.
+ *
+ * @param map The definition map containing the definitions to resolve.
+ * @param locale The locale to use.
+ * @throws NoSuchDefinitionException If a parent definition is not found.
+ * @since 2.1.0
+ */
+ protected void resolveInheritances(Map<String, Definition> map, Locale locale) {
+ if (map != null) {
+ Set<String> alreadyResolvedDefinitions = new HashSet<String>();
+ for (Definition definition : map.values()) {
+ resolveInheritance(definition, map, locale,
+ alreadyResolvedDefinitions);
+ } // end loop
+ }
+ }
+
+ /**
+ * Resolve locale-specific inheritance. First, resolve parent's inheritance,
+ * then set template to the parent's template. Also copy attributes setted
+ * in parent, and not set in child If instance doesn't extend anything, do
+ * nothing.
+ *
+ * @param definition The definition to resolve
+ * @param definitions The definitions to take when obtaining a parent
+ * definition.
+ * @param locale The locale to use.
+ * @param alreadyResolvedDefinitions The set of the definitions that have
+ * been already resolved.
+ * @throws NoSuchDefinitionException If an inheritance can not be solved.
+ * @since 2.1.0
+ */
+ protected void resolveInheritance(Definition definition,
+ Map<String, Definition> definitions, Locale locale,
+ Set<String> alreadyResolvedDefinitions) {
+ // Already done, or not needed ?
+ if (!definition.isExtending()
+ || alreadyResolvedDefinitions.contains(definition.getName())) {
+ return;
+ }
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Resolve definition for child name='"
+ + definition.getName()
+ + "' extends='" + definition.getExtends() + "'.");
+ }
+
+ // Set as visited to avoid endless recurisvity.
+ alreadyResolvedDefinitions.add(definition.getName());
+
+ // Resolve parent before itself.
+ Definition parent = definitions.get(definition.getExtends());
+ if (parent == null) { // error
+ String msg = "Error while resolving definition inheritance: child '"
+ + definition.getName()
+ + "' can't find its ancestor '"
+ + definition.getExtends()
+ + "'. Please check your description file.";
+ // to do : find better exception
+ throw new NoSuchDefinitionException(msg);
+ }
+
+ resolveInheritance(parent, definitions, locale,
+ alreadyResolvedDefinitions);
+
+ definition.inherit(parent);
+ }
+}
Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAO.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAO.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/factory/BasicTilesContainerFactory.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/factory/BasicTilesContainerFactory.java?rev=672360&r1=672359&r2=672360&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/factory/BasicTilesContainerFactory.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/factory/BasicTilesContainerFactory.java Fri Jun 27 12:12:29 2008
@@ -39,7 +39,7 @@
import org.apache.tiles.definition.UrlDefinitionsFactory;
import org.apache.tiles.definition.dao.BaseLocaleUrlDefinitionDAO;
import org.apache.tiles.definition.dao.DefinitionDAO;
-import org.apache.tiles.definition.dao.LocaleUrlDefinitionDAO;
+import org.apache.tiles.definition.dao.ResolvingLocaleUrlDefinitionDAO;
import org.apache.tiles.definition.digester.DigesterDefinitionsReader;
import org.apache.tiles.evaluator.AttributeEvaluator;
import org.apache.tiles.evaluator.impl.DirectAttributeEvaluator;
@@ -214,7 +214,7 @@
protected BaseLocaleUrlDefinitionDAO instantiateLocaleDefinitionDao(Object context,
TilesApplicationContext applicationContext,
TilesContextFactory contextFactory, LocaleResolver resolver) {
- return new LocaleUrlDefinitionDAO();
+ return new ResolvingLocaleUrlDefinitionDAO();
}
/**
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=672360&r1=672359&r2=672360&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 Fri Jun 27 12:12:29 2008
@@ -85,7 +85,6 @@
*
* @throws Exception If something goes wrong.
*/
- @SuppressWarnings("unchecked")
public void testReadDefinitions() throws Exception {
// Set up multiple data sources.
URL url1 = this.getClass().getClassLoader().getResource(
@@ -111,6 +110,7 @@
.andReturn(url3);
EasyMock.replay(applicationContext);
factory.setApplicationContext(applicationContext);
+ TilesRequestContext emptyContext = new MockOnlyLocaleTilesContext(null);
Map<String, String> params = new HashMap<String, String>();
params.put(DefinitionsFactory.DEFINITIONS_CONFIG,
@@ -119,11 +119,11 @@
factory.init(params);
assertNotNull("test.def1 definition not found.", factory.getDefinition(
- "test.def1", (TilesRequestContext) null));
+ "test.def1", emptyContext));
assertNotNull("test.def2 definition not found.", factory.getDefinition(
- "test.def2", (TilesRequestContext) null));
+ "test.def2", emptyContext));
assertNotNull("test.def3 definition not found.", factory.getDefinition(
- "test.def3", (TilesRequestContext) null));
+ "test.def3", emptyContext));
}
/**
@@ -131,7 +131,6 @@
*
* @throws Exception If something goes wrong.
*/
- @SuppressWarnings("unchecked")
public void testGetDefinition() throws Exception {
// Set up multiple data sources.
URL url1 = this.getClass().getClassLoader().getResource(
@@ -224,108 +223,6 @@
}
/**
- * Tests the addDefinitions method under normal
- * circumstances.
- *
- * @throws Exception If something goes wrong.
- */
- @SuppressWarnings("unchecked")
- public void testReadByLocale() throws Exception {
- // 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);
- factory.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");
- factory.init(params);
-
- // Parse files.
- factory.addDefinitions(new MockOnlyLocaleTilesContext(Locale.US));
- factory.addDefinitions(new MockOnlyLocaleTilesContext(Locale.FRENCH));
-
- assertNotNull("test.def1 definition not found.", factory.getDefinition(
- "test.def1", (Locale) null));
- assertNotNull("test.def1 US definition not found.", factory
- .getDefinition("test.def1", Locale.US));
- assertNotNull("test.def1 France definition not found.", factory
- .getDefinition("test.def1", Locale.FRENCH));
- assertNotNull("test.def1 China should return default.", factory
- .getDefinition("test.def1", Locale.CHINA));
-
- assertEquals("Incorrect default country value", "default", factory
- .getDefinition("test.def1", (Locale) null).getAttribute(
- "country").getValue());
- assertEquals("Incorrect US country value", "US", factory.getDefinition(
- "test.def1", Locale.US).getAttribute("country").getValue());
- assertEquals("Incorrect France country value", "France", factory
- .getDefinition("test.def1", Locale.FRENCH).getAttribute(
- "country").getValue());
- assertEquals("Incorrect Chinese country value (should default)",
- "default", factory.getDefinition("test.def1", Locale.CHINA)
- .getAttribute("country").getValue());
- }
-
- /**
- * Tests the isContextProcessed method.
- *
- * @throws Exception If something goes wrong.
- */
- @SuppressWarnings("unchecked")
- public void testIsContextProcessed() throws Exception {
-
- // Set up multiple data sources.
- URL url1 = this.getClass().getClassLoader().getResource(
- "org/apache/tiles/config/defs1.xml");
- assertNotNull("Could not load defs1 file.", url1);
-
- TilesApplicationContext applicationContext = EasyMock
- .createMock(TilesApplicationContext.class);
- EasyMock.expect(applicationContext
- .getResource("org/apache/tiles/config/defs1.xml"))
- .andReturn(url1);
- EasyMock.replay(applicationContext);
- factory.setApplicationContext(applicationContext);
-
- Map<String, String> params = new HashMap<String, String>();
- params.put(DefinitionsFactory.DEFINITIONS_CONFIG,
- "org/apache/tiles/config/defs1.xml");
- factory.init(params);
-
- // Parse files.
- TilesRequestContext tilesContext =
- new MockOnlyLocaleTilesContext(Locale.US);
- assertFalse("Locale should not be processed.",
- factory.isContextProcessed(tilesContext));
-
- factory.addDefinitions(tilesContext);
- assertTrue("Locale should be processed.",
- factory.isContextProcessed(tilesContext));
- }
-
- /**
* Tests the reader init param.
*
* @throws Exception If something goes wrong.
Added: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAOTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAOTest.java?rev=672360&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAOTest.java (added)
+++ tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAOTest.java Fri Jun 27 12:12:29 2008
@@ -0,0 +1,616 @@
+/*
+ * $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 ResolvingLocaleUrlDefinitionDAO}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ResolvingLocaleUrlDefinitionDAOTest 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 ResolvingLocaleUrlDefinitionDAO definitionDao;
+
+ /** {@inheritDoc} */
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ definitionDao = new ResolvingLocaleUrlDefinitionDAO();
+ }
+
+ /**
+ * 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());
+ assertEquals("Incorrect France title value",
+ "Definition to be extended", definitionDao.getDefinition(
+ "test.def.overridden", Locale.FRENCH).getAttribute(
+ "title").getValue());
+ }
+
+ /**
+ * 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());
+ assertEquals("Incorrect France title value",
+ "Definition to be extended", frenchDefinitions.get(
+ "test.def.overridden").getAttribute("title").getValue());
+ }
+
+ /**
+ * 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());
+ }
+
+ /**
+ * Tests wildcard mappings.
+ *
+ * @throws IOException If something goes wrong.
+ */
+ public void testWildcardMapping() throws IOException {
+ URL url = this.getClass().getClassLoader().getResource(
+ "org/apache/tiles/config/defs-wildcard.xml");
+ definitionDao.addSourceURL(url);
+ TilesApplicationContext applicationContext = EasyMock
+ .createMock(TilesApplicationContext.class);
+ EasyMock.expect(applicationContext
+ .getResource("org/apache/tiles/config/defs-wildcard.xml"))
+ .andReturn(url);
+ EasyMock.replay(applicationContext);
+ ((TilesApplicationContextAware) definitionDao)
+ .setApplicationContext(applicationContext);
+ definitionDao.setReader(new DigesterDefinitionsReader());
+
+ Definition definition = definitionDao.getDefinition("test.defName.subLayered", null);
+ assertEquals("The template is not correct", "/testName.jsp", definition.getTemplate());
+ assertEquals("The header attribute is not correct",
+ "/common/headerLayered.jsp", definition.getAttribute("header")
+ .getValue());
+ definition = definitionDao.getDefinition("test.def3", null);
+ assertNotNull("The simple definition is null", definition);
+ }
+}
Propchange: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAOTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/dao/ResolvingLocaleUrlDefinitionDAOTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL