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.extractor.html;
    19
    20  import org.apache.any23.extractor.ExtractorFactory;
    21  import org.apache.any23.rdf.RDFUtils;
    22  import org.apache.any23.vocab.FOAF;
    23  import org.apache.any23.vocab.SINDICE;
    24  import org.apache.any23.vocab.XFN;
    25  import org.junit.Assert;
    26  import org.junit.Test;
    27  import org.eclipse.rdf4j.model.Resource;
    28  import org.eclipse.rdf4j.model.IRI;
    29  import org.eclipse.rdf4j.model.vocabulary.RDF;
    30  import org.eclipse.rdf4j.repository.RepositoryException;
    31
    32  /**
    33   *
    34   * Reference Test class for the {@link XFNExtractor} extractor.
    35   *
    36   */
    37  public class XFNExtractorTest extends AbstractExtractorTestCase {
    38
    39      private static final FOAF vFOAF = FOAF.getInstance();
    40      private static final SINDICE vSINDICE = SINDICE.getInstance();
    41      private static final XFN vXFN = XFN.getInstance();
    42
    43      private final static IRI bobsHomepage = baseIRI;
    44
    45      private final static IRI alicesHomepage = RDFUtils.iri("http://alice.example.com/");
    46
    47      private final static IRI charliesHomepage = RDFUtils.iri("http://charlie.example.com/");
    48
    49      protected ExtractorFactory<?> getExtractorFactory() {
    50          return new XFNExtractorFactory();
    51      }
    52
    53      @Test
    54      public void testNoMicroformats() throws RepositoryException {
    55          assertExtract("/html/html-without-uf.html");
    56          assertModelEmpty();
    57      }
    58
    59      @Test
    60      public void testLinkWithoutRel() throws RepositoryException {
    61          assertExtract("/microformats/xfn/no-rel.html");
    62          assertModelEmpty();
    63      }
    64
    65      @Test
    66      public void testNoXFNRel() throws RepositoryException {
    67          assertExtract("/microformats/xfn/no-valid-rel.html");
    68          assertModelEmpty();
    69      }
    70
    71      @Test
    72      public void testDetectPresenceOfXFN() throws RepositoryException {
    73          assertExtract("/microformats/xfn/simple-me.html");
    74      }
    75
    76      @Test
    77      public void testSimpleMeLink() throws RepositoryException {
    78          assertExtract("/microformats/xfn/simple-me.html");
    79          Resource person = findExactlyOneBlankSubject(RDF.TYPE, vFOAF.Person);
    80          assertContains(person, vXFN.mePage, baseIRI);
    81          assertContains(person, vXFN.mePage, bobsHomepage);
    82      }
    83
    84      @Test
    85      public void testRelativeIRIisResolvedAgainstBase() throws RepositoryException {
    86          assertExtract("/microformats/xfn/with-relative-uri.html");
    87          assertContains(null, vXFN.mePage, RDFUtils.iri("http://bob.example.com/foo"));
    88      }
    89
    90      @Test
    91      public void testParseTagSoup() throws RepositoryException {
    92          assertExtract("/microformats/xfn/tagsoup.html");
    93          Resource person = findExactlyOneBlankSubject(RDF.TYPE, vFOAF.Person);
    94          assertContains(person, vXFN.mePage, baseIRI);
    95      }
    96
    97      @Test
    98      public void testSimpleFriend() throws RepositoryException {
    99          assertExtract("/microformats/xfn/simple-friend.html");
    100         Resource bob = findExactlyOneBlankSubject(vXFN.mePage, baseIRI);
    101         Resource alice = findExactlyOneBlankSubject(vXFN.mePage, alicesHomepage);
    102         assertContains(bob, RDF.TYPE, vFOAF.Person);
    103         assertContains(alice, RDF.TYPE, vFOAF.Person);
    104         Assert.assertFalse(alice.equals(bob));
    105         assertContains(bob, vXFN.friend, alice);
    106         assertContains(baseIRI, vXFN.getExtendedProperty("friend"), alicesHomepage);
    107     }
    108
    109     @Test
    110     public void testFriendAndSweetheart() throws RepositoryException {
    111         assertExtract("/microformats/xfn/multiple-rel.html");
    112         Resource bob = findExactlyOneBlankSubject(vXFN.mePage, baseIRI);
    113         Resource alice = findExactlyOneBlankSubject(vXFN.mePage, alicesHomepage);
    114         assertContains(bob, vXFN.friend, alice);
    115         assertContains(baseIRI, vXFN.getExtendedProperty("friend"), alicesHomepage);
    116
    117         assertContains(bob, vXFN.sweetheart, alice);
    118         assertContains(baseIRI, vXFN.getExtendedProperty("sweetheart"), alicesHomepage);
    119     }
    120
    121     @Test
    122     public void testMultipleFriends() throws RepositoryException {
    123         assertExtract("/microformats/xfn/multiple-friends.html");
    124         Resource bob = findExactlyOneBlankSubject(vXFN.mePage, baseIRI);
    125         Resource alice = findExactlyOneBlankSubject(vXFN.mePage, alicesHomepage);
    126         Resource charlie = findExactlyOneBlankSubject(vXFN.mePage, charliesHomepage);
    127         assertContains(bob, vXFN.friend, alice);
    128         assertContains(baseIRI, vXFN.getExtendedProperty("friend"), alicesHomepage);
    129
    130         assertContains(bob, vXFN.friend, charlie);
    131         assertContains(baseIRI, vXFN.getExtendedProperty("friend"), charliesHomepage);
    132     }
    133
    134     @Test
    135     public void testSomeLinksWithoutRel() throws RepositoryException {
    136         assertExtract("/microformats/xfn/some-links-without-rel.html");
    137         assertNotContains(null, null, alicesHomepage);
    138         assertContains(null, null, charliesHomepage);
    139     }
    140
    141     @Test
    142     public void testForSomeReasonICantBeMyOwnSweetheart() throws RepositoryException {
    143         assertExtract("/microformats/xfn/me-and-sweetheart.html");
    144         assertModelEmpty();
    145     }
    146
    147     @Test
    148     public void testIgnoreExtraSpacesInRel() throws RepositoryException {
    149         assertExtract("/microformats/xfn/strip-spaces.html");
    150         assertContains(null, vXFN.mePage, baseIRI);
    151     }
    152
    153     @Test
    154     public void testMixedCaseATag() throws RepositoryException {
    155         assertExtract("/microformats/xfn/mixed-case.html");
    156         assertContains(null, vXFN.mePage, baseIRI);
    157     }
    158
    159     @Test
    160     public void testUpcaseHREF() throws RepositoryException {
    161         assertExtract("/microformats/xfn/upcase-href.html");
    162         assertContains(null, vXFN.mePage, baseIRI);
    163     }
    164 }