Skip to content

Commit

Permalink
Unstatic-ifying NodePropertiesTools
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Nov 21, 2013
1 parent 29f4faf commit 0fdfc80
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 76 deletions.
Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Expand Up @@ -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";

/**
Expand All @@ -57,7 +58,7 @@ public abstract class NodePropertiesTools {
* @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)
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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());

Expand All @@ -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());

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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))) {
Expand All @@ -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());
}
/**
Expand All @@ -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();
Expand All @@ -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 =
Expand Down
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -44,6 +43,8 @@ public class RdfAdder extends PersistingRdfStreamConsumer {

private static final Logger LOGGER = getLogger(RdfAdder.class);

private final NodePropertiesTools propertiesTools = new NodePropertiesTools();

/**
* Ordinary constructor.
*
Expand Down Expand Up @@ -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);
}
}
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -101,10 +106,13 @@ public class JcrPropertyStatementListenerTest {
@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);

Expand All @@ -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);
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 0fdfc80

Please sign in to comment.