1 17 package servletunit.tests; 18 19 import junit.framework.TestCase; 20 import servletunit.ServletContextSimulator; 21 22 import java.util.Enumeration ; 23 import java.io.File ; 24 25 public class TestServletContext extends TestCase { 26 27 String contextDirectory = "src/examples"; 28 ServletContextSimulator context; 29 30 public TestServletContext(String testName) { 31 super(testName); 32 } 33 34 public void setUp() { 35 context = new ServletContextSimulator(); 36 } 37 38 public void testSetAttribute() { 39 context.setAttribute("test","testValue"); 40 assertEquals("testValue",context.getAttribute("test")); 41 } 42 43 public void testNoAttribute() { 44 assertNull(context.getAttribute("badValue")); 45 } 46 47 public void testGetAttributeNames() { 48 context.setAttribute("test","testValue"); 49 context.setAttribute("another","anotherValue"); 50 assertEquals("testValue",context.getAttribute("test")); 51 assertEquals("anotherValue",context.getAttribute("another")); 52 Enumeration names = context.getAttributeNames(); 53 boolean fail = true; 54 while (names.hasMoreElements()) { 55 fail = true; 56 String name = (String ) names.nextElement(); 57 if ((name.equals("test")) || (name.equals("another"))) 58 fail = false; 59 } 60 if (fail) 61 fail(); 62 } 63 64 public void testGetRealPath() { 65 File file = new File (System.getProperty("basedir")); 66 context.setContextDirectory(file); 67 assertEquals(new File (file,"test.html").getAbsolutePath(),context.getRealPath("/test.html")); 68 } 69 70 public void testGetRealPathNotSet() { 71 context.setContextDirectory(null); 72 assertNull(context.getRealPath("/test.html")); 73 } 74 75 79 public void testGetResourceAsStreamFromClasspath(){ 80 assertNotNull("resource was not found", context.getResourceAsStream("/WEB-INF/web.xml")); 81 } 82 83 87 public void testGetResourceFromClasspath() throws Exception { 88 assertNotNull("resource was not found", context.getResource("/WEB-INF/web.xml")); 89 } 90 91 97 public void testGetResourceAsStreamFromFileSystem(){ 98 context.setContextDirectory(new File (contextDirectory)); 99 assertNotNull("resource was not found", context.getResourceAsStream("/WEB-INF/web.xml")); 100 } 101 102 106 public void testGetResourceFromFileSystem() throws Exception { 107 context.setContextDirectory(new File (contextDirectory)); 108 assertNotNull("resource was not found", context.getResource("/WEB-INF/web.xml")); 109 } 110 111 116 public void testGetResourceFromFileSystemWithPathCorrection() throws Exception { 117 context.setContextDirectory(new File (contextDirectory)); 118 assertNotNull("resource was not found", context.getResource("WEB-INF/web.xml")); 119 } 120 121 126 public void testGetResourceFromClasspathWithPathCorrection() throws Exception { 127 assertNotNull("resource was not found", context.getResource("WEB-INF/web.xml")); 128 } 129 130 135 public void testGetResourceAsStreamFromFileSystemWithPathCorrection() throws Exception { 136 context.setContextDirectory(new File (contextDirectory)); 137 assertNotNull("resource was not found", context.getResourceAsStream("WEB-INF/web.xml")); 138 } 139 140 145 public void testGetResourceAsStreamFromClasspathWithPathCorrection() throws Exception { 146 assertNotNull("resource was not found", context.getResourceAsStream("WEB-INF/web.xml")); 147 } 148 149 157 public void testGetResourceAsFileFromFileSystem(){ 158 context.setContextDirectory(new File (contextDirectory)); 159 File file = context.getResourceAsFile("/WEB-INF/web.xml"); 160 assertNotNull("resource was not found", file); 161 assertTrue("resource was not found", file.exists()); 162 } 163 164 172 public void testGetResourceAsFileFromFileSystemWithRelativePath(){ 173 File file = context.getResourceAsFile("/src/examples/WEB-INF/web.xml"); 174 assertNotNull("resource was not found", file); 175 assertTrue("resource was not found", file.exists()); 176 } 177 178 179 180 181 182 183 } 184 | Popular Tags |