This project has retired. For details please refer to its Attic page.
RDFa11ParserTest 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.rdfa;
19  
20  import org.apache.any23.rdf.RDFUtils;
21  import org.junit.Assert;
22  import org.junit.Test;
23  import org.eclipse.rdf4j.model.Literal;
24  import org.eclipse.rdf4j.model.vocabulary.RDF;
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Element;
27  
28  import javax.xml.parsers.DocumentBuilder;
29  import javax.xml.parsers.DocumentBuilderFactory;
30  import javax.xml.parsers.ParserConfigurationException;
31  import javax.xml.transform.TransformerException;
32  import java.io.IOException;
33  import java.net.MalformedURLException;
34  import java.net.URL;
35  
36  import static org.mockito.Mockito.mock;
37  
38  /**
39   * @author Michele Mostarda (mostarda@fbk.eu)
40   * 
41   * @deprecated since 2.3
42   */
43  @Deprecated
44  public class RDFa11ParserTest {
45  
46      @Test
47      public void testGetDocumentBase() throws MalformedURLException {
48          final URL in = new URL("http://fake.doc/url");
49          final URL out = RDFa11Parser.getDocumentBase(in, mock(Document.class));
50          Assert.assertEquals(in, out);
51      }
52  
53      @Test
54      public void testExtractPrefixSections() {
55          final String[] sections = RDFa11Parser.extractPrefixSections("p1:v1 p2: v2 p3:   v3\np4:v4      p5:     v5");
56          Assert.assertEquals(5, sections.length);
57          int i = 0;
58          Assert.assertEquals("p1:v1", sections[i++]);
59          Assert.assertEquals("p2:v2", sections[i++]);
60          Assert.assertEquals("p3:v3", sections[i++]);
61          Assert.assertEquals("p4:v4", sections[i++]);
62          Assert.assertEquals("p5:v5", sections[i]);
63      }
64  
65      @Test
66      public void testIsCURIEPositive() {
67          Assert.assertTrue(RDFa11Parser.isCURIE("[dbr:Albert_Einstein]"));
68      }
69  
70      @Test
71      public void testIsCURIENegative() {
72          Assert.assertFalse(RDFa11Parser.isCURIE("[Albert_Einstein]"));
73      }
74  
75      @Test
76      public void testIsCURIEBNodePositive() {
77          Assert.assertTrue(RDFa11Parser.isCURIEBNode("[_:john]"));
78      }
79  
80      @Test
81      public void testIsCURIEBNodeNegative() {
82          Assert.assertFalse(RDFa11Parser.isCURIEBNode("[:john]"));
83      }
84  
85      @Test
86      public void testIsRelativeNegative() {
87          Assert.assertFalse(RDFa11Parser.isRelativeNode(mock(Document.class)));
88      }
89  
90      @Test
91      public void testIsRelativePositive1() throws ParserConfigurationException {
92          Element div = getRootDocument().createElement("DIV");
93          div.setAttribute("rel", "http://fake");
94          Assert.assertTrue(RDFa11Parser.isRelativeNode(div));
95      }
96  
97      @Test
98      public void testIsRelativePositive2() throws ParserConfigurationException {
99          Element div = getRootDocument().createElement("DIV");
100         div.setAttribute("rev", "http://fake");
101         Assert.assertTrue(RDFa11Parser.isRelativeNode(div));
102     }
103 
104     @Test
105     public void testUpdateIRIMapping() throws ParserConfigurationException {
106         Element div = getRootDocument().createElement("DIV");
107         div.setAttribute("xmlns:dc", "http://purl.org/dc/terms/");
108         div.setAttribute("xmlns:fake", "http://fake.org/");
109         final RDFa11Parser parser = new RDFa11Parser();
110         parser.updateIRIMapping(div);
111         Assert.assertEquals("http://purl.org/dc/terms/", parser.getMapping("dc").toString());
112         Assert.assertEquals("http://fake.org/", parser.getMapping("fake").toString());
113     }
114 
115     @Test
116     public void testGetAsPlainLiteral() throws ParserConfigurationException {
117         Document doc = getRootDocument();
118         Element div = doc.createElement("DIV");
119         div.setTextContent("text");
120 
121         final Literal literal = RDFa11Parser.getAsPlainLiteral(div, null);
122         Assert.assertEquals(RDFUtils.literal("text"), literal);
123     }
124 
125     @Test
126     public void testGetAsXMLLiteral() throws ParserConfigurationException, IOException, TransformerException {
127         Document doc = getRootDocument();
128         Element root = doc.createElement("DIV");
129         Element child1 = doc.createElement("DIV");
130         Element child2 = doc.createElement("DIV");
131         root.setAttribute(RDFa11Parser.DATATYPE_ATTRIBUTE, RDFa11Parser.XML_LITERAL_DATATYPE);
132         child1.setTextContent("text 1");
133         child2.setTextContent("text 2");
134         root.appendChild(child1);
135         root.appendChild(child2);
136 
137         final Literal literal = RDFa11Parser.getAsXMLLiteral(root);
138         final String value = "<DIV datatype=\"rdf:XMLLiteral\">" + "<DIV>text 1</DIV><DIV>text 2</DIV>" + "</DIV>";
139         Assert.assertEquals(RDFUtils.literal(value, RDF.XMLLITERAL), literal);
140     }
141 
142     private Document getRootDocument() throws ParserConfigurationException {
143         final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
144         final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
145         return docBuilder.newDocument();
146     }
147 
148 }