我正在尝试以编程方式使用 Mojarra JSF 2 编译器,以检查任何页面的 xhtml 的正确性。
我到目前为止,但编译器不会为特定标签库中不存在的标签出错。它执行标准的 XML 命名空间检查,但如果说 rich:spacer 存在,它应该会出错(它在 Richfaces 4.x 中被删除)。在运行时,确实会进行此检查。
有什么想法吗?这是我的代码:
@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 );
}
}
}
}
代码中使用的 facelet 缓存工厂是一个 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 );
}
}