1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */1718package org.apache.any23.extractor.rdfa;
1920import org.apache.any23.extractor.IssueReport;
21import org.apache.any23.extractor.ExtractionContext;
22import org.apache.any23.extractor.ExtractionException;
23import org.apache.any23.extractor.ExtractionResult;
24import org.apache.any23.extractor.ExtractionResultImpl;
25import org.apache.any23.extractor.Extractor;
26import org.apache.any23.extractor.ExtractorDescription;
27import org.apache.any23.writer.TripleHandler;
28import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
29import org.junit.Assert;
30import org.junit.Test;
31import java.io.ByteArrayOutputStream;
32import java.io.IOException;
33import java.io.PrintWriter;
34import java.nio.charset.StandardCharsets;
3536importstatic org.mockito.Mockito.mock;
37importstatic org.mockito.Mockito.when;
3839/**40 * Test case for {@link org.apache.any23.extractor.ExtractionException}.41 *42 * @author Michele Mostarda (mostarda@fbk.eu)43 */44publicclassExtractionExceptionTest {
4546 @Test
47publicvoid testPrintStackTrace() throws ExtractionException, IOException {
48final String FAKE_EXTRACTOR_NAME = "fake-extractor-name";
49final Extractor extractor = mock(Extractor.class);
50final ExtractorDescription ed = mock(ExtractorDescription.class);
51 when(ed.getExtractorName()).thenReturn(FAKE_EXTRACTOR_NAME);
52 when(extractor.getDescription()).thenReturn(ed);
5354final TripleHandler th = mock(TripleHandler.class);
55final ExtractionContext extractionContext = new ExtractionContext(extractor.getDescription().getExtractorName(),
56 SimpleValueFactory.getInstance().createIRI("http://fake.document.uri"));
57final 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);
6162 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));
65final 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 }