1 37 package net.sourceforge.cruisecontrol.taglib; 38 39 import java.io.File ; 40 import java.io.IOException ; 41 import java.text.DateFormat ; 42 import java.text.ParseException ; 43 import java.text.SimpleDateFormat ; 44 import java.util.Date ; 45 import java.util.Locale ; 46 47 import javax.servlet.jsp.JspException ; 48 import javax.servlet.jsp.JspTagException ; 49 import javax.servlet.jsp.tagext.BodyTag ; 50 import javax.servlet.jsp.tagext.Tag ; 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 [] logFiles; 63 private File logDir; 64 65 public void setUp() throws IOException { 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 ("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 [] { new File (logDir, "log20020222120000.xml"), new File (logDir, "log20020223120000.xml"), 83 new File (logDir, "log20020224120000.xml"), new File (logDir, "log20020225120000.xml") }; 84 for (int i = 0; i < logFiles.length; i++) { 85 File logFile = logFiles[i]; 86 logFile.createNewFile(); 87 } 88 } 89 90 protected void tearDown() throws Exception { 91 for (int i = 0; i < logFiles.length; i++) { 92 logFiles[i].delete(); 93 } 94 logDir.delete(); 95 } 96 97 private String getLinkText(String infoText) throws ParseException { 98 return tag.getLinkText(new BuildInfo(new File (infoText + ".xml"))); 99 } 100 public void testGetLinkText() throws JspTagException , ParseException { 101 assertEquals("02/22/2002 12:00:00", getLinkText("log20020222120000")); 102 103 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 , JspTagException { 110 String formatString = "dd-MMM-yyyy HH:mm:ss"; 111 tag.setDateFormat(formatString); 112 113 DateFormat inputFormat = new SimpleDateFormat ("yyyyMMddHHmmss"); 114 Date date = inputFormat.parse("20020222120000"); 115 DateFormat expectedFormat = new SimpleDateFormat (formatString); 116 String 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 { 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 { 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 { 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 { 160 tag.setStartingBuildNumber(10); 161 assertEquals(Tag.SKIP_BODY, tag.doStartTag()); 162 } 163 164 public void testGetLinksWithLowEndRange() throws Exception { 165 tag.setFinalBuildNumber(-1); 166 assertEquals(Tag.SKIP_BODY, tag.doStartTag()); 167 } 168 169 public void testGetLinksWithInvertedRange() throws Exception { 170 tag.setFinalBuildNumber(0); 171 tag.setStartingBuildNumber(10); 172 assertEquals(Tag.SKIP_BODY, tag.doStartTag()); 173 } 174 } 175 | Popular Tags |