use of org.alfresco.repo.forms.FormException in project alfresco-remote-api by Alfresco.
the class RepositoryContainerTest method testHideExceptions.
public void testHideExceptions() throws Exception {
final String messageFormException = "Failed to persist field 'prop_cm_name'";
final String messageAuthenticationException = "Authentication failed for Web Script";
RepositoryContainer repoContainer = (RepositoryContainer) getServer().getApplicationContext().getBean("webscripts.container");
RepositoryContainer repoContainerMock = Mockito.spy(repoContainer);
Class<?>[] defaultConfiguration = repoContainerMock.getNotPublicExceptions();
try {
List<Class<?>> testExceptoins = new LinkedList<Class<?>>();
testExceptoins.add(SQLException.class);
repoContainerMock.setNotPublicExceptions(testExceptoins);
// case: AlfrescoRuntimeException with SQLException cause
Mockito.doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
throw new AlfrescoRuntimeException("AlfrescoRuntimeException", new SQLException("SQLException"));
}
}).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
try {
repoContainerMock.executeScript(null, null, null);
} catch (Exception e) {
assertNull("SQLException cause should be hidden for client", ExceptionStackUtil.getCause(e, new Class[] { SQLException.class }));
assertTrue("Details should be in the server logs.", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
}
// case: AlfrescoRuntimeException with NOT SQLException cause
Mockito.doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
throw new AlfrescoRuntimeException("AlfrescoRuntimeException", new NullPointerException());
}
}).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
try {
repoContainerMock.executeScript(null, null, null);
} catch (Exception e) {
assertNotNull("NullPointerException cause should be visible for client", ExceptionStackUtil.getCause(e, new Class[] { NullPointerException.class }));
assertFalse("Details should be available for client", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
}
// case: RuntimeException with SQLException cause
Mockito.doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
throw new RuntimeException("AlfrescoRuntimeException", new SQLException("SQLException"));
}
}).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
try {
repoContainerMock.executeScript(null, null, null);
} catch (Exception e) {
assertNull("SQLException cause should be hidden for client", ExceptionStackUtil.getCause(e, new Class[] { SQLException.class }));
assertTrue("Details should be in the server logs.", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
}
// case: FormException
Mockito.doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
throw new FormException(messageFormException);
}
}).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
try {
repoContainerMock.executeScript(null, null, null);
} catch (Exception e) {
assertTrue("FormException should be visible for client", e instanceof FormException);
assertFalse("Details should be available for client", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
assertTrue("Actual message should be available for client", e.getMessage().contains(messageFormException));
}
// case: WebScriptException
Mockito.doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
throw new WebScriptException(HttpServletResponse.SC_UNAUTHORIZED, messageAuthenticationException);
}
}).when(repoContainerMock).executeScriptInternal(any(WebScriptRequest.class), any(WebScriptResponse.class), any(Authenticator.class));
try {
repoContainerMock.executeScript(null, null, null);
} catch (Exception e) {
assertTrue("WebScriptException should be visible for client", e instanceof WebScriptException);
assertFalse("Details should be available for client", HIDDEN_EXCEPTION_PATTERN.matcher(e.getMessage()).matches());
assertTrue("Actual message should be available for client", e.getMessage().contains(messageAuthenticationException));
}
} finally {
repoContainerMock.setNotPublicExceptions(Arrays.asList(defaultConfiguration));
}
}
Aggregations