1 37 package net.sourceforge.cruisecontrol.taglib; 38 39 import java.io.IOException ; 40 import javax.servlet.jsp.JspException ; 41 import javax.servlet.jsp.tagext.BodyTag ; 42 import javax.servlet.jsp.tagext.Tag ; 43 44 import junit.framework.TestCase; 45 import net.sourceforge.cruisecontrol.mock.MockBodyContent; 46 import net.sourceforge.cruisecontrol.mock.MockPageContext; 47 import net.sourceforge.cruisecontrol.mock.MockServletRequest; 48 49 67 public class TabSheetTagTest extends TestCase { 68 private TabSheetTag tabSheet; 69 private MockBodyContent content; 70 private MockServletRequest request; 71 private MockPageContext pageContext; 72 private static final String START_SHEET = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">" 73 + "<tbody>\r\n" 74 + " <tr>\r\n" 75 + " <td bgcolor=\"#FFFFFF\"><img border=\"0\" SRC=\"images/bluestripestop.gif\"></td>\r\n" 76 + " </tr>\r\n"; 77 private static final String END_OF_TABLE = " <tr>\r\n" 78 + " <td bgcolor=\"#FFFFFF\"><img border=\"0\" SRC=\"images/bluestripesbottom.gif\"></td>\r\n" 79 + " </tr>\r\n" 80 + "</tbody></table>\r\n"; 81 private static final String START_OF_HEADERS = "<tr><td bgcolor=\"#FFFFFF\"><div align=\"left\"><table" 82 + " class=\"tab-table\" align=\"center\" valign=\"middle\" cellspacing=\"0\"" 83 + " cellpadding=\"0\" border=\"1\"><tbody><tr>"; 84 private static final String END_OF_HEADERS = "</tr></tbody></table></div></td></tr>"; 85 private static final String NEW_TAB_ROW = "</tr><tr>"; 86 87 public TabSheetTagTest(String name) { 88 super(name); 89 } 90 91 protected void setUp() { 92 tabSheet = new TabSheetTag(); 93 content = new MockBodyContent(); 94 tabSheet.setBodyContent(content); 95 pageContext = new MockPageContext(); 96 request = new MockServletRequest("context", "servlet"); 97 pageContext.setHttpServletRequest(request); 98 tabSheet.setPageContext(pageContext); 99 } 100 101 public void testClearTabsOnStart() throws JspException { 102 tabSheet.addTab(new Tab("tabname1", null, "Tab Name 1", false)); 103 assertEquals(BodyTag.EVAL_BODY_TAG, tabSheet.doStartTag()); assertEquals(Tag.EVAL_PAGE, tabSheet.doEndTag()); 105 final String expected = START_SHEET 106 + START_OF_HEADERS 107 + END_OF_HEADERS 108 + END_OF_TABLE; 109 assertEquals(expected, pageContext.getOut().toString()); 110 } 111 112 public void testClearTabsOnRelease() throws JspException { 113 tabSheet.addTab(new Tab("tabname1", null, "Tab Name 1", false)); 114 tabSheet.release(); 115 assertEquals(Tag.EVAL_PAGE, tabSheet.doEndTag()); String expected = START_SHEET 118 + START_OF_HEADERS + END_OF_HEADERS + END_OF_TABLE; 119 120 assertEquals(expected, pageContext.getOut().toString()); 121 } 122 123 public void testPrintTabSheetTab1Selected() throws JspException , IOException { 125 final Tab tab1 = new Tab("tabname1", null, "Tab Name 1", true); 126 final Tab tab2 = new Tab("tabname2", null, "Tab Name 2", false); 127 final Tab tab3 = new Tab("tabname3", "mylink", "Tab Name 3", false); 128 129 assertEquals(BodyTag.EVAL_BODY_TAG, tabSheet.doStartTag()); 130 131 tabSheet.getBodyContent().write("This is Tab 1"); 132 133 tabSheet.addTab(tab1); 134 tabSheet.addTab(tab2); 135 tabSheet.addTab(tab3); 136 assertEquals(Tag.SKIP_BODY, tabSheet.doAfterBody()); 137 assertEquals("", pageContext.getOut().toString()); assertEquals(Tag.EVAL_PAGE, tabSheet.doEndTag()); 139 140 String expected = START_SHEET 141 + START_OF_HEADERS 142 + "<td class=\"tabs-selected\">" 143 + "Tab Name 1</td>" 144 + "<td class=\"tabs\"><a class=\"tabs-link\" HREF=\"/context/servlet?tab=tabname2\">" 145 + "Tab Name 2</a></td>" 146 + "<td class=\"tabs\"><a class=\"tabs-link\" HREF=\"mylink\">" 147 + "Tab Name 3</a></td>" 148 + END_OF_HEADERS 149 + "This is Tab 1" + END_OF_TABLE; 150 151 assertEquals(expected, pageContext.getOut().toString()); 152 } 153 154 public void testPrintTabSheetTab2Selected() throws JspException , IOException { 155 final Tab tab1 = new Tab("tabname1", null, "Tab Name 1", false); 156 final Tab tab2 = new Tab("tabname2", null, "Tab Name 2", true); 157 158 tabSheet.getBodyContent().write("This is Tab 2"); 159 160 assertEquals(BodyTag.EVAL_BODY_TAG, tabSheet.doStartTag()); 161 162 tabSheet.addTab(tab1); 163 tabSheet.addTab(tab2); 164 assertEquals(Tag.SKIP_BODY, tabSheet.doAfterBody()); 165 assertEquals("", pageContext.getOut().toString()); assertEquals(Tag.EVAL_PAGE, tabSheet.doEndTag()); 167 168 String expected = START_SHEET 169 + START_OF_HEADERS 170 + "<td class=\"tabs\"><a class=\"tabs-link\" HREF=\"/context/servlet?tab=tabname1\">" 171 + "Tab Name 1</a></td>" 172 + "<td class=\"tabs-selected\">Tab Name 2</td>" 173 + END_OF_HEADERS 174 + "This is Tab 2" + END_OF_TABLE; 175 assertEquals(expected, pageContext.getOut().toString()); 176 } 177 178 public void testPrintTabSheetTwoRows() throws JspException , IOException { 179 final Tab tab1 = new Tab("tabname1", null, "Tab Name 1", false); 180 final Tab tab2 = new Tab("tabname2", null, "Tab Name 2", false, true); 181 final Tab tab3 = new Tab("tabname3", null, "Tab Name 3", true); 182 183 assertEquals(BodyTag.EVAL_BODY_TAG, tabSheet.doStartTag()); 184 tabSheet.addTab(tab1); 185 tabSheet.addTab(tab2); 186 tabSheet.addTab(tab3); 187 188 String expected = START_SHEET 189 + START_OF_HEADERS 190 + "<td class=\"tabs\"><a class=\"tabs-link\" HREF=\"/context/servlet?tab=tabname1\">" 191 + "Tab Name 1</a></td>" 192 + NEW_TAB_ROW 193 + "<td class=\"tabs-selected\">Tab Name 3</td>" 194 + END_OF_HEADERS 195 + END_OF_TABLE; 196 assertEquals(Tag.SKIP_BODY, tabSheet.doAfterBody()); 197 assertEquals(Tag.EVAL_PAGE, tabSheet.doEndTag()); 198 assertEquals(expected, pageContext.getOut().toString()); 199 } 200 201 public void testPrintTabSheetNoTab() throws JspException { 202 assertEquals(BodyTag.EVAL_BODY_TAG, tabSheet.doStartTag()); 203 assertEquals(Tag.SKIP_BODY, tabSheet.doAfterBody()); 204 assertEquals("", pageContext.getOut().toString()); assertEquals(Tag.EVAL_PAGE, tabSheet.doEndTag()); 206 207 String expected = START_SHEET + START_OF_HEADERS + END_OF_HEADERS + END_OF_TABLE; 208 assertEquals(expected, pageContext.getOut().toString()); 209 } 210 211 } 212 | Popular Tags |