001// Generated by delombok at Sun Jul 30 17:21:56 UTC 2023 002package de.cuioss.test.jsf.util; 003 004import static de.cuioss.tools.string.MoreStrings.isEmpty; 005import static java.util.Objects.requireNonNull; 006import java.util.ResourceBundle; 007import javax.faces.FactoryFinder; 008import javax.faces.application.Application; 009import javax.faces.application.ApplicationFactory; 010import javax.faces.application.ApplicationWrapper; 011import javax.faces.component.UIComponent; 012import javax.faces.component.search.SearchExpressionHandler; 013import javax.faces.context.FacesContext; 014import org.apache.myfaces.test.config.ResourceBundleVarNames; 015import org.apache.myfaces.test.mock.MockFacesContext; 016import de.cuioss.test.jsf.mocks.CuiMockSearchExpressionHandler; 017import de.cuioss.test.valueobjects.util.IdentityResourceBundle; 018 019/** 020 * An {@link ApplicationWrapper} that is capable to do more programmatic 021 * configuration compared to the ones provided by myfaces-test 022 * 023 * @author Oliver Wolff 024 */ 025public class ConfigurableApplication extends ApplicationWrapper { 026 private static final String COMPONENT_CONTAINER_DEFAULT_RENDERER = "javax.faces.Text"; 027 private static final String COMPONENT_RESOUCE_CONTAINER_COMPONENT = "javax.faces.ComponentResourceContainer"; 028 private final Application wrapped; 029 private boolean useIdentityResouceBundle = true; 030 private SearchExpressionHandler searchExpressionHandler = new CuiMockSearchExpressionHandler(); 031 032 @Override 033 public ResourceBundle getResourceBundle(final FacesContext ctx, final String name) { 034 if (useIdentityResouceBundle && null != ResourceBundleVarNames.getVarName(name)) { 035 return new IdentityResourceBundle(); 036 } 037 return wrapped.getResourceBundle(ctx, name); 038 } 039 040 @Override 041 public String getMessageBundle() { 042 if (useIdentityResouceBundle) { 043 return IdentityResourceBundle.class.getName(); 044 } 045 return wrapped.getMessageBundle(); 046 } 047 048 /** 049 * Creates a new {@link ConfigurableApplication} by loading the existing 050 * {@link Application} from the {@link ApplicationFactory} and registers itself 051 * again to the {@link ApplicationFactory} and {@link MockFacesContext} 052 * 053 * @param facesContext to be used for adding the created 054 * {@link ConfigurableApplication} to. Must not be null 055 * @return the created {@link ConfigurableApplication} 056 */ 057 public static final ConfigurableApplication createWrapAndRegister(final MockFacesContext facesContext) { 058 requireNonNull(facesContext); 059 final var factory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); 060 final var old = factory.getApplication(); 061 final var application = new ConfigurableApplication(old); 062 factory.setApplication(application); 063 facesContext.setApplication(application); 064 return application; 065 } 066 067 /** 068 * Intercept invalid argument for MyFaces Api passing null as rendererType -> 069 * UiViewRoot#getComponentResources 070 */ 071 @Override 072 public UIComponent createComponent(final FacesContext context, final String componentType, final String rendererType) { 073 // 074 if (COMPONENT_RESOUCE_CONTAINER_COMPONENT.equals(componentType) && isEmpty(rendererType)) { 075 return wrapped.createComponent(context, componentType, COMPONENT_CONTAINER_DEFAULT_RENDERER); 076 } 077 return wrapped.createComponent(context, componentType, rendererType); 078 } 079 080 @java.lang.SuppressWarnings("all") 081 @lombok.Generated 082 public ConfigurableApplication(final Application wrapped) { 083 this.wrapped = wrapped; 084 } 085 086 @java.lang.SuppressWarnings("all") 087 @lombok.Generated 088 public Application getWrapped() { 089 return this.wrapped; 090 } 091 092 @java.lang.SuppressWarnings("all") 093 @lombok.Generated 094 public boolean isUseIdentityResouceBundle() { 095 return this.useIdentityResouceBundle; 096 } 097 098 @java.lang.SuppressWarnings("all") 099 @lombok.Generated 100 public void setUseIdentityResouceBundle(final boolean useIdentityResouceBundle) { 101 this.useIdentityResouceBundle = useIdentityResouceBundle; 102 } 103 104 @java.lang.SuppressWarnings("all") 105 @lombok.Generated 106 public SearchExpressionHandler getSearchExpressionHandler() { 107 return this.searchExpressionHandler; 108 } 109 110 @java.lang.SuppressWarnings("all") 111 @lombok.Generated 112 public void setSearchExpressionHandler(final SearchExpressionHandler searchExpressionHandler) { 113 this.searchExpressionHandler = searchExpressionHandler; 114 } 115}