1 20 package org.apache.cactus.server.runner; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 25 import javax.servlet.ServletConfig ; 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.UnavailableException ; 29 30 import com.mockobjects.dynamic.C; 31 import com.mockobjects.dynamic.Mock; 32 33 import junit.framework.TestCase; 34 35 40 public final class TestServletTestRunner extends TestCase 41 { 42 45 private Mock mockServletConfig; 46 47 50 private ServletConfig servletConfig; 51 52 55 private Mock mockServletContext; 56 57 60 private ServletContext servletContext; 61 62 65 private ServletTestRunner runner; 66 67 70 protected void setUp() 71 { 72 mockServletConfig = new Mock(ServletConfig .class); 73 servletConfig = (ServletConfig ) mockServletConfig.proxy(); 74 75 mockServletContext = new Mock(ServletContext .class); 76 servletContext = (ServletContext ) mockServletContext.proxy(); 77 78 mockServletConfig.matchAndReturn("getServletContext", servletContext); 79 mockServletConfig.matchAndReturn("getServletName", "TestServlet"); 80 mockServletContext.expect("log", C.ANY_ARGS); 81 82 runner = new ServletTestRunner(); 83 } 84 85 91 public void testInitWhenNoXslStylesheet() throws ServletException 92 { 93 mockServletConfig.expectAndReturn("getInitParameter", 94 "xsl-stylesheet", null); 95 96 runner.init(servletConfig); 97 } 98 99 106 public void testInitWhenXslStylesheetNotFound() throws ServletException 107 { 108 mockServletConfig.expectAndReturn("getInitParameter", 109 "xsl-stylesheet", "some-stylesheet.xsl"); 110 mockServletContext.expectAndReturn("getResourceAsStream", C.ANY_ARGS, 111 null); 112 113 try 114 { 115 runner.init(servletConfig); 116 fail("Should have thrown an UnavailableException exception"); 117 } 118 catch (UnavailableException expected) 119 { 120 assertEquals("The initialization parameter 'xsl-stylesheet' does " 121 + "not refer to an existing resource", expected.getMessage()); 122 } 123 } 124 125 131 public void testInitWithXslStylesheet() throws ServletException 132 { 133 mockServletConfig.expectAndReturn("getInitParameter", 134 "xsl-stylesheet", "some-stylesheet.xsl"); 135 136 InputStream mockInputStream = new InputStream () 137 { 138 private int counter = 0; 139 140 private static final String CONTENT = "" 141 + "<xsl:stylesheet xmlns:xsl=\"" 142 + "http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" 143 + "</xsl:stylesheet>"; 144 145 public int read() throws IOException 146 { 147 while (counter < CONTENT.length()) 148 { 149 return CONTENT.charAt(counter++); 150 } 151 return -1; 152 } 153 }; 154 155 mockServletContext.expectAndReturn("getResourceAsStream", C.ANY_ARGS, 156 mockInputStream); 157 158 161 runner.init(servletConfig); 162 } 163 164 } 165 | Popular Tags |