Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Minor improvements to unit testing
  • Loading branch information
ajs6f authored and Andrew Woods committed Nov 1, 2013
1 parent 55b7a00 commit 5b6ef11
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 195 deletions.
Expand Up @@ -16,13 +16,12 @@

package org.fcrepo.audit;

import static org.fcrepo.kernel.utils.EventType.getEventName;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.jcr.RepositoryException;
import javax.jcr.observation.Event;

import org.fcrepo.kernel.utils.EventType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -31,7 +30,7 @@

/**
* A proof-of-concept Auditor implementation that uses Logback.
*
*
* @author Edwin Shin
*/
public class LogbackAuditor implements Auditor {
Expand All @@ -56,7 +55,7 @@ public void register() {
@Override
@Subscribe
public void recordEvent(final Event e) throws RepositoryException {
logger.info(e.getUserID() + " " + getEventName(e.getType()) + " " +
logger.info(e.getUserID() + " " + EventType.valueOf(e.getType()).getName() + " " +
e.getPath());
}
}
61 changes: 28 additions & 33 deletions fcrepo-jms/src/main/java/org/fcrepo/jms/legacy/LegacyMethod.java
Expand Up @@ -96,7 +96,7 @@ public class LegacyMethod {

/**
* TODO
*
*
* @param jcrEvent
* @param resource
* @throws RepositoryException
Expand Down Expand Up @@ -133,7 +133,7 @@ public LegacyMethod(final Event jcrEvent, final Node resource)

/**
* TODO
*
*
* @param atomEntry
*/
public LegacyMethod(final Entry atomEntry) {
Expand All @@ -142,7 +142,7 @@ public LegacyMethod(final Entry atomEntry) {

/**
* TODO
*
*
* @param atomEntry
*/
public LegacyMethod(final String atomEntry) {
Expand All @@ -151,7 +151,7 @@ public LegacyMethod(final String atomEntry) {

/**
* TODO
*
*
* @return
*/
public Entry getEntry() {
Expand All @@ -160,7 +160,7 @@ public Entry getEntry() {

/**
* TODO
*
*
* @param content
*/
public void setContent(final String content) {
Expand All @@ -169,11 +169,11 @@ public void setContent(final String content) {

/**
* TODO
*
*
* @param val
*/
public void setUserId(String val) {
if (val == null) {
public void setUserId(final String val) {
if (val == null || "<anonymous>".equals(val)) {
delegate.addAuthor("unknown", null, getBaseURL());
} else {
delegate.addAuthor(val, null, getBaseURL());
Expand All @@ -182,7 +182,7 @@ public void setUserId(String val) {

/**
* TODO
*
*
* @return
*/
public String getUserID() {
Expand All @@ -191,7 +191,7 @@ public String getUserID() {

/**
* TODO
*
*
* @param date
*/
public void setModified(final Date date) {
Expand All @@ -200,7 +200,7 @@ public void setModified(final Date date) {

/**
* TODO
*
*
* @return
*/
public Date getModified() {
Expand All @@ -209,7 +209,7 @@ public Date getModified() {

/**
* TODO
*
*
* @param val
*/
public void setMethodName(final String val) {
Expand All @@ -218,18 +218,18 @@ public void setMethodName(final String val) {

/**
* TODO
*
*
* @return
*/
public String getMethodName() {
return delegate.getTitle();
}

private void setLabelledCategory(String label, String val) {
private void setLabelledCategory(final String label, final String val) {
final List<Category> vals = delegate.getCategories(FEDORA_ID_SCHEME);
Category found = null;
if (vals != null && !vals.isEmpty()) {
for (Category c : vals) {
for (final Category c : vals) {
if (label.equals(c.getLabel())) {
found = c.setTerm(val);
}
Expand All @@ -240,7 +240,7 @@ private void setLabelledCategory(String label, String val) {
}
}

private String getLabelledCategory(String label) {
private String getLabelledCategory(final String label) {
final List<Category> categories =
delegate.getCategories(FEDORA_ID_SCHEME);
for (final Category c : categories) {
Expand All @@ -253,7 +253,7 @@ private String getLabelledCategory(String label) {

/**
* TODO
*
*
* @param val
*/
public void setPid(final String val) {
Expand All @@ -263,7 +263,7 @@ public void setPid(final String val) {

/**
* TODO
*
*
* @return
*/
public String getPid() {
Expand All @@ -272,7 +272,7 @@ public String getPid() {

/**
* TODO
*
*
* @param val
*/
public void setDsId(final String val) {
Expand All @@ -281,7 +281,7 @@ public void setDsId(final String val) {

/**
* TODO
*
*
* @return
*/
public String getDsId() {
Expand All @@ -292,15 +292,15 @@ public String getDsId() {
* Adds node path as a category
* @param path
*/
private void setPath(String path) {
private void setPath(final String path) {
setLabelledCategory("path", path);
}

protected String getBaseURL() {
StringBuilder url = new StringBuilder();
String host = System.getProperty("fcrepo.host", "localhost");
String port = System.getProperty("fcrepo.port", "8080");
String ctxt = System.getProperty("fcrepo.ctxt", "rest");
final StringBuilder url = new StringBuilder();
final String host = System.getProperty("fcrepo.host", "localhost");
final String port = System.getProperty("fcrepo.port", "8080");
final String ctxt = System.getProperty("fcrepo.ctxt", "rest");

url.append(port.equalsIgnoreCase("443") ? "https://" : "http://");
url.append(host);
Expand All @@ -313,7 +313,7 @@ protected String getBaseURL() {

/**
* TODO
*
*
* @param writer
* @throws IOException
*/
Expand Down Expand Up @@ -381,19 +381,14 @@ protected static String mapMethodName(final int eventType,
return isObjectNode ? INGEST_METHOD : ADD_DS_METHOD;
case NODE_REMOVED:
return isObjectNode ? PURGE_OBJ_METHOD : PURGE_DS_METHOD;
case PROPERTY_ADDED:
return isObjectNode ? MODIFY_OBJ_METHOD : MODIFY_DS_METHOD;
case PROPERTY_CHANGED:
return isObjectNode ? MODIFY_OBJ_METHOD : MODIFY_DS_METHOD;
case PROPERTY_REMOVED:
default :
return isObjectNode ? MODIFY_OBJ_METHOD : MODIFY_DS_METHOD;
}
return null;
}

/**
* TODO
*
*
* @param jmsMessage
* @return
*/
Expand Down
98 changes: 48 additions & 50 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/utils/EventType.java
Expand Up @@ -16,70 +16,68 @@

package org.fcrepo.kernel.utils;

import static com.google.common.base.Functions.forMap;
import static com.google.common.collect.ImmutableMap.builder;
import java.util.Map;

import com.google.common.collect.ImmutableMap;

/**
* A convenient abstraction over JCR's integer-typed events.
*
* @author ajs6f
* @date Feb 7, 2013
*/
/**
* @author ajs6f
* @date Oct 22, 2013
*/
public enum EventType {
NODE_ADDED, NODE_REMOVED, PROPERTY_ADDED, PROPERTY_REMOVED,
PROPERTY_CHANGED, NODE_MOVED, PERSIST;
NODE_ADDED(javax.jcr.observation.Event.NODE_ADDED, "node added"),
NODE_REMOVED(javax.jcr.observation.Event.NODE_REMOVED, "node removed"),
PROPERTY_ADDED(javax.jcr.observation.Event.PROPERTY_ADDED, "property added"),
PROPERTY_REMOVED(javax.jcr.observation.Event.PROPERTY_REMOVED, "property removed"),
PROPERTY_CHANGED(javax.jcr.observation.Event.PROPERTY_CHANGED, "property changed"),
NODE_MOVED(javax.jcr.observation.Event.NODE_MOVED, "node moved"),
PERSIST(javax.jcr.observation.Event.PERSIST, "persist");

/**
* Get the Fedora event type for a JCR type
*
* @param i
* @return
private final static Map<Integer, EventType> translation;

private final Integer jcrEventType;

private final String eventName;


/*
* Create a translation map
*/
public static EventType getEventType(final Integer i) {
switch (i) {
case javax.jcr.observation.Event.NODE_ADDED:
return NODE_ADDED;
case javax.jcr.observation.Event.NODE_REMOVED:
return NODE_REMOVED;
case javax.jcr.observation.Event.PROPERTY_ADDED:
return PROPERTY_ADDED;
case javax.jcr.observation.Event.PROPERTY_REMOVED:
return PROPERTY_REMOVED;
case javax.jcr.observation.Event.PROPERTY_CHANGED:
return PROPERTY_CHANGED;
case javax.jcr.observation.Event.NODE_MOVED:
return NODE_MOVED;
case javax.jcr.observation.Event.PERSIST:
return PERSIST;
// no default
default:
throw new IllegalArgumentException("Invalid JCR event type: "
+ i);
static {
final ImmutableMap.Builder<Integer, EventType> b = builder();
for (final EventType eventType : values()) {
b.put(eventType.jcrEventType, eventType);
}
translation = b.build();
}

EventType(final Integer jcrEventType, final String eventName) {
this.jcrEventType = jcrEventType;
this.eventName = eventName;
}

/**
* @param jcrEvent
* @return A human-readable name for the type of this JCR event.
* @return a human-readable name for this event
*/
public static String getEventName(final Integer jcrEvent) {
public String getName() {
return this.eventName;
}

switch (getEventType(jcrEvent)) {
case NODE_ADDED:
return "node added";
case NODE_REMOVED:
return "node removed";
case PROPERTY_ADDED:
return "property added";
case PROPERTY_CHANGED:
return "property changed";
case PROPERTY_REMOVED:
return "property removed";
case NODE_MOVED:
return "node moved";
case PERSIST:
return "persist";
// no default
default:
throw new IllegalArgumentException("Invalid JCR event type: "
+ jcrEvent);
}
/**
* Get the Fedora event type for a JCR type
*
* @param i
* @return
*/
public static EventType valueOf(final Integer i) {
return forMap(translation).apply(i);
}
}

0 comments on commit 5b6ef11

Please sign in to comment.