}
public void testMessagesRenderer(UIComponent root) throws IOException {
System.out.println("Testing MessagesRenderer");
UIMessages messages = new UIMessages();
messages.setId("myMessage_0");
String myFor = "myMessage_0";
// messages.setFor("myMessage_0");
root.getChildren().add(messages);
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);
MessagesRenderer messagesRenderer = new MessagesRenderer();
// populate facescontext with some errors
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_INFO,
"global message summary_0.0",
"global message detail_0.0"));
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_INFO,
"global message summary_0.1",
"global message detail_0.1"));
// test encode method
messagesRenderer.encodeBegin(getFacesContext(), messages);
messagesRenderer.encodeEnd(getFacesContext(), messages);
String result = writer.toString();
//no span should be rendered since none of the criteria was met
assertTrue(result.indexOf("span") == -1);
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
root.getChildren().remove(messages);
messages = new UIMessages();
messages.setId("myMessage_1");
myFor = "myMessage_1";
// messages.setFor("myMessage_1");
messages.setShowDetail(true);
messages.setShowSummary(true);
messages.getAttributes().put("warnClass", "warnClass");
messages.getAttributes().put("errorClass", "errorClass");
messages.getAttributes().put("infoClass", "infoClass");
messages.getAttributes().put("fatalClass", "fatalClass");
root.getChildren().add(messages);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messagesRenderer = new MessagesRenderer();
//add a styleClass so span is rendered
messages.getAttributes().put("styleClass", "styleClass");
// populate facescontext with some errors
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_WARN,
"global message summary_1.0",
"global message detail_1.0"));
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_WARN,
"global message summary_1.1",
"global message detail_1.1"));
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_WARN,
"global message summary_1.1",
"global message detail_1.1"));
// test encode method
messagesRenderer.encodeBegin(getFacesContext(), messages);
messagesRenderer.encodeEnd(getFacesContext(), messages);
result = writer.toString();
//Verify Html list (<ul> with id/class
assertTrue(
result.indexOf("<ul id=\"myMessage_1\" class=\"styleClass\">")
!= -1);
//Verify Html list items
assertTrue(
result.indexOf("<li class=\"infoClass\">") != -1);
assertTrue(
result.indexOf("<li class=\"warnClass\">") != -1);
assertTrue(
result.indexOf("<li class=\"fatalClass\">") != -1);
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
root.getChildren().remove(messages);
messages = new UIMessages();
messages.setId("myMessage_2");
// messages.setFor("myMessage_2");
myFor = "myMessage_2";
messages.getAttributes().put("warnClass", "warnClass");
messages.getAttributes().put("errorClass", "errorClass");
messages.getAttributes().put("infoClass", "infoClass");
messages.getAttributes().put("fatalClass", "fatalClass");
messages.setShowDetail(true);
messages.setShowSummary(true);
root.getChildren().add(messages);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messagesRenderer = new MessagesRenderer();
//add a style
messages.getAttributes().put("style", "style");
// populate facescontext with some errors
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"global message summary_2.0",
"global message detail_2.0"));
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"global message summary_2.1",
"global message detail_2.1"));
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"global message summary_2.2",
"global message detail_2.2"));
// test encode method
messagesRenderer.encodeBegin(getFacesContext(), messages);
messagesRenderer.encodeEnd(getFacesContext(), messages);
result = writer.toString();
//Verify Html list (<ul> with id/style)
assertTrue(
result.indexOf("<ul id=\"myMessage_2\" style=\"style\">") != -1);
//Verify Html item (<li> with class)
assertTrue(
result.indexOf("<li class=\"errorClass\">") != -1);
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
root.getChildren().remove(messages);
messages = new UIMessages();
messages.setId("myMessage_3");
// messages.setFor("myMessage_3");
myFor = "myMessage_3";
messages.getAttributes().put("warnClass", "warnClass");
messages.getAttributes().put("errorClass", "errorClass");
messages.getAttributes().put("infoClass", "infoClass");
messages.getAttributes().put("fatalClass", "fatalClass");
messages.setShowDetail(true);
messages.setShowSummary(true);
root.getChildren().add(messages);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messagesRenderer = new MessagesRenderer();
//add a styleClass so span is rendered
messages.getAttributes().put("styleClass", "styleClass");
messages.getAttributes().put("style", "style");
// populate facescontext with some errors
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_3.0",
"global message detail_3.0"));
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_3.1",
"global message detail_3.1"));
// test encode method
messagesRenderer.encodeBegin(getFacesContext(), messages);
messagesRenderer.encodeEnd(getFacesContext(), messages);
result = writer.toString();
//Verify Html list (<ul> with id/style/class)
assertTrue(
result.indexOf("<ul id=\"myMessage_3\" class=\"styleClass\" style=\"style\">") != -1);
//Verify Html item (<li> with class)
assertTrue(
result.indexOf("<li class=\"fatalClass\">") != -1);
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
root.getChildren().remove(messages);
messages = new UIMessages();
messages.setId("myMessage_4");
// messages.setFor("myMessage_4");
myFor = "myMessage_4";
messages.getAttributes().put("warnClass", "warnClass");
messages.getAttributes().put("errorClass", "errorClass");
messages.getAttributes().put("infoClass", "infoClass");
messages.getAttributes().put("fatalClass", "fatalClass");
root.getChildren().add(messages);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messagesRenderer = new MessagesRenderer();
//add a styleClass so span is rendered
messages.getAttributes().put("styleClass", "styleClass");
messages.getAttributes().put("style", "style");
//set tooltip criteria to true
messages.getAttributes().put("tooltip", new Boolean(true));
messages.setShowDetail(true);
messages.setShowSummary(true);
// populate facescontext with some errors
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_4.0",
"global message detail_4.0"));
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_4.1",
"global message detail_4.1"));
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_4.2",
"global message detail_4.2"));
// test encode method
messagesRenderer.encodeBegin(getFacesContext(), messages);
messagesRenderer.encodeEnd(getFacesContext(), messages);
result = writer.toString();
//Verify <ul> with id/style/class
//Verify <li> with class;span containing tool tip
assertTrue(
result.indexOf("<ul id=\"myMessage_4\" class=\"styleClass\" style=\"style\"><li class=\"infoClass\"><span title=\"global message summary_0\">") != -1);
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
root.getChildren().remove(messages);
messages = new UIMessages();
messages.setId("myMessage_5");
// messages.setFor("myMessage_5");
myFor = "myMessage_5";
root.getChildren().add(messages);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messagesRenderer = new MessagesRenderer();
//add a styleClass
messages.getAttributes().put("styleClass", "styleClass");
messages.getAttributes().put("style", "style");
//set tooltip criteria to true
messages.getAttributes().put("tooltip", new Boolean(true));
messages.setShowDetail(true);
messages.setShowSummary(true);
//Set layout to table
messages.getAttributes().put("layout", "table");
// populate facescontext with some errors
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_5.0",
"global message detail_5.0"));
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_5.1",
"global message detail_5.1"));
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_FATAL,
"global message summary_5.2",
"global message detail_5.2"));
// test encode method
messagesRenderer.encodeBegin(getFacesContext(), messages);
messagesRenderer.encodeEnd(getFacesContext(), messages);
result = writer.toString();
//Verify wrapped in <table>
assertTrue(
result.indexOf("<table id=\"myMessage_5\" class=\"styleClass\" style=\"style\">") != -1);
assertTrue(
result.indexOf("<tr><td><span title=\"global message summary_5.0\">") != -1);
try {
writer.close();
} catch (IOException ioe) {
; // ignore
}
root.getChildren().remove(messages);
messages = new UIMessages();
messages.setId("myMessage_6");
// messages.setFor("myMessage_6");
myFor = "myMessage_6";
root.getChildren().add(messages);
writer = new StringWriter();
htmlWriter = new HtmlResponseWriter(writer, "text/html", "ISO-8859-1");
getFacesContext().setResponseWriter(htmlWriter);
messagesRenderer = new MessagesRenderer();
//add a styleClass
messages.getAttributes().put("styleClass", "styleClass");
messages.getAttributes().put("style", "style");
//set tooltip criteria to false
messages.getAttributes().put("tooltip", new Boolean(false));
messages.setShowDetail(false);
messages.setShowSummary(true);
//Set layout to table
messages.getAttributes().put("layout", "table");
// populate facescontext with some errors
getFacesContext().addMessage(myFor,
new FacesMessage(
FacesMessage.SEVERITY_FATAL,