}
public void testMessageRenderer(UIComponent root) throws IOException {
System.out.println("Testing MessageRenderer");
UIMessage message = new UIMessage();
message.setId("myMessage_0");
message.setFor("myMessage_0");
root.getChildren().add(message);
ResponseWriter originalWriter = getFacesContext().getResponseWriter();
UIViewRoot originalRoot = getFacesContext().getViewRoot();
getFacesContext().setViewRoot((UIViewRoot) root);
// setup a new HtmlResponseWriter using a StringWriter.
// This allows us to capture the output and check for
// correctness without using a goldenfile.
StringWriter writer = new StringWriter();
HtmlResponseWriter htmlWriter = new HtmlResponseWriter(writer,
"text/html",
"ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
MessageRenderer messageRenderer = new MessageRenderer();
// populate facescontext with some errors
getFacesContext().addMessage(message.getFor(),
new FacesMessage(
FacesMessage.SEVERITY_INFO,
"global message summary_0",
"global message detail_0"));
// test encode method
messageRenderer.encodeBegin(getFacesContext(), message);
messageRenderer.encodeEnd(getFacesContext(), message);
String result = writer.toString();
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
root.getChildren().remove(message);
message = new UIMessage();
message.setId("myMessage_1");
message.setFor("myMessage_1");
message.getAttributes().put("warnClass", "warnClass");
message.getAttributes().put("errorClass", "errorClass");
message.getAttributes().put("infoClass", "infoClass");
message.getAttributes().put("fatalClass", "fatalClass");
message.setShowDetail(true);
message.setShowSummary(true);
root.getChildren().add(message);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messageRenderer = new MessageRenderer();
//add a styleClass so span is rendered
message.getAttributes().put("styleClass", "styleClass");
// populate facescontext with some errors
getFacesContext().addMessage(message.getFor(),
new FacesMessage(
FacesMessage.SEVERITY_WARN,
"global message summary_1",
"global message detail_1"));
// test encode method
messageRenderer.encodeBegin(getFacesContext(), message);
messageRenderer.encodeEnd(getFacesContext(), message);
result = writer.toString();
//Span should have class attribute for styleClass
//Summary and detail should be in body of span separated by space
assertTrue(
result.indexOf(
"<span id=\"myMessage_1\" class=\"warnClass\"> global message summary_1 global message detail_1</span>") !=
-1);
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
root.getChildren().remove(message);
message = new UIMessage();
message.setId("myMessage_2");
message.setFor("myMessage_2");
message.getAttributes().put("warnClass", "warnClass");
message.getAttributes().put("errorClass", "errorClass");
message.getAttributes().put("infoClass", "infoClass");
message.getAttributes().put("fatalClass", "fatalClass");
message.setShowDetail(true);
message.setShowSummary(true);
root.getChildren().add(message);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messageRenderer = new MessageRenderer();
//add a styleClass so span is rendered
message.getAttributes().put("style", "style");
// populate facescontext with some errors
getFacesContext().addMessage(message.getFor(),
new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"global message summary_2",
"global message detail_2"));
// test encode method
messageRenderer.encodeBegin(getFacesContext(), message);
messageRenderer.encodeEnd(getFacesContext(), message);
result = writer.toString();
//Span should have style attribute
//Summary and detail should be in body of span separated by space
assertTrue(
result.indexOf(
"<span id=\"myMessage_2\" style=\"style\" class=\"errorClass\"> global message summary_2 global message detail_2</span>") !=
-1);
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
root.getChildren().remove(message);
message = new UIMessage();
message.setId("myMessage_3");
message.setFor("myMessage_3");
message.getAttributes().put("warnClass", "warnClass");
message.getAttributes().put("errorClass", "errorClass");
message.getAttributes().put("infoClass", "infoClass");
message.getAttributes().put("fatalClass", "fatalClass");
message.setShowDetail(true);
message.setShowSummary(true);
root.getChildren().add(message);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messageRenderer = new MessageRenderer();
//add a styleClass so span is rendered
message.getAttributes().put("styleClass", "styleClass");
message.getAttributes().put("style", "style");
// populate facescontext with some errors
getFacesContext().addMessage(message.getFor(),
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_3",
"global message detail_3"));
// test encode method
messageRenderer.encodeBegin(getFacesContext(), message);
messageRenderer.encodeEnd(getFacesContext(), message);
result = writer.toString();
//Span should have class attribute for styleClass and style attribute
//Summary and detail should be in body of span separated by space
assertTrue(
result.indexOf(
"<span id=\"myMessage_3\" style=\"style\" class=\"fatalClass\"> global message summary_3 global message detail_3</span>") !=
-1);
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
root.getChildren().remove(message);
message = new UIMessage();
message.setId("myMessage_4");
message.setFor("myMessage_4");
root.getChildren().add(message);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messageRenderer = new MessageRenderer();
//add a styleClass so span is rendered
message.getAttributes().put("styleClass", "styleClass");
message.getAttributes().put("style", "style");
//set tooltip criteria to true
message.getAttributes().put("tooltip", new Boolean(true));
message.setShowDetail(true);
message.setShowSummary(true);
// populate facescontext with some errors
getFacesContext().addMessage(message.getFor(),
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_4",
"global message detail_4"));
// test encode method
messageRenderer.encodeBegin(getFacesContext(), message);
messageRenderer.encodeEnd(getFacesContext(), message);
result = writer.toString();
//Span should containt class for styleClass, style,
// and title for tooltip attributes
//Summary should go in the title attribute and only the
// detail displayed in the body of the span
assertTrue(
result.indexOf(
"<span id=\"myMessage_4\" style=\"style\" class=\"styleClass\" title=\"global message summary_4\"> global message detail_4</span>") !=
-1);
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
root.getChildren().remove(message);
message = new UIMessage();
message.setId("myMessage_5");
message.setFor("myMessage_5");
root.getChildren().add(message);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messageRenderer = new MessageRenderer();
//add a styleClass so span is rendered
message.getAttributes().put("styleClass", "styleClass");
message.getAttributes().put("style", "style");
//set tooltip criteria to true
message.getAttributes().put("tooltip", new Boolean(true));
message.setShowDetail(true);
message.setShowSummary(true);
message.getAttributes().put("warnClass", "warnClass");
message.getAttributes().put("errorClass", "errorClass");
message.getAttributes().put("infoClass", "infoClass");
message.getAttributes().put("fatalClass", "fatalClass");
// populate facescontext with some errors
getFacesContext().addMessage(message.getFor(),
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_5",
"global message detail_5"));
// test encode method
messageRenderer.encodeBegin(getFacesContext(), message);
messageRenderer.encodeEnd(getFacesContext(), message);
result = writer.toString();
//Span should containt class for styleClass, style,
// and title for tooltip attributes
//Summary should go in the title attribute and only the
// detail displayed in the body of the span
//Should be wrapped in a table
assertTrue(
result.indexOf(
"<span id=\"myMessage_5\" style=\"style\" class=\"fatalClass\" title=\"global message summary_5\"> global message detail_5</span>") !=
-1);
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
//
// test showSummary(false) works
//
root.getChildren().remove(message);
message = new UIMessage();
message.setId("myMessage_6");
message.setFor("myMessage_6");
root.getChildren().add(message);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messageRenderer = new MessageRenderer();
//set tooltip criteria to true
message.getAttributes().put("tooltip", new Boolean(true));
message.setShowDetail(true);
message.setShowSummary(false);
// populate facescontext with some errors
getFacesContext().addMessage(message.getFor(),
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_6",
"global message detail_6"));
// test encode method
messageRenderer.encodeBegin(getFacesContext(), message);
messageRenderer.encodeEnd(getFacesContext(), message);
result = writer.toString();
// should not contain summary.
assertTrue(-1 != result.indexOf("global message detail_6"));
assertEquals(-1, result.indexOf("global message summary_6"));
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
//
// test showDetail(false) works
//
root.getChildren().remove(message);
message = new UIMessage();
message.setId("myMessage_6");
message.setFor("myMessage_6");
root.getChildren().add(message);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messageRenderer = new MessageRenderer();
//set tooltip criteria to true
message.getAttributes().put("tooltip", new Boolean(true));
message.setShowDetail(false);
message.setShowSummary(true);
// populate facescontext with some errors
getFacesContext().addMessage(message.getFor(),
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_6",
"global message detail_6"));
// test encode method
messageRenderer.encodeBegin(getFacesContext(), message);
messageRenderer.encodeEnd(getFacesContext(), message);
result = writer.toString();
// should not contain detail.
assertEquals(-1, result.indexOf("global message detail_6"));
assertTrue(-1 != result.indexOf("global message summary_6"));
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
//
// test showDetail(false), showSummary(false) works
//
root.getChildren().remove(message);
message = new UIMessage();
message.setId("myMessage_6");
message.setFor("myMessage_6");
root.getChildren().add(message);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messageRenderer = new MessageRenderer();
//set tooltip criteria to true
message.getAttributes().put("tooltip", new Boolean(true));
message.setShowDetail(false);
message.setShowSummary(false);
// populate facescontext with some errors
getFacesContext().addMessage(message.getFor(),
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_6",
"global message detail_6"));