Skip to content

Commit

Permalink
More unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Jul 6, 2013
1 parent 3f6a258 commit 95ecf12
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 60 deletions.
110 changes: 56 additions & 54 deletions fcrepo-kernel/src/main/java/org/fcrepo/utils/NodePropertiesTools.java
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo.utils;

import static org.fcrepo.utils.FedoraTypesUtils.getDefinitionForPropertyName;
Expand All @@ -32,27 +33,33 @@

/**
* Tools for replacing, appending and deleting JCR node properties
*
* @author Chris Beer
* @date May 10, 2013
*/
public abstract class NodePropertiesTools {
public final class NodePropertiesTools {

private static final Logger logger = getLogger(NodePropertiesTools.class);

private NodePropertiesTools() {
throw new AssertionError(
"NodePropertiesTools is a helper class which should not be instantiated!");

}

/**
* Given a JCR node, property and value, either:
* - if the property is single-valued, replace the existing property with
* the new value
* - if the property is multivalued, append the new value to the property
* Given a JCR node, property and value, either: - if the property is
* single-valued, replace the existing property with the new value - if the
* property is multivalued, append the new value to the property
*
* @param node the JCR node
* @param propertyName a name of a JCR property (either pre-existing or
* otherwise)
* otherwise)
* @param newValue the JCR value to insert
* @throws RepositoryException
*/
public static void appendOrReplaceNodeProperty(final Node node,
final String propertyName,
final Value newValue)
final String propertyName, final Value newValue)
throws RepositoryException {

// if it already exists, we can take some shortcuts
Expand All @@ -62,38 +69,38 @@ public static void appendOrReplaceNodeProperty(final Node node,

if (property.isMultiple()) {
logger.debug("Appending value {} to {} property {}", newValue,
PropertyType.nameFromValue(property.getType()),
propertyName);
PropertyType.nameFromValue(property.getType()),
propertyName);

// if the property is multi-valued, go ahead and append to it.
ArrayList<Value> newValues = new ArrayList<Value>();
Collections.addAll(newValues,
node.getProperty(propertyName).getValues());
final ArrayList<Value> newValues = new ArrayList<Value>();
Collections.addAll(newValues, node.getProperty(propertyName)
.getValues());

if (!newValues.contains(newValue)) {
newValues.add(newValue);
property.setValue(newValues
.toArray(new Value[newValues.size()]));
property.setValue(newValues.toArray(new Value[newValues
.size()]));
}
} else {
// or we'll just overwrite it
logger.debug("Overwriting {} property {} with new value {}",
PropertyType.nameFromValue(property.getType()),
propertyName, newValue);
PropertyType.nameFromValue(property.getType()),
propertyName, newValue);
property.setValue(newValue);
}
} else {
if (isMultivaluedProperty(node, propertyName)) {
logger.debug("Creating new multivalued {} property {} with " +
"initial value [{}]",
PropertyType.nameFromValue(newValue.getType()),
propertyName, newValue);
node.setProperty(propertyName, new Value[]{newValue});
logger.debug("Creating new multivalued {} property {} with "
+ "initial value [{}]", PropertyType
.nameFromValue(newValue.getType()), propertyName,
newValue);
node.setProperty(propertyName, new Value[] {newValue});
} else {
logger.debug("Creating new single-valued {} property {} with " +
"initial value {}",
PropertyType.nameFromValue(newValue.getType()),
propertyName, newValue);
logger.debug("Creating new single-valued {} property {} with "
+ "initial value {}", PropertyType
.nameFromValue(newValue.getType()), propertyName,
newValue);
node.setProperty(propertyName, newValue);
}
}
Expand All @@ -102,18 +109,16 @@ public static void appendOrReplaceNodeProperty(final Node node,

/**
* Given a JCR node, property and value, remove the value (if it exists)
* from the property, and remove the
* property if no values remove
*
* from the property, and remove the property if no values remove
*
* @param node the JCR node
* @param propertyName a name of a JCR property (either pre-existing or
* otherwise)
* otherwise)
* @param valueToRemove the JCR value to remove
* @throws RepositoryException
*/
public static void removeNodeProperty(final Node node,
final String propertyName,
final Value valueToRemove)
final String propertyName, final Value valueToRemove)
throws RepositoryException {
// if the property doesn't exist, we don't need to worry about it.
if (node.hasProperty(propertyName)) {
Expand All @@ -122,11 +127,11 @@ public static void removeNodeProperty(final Node node,

if (FedoraTypesUtils.isMultipleValuedProperty.apply(property)) {

ArrayList<Value> newValues = new ArrayList<Value>();
final ArrayList<Value> newValues = new ArrayList<Value>();

boolean remove = false;

for ( Value v : node.getProperty(propertyName).getValues() ) {
for (final Value v : node.getProperty(propertyName).getValues()) {
if (v.equals(valueToRemove)) {
remove = true;
} else {
Expand All @@ -138,20 +143,19 @@ public static void removeNodeProperty(final Node node,
if (remove) {
if (newValues.size() == 0) {
logger.debug("Removing property {}", propertyName);
property.setValue((Value[])null);
property.setValue((Value[]) null);
} else {
logger.debug("Removing value {} from property {}",
valueToRemove, propertyName);
property
.setValue(newValues
.toArray(new Value[newValues.size()]));
valueToRemove, propertyName);
property.setValue(newValues.toArray(new Value[newValues
.size()]));
}
}

} else {
if (property.getValue().equals(valueToRemove)) {
logger.debug("Removing value {} property {}", propertyName);
property.setValue((Value)null);
property.setValue((Value) null);
}
}
}
Expand All @@ -160,17 +164,17 @@ public static void removeNodeProperty(final Node node,
/**
* Get the JCR property type ID for a given property name. If unsure, mark
* it as UNDEFINED.
*
*
* @param node the JCR node to add the property on
* @param propertyName the property name
* @return a PropertyType value
* @throws RepositoryException
*/
public static int getPropertyType(final Node node,
final String propertyName)
throws RepositoryException {
public static int
getPropertyType(final Node node, final String propertyName)
throws RepositoryException {
final PropertyDefinition def =
getDefinitionForPropertyName(node, propertyName);
getDefinitionForPropertyName(node, propertyName);

if (def == null) {
return PropertyType.UNDEFINED;
Expand All @@ -180,21 +184,19 @@ public static int getPropertyType(final Node node,
}

/**
* Determine if a given JCR property name is single- or multi- valued.
* If unsure, choose the least restrictive
* option (multivalued)
*
* Determine if a given JCR property name is single- or multi- valued. If
* unsure, choose the least restrictive option (multivalued)
*
* @param node the JCR node to check
* @param propertyName the property name
* (which may or may not already exist)
* @param propertyName the property name (which may or may not already
* exist)
* @return true if the property is (or could be) multivalued
* @throws RepositoryException
*/
public static boolean isMultivaluedProperty(final Node node,
final String propertyName)
throws RepositoryException {
final String propertyName) throws RepositoryException {
final PropertyDefinition def =
getDefinitionForPropertyName(node, propertyName);
getDefinitionForPropertyName(node, propertyName);

if (def == null) {
return true;
Expand Down
Expand Up @@ -17,8 +17,10 @@
package org.fcrepo.utils;

import static java.util.Arrays.asList;
import static javax.jcr.PropertyType.UNDEFINED;
import static org.fcrepo.utils.FedoraTypesUtils.getDefinitionForPropertyName;
import static org.fcrepo.utils.NodePropertiesTools.appendOrReplaceNodeProperty;
import static org.fcrepo.utils.NodePropertiesTools.getPropertyType;
import static org.fcrepo.utils.NodePropertiesTools.removeNodeProperty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -53,6 +55,8 @@
@PrepareForTest({FedoraTypesUtils.class})
public class NodePropertiesToolsTest {

private static final String NOT_A_PROPERTY_NAME = "fbghdfghjldsfhglfs";

@Mock
private PropertyDefinition mockDefinition;

Expand Down Expand Up @@ -96,7 +100,7 @@ public void addNewMultiValuedProperty() throws RepositoryException {

@Test
public void replaceExistingSingleValuedPropertyWithValue()
throws RepositoryException {
throws RepositoryException {
when(mockProperty.isMultiple()).thenReturn(false);
when(mockNode.hasProperty("mockPropertyName")).thenReturn(true);
appendOrReplaceNodeProperty(mockNode, "mockPropertyName", mockValue);
Expand All @@ -105,7 +109,7 @@ public void replaceExistingSingleValuedPropertyWithValue()

@Test
public void appendValueToExistingMultivaluedProperty()
throws RepositoryException {
throws RepositoryException {
when(mockProperty.isMultiple()).thenReturn(true);
when(mockNode.hasProperty("mockPropertyName")).thenReturn(true);
final Value[] values = new Value[] {previousValue};
Expand All @@ -125,7 +129,7 @@ public void appendValueToExistingMultivaluedProperty()

@Test
public void addMultiValuedPropertyWithSameValueAsExistingProperty()
throws RepositoryException {
throws RepositoryException {

when(mockProperty.isMultiple()).thenReturn(true);
when(mockNode.hasProperty("mockPropertyName")).thenReturn(true);
Expand All @@ -138,7 +142,7 @@ public void addMultiValuedPropertyWithSameValueAsExistingProperty()

@Test
public void shouldBeANoopWhenRemovingPropertyThatDoesntExist()
throws RepositoryException {
throws RepositoryException {
when(mockNode.hasProperty("mockPropertyName")).thenReturn(false);
removeNodeProperty(mockNode, "mockPropertyName", mockValue);
verify(mockNode).hasProperty("mockPropertyName");
Expand Down Expand Up @@ -170,7 +174,7 @@ public void shouldRemoveAMultiValuedProperty() throws RepositoryException {

@Test
public void shouldRemoveAValueFromMultiValuedProperty()
throws RepositoryException {
throws RepositoryException {
when(mockProperty.isMultiple()).thenReturn(true);
when(mockNode.hasProperty("mockPropertyName")).thenReturn(true);
final Value[] values = new Value[] {previousValue, mockValue};
Expand All @@ -190,7 +194,7 @@ public void shouldRemoveAValueFromMultiValuedProperty()

@Test
public void shouldRemoveAllMatchingValuesFromAMultivaluedProperty()
throws RepositoryException {
throws RepositoryException {

when(mockProperty.isMultiple()).thenReturn(true);
when(mockNode.hasProperty("mockPropertyName")).thenReturn(true);
Expand All @@ -199,4 +203,12 @@ public void shouldRemoveAllMatchingValuesFromAMultivaluedProperty()
removeNodeProperty(mockNode, "mockPropertyName", mockValue);
verify(mockProperty).setValue((Value[]) null);
}

@Test
public void testGetPropertyTypeForBadPropertyName()
throws RepositoryException {
assertEquals("Should have been an undefined property type!", UNDEFINED,
getPropertyType(mockNode, NOT_A_PROPERTY_NAME));
}

}

0 comments on commit 95ecf12

Please sign in to comment.