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.calendar;
    19
    20  import org.apache.any23.extractor.html.AbstractExtractorTestCase;
    21  import org.eclipse.rdf4j.model.BNode;
    22  import org.eclipse.rdf4j.model.Statement;
    23  import org.eclipse.rdf4j.repository.RepositoryException;
    24  import org.eclipse.rdf4j.rio.RDFFormat;
    25  import org.eclipse.rdf4j.rio.RDFHandler;
    26  import org.eclipse.rdf4j.rio.RDFHandlerException;
    27  import org.eclipse.rdf4j.rio.RDFParseException;
    28  import org.eclipse.rdf4j.rio.RDFParser;
    29  import org.eclipse.rdf4j.rio.Rio;
    30  import org.junit.Assert;
    31
    32  import java.io.File;
    33  import java.io.FileReader;
    34  import java.io.IOException;
    35  import java.nio.charset.StandardCharsets;
    36  import java.util.ArrayList;
    37  import java.util.List;
    38
    39  abstract class BaseCalendarExtractorTest extends AbstractExtractorTestCase {
    40
    41      abstract String filePrefix();
    42
    43      protected void extractAndVerifyAgainstNQuads(String actual, String expected)
    44              throws RepositoryException, RDFHandlerException, IOException, RDFParseException {
    45          String filePrefix = filePrefix();
    46          assertExtract(filePrefix + actual);
    47          assertModelNotEmpty();
    48          List<Statement> expectedStatements = loadResultStatement(filePrefix + expected);
    49          int actualStmtSize = getStatementsSize(null, null, null);
    50          Assert.assertEquals(expectedStatements.size(), actualStmtSize);
    51          for (Statement statement : expectedStatements) {
    52              assertContains(statement.getSubject() instanceof BNode ? null : statement.getSubject(),
    53                      statement.getPredicate(), statement.getObject() instanceof BNode ? null : statement.getObject());
    54          }
    55      }
    56
    57      private List<Statement> loadResultStatement(String resultFilePath)
    58              throws RDFHandlerException, IOException, RDFParseException {
    59          RDFParser nQuadsParser = Rio.createParser(RDFFormat.NQUADS);
    60          TestRDFHandler rdfHandler = new TestRDFHandler();
    61          nQuadsParser.setRDFHandler(rdfHandler);
    62          File file = copyResourceToTempFile(resultFilePath);
    63          nQuadsParser.parse(new FileReader(file, StandardCharsets.UTF_8), baseIRI.toString());
    64          return rdfHandler.getStatements();
    65      }
    66
    67      private static class TestRDFHandler implements RDFHandler {
    68
    69          private final List<Statement> statements = new ArrayList<Statement>();
    70
    71          protected List<Statement> getStatements() {
    72              return statements;
    73          }
    74
    75          public void startRDF() throws RDFHandlerException {
    76          }
    77
    78          public void endRDF() throws RDFHandlerException {
    79          }
    80
    81          public void handleNamespace(String s, String s1) throws RDFHandlerException {
    82              throw new UnsupportedOperationException();
    83          }
    84
    85          public void handleStatement(Statement statement) throws RDFHandlerException {
    86              statements.add(statement);
    87          }
    88
    89          public void handleComment(String s) throws RDFHandlerException {
    90              throw new UnsupportedOperationException();
    91          }
    92      }
    93  }