I am trying to use the Mojarra JSF 2 compiler programmatically with the goal of verifying the correct xhtml of any pages.
I have so far, but the compiler is not an error for tags that do not exist in a specific tag library. It checks the standard XML namespace, but if rich: spacer is present, it must be wrong (it retired in Richfaces 4.x). At run time, this check is performed.
Any thoughts? Here is my code:
@RunWith( PowerMockRunner.class ) @PrepareForTest( { WebConfiguration.class, FacesContext.class } ) public class XhtmlValidatorTest { @Test public void test() throws IOException { WebConfiguration webConfiguration = PowerMock.createMock( WebConfiguration.class ); PowerMock.mockStatic( WebConfiguration.class ); WebConfiguration.getInstance(); PowerMock.expectLastCall().andReturn( webConfiguration ).anyTimes(); FaceletsConfiguration faceletsConfiguration = PowerMock.createMock( FaceletsConfiguration.class ); webConfiguration.getFaceletsConfiguration(); PowerMock.expectLastCall().andReturn( faceletsConfiguration ).anyTimes(); faceletsConfiguration.isProcessCurrentDocumentAsFaceletsXhtml(EasyMock.isA( String.class ) ); PowerMock.expectLastCall().andReturn(true).anyTimes(); faceletsConfiguration.isConsumeComments( EasyMock.isA( String.class) ); PowerMock.expectLastCall().andReturn(false).anyTimes(); faceletsConfiguration.isConsumeCDATA( EasyMock.isA( String.class ) ); PowerMock.expectLastCall().andReturn(false).anyTimes(); webConfiguration.isOptionEnabled(BooleanWebContextInitParameter.EnableMissingResourceLibraryDetection); PowerMock.expectLastCall().andReturn( false ).anyTimes(); webConfiguration.isOptionEnabled(BooleanWebContextInitParameter.EnableCoreTagLibraryValidator ); PowerMock.expectLastCall().andReturn( true ).anyTimes(); FacesContext facesContext = PowerMock.createMock( FacesContext.class ); PowerMock.mockStatic( FacesContext.class ); FacesContext.getCurrentInstance(); PowerMock.expectLastCall().andReturn( facesContext ).anyTimes(); facesContext.isProjectStage( ProjectStage.Development ); PowerMock.expectLastCall().andReturn( false ).anyTimes(); Application application = PowerMock.createMock( Application.class ); facesContext.getApplication(); PowerMock.expectLastCall().andReturn( application ).anyTimes(); application.getExpressionFactory(); PowerMock.expectLastCall().andReturn( new org.jboss.el.ExpressionFactoryImpl() ).anyTimes(); PowerMock.replayAll(); long refreshPeriod = -1; com.sun.faces.facelets.compiler.Compiler compiler = new SAXCompiler(); compiler.setValidating( true ); System.out.println( "Compiler.isValidating() " + compiler.isValidating() ); FaceletCache cache = new UnittestFaceletCacheFactory().getCache( refreshPeriod ); ResourceResolver resolver = new ResourceResolver() { @Override public URL resolveUrl(String path) { URL url = null; try { url = new URL( BASE_PATH + path ); } catch (MalformedURLException e) { throw new RuntimeException( e ); } return url; } }; DefaultFaceletFactory defaultFaceletFactory = new DefaultFaceletFactory( compiler, resolver, refreshPeriod, cache ); File file = new File( "WebContent" ); File[] files = file.listFiles(); for( File xhtmlFile : files ) { if( xhtmlFile.isFile() ) { String name = xhtmlFile.getName(); if( name.endsWith(".xhtml" ) ) { System.out.println( "compiling: " + name ); defaultFaceletFactory.getFacelet( name ); } } } }
The factory cache folder used in the code is a hack:
package com.sun.faces.facelets.impl; import javax.faces.view.facelets.FaceletCache; public class UnittestFaceletCacheFactory { public FaceletCache getCache( long refreshPeriod ) { return new DefaultFaceletCache( refreshPeriod ); } }
source share