diff --git a/plugins/tiles/pom.xml b/plugins/tiles/pom.xml index 208e50fd3..845062b6b 100644 --- a/plugins/tiles/pom.xml +++ b/plugins/tiles/pom.xml @@ -102,6 +102,16 @@ log4j-jcl test + + org.assertj + assertj-core + test + + + org.springframework + spring-test + test + UTF-8 diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java index ed03f8268..4cda08522 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java @@ -68,10 +68,7 @@ import javax.el.MapELResolver; import javax.el.ResourceBundleELResolver; import javax.servlet.jsp.JspFactory; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; @@ -103,21 +100,18 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { public static final String PATTERN_WILDCARD = "WILDCARD"; public static final String PATTERN_REGEXP = "REGEXP"; + /** + * Default pattern to be used to collect Tiles definitions if user didn't configure any + */ + public static final Set TILES_DEFAULT_PATTERNS = TextParseUtil.commaDelimitedStringToSet("*tiles*.xml"); + /** * Default pattern to be used to collect Tiles definitions if user didn't configure any * * @deprecated since Struts 6.4.0, use {@link #TILES_DEFAULT_PATTERNS} instead */ @Deprecated - public static final String TILES_DEFAULT_PATTERN = "/WEB-INF/**/tiles*.xml,classpath*:META-INF/**/tiles*.xml"; - - /** - * Default pattern to be used to collect Tiles definitions if user didn't configure any - */ - public static final Set TILES_DEFAULT_PATTERNS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( - "/WEB-INF/**/tiles*.xml", - "classpath*:META-INF/**/tiles*.xml" - ))); + public static final String TILES_DEFAULT_PATTERN = String.join(",", TILES_DEFAULT_PATTERNS); /** * Supported expression languages diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java index 7af6069bb..2a30eb626 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java @@ -43,7 +43,7 @@ public class StrutsWildcardServletApplicationContext extends ServletApplicationC private static final Logger LOG = LogManager.getLogger(StrutsWildcardServletApplicationContext.class); - private ResourceFinder finder; + private final ResourceFinder finder; public StrutsWildcardServletApplicationContext(ServletContext context) { super(context); @@ -64,16 +64,15 @@ public class StrutsWildcardServletApplicationContext extends ServletApplicationC } try { - Enumeration resources = getClass().getClassLoader().getResources("/"); + Enumeration resources = getClass().getClassLoader().getResources(""); while (resources.hasMoreElements()) { - URL resource = resources.nextElement(); - urls.add(resource); + urls.add(resources.nextElement()); } } catch (IOException e) { throw new ConfigurationException(e); } - finder = new ResourceFinder(urls.toArray(new URL[urls.size()])); + finder = new ResourceFinder(urls.toArray(new URL[0])); } public Collection getResources(String path) { diff --git a/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsTilesContainerFactoryTest.java b/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsTilesContainerFactoryTest.java index 122bfe51d..944aac2c9 100644 --- a/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsTilesContainerFactoryTest.java +++ b/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsTilesContainerFactoryTest.java @@ -34,8 +34,8 @@ import org.apache.tiles.request.render.Renderer; import org.junit.Before; import org.junit.Test; -import javax.servlet.ServletContext; import javax.servlet.jsp.JspFactory; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -64,12 +64,11 @@ public class StrutsTilesContainerFactoryTest { Objects.requireNonNull(getClass().getResource("/org/apache/tiles/core/config/tiles-defs.xml")) ); ApplicationResource classpathResource = new URLApplicationResource( - "/org/apache/tiles/core/config/defs1.xml", - Objects.requireNonNull(getClass().getResource("/org/apache/tiles/core/config/defs1.xml")) + "/org/apache/tiles/core/config/tiles_defs1.xml", + Objects.requireNonNull(getClass().getResource("/org/apache/tiles/core/config/tiles_defs1.xml")) ); when(applicationContext.getInitParams()).thenReturn(Collections.emptyMap()); - when(applicationContext.getResources("/WEB-INF/**/tiles*.xml")).thenReturn(Collections.singleton(pathResource)); - when(applicationContext.getResources("classpath*:META-INF/**/tiles*.xml")).thenReturn(Collections.singleton(classpathResource)); + when(applicationContext.getResources("*tiles*.xml")).thenReturn(Arrays.asList(pathResource, classpathResource)); List resources = factory.getSources(applicationContext); assertEquals("The urls list is not two-sized", 2, resources.size()); diff --git a/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContextTest.java b/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContextTest.java new file mode 100644 index 000000000..972ed705a --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContextTest.java @@ -0,0 +1,79 @@ +/* + * 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.struts2.tiles; + +import org.apache.tiles.core.definition.DefinitionsFactory; +import org.apache.tiles.request.ApplicationResource; +import org.junit.Before; +import org.junit.Test; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.FileUrlResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.mock.web.MockServletContext; + +import javax.servlet.ServletContext; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collection; + +import static org.assertj.core.api.Assertions.assertThat; + +public class StrutsWildcardServletApplicationContextTest { + + private ServletContext context; + + @Before + public void setUp() throws Exception { + URL resource = getClass().getResource("/"); + context = new MockServletContext(resource.getPath(), new ResourceLoader() { + @Override + public Resource getResource(String location) { + try { + String finalLocation = location.replaceAll("//", "/"); + if (finalLocation.endsWith("/")) { + return new FileSystemResource(finalLocation); + } + return new FileUrlResource(finalLocation); + } catch (MalformedURLException e) { + return null; + } + } + + @Override + public ClassLoader getClassLoader() { + return StrutsWildcardServletApplicationContextTest.class.getClassLoader(); + } + }); + } + + @Test + public void wildcardSupport() { + StrutsWildcardServletApplicationContext applicationContext = new StrutsWildcardServletApplicationContext(context); + + Collection resources = applicationContext.getResources("*tiles*.xml"); + + assertThat(resources) + .hasSize(1) + .extracting(ApplicationResource::getLocalePath) + .first().asString() + .endsWith("/WEB-INF/tiles.xml"); + } + +} \ No newline at end of file diff --git a/plugins/tiles/src/test/resources/WEB-INF/tiles.xml b/plugins/tiles/src/test/resources/WEB-INF/tiles.xml new file mode 100644 index 000000000..9fc36a4d6 --- /dev/null +++ b/plugins/tiles/src/test/resources/WEB-INF/tiles.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + diff --git a/plugins/tiles/src/test/resources/org/apache/tiles/core/config/tiles_defs1.xml b/plugins/tiles/src/test/resources/org/apache/tiles/core/config/tiles_defs1.xml new file mode 100644 index 000000000..621d7cef9 --- /dev/null +++ b/plugins/tiles/src/test/resources/org/apache/tiles/core/config/tiles_defs1.xml @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +