View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.rdf.jsonldjava;
19  
20  import static org.junit.Assert.*;
21  
22  import java.util.Optional;
23  
24  import org.apache.commons.rdf.simple.Types;
25  import org.junit.Test;
26  
27  /**
28   * COMMONSRDF-56: Test Literal comparisons with JSONLD-Java
29   */
30  public class JsonLdComparisonTest {
31  
32      JsonLdRDF rdf = new JsonLdRDF();
33  
34      @Test
35      public void literalEqual() throws Exception {
36          final JsonLdLiteral lit1 = rdf.createLiteral("Hello");
37          final JsonLdLiteral lit2 = rdf.createLiteral("Hello");
38          final JsonLdLiteral lit3 = rdf.createLiteral("Hello", Types.XSD_STRING);
39          assertEquals(lit1, lit2);
40          assertEquals(lit1, lit3);
41      }
42  
43      @Test
44      public void literalNotEqual() throws Exception {
45          final JsonLdLiteral lit1 = rdf.createLiteral("Hello");
46          final JsonLdLiteral lit2 = rdf.createLiteral("Hello there");
47          assertNotEquals(lit1, lit2);
48      }
49  
50      @Test
51      public void literalEqualLang() throws Exception {
52          final JsonLdLiteral lit1 = rdf.createLiteral("Allo Allo", "fr");
53          final JsonLdLiteral lit2 = rdf.createLiteral("Allo Allo", "fr");
54          assertEquals(lit1, lit2);
55      }
56  
57      @Test
58      public void literalNotEqualLang() throws Exception {
59          final JsonLdLiteral lit1 = rdf.createLiteral("Hello", "en");
60          final JsonLdLiteral lit2 = rdf.createLiteral("Hello", "en-us");
61          assertNotEquals(lit1, lit2);
62      }
63  
64      @Test
65      public void literalEqualType() throws Exception {
66          final JsonLdLiteral lit1 = rdf.createLiteral("1", Types.XSD_INTEGER);
67          final JsonLdLiteral lit2 = rdf.createLiteral("1", Types.XSD_INTEGER);
68          assertEquals(lit1, lit2);
69      }
70  
71  
72      @Test
73      public void literalNotEqualType() throws Exception {
74          final JsonLdLiteral lit1 = rdf.createLiteral("1", Types.XSD_INTEGER);
75          final JsonLdLiteral lit2 = rdf.createLiteral("2", Types.XSD_INTEGER);
76          final JsonLdLiteral lit3 = rdf.createLiteral("1", Types.XSD_STRING);
77  
78          assertNotEquals(lit1, lit2);
79          assertNotEquals(lit1, lit3);
80      }
81  
82  
83      @Test
84      public void grahContains() throws Exception {
85          try (final JsonLdGraph graph = rdf.createGraph()) {
86              final JsonLdIRI s = rdf.createIRI("http://example.com/s");
87              final JsonLdIRI p = rdf.createIRI("http://example.com/p");
88              final JsonLdLiteral lit1 = rdf.createLiteral("Hello");
89  
90              graph.add(s, p, lit1);
91              assertTrue(graph.contains(s, p, rdf.createLiteral("Hello")));
92              assertTrue(graph.contains(s, p, rdf.createLiteral("Hello", Types.XSD_STRING)));
93              assertFalse(graph.contains(s, p, rdf.createLiteral("Hello", Types.XSD_NORMALIZEDSTRING)));
94              assertFalse(graph.contains(s, p, rdf.createLiteral("Hello", "en")));
95              assertFalse(graph.contains(s, p, rdf.createLiteral("Other")));
96          }
97      }
98  
99      @Test
100     public void datasetContains() throws Exception {
101         try (final JsonLdDataset dataset = rdf.createDataset()) {
102             final JsonLdIRI s = rdf.createIRI("http://example.com/s");
103             final JsonLdIRI p = rdf.createIRI("http://example.com/p");
104             final JsonLdLiteral lit1 = rdf.createLiteral("Hello");
105 
106             dataset.add(null, s, p, lit1);
107             assertTrue(dataset.contains(Optional.empty(), s, p, rdf.createLiteral("Hello")));
108             assertTrue(dataset.contains(Optional.empty(), s, p, rdf.createLiteral("Hello", Types.XSD_STRING)));
109             assertFalse(
110                     dataset.contains(Optional.empty(), s, p, rdf.createLiteral("Hello", Types.XSD_NORMALIZEDSTRING)));
111             assertFalse(dataset.contains(Optional.empty(), s, p, rdf.createLiteral("Hello", "en")));
112             assertFalse(dataset.contains(Optional.empty(), s, p, rdf.createLiteral("Other")));
113         }
114     }
115 
116     @Test
117     public void datasetRemove() throws Exception {
118         try (final JsonLdDataset dataset = rdf.createDataset()) {
119             final JsonLdIRI s = rdf.createIRI("http://example.com/s");
120             final JsonLdIRI p = rdf.createIRI("http://example.com/p");
121             final JsonLdLiteral lit1 = rdf.createLiteral("Hello");
122 
123             dataset.add(null, s, p, lit1);
124             assertTrue(dataset.contains(Optional.empty(), s, p, lit1));
125             dataset.remove(null, null, null, rdf.createLiteral("Other")); // should NOT match
126             assertTrue(dataset.contains(Optional.empty(), s, p, lit1));
127             dataset.remove(null, null, null, rdf.createLiteral("Hello", Types.XSD_STRING)); // SHOULD match
128             assertFalse(dataset.contains(Optional.empty(), s, p, lit1));
129         }
130     }
131 
132     @Test
133     public void datasetStream() throws Exception {
134         try (final JsonLdDataset dataset = rdf.createDataset()) {
135             final JsonLdIRI s = rdf.createIRI("http://example.com/s");
136             final JsonLdIRI p = rdf.createIRI("http://example.com/p");
137             final JsonLdLiteral lit1 = rdf.createLiteral("Hello");
138             final JsonLdLiteral lit2 = rdf.createLiteral("Other");
139 
140             dataset.add(null, s, p, lit1);
141             assertTrue(dataset.stream(Optional.empty(), s, p, lit1).findAny().isPresent());
142             assertFalse(dataset.stream(Optional.empty(), s, p, lit2).findAny().isPresent());
143         }
144     }
145 
146 }