|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Contributors to Eclipse Foundation. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package com.sun.faces.application.resource; |
| 18 | + |
| 19 | +import static jakarta.faces.application.ProjectStage.Development; |
| 20 | +import static jakarta.faces.application.ResourceHandler.FACES_SCRIPT_LIBRARY_NAME; |
| 21 | +import static jakarta.faces.application.ResourceHandler.FACES_SCRIPT_RESOURCE_NAME; |
| 22 | +import static jakarta.faces.application.ResourceHandler.RESOURCE_IDENTIFIER; |
| 23 | +import static jakarta.servlet.http.MappingMatch.EXACT; |
| 24 | +import static jakarta.servlet.http.MappingMatch.EXTENSION; |
| 25 | +import static jakarta.servlet.http.MappingMatch.PATH; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 28 | + |
| 29 | +import java.io.InputStream; |
| 30 | +import java.net.URL; |
| 31 | + |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | + |
| 34 | +import com.sun.faces.junit.JUnitFacesTestCaseBase; |
| 35 | +import com.sun.faces.mock.MockApplication; |
| 36 | +import com.sun.faces.mock.MockFacesMappingSupport; |
| 37 | + |
| 38 | +import jakarta.faces.context.FacesContext; |
| 39 | + |
| 40 | +public class ResourceImplTest extends JUnitFacesTestCaseBase { |
| 41 | + |
| 42 | + private static final String CONTEXT_PATH = "/app"; |
| 43 | + private static final ResourceHelper RESOURCE_HELPER = new ResourceHelper() { |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getBaseResourcePath() { |
| 47 | + return "/resources"; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getBaseContractsPath() { |
| 52 | + return "/contracts"; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public URL getURL(ResourceInfo resource, FacesContext ctx) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public LibraryInfo findLibrary(String libraryName, String localePrefix, String contract, FacesContext ctx) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public ResourceInfo findResource(LibraryInfo library, String resourceName, String localePrefix, boolean compressable, FacesContext ctx) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected InputStream getNonCompressedInputStream(ResourceInfo resource, FacesContext ctx) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + @Test |
| 77 | + public void getRequestPathReturnsExactHitForExactMappedResource() { |
| 78 | + configureRequest(MockFacesMappingSupport.mapping(EXACT, "/exact", "exact"), "/exact", RESOURCE_IDENTIFIER + "/theme.css", "*.xhtml", "/faces/*"); |
| 79 | + |
| 80 | + assertEquals("/app/jakarta.faces.resource/theme.css", createResource("theme.css", null, null, null, null, null).getRequestPath()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void getRequestPathKeepsExtensionFallbackForExactRequests() { |
| 85 | + ResourceImpl resource = createResource("theme.css", null, null, null, null, null); |
| 86 | + |
| 87 | + configureRequest(MockFacesMappingSupport.mapping(EXACT, "/exact", "exact"), "/exact", "*.xhtml"); |
| 88 | + String exactRequestPath = resource.getRequestPath(); |
| 89 | + |
| 90 | + configureRequest(MockFacesMappingSupport.mapping(EXTENSION, "*.xhtml", "theme"), "/exact", "*.xhtml"); |
| 91 | + String extensionBaselinePath = resource.getRequestPath(); |
| 92 | + |
| 93 | + assertEquals("/app/jakarta.faces.resource/theme.css.xhtml", exactRequestPath); |
| 94 | + assertEquals(extensionBaselinePath, exactRequestPath); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void getRequestPathKeepsDirectPathBehavior() { |
| 99 | + ResourceImpl resource = createResource("theme.css", "layout", "1_0", null, "en", "blue"); |
| 100 | + |
| 101 | + configureRequest(MockFacesMappingSupport.mapping(PATH, "/faces/*", "theme"), "/exact", "/faces/*"); |
| 102 | + String directPathRequestPath = resource.getRequestPath(); |
| 103 | + |
| 104 | + assertEquals("/app/faces/jakarta.faces.resource/theme.css?ln=layout&v=1_0&loc=en&con=blue", directPathRequestPath); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void getRequestPathPrefersExtensionFallbackWhenBothWildcardsExist() { |
| 109 | + ResourceImpl resource = createResource("theme.css", "layout", "1_0", null, "en", "blue"); |
| 110 | + |
| 111 | + configureRequest(MockFacesMappingSupport.mapping(EXACT, "/exact", "exact"), "/exact", "*.jsf", "/faces/*"); |
| 112 | + String exactRequestPath = resource.getRequestPath(); |
| 113 | + |
| 114 | + assertEquals("/app/jakarta.faces.resource/theme.css.jsf?ln=layout&v=1_0&loc=en&con=blue", exactRequestPath); |
| 115 | + assertEquals(exactRequestPath.indexOf("ln="), exactRequestPath.lastIndexOf("ln=")); |
| 116 | + assertEquals(exactRequestPath.indexOf("v="), exactRequestPath.lastIndexOf("v=")); |
| 117 | + assertEquals(exactRequestPath.indexOf("loc="), exactRequestPath.lastIndexOf("loc=")); |
| 118 | + assertEquals(exactRequestPath.indexOf("con="), exactRequestPath.lastIndexOf("con=")); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void getRequestPathPrefersExtensionFallbackForFacesScriptInDevelopment() { |
| 123 | + MockApplication developmentApplication = new MockApplication() { |
| 124 | + |
| 125 | + @Override |
| 126 | + public jakarta.faces.application.ProjectStage getProjectStage() { |
| 127 | + return Development; |
| 128 | + } |
| 129 | + }; |
| 130 | + developmentApplication.setViewHandler(application.getViewHandler()); |
| 131 | + application = developmentApplication; |
| 132 | + facesContext.setApplication(application); |
| 133 | + |
| 134 | + ResourceImpl resource = createResource(FACES_SCRIPT_RESOURCE_NAME, FACES_SCRIPT_LIBRARY_NAME, null, null, null, null); |
| 135 | + |
| 136 | + configureRequest(MockFacesMappingSupport.mapping(EXACT, "/exact", "exact"), "/exact", "*.jsf", "/faces/*"); |
| 137 | + String exactRequestPath = resource.getRequestPath(); |
| 138 | + |
| 139 | + assertEquals("/app/jakarta.faces.resource/faces.js.jsf?ln=jakarta.faces&stage=Development", exactRequestPath); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void getRequestPathThrowsWhenNoWildcardFallbackExists() { |
| 144 | + configureRequest(MockFacesMappingSupport.mapping(EXACT, "/exact", "exact"), "/exact"); |
| 145 | + |
| 146 | + assertThrows(IllegalStateException.class, () -> createResource("theme.css", null, null, null, null, null).getRequestPath()); |
| 147 | + } |
| 148 | + |
| 149 | + private void configureRequest(jakarta.servlet.http.HttpServletMapping mapping, String... servletMappings) { |
| 150 | + request = MockFacesMappingSupport.request(CONTEXT_PATH, mapping, session); |
| 151 | + externalContext = MockFacesMappingSupport.externalContext(servletContext, request, response); |
| 152 | + facesContext.setExternalContext(externalContext); |
| 153 | + MockFacesMappingSupport.setFacesServletMappings(servletContext, servletMappings); |
| 154 | + } |
| 155 | + |
| 156 | + private ResourceImpl createResource(String resourceName, String libraryName, String libraryVersion, String resourceVersion, String localePrefix, String contract) { |
| 157 | + ContractInfo contractInfo = contract != null ? new ContractInfo(contract) : null; |
| 158 | + VersionInfo libraryVersionInfo = libraryVersion != null ? new VersionInfo(libraryVersion, null) : null; |
| 159 | + VersionInfo resourceVersionInfo = resourceVersion != null ? new VersionInfo(resourceVersion, extensionOf(resourceName)) : null; |
| 160 | + |
| 161 | + ResourceInfo resourceInfo; |
| 162 | + if (libraryName != null) { |
| 163 | + LibraryInfo libraryInfo = new LibraryInfo(libraryName, libraryVersionInfo, localePrefix, contract, RESOURCE_HELPER); |
| 164 | + resourceInfo = new ResourceInfo(libraryInfo, contractInfo, resourceName, resourceVersionInfo); |
| 165 | + } else { |
| 166 | + resourceInfo = new ResourceInfo(contractInfo, resourceName, resourceVersionInfo, RESOURCE_HELPER); |
| 167 | + } |
| 168 | + |
| 169 | + return new ResourceImpl(resourceInfo, "text/css", 0, 0); |
| 170 | + } |
| 171 | + |
| 172 | + private String extensionOf(String resourceName) { |
| 173 | + int extensionIndex = resourceName.lastIndexOf('.'); |
| 174 | + return extensionIndex >= 0 ? resourceName.substring(extensionIndex + 1) : null; |
| 175 | + } |
| 176 | +} |
0 commit comments