1 37 package net.sourceforge.cruisecontrol.taglib; 38 39 import java.io.File ; 40 import java.io.IOException ; 41 import java.util.Locale ; 42 43 import javax.servlet.jsp.JspException ; 44 import javax.servlet.jsp.tagext.BodyTag ; 45 import javax.servlet.jsp.tagext.Tag ; 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 logDir; 57 private File [] logProjects; 58 59 public void setUp() throws IOException { 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 ("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 [] { new File (logDir, "ProjectB"), new File (logDir, "Project A"), 79 new File (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 { 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 { 95 String [] projects = tag.findProjects(); 96 assertEquals(logProjects.length, projects.length); 97 assertEquals("First_project", projects[0]); 98 } 99 100 101 public void testGetLinks() throws JspException { 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 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 |