KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.DateFormat JavaDoc;
42 import java.text.ParseException JavaDoc;
43 import java.text.SimpleDateFormat JavaDoc;
44 import java.util.Date JavaDoc;
45 import java.util.Locale JavaDoc;
46
47 import javax.servlet.jsp.JspException JavaDoc;
48 import javax.servlet.jsp.JspTagException JavaDoc;
49 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
50 import javax.servlet.jsp.tagext.Tag JavaDoc;
51
52 import junit.framework.TestCase;
53 import net.sourceforge.cruisecontrol.BuildInfo;
54 import net.sourceforge.cruisecontrol.mock.MockBodyContent;
55 import net.sourceforge.cruisecontrol.mock.MockPageContext;
56 import net.sourceforge.cruisecontrol.mock.MockServletConfig;
57 import net.sourceforge.cruisecontrol.mock.MockServletRequest;
58
59 public class NavigationTagTest extends TestCase {
60     private NavigationTag tag;
61     private MockPageContext pageContext;
62     private File JavaDoc[] logFiles;
63     private File JavaDoc logDir;
64
65     public void setUp() throws IOException JavaDoc {
66         tag = new NavigationTag();
67         pageContext = new MockPageContext();
68         MockServletRequest request = new MockServletRequest("context", "servlet");
69         request.setLocale(Locale.US);
70         pageContext.setHttpServletRequest(request);
71
72         tag.setPageContext(pageContext);
73         tag.setBodyContent(new MockBodyContent());
74
75         logDir = new File JavaDoc("testresults/NavigationTagTest");
76         if (!logDir.exists()) {
77             assertTrue("Failed to create test result dir", logDir.mkdirs());
78         }
79         final MockServletConfig servletConfig = (MockServletConfig) pageContext.getServletConfig();
80         servletConfig.setInitParameter("logDir", logDir.getAbsolutePath());
81
82         logFiles = new File JavaDoc[] { new File JavaDoc(logDir, "log20020222120000.xml"), new File JavaDoc(logDir, "log20020223120000.xml"),
83                                 new File JavaDoc(logDir, "log20020224120000.xml"), new File JavaDoc(logDir, "log20020225120000.xml") };
84         for (int i = 0; i < logFiles.length; i++) {
85             File JavaDoc logFile = logFiles[i];
86             logFile.createNewFile();
87         }
88     }
89
90     protected void tearDown() throws Exception JavaDoc {
91         for (int i = 0; i < logFiles.length; i++) {
92             logFiles[i].delete();
93         }
94         logDir.delete();
95     }
96
97     private String JavaDoc getLinkText(String JavaDoc infoText) throws ParseException JavaDoc {
98         return tag.getLinkText(new BuildInfo(new File JavaDoc(infoText + ".xml")));
99     }
100     public void testGetLinkText() throws JspTagException JavaDoc, ParseException JavaDoc {
101         assertEquals("02/22/2002 12:00:00", getLinkText("log20020222120000"));
102
103         // XXX Do we want to support log files without seconds?
104
//assertEquals("02/22/2002 12:00:00", getLinkText("log200202221200"));
105
assertEquals("02/22/2002 12:00:00 (3.11)", getLinkText("log20020222120000L3.11"));
106         assertEquals("02/22/2002 12:00:00 (L.0)", getLinkText("log20020222120000LL.0"));
107     }
108
109     public void testGetFormattedLinkText() throws ParseException JavaDoc, JspTagException JavaDoc {
110         String JavaDoc formatString = "dd-MMM-yyyy HH:mm:ss";
111         tag.setDateFormat(formatString);
112         
113         DateFormat JavaDoc inputFormat = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
114         Date JavaDoc date = inputFormat.parse("20020222120000");
115         DateFormat JavaDoc expectedFormat = new SimpleDateFormat JavaDoc(formatString);
116         String JavaDoc expectedDate = expectedFormat.format(date);
117
118         assertEquals(expectedDate, getLinkText("log20020222120000"));
119         assertEquals(expectedDate + " (3.11)", getLinkText("log20020222120000L3.11"));
120     }
121     
122     public void testGetLinks() throws JspException JavaDoc {
123         assertEquals(BodyTag.EVAL_BODY_TAG, tag.doStartTag());
124         tag.doInitBody();
125         assertEquals("02/25/2002 12:00:00", pageContext.getAttribute(NavigationTag.LINK_TEXT_ATTR));
126         assertEquals("log20020225120000", pageContext.getAttribute(NavigationTag.LOG_FILE_ATTR));
127         assertEquals("/context/servlet?log=log20020225120000", pageContext.getAttribute(NavigationTag.URL_ATTR));
128         assertEquals("02/25/2002 12:00:00", pageContext.getAttribute(NavigationTag.LINK_TEXT_ATTR));
129         assertEquals(BodyTag.EVAL_BODY_TAG, tag.doAfterBody());
130         assertEquals("02/24/2002 12:00:00", pageContext.getAttribute(NavigationTag.LINK_TEXT_ATTR));
131         assertEquals(BodyTag.EVAL_BODY_TAG, tag.doAfterBody());
132         assertEquals("02/23/2002 12:00:00", pageContext.getAttribute(NavigationTag.LINK_TEXT_ATTR));
133         assertEquals(BodyTag.EVAL_BODY_TAG, tag.doAfterBody());
134         assertEquals("02/22/2002 12:00:00", pageContext.getAttribute(NavigationTag.LINK_TEXT_ATTR));
135         assertEquals(Tag.SKIP_BODY, tag.doAfterBody());
136     }
137
138     public void testGetLinksWithSubRange() throws Exception JavaDoc {
139         tag.setStartingBuildNumber(1);
140         tag.setFinalBuildNumber(2);
141
142         assertEquals(BodyTag.EVAL_BODY_TAG, tag.doStartTag());
143         tag.doInitBody();
144         assertEquals("02/24/2002 12:00:00", pageContext.getAttribute(NavigationTag.LINK_TEXT_ATTR));
145         assertEquals(BodyTag.EVAL_BODY_TAG, tag.doAfterBody());
146         assertEquals("02/23/2002 12:00:00", pageContext.getAttribute(NavigationTag.LINK_TEXT_ATTR));
147         assertEquals(Tag.SKIP_BODY, tag.doAfterBody());
148     }
149
150     public void testGetLinksWithRangeSizeOne() throws Exception JavaDoc {
151         tag.setStartingBuildNumber(1);
152         tag.setFinalBuildNumber(1);
153         assertEquals(BodyTag.EVAL_BODY_TAG, tag.doStartTag());
154         tag.doInitBody();
155         assertEquals("02/24/2002 12:00:00", pageContext.getAttribute(NavigationTag.LINK_TEXT_ATTR));
156         assertEquals(BodyTag.SKIP_BODY, tag.doAfterBody());
157     }
158
159     public void testGetLinksWithLargeStartRange() throws Exception JavaDoc {
160         tag.setStartingBuildNumber(10);
161         assertEquals(Tag.SKIP_BODY, tag.doStartTag());
162     }
163
164     public void testGetLinksWithLowEndRange() throws Exception JavaDoc {
165         tag.setFinalBuildNumber(-1);
166         assertEquals(Tag.SKIP_BODY, tag.doStartTag());
167     }
168
169     public void testGetLinksWithInvertedRange() throws Exception JavaDoc {
170         tag.setFinalBuildNumber(0);
171         tag.setStartingBuildNumber(10);
172         assertEquals(Tag.SKIP_BODY, tag.doStartTag());
173     }
174 }
175
Popular Tags