Unit testing 在单元测试中编译JSF2Facelets以确保正确性

Unit testing 在单元测试中编译JSF2Facelets以确保正确性,unit-testing,jsf,jsf-2,facelets,mojarra,Unit Testing,Jsf,Jsf 2,Facelets,Mojarra,我试图以编程方式使用MojarraJSF2编译器,以检查任何页面的xhtml的正确性 到目前为止,我已经知道了,但是编译器没有针对特定标记库中不存在的标记出错。它执行标准XML名称空间检查,但如果存在say rich:spacer,则应该会出错(它在Richfaces 4.x中被删除)。在运行时,会进行此检查 有什么想法吗?这是我的密码: @RunWith( PowerMockRunner.class ) @PrepareForTest( { WebConfiguration.class, Fa

我试图以编程方式使用MojarraJSF2编译器,以检查任何页面的xhtml的正确性

到目前为止,我已经知道了,但是编译器没有针对特定标记库中不存在的标记出错。它执行标准XML名称空间检查,但如果存在say 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缓存工厂是一个黑客:

package com.sun.faces.facelets.impl;

import javax.faces.view.facelets.FaceletCache;

public class UnittestFaceletCacheFactory
{
    public FaceletCache getCache( long refreshPeriod )
    {
        return new DefaultFaceletCache( refreshPeriod );
    }
}

当测试
JSF
用户界面的外观时(与它们所做的相反),解析器将
facelets
转换为
HTML
的内在需求,以及运行者将转换后的视图变为可用视图的需求,是使容器内测试流行于此技术的原因。最流行的技术似乎是
Selenium
。在测试用户界面时,传统的
jUnit
测试使用测试双倍,并且目标是支持bean和控制器类通常就足够了。像
JSFUnit
这样的库可能会加快对
JSF
API的模拟。我认为OP的目的是在构建时验证所有facelet页面的语法和一致性。他似乎没有谈论功能测试。这看起来是个有趣的主意。