This project has retired. For details please refer to its
Attic page.
DefaultValidatorTest xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.any23.validator;
19
20 import org.apache.any23.extractor.html.DomUtils;
21 import org.apache.any23.extractor.html.TagSoupParser;
22 import org.junit.After;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.w3c.dom.Node;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.net.URI;
33 import java.net.URISyntaxException;
34 import java.util.List;
35
36
37
38
39
40
41 public class DefaultValidatorTest {
42
43 private static final Logger logger = LoggerFactory.getLogger(DefaultValidatorTest.class);
44
45 private DefaultValidator validator;
46
47 @Before
48 public void setUp() {
49 validator = new DefaultValidator();
50 }
51
52 @After
53 public void tearDown() {
54 validator = null;
55 }
56
57 @Test
58 public void testRegisterRule() {
59 validator.addRule(FakeRule.class, FakeFix.class);
60 List<Class<? extends Fix>> fixes = validator.getFixes(FakeRule.class);
61 Assert.assertEquals("Unexpected fixes size.", 1, fixes.size());
62 Assert.assertEquals("Unexpected fix.", FakeFix.class, fixes.get(0));
63 validator.removeRule(FakeRule.class);
64 Assert.assertEquals("Unexpected fixes size.", 0, validator.getFixes(FakeRule.class).size());
65 }
66
67 @Test
68 public void testMissingOGNamespace() throws IOException, ValidatorException, URISyntaxException {
69 DOMDocument document = loadDocument("missing-og-namespace.html");
70 Assert.assertNull(document.getNode("/HTML").getAttributes().getNamedItem("xmlns:og"));
71 ValidationReport validationReport = validator.validate(document, true);
72 Assert.assertNotNull(document.getNode("/HTML").getAttributes().getNamedItem("xmlns:og"));
73 if (logger.isDebugEnabled()) {
74 logger.debug(validationReport.toString());
75 }
76 }
77
78 @Test
79 public void testMissingItemscopeAttributeValue() throws IOException, URISyntaxException, ValidatorException {
80 DOMDocument document = loadDocument("microdata-basic.html");
81 List<Node> nullItemScopeNodes = document.getNodesWithAttribute("itemscope");
82 for (Node node : nullItemScopeNodes) {
83
84 Assert.assertEquals("", node.getAttributes().getNamedItem("itemscope").getNodeValue());
85 }
86 ValidationReport validationReport = validator.validate(document, true);
87 List<Node> fixedItemScopeNodes = document.getNodesWithAttribute("itemscope");
88 for (Node node : fixedItemScopeNodes) {
89
90 Assert.assertNotNull(node.getAttributes().getNamedItem("itemscope").getNodeValue());
91 Assert.assertEquals("itemscope", node.getAttributes().getNamedItem("itemscope").getNodeValue());
92 }
93 if (logger.isDebugEnabled()) {
94 logger.debug(validationReport.toString());
95 }
96 }
97
98 @Test
99 public void testMetaNameMisuse() throws Exception {
100 DOMDocument document = loadDocument("meta-name-misuse.html");
101 ValidationReport validationReport = validator.validate(document, true);
102 if (logger.isDebugEnabled()) {
103 logger.debug(validationReport.toString());
104 logger.debug(DomUtils.serializeToXML(document.getOriginalDocument(), true));
105 }
106
107 List<Node> metas = document.getNodes("/HTML/HEAD/META");
108 for (Node meta : metas) {
109 Node name = meta.getAttributes().getNamedItem("name");
110 if (name != null) {
111 Assert.assertFalse(name.getTextContent().contains(":"));
112 }
113 }
114 }
115
116 @Test
117 public void testAboutNotIRIRule() throws Exception {
118 DOMDocument document = loadDocument("invalid-rdfa-about.html");
119 ValidationReport validationReport = validator.validate(document, true);
120 if (logger.isDebugEnabled()) {
121 logger.debug(validationReport.toString());
122 }
123 Assert.assertEquals("Unexpected number of issues.", 1, validationReport.getIssues().size());
124 }
125
126 public static DOMDocument loadDocument(String document) throws IOException, URISyntaxException {
127 InputStream is = DefaultValidatorTest.class.getResourceAsStream(document);
128 final String documentIRI = "http://test.com";
129 TagSoupParser tsp = new TagSoupParser(is, documentIRI);
130 return new DefaultDOMDocument(new URI(documentIRI), tsp.getDOM());
131 }
132
133 static class FakeRule implements Rule {
134 public String getHRName() {
135 return "fake-rule";
136 }
137
138 public boolean applyOn(DOMDocument document, @SuppressWarnings("rawtypes") RuleContext context,
139 ValidationReportBuilder validationReportBuilder) {
140 throw new UnsupportedOperationException();
141 }
142 }
143
144 static class FakeFix implements Fix {
145 public String getHRName() {
146 return "fake-fix";
147 }
148
149 public void execute(Rule rule, @SuppressWarnings("rawtypes") RuleContext context, DOMDocument document) {
150 throw new UnsupportedOperationException();
151 }
152 }
153
154 }