001/** 002 * Copyright 2015 DuraSpace, Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.fcrepo.kernel.api.utils.iterators; 017 018import com.google.common.collect.ImmutableSet; 019import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; 020import com.hp.hpl.jena.graph.Graph; 021import com.hp.hpl.jena.graph.Triple; 022import com.hp.hpl.jena.sparql.graph.GraphFactory; 023import org.junit.Test; 024 025import static com.google.common.collect.ImmutableSet.copyOf; 026import static com.hp.hpl.jena.graph.NodeFactory.createLiteral; 027import static com.hp.hpl.jena.graph.NodeFactory.createURI; 028import static org.junit.Assert.assertTrue; 029 030/** 031 * <p>DifferencingIteratorTest class.</p> 032 * 033 * @author ksclarke 034 */ 035public class GraphDifferencingIteratorTest { 036 037 private Triple t_xyz = new Triple(createURI("x"), createURI("y"), createURI("z")); 038 private Triple t_abc = new Triple(createURI("a"), createURI("b"), createURI("c")); 039 private Triple t_typed_string = new Triple(createURI("i"), 040 createURI("j"), 041 createLiteral("k", XSDDatatype.XSDstring)); 042 private Triple t_untyped_string = new Triple(createURI("i"), 043 createURI("j"), 044 createLiteral("k")); 045 private Triple t_int = new Triple(createURI("i"), 046 createURI("j"), 047 createLiteral("0", XSDDatatype.XSDint)); 048 private Triple t_int_equivalent = new Triple(createURI("i"), 049 createURI("j"), 050 createLiteral("000", XSDDatatype.XSDint)); 051 052 053 @Test 054 public void testAllCommon() { 055 056 final Graph graph = GraphFactory.createDefaultGraph(); 057 graph.add(t_xyz); 058 059 final GraphDifferencingIterator iterator = new GraphDifferencingIterator(graph, new RdfStream(t_xyz)); 060 061 final ImmutableSet<Triple> removed = copyOf(iterator); 062 final ImmutableSet<Triple> added = copyOf(iterator.notCommon()); 063 final ImmutableSet<Triple> common = copyOf(iterator.common()); 064 065 assertTrue(removed.isEmpty()); 066 067 assertTrue(added.isEmpty()); 068 069 assertTrue(common.contains(t_xyz)); 070 } 071 072 @Test 073 public void testRemoveOne() { 074 075 final Graph graph = GraphFactory.createDefaultGraph(); 076 graph.add(t_xyz); 077 078 final GraphDifferencingIterator iterator = new GraphDifferencingIterator(graph, new RdfStream(t_xyz, t_abc)); 079 080 final ImmutableSet<Triple> removed = copyOf(iterator); 081 final ImmutableSet<Triple> added = copyOf(iterator.notCommon()); 082 final ImmutableSet<Triple> common = copyOf(iterator.common()); 083 084 assertTrue(removed.contains(t_abc)); 085 086 assertTrue(added.isEmpty()); 087 088 assertTrue(common.contains(t_xyz)); 089 } 090 091 @Test 092 public void testAddOne() { 093 094 final Graph graph = GraphFactory.createDefaultGraph(); 095 graph.add(t_abc); 096 graph.add(t_xyz); 097 098 final GraphDifferencingIterator iterator = new GraphDifferencingIterator(graph, new RdfStream(t_xyz)); 099 100 final ImmutableSet<Triple> removed = copyOf(iterator); 101 final ImmutableSet<Triple> added = copyOf(iterator.notCommon()); 102 final ImmutableSet<Triple> common = copyOf(iterator.common()); 103 104 assertTrue(removed.isEmpty()); 105 106 assertTrue(added.contains(t_abc)); 107 108 assertTrue(common.contains(t_xyz)); 109 } 110 111 @Test 112 public void testAllDifferent() { 113 114 final Graph graph = GraphFactory.createDefaultGraph(); 115 graph.add(t_xyz); 116 117 final GraphDifferencingIterator iterator = new GraphDifferencingIterator(graph, new RdfStream(t_abc)); 118 119 final ImmutableSet<Triple> removed = copyOf(iterator); 120 final ImmutableSet<Triple> added = copyOf(iterator.notCommon()); 121 final ImmutableSet<Triple> common = copyOf(iterator.common()); 122 123 assertTrue(removed.contains(t_abc)); 124 125 assertTrue(added.contains(t_xyz)); 126 127 assertTrue(common.isEmpty()); 128 } 129 130 @Test 131 public void testCommonRDFEqualStrings() { 132 133 final Graph graph = GraphFactory.createDefaultGraph(); 134 graph.add(t_untyped_string); 135 136 final GraphDifferencingIterator iterator = new GraphDifferencingIterator(graph, new RdfStream(t_typed_string)); 137 138 final ImmutableSet<Triple> removed = copyOf(iterator); 139 final ImmutableSet<Triple> added = copyOf(iterator.notCommon()); 140 final ImmutableSet<Triple> common = copyOf(iterator.common()); 141 142 assertTrue(removed.isEmpty()); 143 assertTrue(added.isEmpty()); 144 assertTrue(common.contains(t_typed_string)); 145 146 } 147 148 @Test 149 public void testCommonRDFEqualIntegers() { 150 151 final Graph graph = GraphFactory.createDefaultGraph(); 152 graph.add(t_int_equivalent); 153 154 final GraphDifferencingIterator iterator = new GraphDifferencingIterator(graph, new RdfStream(t_int)); 155 156 final ImmutableSet<Triple> removed = copyOf(iterator); 157 final ImmutableSet<Triple> added = copyOf(iterator.notCommon()); 158 final ImmutableSet<Triple> common = copyOf(iterator.common()); 159 160 assertTrue(removed.isEmpty()); 161 assertTrue(added.isEmpty()); 162 assertTrue(common.contains(t_int)); 163 164 } 165}