Snippets

Bill Collins Template tester

Created by Bill Collins
package uk.ac.cam.ice;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * Created by wac26 on 29/04/2016.
 */
abstract class AbstractTemplateTest {

    private SAXParser saxParser;

    abstract List<String> getNamespaces();

    abstract List<String> getIgnoredTags();

    abstract List<String> getExtraTags();

    @Before
    public void spinUp() throws ParserConfigurationException, SAXException {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        saxParser = factory.newSAXParser();
    }

    int testFiles(String path) throws Exception {
        return Files.walk(Paths.get(path))
                .filter(Files::isRegularFile)
                .map(Path::toFile)
                .filter(f -> f.getAbsolutePath().endsWith(".xhtml"))
                .mapToInt(this::testFile)
                .sum();
    }

    private int testFile(File file) {
        IdCheckingHandler handler = new IdCheckingHandler(getNamespaces(), getIgnoredTags(), getExtraTags());
        try {
            saxParser.parse(file, handler);
        } catch (SAXException | IOException e) {
            throw new RuntimeException(e);
        }
        return handler.getCount();
    }

}

class IdCheckingHandler extends DefaultHandler {

    private List<String> namespaces;

    private List<String> ignoredTags;

    private List<String> extraTags;

    IdCheckingHandler(List<String> namespaces, List<String> ignoredTags, List<String> extraTags) {
        super();
        this.namespaces = namespaces;
        this.ignoredTags = ignoredTags;
        this.extraTags = extraTags;
    }

    private Locator locator;

    private int count = 0;

    private static final Logger log = Logger.getLogger(AbstractTemplateTest.class);

    @Override
    public void setDocumentLocator(Locator locator) {
        this.locator = locator;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (this.extraTags.contains(qName) || (!this.ignoredTags.contains(qName) && this.namespaces.stream().map(s -> s + ":").map(qName::startsWith).reduce(Boolean.FALSE, Boolean::logicalOr))) {
            if (attributes.getValue("id") == null) {
                log.warn("Element " + qName + " missing ID. " + locator.getSystemId().substring(locator.getSystemId().indexOf("./")) + ":" + locator.getLineNumber());
                count ++;
            }
        }
    }

    int getCount() {
        return count;
    }
}
package uk.ac.cam.ice;

import org.junit.Test;

import javax.persistence.Table;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

/**
 * Created by wac26 on 03/05/2016.
 */
public class TemplateTest extends AbstractTemplateTest {

    @Override
    List<String> getNamespaces() {
        return Arrays.asList("h", "ice", "p", "iceadmin", "campl", "course", "iceworld");
    }

    @Override
    List<String> getIgnoredTags() {
        return Arrays.asList("h:head", "h:body", "h:outputText", "p:ajax", "h:panelGroup", "p:defaultCommand", "p:confirm", "iceadmin:ajaxSearch", "campl:strikethrough");
    }

    @Override
    List<String> getExtraTags() { return
        Arrays.asList("ui:repeat");
    }

    @Test
    public void checkTemplates() throws Exception {
        assertEquals(0, testFiles("./src/main/webapp/templates/"));
    }

    @Test
    public void checkPages() throws Exception {
        assertEquals(0, testFiles("./src/main/webapp/pages/"));
    }

    @Test
    public void checkCamPLComponents() throws Exception {
        assertEquals(0, testFiles("./src/main/webapp/resources/components/"));
    }

    @Test
    public void checkIceComponents() throws Exception {
        assertEquals(0, testFiles("./src/main/webapp/resources/ice/"));
    }

    @Test
    public void checkIceAdminComponents() throws Exception {
        assertEquals(0, testFiles("./src/main/webapp/resources/iceadmin/"));
    }

    @Test
    public void checkCourseComponents() throws Exception {
        assertEquals(0, testFiles("./src/main/webapp/resources/course/"));
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.