View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *  http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    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   * Test case for {@link DefaultValidator}.
    38   *
    39   * @author Michele Mostarda (mostarda@fbk.eu)
    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              // all nodes with itemscope have an empty string value
    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              // all nodes with itemscope now have a default value of "itemscope"
    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 }