This project has retired. For details please refer to its Attic page.
ExtractionAPITest xref
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.apache.any23.extractor.rdf.JSONLDExtractorFactory;
21  import org.apache.any23.extractor.rdf.NQuadsExtractorFactory;
22  import org.apache.any23.extractor.rdf.NTriplesExtractorFactory;
23  import org.apache.any23.extractor.rdf.RDFXMLExtractorFactory;
24  import org.apache.any23.extractor.rdf.TriXExtractorFactory;
25  import org.apache.any23.extractor.rdf.TurtleExtractorFactory;
26  import org.apache.any23.extractor.rdfa.RDFa11ExtractorFactory;
27  import org.apache.any23.mime.MIMEType;
28  import org.eclipse.rdf4j.rio.RDFFormat;
29  import org.junit.Assert;
30  import org.apache.any23.extractor.example.ExampleExtractor;
31  import org.apache.any23.rdf.RDFUtils;
32  import org.apache.any23.writer.CountingTripleHandler;
33  import org.junit.Test;
34  import org.eclipse.rdf4j.model.IRI;
35  
36  import java.util.List;
37  import java.util.stream.Collectors;
38  
39  /**
40   * Tests the <i>extraction</i> scenario.
41   */
42  public class ExtractionAPITest {
43  
44      private static final String exampleDoc = "http://example.com/";
45      private static final IRI uri = RDFUtils.iri(exampleDoc);
46  
47      @Test
48      public void testDirectInstantiation() throws Exception {
49          CountingTripleHandler out = new CountingTripleHandler();
50          ExampleExtractoreExtractor.html#ExampleExtractor">ExampleExtractor extractor = new ExampleExtractor();
51          ExtractionContext extractionContext = new ExtractionContext("extractor-name", uri);
52          ExtractionResultImpl writer = new ExtractionResultImpl(extractionContext, extractor, out);
53          extractor.run(ExtractionParameters.newDefault(), extractionContext, uri, writer);
54          writer.close();
55          Assert.assertEquals(1, out.getCount());
56      }
57  
58      private static void test(ExtractorFactory<?> factory, RDFFormat... formats) {
59          List<String> mimetypes = factory.getSupportedMIMETypes().stream().map(MIMEType::getFullType)
60                  .collect(Collectors.toList());
61  
62          Assert.assertEquals(formats[0].getDefaultMIMEType(), mimetypes.get(0));
63  
64          for (RDFFormat format : formats) {
65              for (String mimeType : format.getMIMETypes()) {
66                  if (mimeType.endsWith("/xml")) {
67                      // TODO: xml mimetypes are commented out in RDFXML extractor. Why?
68                      continue;
69                  }
70                  Assert.assertTrue(mimeType, mimetypes.contains(mimeType));
71              }
72          }
73      }
74  
75      @Test
76      public void testMimetypes() {
77          test(new JSONLDExtractorFactory(), RDFFormat.JSONLD);
78          test(new NTriplesExtractorFactory(), RDFFormat.NTRIPLES);
79          test(new NQuadsExtractorFactory(), RDFFormat.NQUADS);
80          test(new TurtleExtractorFactory(), RDFFormat.TURTLE, RDFFormat.N3);
81          test(new RDFXMLExtractorFactory(), RDFFormat.RDFXML);
82          test(new TriXExtractorFactory(), RDFFormat.TRIX);
83          test(new RDFa11ExtractorFactory(), RDFFormat.RDFA);
84      }
85  
86  }