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.rdfa;
    19
    20  import org.apache.any23.extractor.IssueReport;
    21  import org.apache.any23.extractor.ExtractionContext;
    22  import org.apache.any23.extractor.ExtractionException;
    23  import org.apache.any23.extractor.ExtractionResult;
    24  import org.apache.any23.extractor.ExtractionResultImpl;
    25  import org.apache.any23.extractor.Extractor;
    26  import org.apache.any23.extractor.ExtractorDescription;
    27  import org.apache.any23.writer.TripleHandler;
    28  import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
    29  import org.junit.Assert;
    30  import org.junit.Test;
    31  import java.io.ByteArrayOutputStream;
    32  import java.io.IOException;
    33  import java.io.PrintWriter;
    34  import java.nio.charset.StandardCharsets;
    35
    36  import static org.mockito.Mockito.mock;
    37  import static org.mockito.Mockito.when;
    38
    39  /**
    40   * Test case for {@link org.apache.any23.extractor.ExtractionException}.
    41   *
    42   * @author Michele Mostarda (mostarda@fbk.eu)
    43   */
    44  public class ExtractionExceptionTest {
    45
    46      @Test
    47      public void testPrintStackTrace() throws ExtractionException, IOException {
    48          final String FAKE_EXTRACTOR_NAME = "fake-extractor-name";
    49          final Extractor extractor = mock(Extractor.class);
    50          final ExtractorDescription ed = mock(ExtractorDescription.class);
    51          when(ed.getExtractorName()).thenReturn(FAKE_EXTRACTOR_NAME);
    52          when(extractor.getDescription()).thenReturn(ed);
    53
    54          final TripleHandler th = mock(TripleHandler.class);
    55          final ExtractionContext extractionContext = new ExtractionContext(extractor.getDescription().getExtractorName(),
    56                  SimpleValueFactory.getInstance().createIRI("http://fake.document.uri"));
    57          final ExtractionResult er = new ExtractionResultImpl(extractionContext, extractor, th);
    58          er.notifyIssue(IssueReport.IssueLevel.FATAL, "Fake fatal error.", 1, 2);
    59          er.notifyIssue(IssueReport.IssueLevel.ERROR, "Fake error.", 3, 4);
    60          er.notifyIssue(IssueReport.IssueLevel.WARNING, "Fake warning.", 5, 6);
    61
    62          ExtractionException ee = new ExtractionException("Fake message.", new RuntimeException("Fake cause"), er);
    63          ByteArrayOutputStream baos = new ByteArrayOutputStream();
    64          ee.printStackTrace(new PrintWriter(baos, true, StandardCharsets.UTF_8));
    65          final String bufferContent = baos.toString(StandardCharsets.UTF_8);
    66          Assert.assertTrue("Unexpected message content.", bufferContent.contains(FAKE_EXTRACTOR_NAME));
    67          Assert.assertTrue("Unexpected message content.", bufferContent.contains("http://fake.document.uri"));
    68          Assert.assertTrue("Unexpected message content.",
    69                  bufferContent.contains(ExtractionContext.ROOT_EXTRACTION_RESULT_ID));
    70          Assert.assertTrue("Unexpected message content.", bufferContent.contains("Fake fatal error."));
    71          Assert.assertTrue("Unexpected message content.", bufferContent.contains("(1,2)"));
    72          Assert.assertTrue("Unexpected message content.", bufferContent.contains("Fake error."));
    73          Assert.assertTrue("Unexpected message content.", bufferContent.contains("(3,4)"));
    74          Assert.assertTrue("Unexpected message content.", bufferContent.contains("Fake warning."));
    75          Assert.assertTrue("Unexpected message content.", bufferContent.contains("(5,6)"));
    76          baos.close();
    77      }
    78  }