KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.util.Locale JavaDoc;
42
43 import javax.servlet.jsp.JspException JavaDoc;
44 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
45 import javax.servlet.jsp.tagext.Tag JavaDoc;
46
47 import junit.framework.TestCase;
48 import net.sourceforge.cruisecontrol.mock.MockBodyContent;
49 import net.sourceforge.cruisecontrol.mock.MockPageContext;
50 import net.sourceforge.cruisecontrol.mock.MockServletConfig;
51 import net.sourceforge.cruisecontrol.mock.MockServletRequest;
52
53 public class ProjectNavigationTagTest extends TestCase {
54     private ProjectNavigationTag tag;
55     private MockPageContext pageContext;
56     private File JavaDoc logDir;
57     private File JavaDoc[] logProjects;
58
59     public void setUp() throws IOException JavaDoc {
60         tag = new ProjectNavigationTag();
61         pageContext = new MockPageContext();
62         MockServletRequest request = new MockServletRequest("context", "servlet");
63         request.setLocale(Locale.US);
64         request.setPathInfo("/Project A");
65         pageContext.setHttpServletRequest(request);
66
67         tag.setPageContext(pageContext);
68         tag.setBodyContent(new MockBodyContent());
69
70         logDir = new File JavaDoc("testresults/ProjectNavigationTagTest");
71         if (!logDir.exists()) {
72             assertTrue("Failed to create test result dir", logDir.mkdir());
73         }
74         final MockServletConfig servletConfig = (MockServletConfig) pageContext.getServletConfig();
75         servletConfig.setInitParameter("logDir", logDir.getAbsolutePath());
76         
77
78         logProjects = new File JavaDoc[] { new File JavaDoc(logDir, "ProjectB"), new File JavaDoc(logDir, "Project A"),
79                                    new File JavaDoc(logDir, "First_project") };
80         for (int i = 0; i < logProjects.length; i++) {
81             if (!logProjects[i].exists()) {
82                 assertTrue("Failed to create test project directories", logProjects[i].mkdir());
83             }
84         }
85     }
86
87     protected void tearDown() throws Exception JavaDoc {
88         for (int i = 0; i < logProjects.length; i++) {
89             logProjects[i].delete();
90         }
91         logDir.delete();
92     }
93
94     public void testFindProjects() throws JspException JavaDoc {
95         String JavaDoc[] projects = tag.findProjects();
96         assertEquals(logProjects.length, projects.length);
97         assertEquals("First_project", projects[0]);
98     }
99
100         
101     public void testGetLinks() throws JspException JavaDoc {
102         assertEquals(BodyTag.EVAL_BODY_TAG, tag.doStartTag());
103         tag.doInitBody();
104         
105         assertEquals("", pageContext.getAttribute(ProjectNavigationTag.SELECTED_ATTR));
106         assertEquals("/context/", pageContext.getAttribute(ProjectNavigationTag.URL_ATTR));
107         assertEquals(ProjectNavigationTag.STATUS_PAGE_TEXT,
108                      pageContext.getAttribute(ProjectNavigationTag.LINK_TEXT_ATTR));
109         
110         assertEquals(BodyTag.EVAL_BODY_TAG, tag.doAfterBody());
111         assertEquals("", pageContext.getAttribute(ProjectNavigationTag.SELECTED_ATTR));
112         assertEquals("/context/servlet/First_project", pageContext.getAttribute(ProjectNavigationTag.URL_ATTR));
113         assertEquals("First_project", pageContext.getAttribute(NavigationTag.LINK_TEXT_ATTR));
114         
115         assertEquals(BodyTag.EVAL_BODY_TAG, tag.doAfterBody());
116         assertEquals(ProjectNavigationTag.SELECTED_ATTR_VALUE,
117                      // selected because the getPathInfo is set to this project
118
pageContext.getAttribute(ProjectNavigationTag.SELECTED_ATTR));
119         assertEquals("/context/servlet/Project A", pageContext.getAttribute(ProjectNavigationTag.URL_ATTR));
120         assertEquals("Project A", pageContext.getAttribute(NavigationTag.LINK_TEXT_ATTR));
121         
122         assertEquals(BodyTag.EVAL_BODY_TAG, tag.doAfterBody());
123         assertEquals("", pageContext.getAttribute(ProjectNavigationTag.SELECTED_ATTR));
124         assertEquals("ProjectB", pageContext.getAttribute(NavigationTag.LINK_TEXT_ATTR));
125         
126         assertEquals(Tag.SKIP_BODY, tag.doAfterBody());
127     }
128
129
130 }
131
Popular Tags