KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > taglib > TabSheetTagTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.taglib;
38
39 import java.io.IOException JavaDoc;
40 import javax.servlet.jsp.JspException JavaDoc;
41 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
42 import javax.servlet.jsp.tagext.Tag JavaDoc;
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 /**
50  * Test Details... take the following set up:
51  * <cruisecontrol:tabsheet>
52  * <cruisecontrol:tab name="tabname1" label="Tab Name 1">Some Text 1</cruisecontrol:tab>
53  * <cruisecontrol:tab name="tabname2" label="Tab Name 2">Some Text 2</cruisecontrol:tab>
54  * </cruisecontrol:tabsheet>
55  *
56  * And turn it into this:
57  * <tr>
58  * <th>Tab Name 1</th>
59  * <th>Tab Name 2</th>
60  * </tr>
61  * <tr>
62  * <td><!-- BODY OF SELECTED TAB HERE -->
63  * </tr>
64  *
65  * @author <a HREF="mailto:robertdw@users.sourceforge.net>Robert Watkins</a>
66  */

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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc END_OF_HEADERS = "</tr></tbody></table></div></td></tr>";
85     private static final String JavaDoc NEW_TAB_ROW = "</tr><tr>";
86
87     public TabSheetTagTest(String JavaDoc 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 JavaDoc {
102         tabSheet.addTab(new Tab("tabname1", null, "Tab Name 1", false));
103         assertEquals(BodyTag.EVAL_BODY_TAG, tabSheet.doStartTag()); // side effect of this should clear
104
assertEquals(Tag.EVAL_PAGE, tabSheet.doEndTag());
105         final String JavaDoc 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 JavaDoc {
113         tabSheet.addTab(new Tab("tabname1", null, "Tab Name 1", false));
114         tabSheet.release();
115         assertEquals(Tag.EVAL_PAGE, tabSheet.doEndTag()); // wouldn't normally call this after a release
116
// but good enough way to test.
117
String JavaDoc expected = START_SHEET
118                           + START_OF_HEADERS + END_OF_HEADERS + END_OF_TABLE;
119
120         assertEquals(expected, pageContext.getOut().toString());
121     }
122
123     // Let's put it all together...
124
public void testPrintTabSheetTab1Selected() throws JspException JavaDoc, IOException JavaDoc {
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()); // don't write out anything during afterbody
138
assertEquals(Tag.EVAL_PAGE, tabSheet.doEndTag());
139
140         String JavaDoc 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 JavaDoc, IOException JavaDoc {
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()); // don't write out anything during afterbody
166
assertEquals(Tag.EVAL_PAGE, tabSheet.doEndTag());
167
168         String JavaDoc 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 JavaDoc, IOException JavaDoc {
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 JavaDoc 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 JavaDoc {
202         assertEquals(BodyTag.EVAL_BODY_TAG, tabSheet.doStartTag());
203         assertEquals(Tag.SKIP_BODY, tabSheet.doAfterBody());
204         assertEquals("", pageContext.getOut().toString()); // don't write out anything during afterbody
205
assertEquals(Tag.EVAL_PAGE, tabSheet.doEndTag());
206
207         String JavaDoc expected = START_SHEET + START_OF_HEADERS + END_OF_HEADERS + END_OF_TABLE;
208         assertEquals(expected, pageContext.getOut().toString());
209     }
210
211 }
212
Popular Tags