This project has retired. For details please refer to its Attic page.
MockTripleHandler 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.rdf.RDFUtils;
21  import org.apache.any23.writer.TripleHandler;
22  import org.junit.Assert;
23  import org.eclipse.rdf4j.model.Resource;
24  import org.eclipse.rdf4j.model.IRI;
25  import org.eclipse.rdf4j.model.Value;
26  
27  import java.util.LinkedList;
28  import java.util.List;
29  
30  /**
31   *
32   * Just a mockup implementing a {@link org.apache.any23.writer.TripleHandler}. Used only for test purposes.
33   *
34   */
35  // TODO: replace with Mockito
36  public class MockTripleHandler implements TripleHandler {
37  
38      private final List<String> expectations = new LinkedList<String>();
39  
40      public void expectStartDocument(IRI documentIRI) {
41          expectations.add("startDocument(" + documentIRI + ")");
42      }
43  
44      public void expectEndDocument(IRI documentIRI) {
45          expectations.add("endDocument(" + documentIRI + ")");
46      }
47  
48      public void expectSetContentLength(long contentLength) {
49          expectations.add("setContentLength(" + contentLength + ")");
50      }
51  
52      public void expectClose() {
53          expectations.add("close()");
54      }
55  
56      public void expectOpenContext(String extractorName, IRI documentIRI, String localID) {
57          expectations.add("openContext(" + new ExtractionContext(extractorName, documentIRI, localID) + ")");
58      }
59  
60      public void expectCloseContext(String extractorName, IRI documentIRI, String localID) {
61          expectations.add("closeContext(" + new ExtractionContext(extractorName, documentIRI, localID) + ")");
62      }
63  
64      public void expectTriple(Resource s, IRI p, Value o, IRI g, String extractorName, IRI documentIRI, String localID) {
65          expectations.add("triple(" + RDFUtils.quad(s, p, o, g) + ", "
66                  + new ExtractionContext(extractorName, documentIRI, localID) + ")");
67      }
68  
69      public void expectNamespace(String prefix, String uri, String extractorName, IRI documentIRI, String localID) {
70          expectations.add("namespace(" + prefix + ", " + uri + ", "
71                  + new ExtractionContext(extractorName, documentIRI, localID) + ")");
72      }
73  
74      public void verify() {
75          if (!expectations.isEmpty()) {
76              Assert.fail("Expected " + expectations.size() + " more invocation(s), first: " + expectations.get(0));
77          }
78      }
79  
80      public void startDocument(IRI documentIRI) {
81          assertNextExpectation("startDocument(" + documentIRI + ")");
82      }
83  
84      public void endDocument(IRI documentIRI) {
85          assertNextExpectation("endDocument(" + documentIRI + ")");
86      }
87  
88      public void openContext(ExtractionContext context) {
89          assertNextExpectation("openContext(" + context + ")");
90      }
91  
92      public void closeContext(ExtractionContext context) {
93          assertNextExpectation("closeContext(" + context + ")");
94      }
95  
96      public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context) {
97          assertNextExpectation("triple(" + RDFUtils.quad(s, p, o, g) + ", " + context + ")");
98      }
99  
100     public void receiveNamespace(String prefix, String uri, ExtractionContext context) {
101         assertNextExpectation("namespace(" + prefix + ", " + uri + ", " + context + ")");
102     }
103 
104     public void close() {
105         assertNextExpectation("close()");
106     }
107 
108     public void setContentLength(long contentLength) {
109         assertNextExpectation("setContentLength(" + contentLength + ")");
110     }
111 
112     private void assertNextExpectation(String invocation) {
113         if (expectations.isEmpty()) {
114             Assert.fail("Next expectation was <null>, invocation was " + invocation);
115         }
116         String expectation = expectations.remove(0);
117         Assert.assertEquals("Invocation doesn't match expectation", expectation, invocation);
118     }
119 
120 }