Skip to content

Commit

Permalink
RdfStream can now be created from Model
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 23, 2013
1 parent 0d8d13c commit 72aa4d8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 26 deletions.
Expand Up @@ -17,6 +17,7 @@
package org.fcrepo.kernel.utils.iterators;

import static com.google.common.collect.Iterators.singletonIterator;
import static com.google.common.collect.Iterators.transform;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static java.util.Collections.emptySet;

Expand All @@ -26,10 +27,12 @@
import java.util.Map;
import java.util.Set;

import com.google.common.base.Function;
import com.google.common.collect.ForwardingIterator;
import com.google.common.collect.Iterators;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Statement;

/**
* @author ajs6f
Expand Down Expand Up @@ -156,4 +159,24 @@ public Model asModel() {
return model;
}

/**
* @param model A {@link Model} containing the prefix mappings and triples to be put into
* this stream of RDF
* @return
*/
public static RdfStream fromModel(final Model model) {
final Iterator<Triple> triples = transform(model.listStatements(), statement2triple);
return new RdfStream(triples).addNamespaces(model.getNsPrefixMap());
}

public static Function<Statement, Triple> statement2triple = new Function<Statement, Triple>() {

@Override
public Triple apply(final Statement s) {
return s.asTriple();
}

};


}
Expand Up @@ -18,7 +18,10 @@

import static com.hp.hpl.jena.graph.NodeFactory.createAnon;
import static com.hp.hpl.jena.graph.Triple.create;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.fcrepo.kernel.utils.iterators.RdfStream.fromModel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -47,7 +50,10 @@ public class RdfStreamTest {
private Iterator<Triple> mockIterator;

@Mock
private Triple triple1, triple2, triple3;
private Triple mockTriple1, mockTriple2, mockTriple3;

private final static Triple triple = create(createAnon(), createAnon(),
createAnon());

private final static String prefix1 = "testNS";

Expand Down Expand Up @@ -75,9 +81,9 @@ public void testHasNext() {

@Test
public void testNext() {
when(mockIterator.next()).thenReturn(triple1, triple2);
assertEquals(triple1, testStream.next());
assertEquals(triple2, testStream.next());
when(mockIterator.next()).thenReturn(mockTriple1, mockTriple2);
assertEquals(mockTriple1, testStream.next());
assertEquals(mockTriple2, testStream.next());
}

@Test
Expand All @@ -98,43 +104,43 @@ public void testDefaultConstructor() {

@Test
public void testIteratorConstructor() {
testStream = new RdfStream(ImmutableSet.of(triple1, triple2).iterator());
assertEquals(triple1, testStream.next());
testStream = new RdfStream(ImmutableSet.of(mockTriple1, mockTriple2).iterator());
assertEquals(mockTriple1, testStream.next());
}

@Test
public void testCollectionConstructor() {
testStream = new RdfStream(ImmutableSet.of(triple1, triple2));
assertEquals(triple1, testStream.next());
testStream = new RdfStream(ImmutableSet.of(mockTriple1, mockTriple2));
assertEquals(mockTriple1, testStream.next());
}

@Test
public void testConcat() {
when(mockIterator.next()).thenReturn(triple1, triple2);
final RdfStream testStream2 = new RdfStream(ImmutableSet.of(triple3));
when(mockIterator.next()).thenReturn(mockTriple1, mockTriple2);
final RdfStream testStream2 = new RdfStream(ImmutableSet.of(mockTriple3));
testStream.concat(testStream2);
assertEquals(triple3, testStream.next());
assertEquals(mockTriple3, testStream.next());
}

@Test
public void testCollectionConcat() {
when(mockIterator.next()).thenReturn(triple1, triple2);
testStream.concat(ImmutableSet.of(triple3));
assertEquals(triple3, testStream.next());
when(mockIterator.next()).thenReturn(mockTriple1, mockTriple2);
testStream.concat(ImmutableSet.of(mockTriple3));
assertEquals(mockTriple3, testStream.next());
}

@Test
public void testArrayConcat() {
when(mockIterator.next()).thenReturn(triple1, triple2);
testStream.concat(new Triple[]{triple3});
assertEquals(triple3, testStream.next());
when(mockIterator.next()).thenReturn(mockTriple1, mockTriple2);
testStream.concat(new Triple[]{mockTriple3});
assertEquals(mockTriple3, testStream.next());
}

@Test
public void testSingletonConcat() {
when(mockIterator.next()).thenReturn(triple1, triple2);
testStream.concat(triple3);
assertEquals(triple3, testStream.next());
when(mockIterator.next()).thenReturn(mockTriple1, mockTriple2);
testStream.concat(mockTriple3);
assertEquals(mockTriple3, testStream.next());
}

@Test
Expand All @@ -155,24 +161,34 @@ public void testAddNamespaces() {

@Test
public void testAsModel() throws RepositoryException {
final Triple t = create(createAnon(), createAnon(), createAnon());
testStream = new RdfStream(singletonList(t));
testStream = new RdfStream(singletonList(triple));
testStream.addNamespaces(testNamespaces);

final Model testModel = testStream.asModel();
assertEquals(testModel.getNsPrefixMap(), testNamespaces);
assertTrue(testModel.contains(testModel.asStatement(t)));
assertTrue(testModel.contains(testModel.asStatement(triple)));
}

@Test
public void testFromModel() {
final Model model = createDefaultModel();
model.setNsPrefix(prefix1, uri1);
testStream = fromModel(model.add(model.asStatement(triple)));
assertEquals("Didn't find triple in stream from Model!", triple,
testStream.next());
assertEquals("Didn't find namespace mapping in stream from Model!",
singletonMap(prefix1, uri1), testStream.namespaces());
}

@Test
public void testCanContinue() {
when(mockIterator.hasNext()).thenReturn(true).thenThrow(
new RuntimeException("Expected.")).thenReturn(true);
assertTrue(mockIterator.hasNext());
assertTrue(testStream.hasNext());
try {
mockIterator.hasNext();
testStream.hasNext();
} catch (final RuntimeException ex) {
}
assertTrue("Couldn't continue with iteration!",mockIterator.hasNext());
assertTrue("Couldn't continue with iteration!", testStream.hasNext());
}
}

0 comments on commit 72aa4d8

Please sign in to comment.