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;
    19
    20  import org.junit.Assert;
    21  import org.apache.any23.extractor.html.TitleExtractor;
    22  import org.apache.any23.rdf.RDFUtils;
    23  import org.apache.any23.writer.TripleHandler;
    24  import org.apache.any23.writer.TripleHandlerException;
    25  import org.junit.After;
    26  import org.junit.Before;
    27  import org.junit.Test;
    28  import org.mockito.Mockito;
    29  import org.eclipse.rdf4j.model.IRI;
    30
    31  import java.io.ByteArrayOutputStream;
    32  import java.io.IOException;
    33  import java.io.PrintStream;
    34  import java.nio.charset.StandardCharsets;
    35  import java.util.Locale;
    36
    37  /**
    38   * Test case for {@link ExtractionResultImpl} class.
    39   *
    40   * @author Michele Mostarda ( michele.mostarda@gmail.com )
    41   *
    42   * @version $Id$
    43   */
    44  public class ExtractionResultImplTest {
    45
    46      private static final IRI TEST_IRI = RDFUtils.iri("http://host/test/service");
    47
    48      private ExtractionResultImpl extractionResult;
    49      private Extractor extractor;
    50      private TripleHandler mockTripleHandler;
    51
    52      @Before
    53      public void setUp() {
    54          extractor = new TitleExtractor();
    55          mockTripleHandler = Mockito.mock(TripleHandler.class);
    56          extractionResult = new ExtractionResultImpl(new ExtractionContext("test-extractor-name", TEST_IRI), extractor,
    57                  mockTripleHandler);
    58      }
    59
    60      @After
    61      public void tearDown() throws TripleHandlerException {
    62          extractionResult.close();
    63          mockTripleHandler.close();
    64          extractor = null;
    65          mockTripleHandler = null;
    66          extractionResult = null;
    67      }
    68
    69      @Test
    70      public void testNotifyErrors() throws IOException {
    71          notifyErrors(extractionResult);
    72          assertContent(extractionResult, 3);
    73
    74          final ExtractionResult subExtractionResult = extractionResult
    75                  .openSubResult(new ExtractionContext("sub-id", RDFUtils.iri("http://sub/uri")));
    76
    77          notifyErrors(subExtractionResult);
    78          assertContent(subExtractionResult, 6);
    79      }
    80
    81      private void notifyErrors(ExtractionResult er) {
    82          er.notifyIssue(IssueReport.IssueLevel.ERROR, "Error message", 1, 2);
    83          er.notifyIssue(IssueReport.IssueLevel.WARNING, "Warning message", 3, 4);
    84          er.notifyIssue(IssueReport.IssueLevel.FATAL, "Fatal message", 5, 6);
    85      }
    86
    87      private void assertContent(ExtractionResult er, int errorCount) {
    88          Assert.assertEquals("Unexpected errors list size.", errorCount, er.getIssues().size());
    89          assertOutputString(er, IssueReport.IssueLevel.ERROR.toString());
    90          assertOutputString(er, IssueReport.IssueLevel.WARNING.toString());
    91          assertOutputString(er, IssueReport.IssueLevel.FATAL.toString());
    92          assertOutputString(er, "errors: " + errorCount);
    93      }
    94
    95      private void assertOutputString(ExtractionResult er, String s) {
    96          ByteArrayOutputStream baos = new ByteArrayOutputStream();
    97          PrintStream ps = new PrintStream(baos, true, StandardCharsets.UTF_8);
    98          er.printReport(ps);
    99          ps.flush();
    100         Assert.assertTrue(String.format(Locale.ROOT, "Cannot find string '%s' in output stream.", s),
    101                 baos.toString(StandardCharsets.UTF_8).contains(s));
    102     }
    103
    104 }