Permalink
Browse files

Unstatic-ifying NodePropertiesTools

  • Loading branch information...
ajs6f committed Nov 21, 2013
1 parent 29f4faf commit 0fdfc80991ab4d821c87033b74f2b544a4ff3cc3
@@ -44,6 +44,9 @@
public class JcrPropertyStatementListener extends StatementListener {
private final JcrRdfTools jcrRdfTools;
private NodePropertiesTools propertiesTools = new NodePropertiesTools();
private Model problems;
private static final Logger LOGGER =
@@ -145,11 +148,10 @@ public void addedStatement(final Statement s) {
subject, subjectNode, s.getPredicate())) {
final Value v =
jcrRdfTools.createValue(subjectNode, s.getObject(),
NodePropertiesTools.getPropertyType(subjectNode,
propertyName));
NodePropertiesTools.appendOrReplaceNodeProperty(subjects, subjectNode,
propertyName,
v);
propertiesTools.getPropertyType(subjectNode,
propertyName));
propertiesTools.appendOrReplaceNodeProperty(subjects,
subjectNode, propertyName, v);
}
} catch (final RepositoryException e) {
throw propagate(e);
@@ -219,11 +221,10 @@ public void removedStatement(final Statement s) {
s.getPredicate())) {
final Value v =
jcrRdfTools.createValue(subjectNode, s.getObject(),
NodePropertiesTools.getPropertyType(subjectNode,
propertyName));
NodePropertiesTools.removeNodeProperty(subjects, subjectNode,
propertyName,
v);
propertiesTools.getPropertyType(subjectNode,
propertyName));
propertiesTools.removeNodeProperty(subjects, subjectNode,
propertyName, v);
}
} catch (final RepositoryException e) {
@@ -34,16 +34,17 @@
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.slf4j.Logger;
import static javax.jcr.PropertyType.UNDEFINED;
import static javax.jcr.PropertyType.URI;
/**
* Tools for replacing, appending and deleting JCR node properties
* @author Chris Beer
* @date May 10, 2013
*/
public abstract class NodePropertiesTools {
public class NodePropertiesTools {
private static final Logger logger = getLogger(NodePropertiesTools.class);
private static final Logger LOGGER = getLogger(NodePropertiesTools.class);
public static final String REFERENCE_PROPERTY_SUFFIX = "_ref";
/**
@@ -57,7 +58,7 @@
* @param newValue the JCR value to insert
* @throws RepositoryException
*/
public static void appendOrReplaceNodeProperty(final GraphSubjects subjects,
public void appendOrReplaceNodeProperty(final GraphSubjects subjects,
final Node node,
final String propertyName,
final Value newValue)
@@ -69,7 +70,7 @@ public static void appendOrReplaceNodeProperty(final GraphSubjects subjects,
final Property property = node.getProperty(propertyName);
if (property.isMultiple()) {
logger.debug("Appending value {} to {} property {}", newValue,
LOGGER.debug("Appending value {} to {} property {}", newValue,
PropertyType.nameFromValue(property.getType()),
propertyName);
@@ -87,7 +88,7 @@ public static void appendOrReplaceNodeProperty(final GraphSubjects subjects,
addReferencePlaceholders(subjects, node, property, newValue);
} else {
// or we'll just overwrite it
logger.debug("Overwriting {} property {} with new value {}",
LOGGER.debug("Overwriting {} property {} with new value {}",
PropertyType.nameFromValue(property.getType()),
propertyName, newValue);
property.setValue(newValue);
@@ -102,14 +103,14 @@ public static void appendOrReplaceNodeProperty(final GraphSubjects subjects,
// simply represents a new kind of property on this node
}
if (isMultiple) {
logger.debug("Creating new multivalued {} property {} with " +
LOGGER.debug("Creating new multivalued {} property {} with " +
"initial value [{}]",
PropertyType.nameFromValue(newValue.getType()),
propertyName, newValue);
final Property property = node.setProperty(propertyName, new Value[]{newValue}, newValue.getType());
addReferencePlaceholders(subjects, node, property, newValue);
} else {
logger.debug("Creating new single-valued {} property {} with " +
LOGGER.debug("Creating new single-valued {} property {} with " +
"initial value {}",
PropertyType.nameFromValue(newValue.getType()),
propertyName, newValue);
@@ -120,7 +121,7 @@ public static void appendOrReplaceNodeProperty(final GraphSubjects subjects,
}
private static void addReferencePlaceholders(final GraphSubjects subjects, final Node node, final Property property, final Value newValue) throws RepositoryException {
private void addReferencePlaceholders(final GraphSubjects subjects, final Node node, final Property property, final Value newValue) throws RepositoryException {
if (property.getType() == URI) {
final Resource resource = ResourceFactory.createResource(newValue.getString());
@@ -138,7 +139,7 @@ private static void addReferencePlaceholders(final GraphSubjects subjects, final
}
}
private static void removeReferencePlaceholders(final GraphSubjects subjects, final Node node, final Property property, final Value newValue) throws RepositoryException {
private void removeReferencePlaceholders(final GraphSubjects subjects, final Node node, final Property property, final Value newValue) throws RepositoryException {
if (property.getType() == URI) {
final Resource resource = ResourceFactory.createResource(newValue.getString());
@@ -166,7 +167,7 @@ private static void removeReferencePlaceholders(final GraphSubjects subjects, fi
* @param valueToRemove the JCR value to remove
* @throws RepositoryException
*/
public static void removeNodeProperty(final GraphSubjects subjects,
public void removeNodeProperty(final GraphSubjects subjects,
final Node node,
final String propertyName,
final Value valueToRemove)
@@ -193,10 +194,10 @@ public static void removeNodeProperty(final GraphSubjects subjects,
// we only need to update the property if we did anything.
if (remove) {
if (newValues.size() == 0) {
logger.debug("Removing property {}", propertyName);
LOGGER.debug("Removing property {}", propertyName);
property.setValue((Value[])null);
} else {
logger.debug("Removing value {} from property {}",
LOGGER.debug("Removing value {} from property {}",
valueToRemove, propertyName);
property
.setValue(newValues
@@ -207,7 +208,7 @@ public static void removeNodeProperty(final GraphSubjects subjects,
} else {
if (property.getValue().equals(valueToRemove)) {
logger.debug("Removing value {} property {}", propertyName);
LOGGER.debug("Removing value {} property {}", propertyName);
property.setValue((Value)null);
if (property.getType() == URI && node.hasProperty(getReferencePropertyName(propertyName))) {
@@ -223,11 +224,11 @@ public static void removeNodeProperty(final GraphSubjects subjects,
* @param propertyName
* @return
*/
public static String getReferencePropertyName(final String propertyName) {
public String getReferencePropertyName(final String propertyName) {
return propertyName + REFERENCE_PROPERTY_SUFFIX;
}
private static String getReferencePropertyName(final Property property) throws RepositoryException {
private String getReferencePropertyName(final Property property) throws RepositoryException {
return getReferencePropertyName(property.getName());
}
/**
@@ -239,14 +240,15 @@ private static String getReferencePropertyName(final Property property) throws R
* @return a PropertyType value
* @throws RepositoryException
*/
public static int getPropertyType(final Node node,
final String propertyName)
public int getPropertyType(final Node node, final String propertyName)
throws RepositoryException {
LOGGER.debug("Getting type of property: {} from node: {}",
propertyName, node);
final PropertyDefinition def =
getDefinitionForPropertyName(node, propertyName);
if (def == null) {
return PropertyType.UNDEFINED;
return UNDEFINED;
}
return def.getRequiredType();
@@ -263,7 +265,7 @@ public static int getPropertyType(final Node node,
* @return true if the property is (or could be) multivalued
* @throws RepositoryException
*/
public static boolean isMultivaluedProperty(final Node node,
public boolean isMultivaluedProperty(final Node node,
final String propertyName)
throws RepositoryException {
final PropertyDefinition def =
@@ -16,8 +16,6 @@
package org.fcrepo.kernel.utils.iterators;
import static org.fcrepo.kernel.utils.NodePropertiesTools.appendOrReplaceNodeProperty;
import static org.fcrepo.kernel.utils.NodePropertiesTools.getPropertyType;
import static org.slf4j.LoggerFactory.getLogger;
import javax.jcr.Node;
@@ -27,6 +25,7 @@
import org.fcrepo.kernel.exception.MalformedRdfException;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.utils.NodePropertiesTools;
import org.slf4j.Logger;
import com.hp.hpl.jena.rdf.model.Resource;
@@ -44,6 +43,8 @@
private static final Logger LOGGER = getLogger(RdfAdder.class);
private final NodePropertiesTools propertiesTools = new NodePropertiesTools();
/**
* Ordinary constructor.
*
@@ -90,7 +91,8 @@ protected void operateOnProperty(final Statement t, final Node n) throws Reposit
final String propertyName =
getPropertyNameFromPredicate(n, t.getPredicate());
final Value v =
createValue(n, t.getObject(), getPropertyType(n, propertyName));
appendOrReplaceNodeProperty(idTranslator(), n, propertyName, v);
createValue(n, t.getObject(), propertiesTools.getPropertyType(n,
propertyName));
propertiesTools.appendOrReplaceNodeProperty(idTranslator(), n, propertyName, v);
}
}
@@ -16,8 +16,6 @@
package org.fcrepo.kernel.utils.iterators;
import static org.fcrepo.kernel.utils.NodePropertiesTools.getPropertyType;
import static org.fcrepo.kernel.utils.NodePropertiesTools.removeNodeProperty;
import static org.slf4j.LoggerFactory.getLogger;
import javax.jcr.Node;
@@ -27,6 +25,7 @@
import javax.jcr.nodetype.NoSuchNodeTypeException;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.utils.NodePropertiesTools;
import org.slf4j.Logger;
import com.hp.hpl.jena.rdf.model.Resource;
@@ -43,6 +42,7 @@
public class RdfRemover extends PersistingRdfStreamConsumer {
private static final Logger LOGGER = getLogger(RdfRemover.class);
private final NodePropertiesTools propertiesTools = new NodePropertiesTools();
/**
* Ordinary constructor.
@@ -81,9 +81,10 @@ protected void operateOnProperty(final Statement t, final Node n)
getPropertyNameFromPredicate(n, t.getPredicate());
if (n.hasProperty(propertyName)) {
final Value v =
jcrRdfTools().createValue(n, t.getObject(), getPropertyType(n,
propertyName));
removeNodeProperty(idTranslator(), n, propertyName, v);
jcrRdfTools().createValue(n, t.getObject(),
propertiesTools.getPropertyType(n, propertyName));
propertiesTools.removeNodeProperty(idTranslator(), n, propertyName,
v);
}
}
}
@@ -23,7 +23,7 @@
import static javax.jcr.PropertyType.URI;
import static org.fcrepo.kernel.RdfLexicon.COULD_NOT_STORE_PROPERTY;
import static org.fcrepo.kernel.RdfLexicon.RESTAPI_NAMESPACE;
import static org.fcrepo.kernel.utils.NodePropertiesTools.getPropertyType;
import static org.fcrepo.kernel.utils.TestHelpers.setField;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.never;
@@ -32,6 +32,7 @@
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Collections;
import java.util.Map;
@@ -51,6 +52,7 @@
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.slf4j.Logger;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
@@ -60,9 +62,12 @@
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"org.slf4j.*", "javax.xml.parsers.*", "org.apache.xerces.*"})
@PrepareForTest({JcrRdfTools.class, NodePropertiesTools.class})
@PrepareForTest({JcrRdfTools.class})
public class JcrPropertyStatementListenerTest {
private static final Logger LOGGER =
getLogger(JcrPropertyStatementListenerTest.class);
private JcrPropertyStatementListener testObj;
@Mock
@@ -101,10 +106,13 @@
@Mock
private Model mockModel;
Map<String, String> mockNsMapping = Collections.emptyMap();
private Map<String, String> mockNsMapping = Collections.emptyMap();
@Mock
private NodePropertiesTools mockPropertiesTools;
@Before
public void setUp() throws RepositoryException {
public void setUp() throws RepositoryException, NoSuchFieldException {
initMocks(this);
mockStatic(JcrRdfTools.class);
@@ -113,7 +121,7 @@ public void setUp() throws RepositoryException {
testObj = JcrPropertyStatementListener.getListener(mockSubjects, mockSession, mockProblems);
when(mockStatement.getSubject()).thenReturn(mockSubject);
when(mockStatement.getPredicate()).thenReturn(mockPredicate);
setField(testObj, "propertiesTools", mockPropertiesTools);
when(mockStatement.getModel()).thenReturn(mockModel);
when(mockModel.getNsPrefixMap()).thenReturn(mockNsMapping);
}
@@ -146,14 +154,15 @@ public void testAddedStatement() throws RepositoryException {
when(mockSubjects.getNodeFromGraphSubject(mockSubject))
.thenReturn(mockSubjectNode);
final String mockPropertyName = "mock:property";
when(mockJcrRdfTools.getPropertyNameFromPredicate(mockSubjectNode, mockPredicate, mockNsMapping))
.thenReturn(mockPropertyName);
mockStatic(NodePropertiesTools.class);
when(getPropertyType(mockSubjectNode, mockPropertyName)).thenReturn(
STRING);
when(
mockJcrRdfTools.getPropertyNameFromPredicate(mockSubjectNode,
mockPredicate, mockNsMapping)).thenReturn(
mockPropertyName);
when(mockPropertiesTools.getPropertyType(mockSubjectNode, mockPropertyName))
.thenReturn(STRING);
testObj.addedStatement(mockStatement);
verify(mockProblems, times(0)).add(any(Resource.class), any(Property.class), any(String.class));
LOGGER.debug("Finished testAddedStatement()");
}
@Test(expected = RuntimeException.class)
@@ -181,8 +190,7 @@ public void testRemovedStatement() throws RepositoryException {
when(mockJcrRdfTools.getPropertyNameFromPredicate(mockSubjectNode, mockPredicate))
.thenReturn(mockPropertyName);
when(mockSubjectNode.hasProperty(mockPropertyName)).thenReturn(true);
mockStatic(NodePropertiesTools.class);
when(getPropertyType(mockSubjectNode, mockPropertyName)).thenReturn(
when(mockPropertiesTools.getPropertyType(mockSubjectNode, mockPropertyName)).thenReturn(
STRING);
testObj.removedStatement(mockStatement);
verify(mockProblems, times(0)).add(any(Resource.class), any(Property.class), any(String.class));
@@ -215,8 +223,7 @@ public void testRemovedProhibitedStatement() throws RepositoryException {
.thenReturn(mockPropertyName);
when(mockJcrRdfTools.isInternalProperty(mockSubjectNode, mockPredicate)).thenReturn(true);
when(mockSubjectNode.hasProperty(mockPropertyName)).thenReturn(true);
mockStatic(NodePropertiesTools.class);
when(getPropertyType(mockSubjectNode, mockPropertyName)).thenReturn(
when(mockPropertiesTools.getPropertyType(mockSubjectNode, mockPropertyName)).thenReturn(
STRING);
testObj.removedStatement(mockStatement);
verify(mockProblems).add(any(Resource.class), eq(COULD_NOT_STORE_PROPERTY), eq("x"));
@@ -287,8 +294,8 @@ public void testAddRdfTypeForNonMixin() throws RepositoryException {
when(mockNodeTypeManager.hasNodeType("fedora:object")).thenReturn(false);
mockStatic(NodePropertiesTools.class);
when(getPropertyType(mockSubjectNode, "rdf:type")).thenReturn(URI);
when(mockPropertiesTools.getPropertyType(mockSubjectNode, "rdf:type"))
.thenReturn(URI);
when(mockSession.getNamespacePrefix(RESTAPI_NAMESPACE))
.thenReturn("fedora");
Oops, something went wrong.

0 comments on commit 0fdfc80

Please sign in to comment.