packageuk.ac.cam.ice;importorg.apache.log4j.Logger;importorg.junit.Before;importorg.xml.sax.Attributes;importorg.xml.sax.Locator;importorg.xml.sax.SAXException;importorg.xml.sax.helpers.DefaultHandler;importjavax.xml.parsers.ParserConfigurationException;importjavax.xml.parsers.SAXParser;importjavax.xml.parsers.SAXParserFactory;importjava.io.File;importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Path;importjava.nio.file.Paths;importjava.util.List;/** * Created by wac26 on 29/04/2016. */abstractclassAbstractTemplateTest{privateSAXParsersaxParser;abstractList<String>getNamespaces();abstractList<String>getIgnoredTags();abstractList<String>getExtraTags();@BeforepublicvoidspinUp()throwsParserConfigurationException,SAXException{SAXParserFactoryfactory=SAXParserFactory.newInstance();saxParser=factory.newSAXParser();}inttestFiles(Stringpath)throwsException{returnFiles.walk(Paths.get(path)).filter(Files::isRegularFile).map(Path::toFile).filter(f->f.getAbsolutePath().endsWith(".xhtml")).mapToInt(this::testFile).sum();}privateinttestFile(Filefile){IdCheckingHandlerhandler=newIdCheckingHandler(getNamespaces(),getIgnoredTags(),getExtraTags());try{saxParser.parse(file,handler);}catch(SAXException|IOExceptione){thrownewRuntimeException(e);}returnhandler.getCount();}}classIdCheckingHandlerextendsDefaultHandler{privateList<String>namespaces;privateList<String>ignoredTags;privateList<String>extraTags;IdCheckingHandler(List<String>namespaces,List<String>ignoredTags,List<String>extraTags){super();this.namespaces=namespaces;this.ignoredTags=ignoredTags;this.extraTags=extraTags;}privateLocatorlocator;privateintcount=0;privatestaticfinalLoggerlog=Logger.getLogger(AbstractTemplateTest.class);@OverridepublicvoidsetDocumentLocator(Locatorlocator){this.locator=locator;}@OverridepublicvoidstartElement(Stringuri,StringlocalName,StringqName,Attributesattributes)throwsSAXException{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++;}}}intgetCount(){returncount;}}
packageuk.ac.cam.ice;importorg.junit.Test;importjavax.persistence.Table;importjava.util.Arrays;importjava.util.List;import staticorg.junit.Assert.assertEquals;/** * Created by wac26 on 03/05/2016. */publicclassTemplateTestextendsAbstractTemplateTest{@OverrideList<String>getNamespaces(){returnArrays.asList("h","ice","p","iceadmin","campl","course","iceworld");}@OverrideList<String>getIgnoredTags(){returnArrays.asList("h:head","h:body","h:outputText","p:ajax","h:panelGroup","p:defaultCommand","p:confirm","iceadmin:ajaxSearch","campl:strikethrough");}@OverrideList<String>getExtraTags(){returnArrays.asList("ui:repeat");}@TestpublicvoidcheckTemplates()throwsException{assertEquals(0,testFiles("./src/main/webapp/templates/"));}@TestpublicvoidcheckPages()throwsException{assertEquals(0,testFiles("./src/main/webapp/pages/"));}@TestpublicvoidcheckCamPLComponents()throwsException{assertEquals(0,testFiles("./src/main/webapp/resources/components/"));}@TestpublicvoidcheckIceComponents()throwsException{assertEquals(0,testFiles("./src/main/webapp/resources/ice/"));}@TestpublicvoidcheckIceAdminComponents()throwsException{assertEquals(0,testFiles("./src/main/webapp/resources/iceadmin/"));}@TestpublicvoidcheckCourseComponents()throwsException{assertEquals(0,testFiles("./src/main/webapp/resources/course/"));}}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.