use of org.apache.struts.action.ActionMessages in project sonarqube by SonarSource.
the class TagUtils method getActionMessages.
/**
* Retrieves the value from request scope and if it isn't already an
* <code>ActionMessages</code>, some classes are converted to one.
*
* @param pageContext The PageContext for the current page
* @param paramName Key for parameter value
* @return ActionErrors in page context.
* @throws JspException
*/
public ActionMessages getActionMessages(PageContext pageContext, String paramName) throws JspException {
ActionMessages am = new ActionMessages();
Object value = pageContext.findAttribute(paramName);
if (value != null) {
try {
if (value instanceof String) {
am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage((String) value));
} else if (value instanceof String[]) {
String[] keys = (String[]) value;
for (int i = 0; i < keys.length; i++) {
am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(keys[i]));
}
} else if (value instanceof ActionErrors) {
ActionMessages m = (ActionMessages) value;
am.add(m);
} else if (value instanceof ActionMessages) {
am = (ActionMessages) value;
} else {
throw new JspException(messages.getMessage("actionMessages.errors", value.getClass().getName()));
}
} catch (JspException e) {
throw e;
} catch (Exception e) {
log.warn("Unable to retieve ActionMessage for paramName : " + paramName, e);
}
}
return am;
}
use of org.apache.struts.action.ActionMessages in project sonarqube by SonarSource.
the class MessagesPresentTag method condition.
/**
* Evaluate the condition that is being tested by this particular tag, and
* return <code>true</code> if there is at least one message in the class
* or for the property specified.
*
* @param desired Desired outcome for a true result
* @throws JspException if a JSP exception occurs
*/
protected boolean condition(boolean desired) throws JspException {
ActionMessages am = null;
String key = name;
if ((message != null) && "true".equalsIgnoreCase(message)) {
key = Globals.MESSAGE_KEY;
}
try {
am = TagUtils.getInstance().getActionMessages(pageContext, key);
} catch (JspException e) {
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
Iterator iterator = (property == null) ? am.get() : am.get(property);
return (iterator.hasNext() == desired);
}
use of org.apache.struts.action.ActionMessages in project sonarqube by SonarSource.
the class TestTagUtils method testActionMessages_getActionMessages_PageContext_String3.
// -- using String
public void testActionMessages_getActionMessages_PageContext_String3() {
request.setAttribute("foo", "bar");
try {
ActionMessages messages = tagutils.getActionMessages(pageContext, "foo");
assertNotNull("messages should not be null", messages);
assertNotNull("messages prop should not be null", messages.get("prop"));
String key = null;
int i = 0;
for (Iterator iter = messages.get(ActionMessages.GLOBAL_MESSAGE); iter.hasNext(); ) {
ActionMessage message = (ActionMessage) iter.next();
key = message.getKey();
Object[] values = message.getValues();
assertNull(values);
i++;
}
assertEquals("only 1 message", i, 1);
assertEquals("key should match", key, "bar");
} catch (JspException e) {
fail(e.getMessage());
}
}
use of org.apache.struts.action.ActionMessages in project sonarqube by SonarSource.
the class TestTagUtils method testGetActionErrors1c.
// String Array
public void testGetActionErrors1c() {
String[] vals = new String[] { "bar", "baz" };
request.setAttribute("foo", vals);
try {
ActionMessages errors = tagutils.getActionMessages(pageContext, "foo");
assertNotNull("errors should not be null", errors);
assertNotNull("errors prop should not be null", errors.get("prop"));
String key = null;
int i = 0;
for (Iterator iter = errors.get(ActionMessages.GLOBAL_MESSAGE); iter.hasNext(); ) {
ActionMessage error = (ActionMessage) iter.next();
key = error.getKey();
Object[] values = error.getValues();
assertNull((values));
assertEquals("1st key should match", key, vals[i]);
i++;
}
assertEquals("only 1 error", i, 2);
} catch (JspException e) {
fail(e.getMessage());
}
}
use of org.apache.struts.action.ActionMessages in project sonarqube by SonarSource.
the class TestTagUtils method testGetActionErrors1d.
// String Array (thrown JspException)
public void testGetActionErrors1d() {
request.setAttribute("foo", new MockFormBean());
ActionMessages errors = null;
try {
errors = tagutils.getActionMessages(pageContext, "foo");
fail("should have thrown JspException");
} catch (JspException e) {
assertNull("errors should be null", errors);
}
}
Aggregations