Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit tests for NamespaceChangedStatementListener
- Loading branch information
Showing
2 changed files
with
101 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
fcrepo-kernel/src/test/java/org/fcrepo/utils/NamespaceChangedStatementListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.fcrepo.utils; | ||
|
||
import com.hp.hpl.jena.rdf.model.ResourceFactory; | ||
import com.hp.hpl.jena.rdf.model.Statement; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import javax.jcr.NamespaceRegistry; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import javax.jcr.Workspace; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class NamespaceChangedStatementListenerTest { | ||
|
||
private NamespaceChangedStatementListener testObj; | ||
private NamespaceRegistry mockNamespaceRegistry; | ||
|
||
@Before | ||
public void setUp() throws RepositoryException { | ||
Session mockSession = mock(Session.class); | ||
Workspace mockWorkspace = mock(Workspace.class); | ||
mockNamespaceRegistry = mock(NamespaceRegistry.class); | ||
when(mockWorkspace.getNamespaceRegistry()).thenReturn(mockNamespaceRegistry); | ||
when(mockSession.getWorkspace()).thenReturn(mockWorkspace); | ||
testObj = new NamespaceChangedStatementListener(mockSession); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldAddNamespaceStatement() throws RepositoryException { | ||
Statement mockStatement = mock(Statement.class); | ||
when(mockStatement.getSubject()).thenReturn(ResourceFactory.createResource("uri")); | ||
when(mockStatement.getPredicate()).thenReturn(ResourceFactory.createProperty(JcrRdfTools.HAS_NAMESPACE_PREDICATE)); | ||
when(mockStatement.getObject()).thenReturn(ResourceFactory.createPlainLiteral("123")); | ||
|
||
testObj.addedStatement(mockStatement); | ||
verify(mockNamespaceRegistry).registerNamespace("123", "uri"); | ||
} | ||
|
||
@Test | ||
public void shouldIgnoreNonNamespaceStatements() throws RepositoryException { | ||
Statement mockStatement = mock(Statement.class); | ||
when(mockStatement.getSubject()).thenReturn(ResourceFactory.createResource("uri")); | ||
when(mockStatement.getPredicate()).thenReturn(ResourceFactory.createProperty("some-random-predicate")); | ||
when(mockStatement.getObject()).thenReturn(ResourceFactory.createPlainLiteral("abc")); | ||
|
||
testObj.addedStatement(mockStatement); | ||
|
||
verify(mockNamespaceRegistry, never()).registerNamespace("abc", "uri"); | ||
} | ||
|
||
|
||
@Test | ||
public void shouldRemoveNamespaceStatement() throws RepositoryException { | ||
Statement mockStatement = mock(Statement.class); | ||
when(mockStatement.getSubject()).thenReturn(ResourceFactory.createResource("uri")); | ||
when(mockStatement.getPredicate()).thenReturn(ResourceFactory.createProperty(JcrRdfTools.HAS_NAMESPACE_PREDICATE)); | ||
when(mockStatement.getObject()).thenReturn(ResourceFactory.createPlainLiteral("123")); | ||
|
||
when(mockNamespaceRegistry.getPrefix("uri")).thenReturn("123"); | ||
testObj.removedStatement(mockStatement); | ||
verify(mockNamespaceRegistry).unregisterNamespace("123"); | ||
} | ||
|
||
|
||
@Test | ||
public void shouldIgnoreNonMatchingNamespacesOnRemoveNamespaceStatement() throws RepositoryException { | ||
Statement mockStatement = mock(Statement.class); | ||
when(mockStatement.getSubject()).thenReturn(ResourceFactory.createResource("uri")); | ||
when(mockStatement.getPredicate()).thenReturn(ResourceFactory.createProperty(JcrRdfTools.HAS_NAMESPACE_PREDICATE)); | ||
when(mockStatement.getObject()).thenReturn(ResourceFactory.createPlainLiteral("456")); | ||
|
||
when(mockNamespaceRegistry.getPrefix("uri")).thenReturn("123"); | ||
testObj.removedStatement(mockStatement); | ||
verify(mockNamespaceRegistry, never()).unregisterNamespace("456"); | ||
} | ||
|
||
@Test | ||
public void shouldIgnoreNonNamespaceStatementsOnRemove() throws RepositoryException { | ||
Statement mockStatement = mock(Statement.class); | ||
when(mockStatement.getSubject()).thenReturn(ResourceFactory.createResource("uri")); | ||
when(mockStatement.getPredicate()).thenReturn(ResourceFactory.createProperty("some-random-predicate")); | ||
when(mockStatement.getObject()).thenReturn(ResourceFactory.createPlainLiteral("abc")); | ||
|
||
testObj.removedStatement(mockStatement); | ||
|
||
verify(mockNamespaceRegistry, never()).unregisterNamespace("abc"); | ||
|
||
} | ||
} |