Skip to content

Commit

Permalink
Unhandled exception on 404 within Transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
kaimst authored and Andrew Woods committed Apr 3, 2014
1 parent ec72fff commit 7702598
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
Expand Up @@ -338,6 +338,24 @@ public void testIngestNewWithSparqlPatchWithinTransaction() throws Exception {

}

@Test
public void testGetNonExistingObject() throws Exception {
/* create new tx */
final HttpPost createTx = new HttpPost(serverAddress + "fcr:tx");
final HttpResponse response = execute(createTx);
assertEquals(201, response.getStatusLine().getStatusCode());

/* try to retrieve a non existing object inside the tx */
final String txLocation =
response.getFirstHeader("Location").getValue();
final String newObjectLocation = txLocation + "/idontexist";
final HttpGet httpGet = new HttpGet(newObjectLocation);
client = createClient();
HttpResponse responseFromGet = client.execute(httpGet);
int status = responseFromGet.getStatusLine().getStatusCode();
assertEquals("Status should be 404", 404, status);
}


/**
* Tests that transactions are treated as atomic with regards to nodes.
Expand Down
25 changes: 17 additions & 8 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/TxAwareSession.java
Expand Up @@ -19,6 +19,7 @@
import static java.lang.reflect.Proxy.newProxyInstance;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.jcr.Session;
Expand All @@ -32,7 +33,7 @@ public class TxAwareSession implements InvocationHandler {

private final String txId;

private Session session;
private final Session session;

/**
* @param session a JCR session
Expand All @@ -58,16 +59,24 @@ public static Session newInstance(final Session session, final String txId) {

@Override
public Object invoke(final Object proxy, final Method method,
final Object[] args) throws ReflectiveOperationException {
if (method.getName().equals("logout") ||
method.getName().equals("save")) {
final Object[] args) throws Throwable {
final String name = method.getName();
if (name.equals("logout") || name.equals("save")) {
return null;
} else if (method.getName().equals("getTxId")) {
} else if (name.equals("getTxId")) {
return txId;
} else if (method.getName().equals("impersonate")) {
return newInstance((Session) method.invoke(session, args), txId);
} else {
return method.invoke(session, args);
final Object invocationResult;
try {
invocationResult = method.invoke(session, args);
} catch (final InvocationTargetException e) {
throw e.getCause();
}
if (name.equals("impersonate")) {
return newInstance((Session) invocationResult, txId);
} else {
return invocationResult;
}
}
}
}
Expand Up @@ -26,6 +26,7 @@
import static org.mockito.MockitoAnnotations.initMocks;

import javax.jcr.Credentials;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

Expand All @@ -40,17 +41,18 @@ public class TxAwareSessionTest {

private Session testObj;

private static final String PATH = "/xyz";

@Before
public void setUp() {
initMocks(this);
testObj = newInstance(mockSession, "txid");

}

@Test
public void shouldProxyMethods() throws RepositoryException {
testObj.getItem("/xyz");
verify(mockSession).getItem("/xyz");
testObj.getItem(PATH);
verify(mockSession).getItem(PATH);
}

@Test
Expand All @@ -77,4 +79,11 @@ public void shouldMakeSaveANoop() throws RepositoryException {
verify(mockSession, never()).save();
}

@Test(expected=PathNotFoundException.class)
public void shouldThrowUnderlyingException() throws RepositoryException {
when(mockSession.getNode(PATH)).thenThrow(
new PathNotFoundException());
testObj.getNode(PATH);
}

}

0 comments on commit 7702598

Please sign in to comment.