Skip to content

Commit

Permalink
Cleaner EventType enum and better test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 22, 2013
1 parent 4efdca8 commit e81c314
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 56 deletions.
Expand Up @@ -40,6 +40,9 @@
*
* @author escowles
* @date Oct 3, 2013
*
* @author ajs6f
* @date 2013
*/
public class DefaultFilter implements EventFilter {

Expand Down
99 changes: 50 additions & 49 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/utils/EventType.java
Expand Up @@ -16,70 +16,71 @@

package org.fcrepo.kernel.utils;

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) {
if (translation.containsKey(i)) {
return translation.get(i);
} else {
throw new IllegalArgumentException("Invalid JCR event type: " + i);
}
}
}
Expand Up @@ -21,8 +21,9 @@
import static javax.jcr.observation.Event.NODE_REMOVED;
import static javax.jcr.observation.Event.PERSIST;
import static javax.jcr.observation.Event.PROPERTY_ADDED;
import static javax.jcr.observation.Event.PROPERTY_CHANGED;
import static javax.jcr.observation.Event.PROPERTY_REMOVED;
import static org.fcrepo.kernel.utils.EventType.getEventName;
import static org.fcrepo.kernel.utils.EventType.valueOf;
import static org.junit.Assert.assertEquals;

import org.junit.Test;
Expand All @@ -31,11 +32,24 @@ public class EventTypeTest {

@Test
public void testGetEventName() throws Exception {
assertEquals("node added", getEventName(NODE_ADDED));
assertEquals("node removed", getEventName(NODE_REMOVED));
assertEquals("property added", getEventName(PROPERTY_ADDED));
assertEquals("property removed", getEventName(PROPERTY_REMOVED));
assertEquals("node moved", getEventName(NODE_MOVED));
assertEquals("persist", getEventName(PERSIST));
assertEquals("node added", valueOf(NODE_ADDED).getName());
assertEquals("node removed", valueOf(NODE_REMOVED).getName());
assertEquals("property added", valueOf(PROPERTY_ADDED).getName());
assertEquals("property removed", valueOf(PROPERTY_REMOVED).getName());
assertEquals("property changed", valueOf(PROPERTY_CHANGED).getName());
assertEquals("node moved", valueOf(NODE_MOVED).getName());
assertEquals("persist", valueOf(PERSIST).getName());
}

@Test(expected=IllegalArgumentException.class)
public void testBadEvent() {
valueOf(9999999);
}

@Test()
public void testValueOf() {
assertEquals(EventType.PERSIST, EventType.valueOf("PERSIST"));
}


}

0 comments on commit e81c314

Please sign in to comment.