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