Skip to content

Commit

Permalink
fixing up tests in fcrepo-jms for better instrumentation; splitting f…
Browse files Browse the repository at this point in the history
…alsafe and surefire into separate jacoco reports
  • Loading branch information
barmintor committed May 2, 2013
1 parent 87c642a commit 35c395f
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 58 deletions.
@@ -0,0 +1,49 @@
package org.fcrepo.messaging.legacy;

import java.io.Reader;
import java.io.StringReader;
import java.util.UUID;

import org.apache.abdera.Abdera;
import org.apache.abdera.model.Entry;
import org.apache.abdera.parser.Parser;

public abstract class EntryFactory {

private static final Abdera ABDERA = new Abdera();

private static final Parser ABDERA_PARSER = ABDERA.getParser();

public static final String FORMAT =
"info:fedora/fedora-system:ATOM-APIM-1.0";

//TODO get this out of the build properties
public static final String SERVER_VERSION = "4.0.0-SNAPSHOT";

private static final String TYPES_NS =
"http://www.fedora.info/definitions/1/0/types/";

public static final String VERSION_PREDICATE =
"info:fedora/fedora-system:def/view#version";

private static final String XSD_NS = "http://www.w3.org/2001/XMLSchema";

public static final String FORMAT_PREDICATE =
"http://www.fedora.info/definitions/1/0/types/formatURI";

static Entry newEntry() {
final Entry entry = ABDERA.newEntry();
entry.declareNS(XSD_NS, "xsd");
entry.declareNS(TYPES_NS, "fedora-types");
entry.setId("urn:uuid:" + UUID.randomUUID().toString());
entry.addCategory(FORMAT_PREDICATE, FORMAT, "format");
entry.addCategory(VERSION_PREDICATE, SERVER_VERSION, "version");
return entry;
}

static Entry parse(Reader input) {
return (Entry) ABDERA_PARSER.parse(input)
.getRoot();
}

}
Expand Up @@ -17,18 +17,15 @@
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.observation.Event;
import javax.jms.JMSException;
import javax.jms.Message;

import org.apache.abdera.Abdera;
import org.apache.abdera.model.Category;
import org.apache.abdera.model.Entry;
import org.apache.abdera.parser.Parser;
import org.fcrepo.utils.FedoraTypesUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
Expand All @@ -37,38 +34,17 @@

public class LegacyMethod {

private static final Abdera ABDERA = new Abdera();

private static final Parser ABDERA_PARSER = ABDERA.getParser();

//TODO Figure out where to get the base url
private static final String BASE_URL = "http://localhost:8080/rest";

private static final Properties FEDORA_TYPES = new Properties();

public static final String FORMAT =
"info:fedora/fedora-system:ATOM-APIM-1.0";

private static final String FORMAT_PREDICATE =
"http://www.fedora.info/definitions/1/0/types/formatURI";

//TODO get this out of the build properties
public static final String SERVER_VERSION = "4.0.0-SNAPSHOT";

public static final String FEDORA_ID_SCHEME = "xsd:string";

public static final String DSID_CATEGORY_LABEL = "fedora-types:dsID";

public static final String PID_CATEGORY_LABEL = "fedora-types:pid";

private static final String TYPES_NS =
"http://www.fedora.info/definitions/1/0/types/";

private static final String VERSION_PREDICATE =
"info:fedora/fedora-system:def/view#version";

private static final String XSD_NS = "http://www.w3.org/2001/XMLSchema";

private static final String INGEST_METHOD = "ingest";

private static final String MODIFY_OBJ_METHOD = "modifyObject";
Expand Down Expand Up @@ -106,7 +82,7 @@ public class LegacyMethod {

public LegacyMethod(final Event jcrEvent, final Node resource)
throws RepositoryException {
this(newEntry());
this(EntryFactory.newEntry());

final boolean isDatastreamNode =
FedoraTypesUtils.isFedoraDatastream.apply(resource);
Expand Down Expand Up @@ -139,8 +115,7 @@ public LegacyMethod(final Entry atomEntry) {

public LegacyMethod(final String atomEntry) {
delegate =
(Entry) ABDERA_PARSER.parse(new StringReader(atomEntry))
.getRoot();
EntryFactory.parse(new StringReader(atomEntry));
}

public Entry getEntry() {
Expand Down Expand Up @@ -225,16 +200,6 @@ public void writeTo(final Writer writer) throws IOException {
delegate.writeTo(writer);
}

static Entry newEntry() {
final Entry entry = ABDERA.newEntry();
entry.declareNS(XSD_NS, "xsd");
entry.declareNS(TYPES_NS, "fedora-types");
entry.setId("urn:uuid:" + UUID.randomUUID().toString());
entry.addCategory(FORMAT_PREDICATE, FORMAT, "format");
entry.addCategory(VERSION_PREDICATE, SERVER_VERSION, "version");
return entry;
}

private static String getEntryContent(final String methodName,
final String returnVal) {
final String datatype =
Expand Down Expand Up @@ -331,7 +296,7 @@ private static String convertDateToXSDString(final long date) {

public static boolean canParse(final Message jmsMessage) {
try {
return FORMAT.equals(jmsMessage.getJMSType()) &&
return EntryFactory.FORMAT.equals(jmsMessage.getJMSType()) &&
METHOD_NAMES.contains(jmsMessage
.getStringProperty("methodName"));
} catch (final JMSException e) {
Expand Down
Expand Up @@ -44,9 +44,9 @@ public Message getMessage(final Event jcrEvent,
tm.setStringProperty("pid", pid);
}
tm.setStringProperty("methodName", legacy.getMethodName());
tm.setJMSType(LegacyMethod.FORMAT);
tm.setJMSType(EntryFactory.FORMAT);
tm.setStringProperty("fcrepo.server.version",
LegacyMethod.SERVER_VERSION);
EntryFactory.SERVER_VERSION);
logger.trace("Successfully created JMS message from event.");
return tm;
}
Expand Down
@@ -0,0 +1,33 @@
package org.fcrepo.messaging.legacy;

import static org.junit.Assert.assertEquals;

import java.io.StringReader;
import java.util.List;

import org.apache.abdera.model.Category;
import org.apache.abdera.model.Entry;
import org.junit.Test;

public class EntryFactoryTest {

private static String ATOM =
"<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:fedora-types=\"http://www.fedora.info/definitions/1/0/types/\"><id>urn:uuid:a447ba5a-4ff5-42e1-8d4d-b728ea86155c</id><category term=\"info:fedora/fedora-system:ATOM-APIM-1.0\" scheme=\"http://www.fedora.info/definitions/1/0/types/formatURI\" label=\"format\"/><category term=\"4.0.0-SNAPSHOT\" scheme=\"info:fedora/fedora-system:def/view#version\" label=\"version\"/></entry>";


@Test
public void testNewEntry() {
Entry actual = EntryFactory.newEntry();
List<Category> categories = actual.getCategories(EntryFactory.FORMAT_PREDICATE);
assertEquals(1, categories.size());
Category category = categories.get(0);
System.out.println("HELLO! " + actual.toString());
assertEquals(EntryFactory.FORMAT, category.getTerm());
assertEquals("format", category.getLabel());
}

@Test
public void testParse() {
EntryFactory.parse(new StringReader(ATOM));
}
}
@@ -1,7 +1,7 @@
package org.fcrepo.messaging.legacy;

import static javax.jcr.observation.Event.NODE_ADDED;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.whenNew;

import java.io.IOException;

Expand All @@ -21,8 +21,6 @@
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({LegacyMethodEventFactory.class})
public class LegacyMethodEventFactoryTest {

private LegacyMethodEventFactory testObj;
Expand All @@ -41,17 +39,15 @@ public void testGetMessage() throws Exception {
when(mockJMS.createTextMessage(anyString())).thenReturn(mockText);
Event mockEvent = mock(Event.class);
when(mockEvent.getPath()).thenReturn(testPath);
when(mockEvent.getType()).thenReturn(NODE_ADDED);
Session mockJCR = mock(Session.class);
Node mockSource = mock(Node.class);
NodeType mockType = mock(NodeType.class);
when(mockType.getName()).thenReturn(FedoraJcrTypes.FEDORA_OBJECT);
NodeType[] mockTypes = new NodeType[]{mockType};
when(mockSource.getMixinNodeTypes()).thenReturn(mockTypes);
when(mockJCR.getNode(testPath)).thenReturn(mockSource);
LegacyMethod mockMethod = mock(LegacyMethod.class);
whenNew(LegacyMethod.class).withArguments(mockEvent, mockSource).thenReturn(mockMethod);
when(mockMethod.getMethodName()).thenReturn("foo-method");
Message actual = testObj.getMessage(mockEvent, mockJCR, mockJMS);
verify(mockText).setStringProperty("methodName", "foo-method");
verify(mockText).setStringProperty("methodName", "ingest");
}
}
Expand Up @@ -40,7 +40,7 @@ public void testCanParse() throws JMSException {
// Should the static tests be broken out into a separate test class, so we can use PowerMock with better scope?
boolean result;
Message mockYes = mock(Message.class);
when(mockYes.getJMSType()).thenReturn(LegacyMethod.FORMAT);
when(mockYes.getJMSType()).thenReturn(EntryFactory.FORMAT);
when(mockYes.getStringProperty("methodName")).thenReturn("ingest");
result = LegacyMethod.canParse(mockYes);
assertEquals(true, result);
Expand All @@ -50,7 +50,7 @@ public void testCanParse() throws JMSException {
result = LegacyMethod.canParse(mockNoFormat);
assertEquals(false, result);
Message mockNoMessage = mock(Message.class);
when(mockNoMessage.getJMSType()).thenReturn(LegacyMethod.FORMAT);
when(mockNoMessage.getJMSType()).thenReturn(EntryFactory.FORMAT);
when(mockNoMessage.getStringProperty("methodName")).thenReturn("destroyEverything");
result = LegacyMethod.canParse(mockNoMessage);
assertEquals(false, result);
Expand Down
Expand Up @@ -30,7 +30,7 @@
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({LegacyMethod.class})
@PrepareForTest({EntryFactory.class})
public class LegacyMethodTest {

private LegacyMethod testObj;
Expand Down Expand Up @@ -66,8 +66,8 @@ public void setUp() throws RepositoryException {
Text mockText = mock(Text.class);
when(mockDelegate.setTitle(anyString())).thenReturn(mockText);
// make sure the delegate Entry can be instrumented for tests
PowerMockito.mockStatic(LegacyMethod.class);
when(LegacyMethod.newEntry()).thenReturn(mockDelegate);
PowerMockito.mockStatic(EntryFactory.class);
when(EntryFactory.newEntry()).thenReturn(mockDelegate);
mockPidCategory = mock(Category.class);
when(mockPidCategory.getLabel()).thenReturn(LegacyMethod.PID_CATEGORY_LABEL);
when(mockPidCategory.getTerm()).thenReturn(SOURCE_PID);
Expand Down
37 changes: 31 additions & 6 deletions pom.xml
Expand Up @@ -570,23 +570,42 @@
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.2.201302030002</version>
<configuration>
<destFile>${project.basedir}/target/jacoco.exec</destFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>verify</phase>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>pre-it-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.basedir}/target/jacoco-it.exec</destFile>
<propertyName>failsafe.argLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-it-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.basedir}/target/jacoco-it.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand All @@ -608,6 +627,9 @@
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>${failsafe.argLine}</argLine>
</configuration>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
Expand All @@ -617,6 +639,9 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<jacoco-agent.destFile>${project.basedir}/target/jacoco-it.exec</jacoco-agent.destFile>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down

0 comments on commit 35c395f

Please sign in to comment.