KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servletunit > tests > TestServletContext


1 // StrutsTestCase - a JUnit extension for testing Struts actions
2
// within the context of the ActionServlet.
3
// Copyright (C) 2002 Deryl Seale
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the Apache Software License as
7
// published by the Apache Software Foundation; either version 1.1
8
// of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// Apache Software Foundation Licens for more details.
14
//
15
// You may view the full text here: http://www.apache.org/LICENSE.txt
16

17 package servletunit.tests;
18
19 import junit.framework.TestCase;
20 import servletunit.ServletContextSimulator;
21
22 import java.util.Enumeration JavaDoc;
23 import java.io.File JavaDoc;
24
25 public class TestServletContext extends TestCase {
26
27     String JavaDoc contextDirectory = "src/examples";
28     ServletContextSimulator context;
29
30     public TestServletContext(String JavaDoc 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 JavaDoc names = context.getAttributeNames();
53         boolean fail = true;
54         while (names.hasMoreElements()) {
55             fail = true;
56             String JavaDoc name = (String JavaDoc) 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 JavaDoc file = new File JavaDoc(System.getProperty("basedir"));
66         context.setContextDirectory(file);
67         assertEquals(new File JavaDoc(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     /**
76      * verifies that web.xml can be loaded
77      * using the classpath
78      */

79     public void testGetResourceAsStreamFromClasspath(){
80         assertNotNull("resource was not found", context.getResourceAsStream("/WEB-INF/web.xml"));
81     }
82
83     /**
84      * verified that web.xml can be loaded as a URL using the classpath
85      * @throws Exception
86      */

87     public void testGetResourceFromClasspath() throws Exception JavaDoc{
88         assertNotNull("resource was not found", context.getResource("/WEB-INF/web.xml"));
89     }
90
91     /**
92      * verifies that web.xml can be loaded
93      * using the filesystem. Assumes test is being run from
94      * the strutstestcase project root directory
95      * and that WEB-INF is in src/examples
96      */

97     public void testGetResourceAsStreamFromFileSystem(){
98         context.setContextDirectory(new File JavaDoc(contextDirectory));
99         assertNotNull("resource was not found", context.getResourceAsStream("/WEB-INF/web.xml"));
100     }
101
102     /**
103      * verified that web.xml can be loaded as a URL using the classpath
104      * @throws Exception
105      */

106     public void testGetResourceFromFileSystem() throws Exception JavaDoc{
107         context.setContextDirectory(new File JavaDoc(contextDirectory));
108         assertNotNull("resource was not found", context.getResource("/WEB-INF/web.xml"));
109     }
110
111     /**
112      * confirms that calls to getResource will
113      * adjust to a path missing the leading "/"
114      * @throws Exception
115      */

116     public void testGetResourceFromFileSystemWithPathCorrection() throws Exception JavaDoc{
117         context.setContextDirectory(new File JavaDoc(contextDirectory));
118         assertNotNull("resource was not found", context.getResource("WEB-INF/web.xml"));
119     }
120
121     /**
122      * confirms that calls to getResource will
123      * adjust to a path missing the leading "/"
124      * @throws Exception
125      */

126     public void testGetResourceFromClasspathWithPathCorrection() throws Exception JavaDoc{
127         assertNotNull("resource was not found", context.getResource("WEB-INF/web.xml"));
128     }
129
130     /**
131      * confirms that calls to getResource will
132      * adjust to a path missing the leading "/"
133      * @throws Exception
134      */

135     public void testGetResourceAsStreamFromFileSystemWithPathCorrection() throws Exception JavaDoc{
136         context.setContextDirectory(new File JavaDoc(contextDirectory));
137         assertNotNull("resource was not found", context.getResourceAsStream("WEB-INF/web.xml"));
138     }
139
140     /**
141      * confirms that calls to getResource will
142      * adjust to a path missing the leading "/"
143      * @throws Exception
144      */

145     public void testGetResourceAsStreamFromClasspathWithPathCorrection() throws Exception JavaDoc{
146         assertNotNull("resource was not found", context.getResourceAsStream("WEB-INF/web.xml"));
147     }
148
149     /**
150      * verifies that web.xml can be loaded
151      * using the filesystem. Assumes test is being run from
152      * the strutstestcase project root directory
153      * and that WEB-INF is in src/examples
154      * this is necessary because the other tests could be "fooled"
155      * by a file that was actually loaded by the classloader.
156      */

157     public void testGetResourceAsFileFromFileSystem(){
158         context.setContextDirectory(new File JavaDoc(contextDirectory));
159         File JavaDoc 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     /**
165      * verifies that web.xml can be loaded
166      * using the filesystem. Assumes test is being run from
167      * the strutstestcase project root directory
168      * and that WEB-INF is in src/examples
169      * this is necessary because the other tests could be "fooled"
170      * by a file that was actually loaded by the classloader.
171      */

172     public void testGetResourceAsFileFromFileSystemWithRelativePath(){
173         File JavaDoc 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