0 Replies Latest reply on Apr 12, 2007 2:09 PM by Matt Drees

    Testing view syntax

    Matt Drees Master

      I created a somewhat simple test to make sure all the view files are compilable by facelets. It really only checks syntax (not el bindings, etc), but maybe this will prove useful to people anyway.

      (It requires commons-io)

      CompileAllPagesTest.java:

      import java.io.File;
      import java.util.Collection;
      
      import org.apache.commons.io.FileUtils;
      import org.jboss.seam.log.Log;
      import org.jboss.seam.log.Logging;
      import org.testng.annotations.Test;
      
      import com.sun.facelets.Facelet;
      import com.sun.facelets.FaceletFactory;
      
      public class CompileAllPagesTest extends FaceletTest {
      
       private Log log = Logging.getLog(CompileAllPagesTest.class);
      
       @SuppressWarnings("unchecked")
       @Test
       public void testLoginPage() throws Exception{
       File viewDir = new File(rootDir);
      
       Collection<File> files = FileUtils.listFiles(viewDir, new String[]{"xhtml"}, true);
       for (File viewFile : files ){
       String path = viewFile.getPath();
       String viewId = path.replaceFirst(rootDir, "").replace('\\', '/');
       log.debug("testing view " + viewId);
       Facelet facelet = FaceletFactory.getInstance().getFacelet(viewId);
       assert facelet != null : "created facelet for view " + viewId + " is null!";
       createComponentTree(viewId, facelet);
       }
       }
      }
      
      


      FaceletTest.java:
      public class FaceletTest {
      
       public static final String rootDir = "view";
      
       @Configuration(beforeTest= true)
       public void setupFaceletFactory() throws IOException {
       assert FaceletFactory.getInstance() == null;
       ResourceResolver simpleResolver = new ResourceResolver() {
      
      
       public URL resolveUrl(String path) {
       String urlString = rootDir + path;
       File file = new File(urlString);
       assert file != null;
       URL url = null;
       try {
       url = file.toURL();
       } catch (MalformedURLException e) {
       throw new RuntimeException("bad url: " + urlString, e);
       }
       return url;
       }
      
       };
      
       DefaultFaceletFactory defaultFaceletFactory = new DefaultFaceletFactory(
       new SAXCompiler(), simpleResolver);
       FaceletFactory.setInstance(defaultFaceletFactory);
       assert FaceletFactory.getInstance() != null;
       }
      
      }