Skip to content

Commit

Permalink
Improved test.
Browse files Browse the repository at this point in the history
1) renamed 'localized-composite' module to 'localizedComposite' conform
naming conventions of all other tests
2) renamed 'LocalizedComposite' in filenames to 'Issue5160' conform
naming conventions of all other tests
3) extract UIViewRoot locale from Accept-Language header instead of
request.getLocale() because the latter apparently drops unsupported
variants depending on currently used JDK
4) added more tests covering more corner cases
  • Loading branch information
BalusC committed Feb 17, 2024
1 parent 1e5fba6 commit 46f3310
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 101 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
<parent>
<groupId>org.eclipse.ee4j.tck.faces.faces23</groupId>
<artifactId>pom</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.1.0-SNAPSHOT</version>
</parent>

<artifactId>localized-composite</artifactId>
<artifactId>localizedComposite</artifactId>
<packaging>war</packaging>

<name>Jakarta Faces TCK ${project.version} - Test - Faces 2.3 - localized-composite</name>
<name>Jakarta Faces TCK ${project.version} - Test - Faces 2.3 - localizedComposite</name>
<description>Composite .properties l10n files should reflect current locale when using resourceBundleMap.</description>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,39 @@
*/
package ee.jakarta.tck.faces.test.composite;

import java.util.Locale;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.SessionScoped;
import jakarta.faces.component.UIViewRoot;
import jakarta.faces.context.FacesContext;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Locale;

@Named
@SessionScoped
public class LocaleBean implements Serializable {
@RequestScoped
public class Issue5160Bean {

@Inject
private HttpServletRequest request;

private Locale locale;

private String language;


@PostConstruct
public void init() {
Locale requestLocale = request.getLocale();
setLocale(requestLocale != null ? requestLocale.toString() : "en");
locale = parseLocale(request.getHeader("Accept-Language"));
}
private void setLocale(String languageTag) {
String[] chunks = languageTag.split("(-|_)");

private Locale parseLocale(String languageTag) {
String[] chunks = languageTag.split("[_-]");
switch (chunks.length) {
case 1:
locale = new Locale(chunks[0]);
break;
case 2:
locale = new Locale(chunks[0], chunks[1]);
break;
default:
locale = new Locale(chunks[0], chunks[1], chunks[2]);
break;
}
language = locale.toString();
UIViewRoot root = FacesContext.getCurrentInstance().getViewRoot();
if (root != null) {
root.setLocale(locale);
case 1: return new Locale(chunks[0]);
case 2: return new Locale(chunks[0], chunks[1]);
default: return new Locale(chunks[0], chunks[1], chunks[2]);
}
}

public Locale getLocale() {
return locale;
}

public String getLanguage() {
return language;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>localized-composite.xhtml</welcome-file>
<welcome-file>issue5160.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<html xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:comp="http://xmlns.jcp.org/jsf/composite/comps">
<f:view locale="#{localeBean.language}">
<f:view locale="#{issue5160Bean.locale}">
<h:head id="head">
<title>JSF 2.3 Localized Composite</title>
</h:head>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

/**
* Tests if composite component that use resourceBundleMap .properties
* reflects locale changes.
* Tests if composite component that use resourceBundleMap .properties reflects
* locale changes.
*
* @see https://github.com/eclipse-ee4j/mojarra/issues/5160
* @see https://issues.apache.org/jira/browse/MYFACES-4491
*
*/
@RunWith(Arquillian.class)
public class LocalizedCompositeIT {
public class Issue5160IT {

@ArquillianResource
private URL webUrl;
Expand All @@ -69,47 +69,61 @@ public void setUp() {

@Test
public void testLocalizedCompositeEn() throws Exception {
webClient.addRequestHeader("Accept-Language", "en-US, en;q=0.9, es-ES");
HtmlPage page = webClient.getPage(webUrl + "localized-composite.xhtml");

HtmlHeading1 h1 = (HtmlHeading1) page.getHtmlElementById("header");

assertEquals("Application", h1.getTextContent());

HtmlSubmitInput btn1 = (HtmlSubmitInput) page.getHtmlElementById("frm:btn");
assertEquals("My precious button", btn1.getAttribute("value"));
HtmlSubmitInput btn2 = (HtmlSubmitInput) page.getHtmlElementById("frm:btn1:btn");
assertEquals("Button", btn2.getAttribute("value"));
assertLocalizedComposite("en-US", "Application", "My precious button", "Button");
}

@Test
public void testLocalizedCompositeEs() throws Exception {
webClient.addRequestHeader("Accept-Language", "es-ES");
HtmlPage page = webClient.getPage(webUrl + "localized-composite.xhtml");

HtmlHeading1 h1 = (HtmlHeading1) page.getHtmlElementById("header");
assertLocalizedComposite("es-ES", "Aplicación", "Mi precioso botón", "Botón");
}

assertEquals("Aplicación", h1.getTextContent());

HtmlSubmitInput btn1 = (HtmlSubmitInput) page.getHtmlElementById("frm:btn");
assertEquals("Mi precioso botón", btn1.getAttribute("value"));
HtmlSubmitInput btn2 = (HtmlSubmitInput) page.getHtmlElementById("frm:btn1:btn");
assertEquals("Botón", btn2.getAttribute("value"));
@Test
public void testLocalizedCompositePt() throws Exception {
assertLocalizedComposite("pt", "Application", "My precious button", "Accionador");
}

@Test
public void testLocalizedCompositePtBr() throws Exception {
assertLocalizedComposite("pt-BR", "Application", "My precious button", "Botão");
}

@Test
public void testLocalizedCompositePtBrPb() throws Exception {
webClient.addRequestHeader("Accept-Language", "pt-BR-PB");
HtmlPage page = webClient.getPage(webUrl + "localized-composite.xhtml");

assertLocalizedComposite("pt-BR-PB", "Application", "My precious button", "Pitoco");
}

@Test
public void testLocalizedCompositePtBrXx() throws Exception {
assertLocalizedComposite("pt-BR-XX", "Application", "My precious button", "Botão");
}

@Test
public void testLocalizedCompositePtBrXxYy() throws Exception {
assertLocalizedComposite("pt-BR-XX-YY", "Application", "My precious button", "Botão");
}

@Test
public void testLocalizedCompositePtXxYy() throws Exception {
assertLocalizedComposite("pt-XX-YY", "Application", "My precious button", "Accionador");
}

@Test
public void testLocalizedCompositePtXx() throws Exception {
assertLocalizedComposite("pt-XX", "Application", "My precious button", "Accionador");
}

private void assertLocalizedComposite(String acceptLanguage, String headerText, String buttonText, String compositeButtonText) throws Exception {
webClient.addRequestHeader("Accept-Language", acceptLanguage);
HtmlPage page = webClient.getPage(webUrl + "issue5160.xhtml");

HtmlHeading1 h1 = (HtmlHeading1) page.getHtmlElementById("header");
assertEquals(headerText, h1.getTextContent());

assertEquals("Application", h1.getTextContent());

HtmlSubmitInput btn1 = (HtmlSubmitInput) page.getHtmlElementById("frm:btn");
assertEquals("My precious button", btn1.getAttribute("value"));
assertEquals(buttonText, btn1.getAttribute("value"));

HtmlSubmitInput btn2 = (HtmlSubmitInput) page.getHtmlElementById("frm:btn1:btn");
assertEquals("Pitoco", btn2.getAttribute("value"));
assertEquals(compositeButtonText, btn2.getAttribute("value"));
}

@After
Expand Down
2 changes: 1 addition & 1 deletion tck/faces23/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<module>flash</module>
<module>getViews</module>
<module>importConstants</module>
<module>localized-composite</module>
<module>localizedComposite</module>
<module>namespacedView</module>
<module>passthrough</module>
<!--
Expand Down

0 comments on commit 46f3310

Please sign in to comment.