001: // StrutsTestCase - a JUnit extension for testing Struts actions
002: // within the context of the ActionServlet.
003: // Copyright (C) 2002 Deryl Seale
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the Apache Software License as
007: // published by the Apache Software Foundation; either version 1.1
008: // of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: // Apache Software Foundation Licens for more details.
014: //
015: // You may view the full text here: http://www.apache.org/LICENSE.txt
016:
017: package servletunit.tests;
018:
019: import junit.framework.TestCase;
020: import servletunit.ServletContextSimulator;
021:
022: import java.util.Enumeration;
023: import java.io.File;
024:
025: public class TestServletContext extends TestCase {
026:
027: String contextDirectory = "src/examples";
028: ServletContextSimulator context;
029:
030: public TestServletContext(String testName) {
031: super (testName);
032: }
033:
034: public void setUp() {
035: context = new ServletContextSimulator();
036: }
037:
038: public void testSetAttribute() {
039: context.setAttribute("test", "testValue");
040: assertEquals("testValue", context.getAttribute("test"));
041: }
042:
043: public void testNoAttribute() {
044: assertNull(context.getAttribute("badValue"));
045: }
046:
047: public void testGetAttributeNames() {
048: context.setAttribute("test", "testValue");
049: context.setAttribute("another", "anotherValue");
050: assertEquals("testValue", context.getAttribute("test"));
051: assertEquals("anotherValue", context.getAttribute("another"));
052: Enumeration names = context.getAttributeNames();
053: boolean fail = true;
054: while (names.hasMoreElements()) {
055: fail = true;
056: String name = (String) names.nextElement();
057: if ((name.equals("test")) || (name.equals("another")))
058: fail = false;
059: }
060: if (fail)
061: fail();
062: }
063:
064: public void testGetRealPath() {
065: File file = new File(System.getProperty("basedir"));
066: context.setContextDirectory(file);
067: assertEquals(new File(file, "test.html").getAbsolutePath(),
068: context.getRealPath("/test.html"));
069: }
070:
071: public void testGetRealPathNotSet() {
072: context.setContextDirectory(null);
073: assertNull(context.getRealPath("/test.html"));
074: }
075:
076: /**
077: * verifies that web.xml can be loaded
078: * using the classpath
079: */
080: public void testGetResourceAsStreamFromClasspath() {
081: assertNotNull("resource was not found", context
082: .getResourceAsStream("/WEB-INF/web.xml"));
083: }
084:
085: /**
086: * verified that web.xml can be loaded as a URL using the classpath
087: * @throws Exception
088: */
089: public void testGetResourceFromClasspath() throws Exception {
090: assertNotNull("resource was not found", context
091: .getResource("/WEB-INF/web.xml"));
092: }
093:
094: /**
095: * verifies that web.xml can be loaded
096: * using the filesystem. Assumes test is being run from
097: * the strutstestcase project root directory
098: * and that WEB-INF is in src/examples
099: */
100: public void testGetResourceAsStreamFromFileSystem() {
101: context.setContextDirectory(new File(contextDirectory));
102: assertNotNull("resource was not found", context
103: .getResourceAsStream("/WEB-INF/web.xml"));
104: }
105:
106: /**
107: * verified that web.xml can be loaded as a URL using the classpath
108: * @throws Exception
109: */
110: public void testGetResourceFromFileSystem() throws Exception {
111: context.setContextDirectory(new File(contextDirectory));
112: assertNotNull("resource was not found", context
113: .getResource("/WEB-INF/web.xml"));
114: }
115:
116: /**
117: * confirms that calls to getResource will
118: * adjust to a path missing the leading "/"
119: * @throws Exception
120: */
121: public void testGetResourceFromFileSystemWithPathCorrection()
122: throws Exception {
123: context.setContextDirectory(new File(contextDirectory));
124: assertNotNull("resource was not found", context
125: .getResource("WEB-INF/web.xml"));
126: }
127:
128: /**
129: * confirms that calls to getResource will
130: * adjust to a path missing the leading "/"
131: * @throws Exception
132: */
133: public void testGetResourceFromClasspathWithPathCorrection()
134: throws Exception {
135: assertNotNull("resource was not found", context
136: .getResource("WEB-INF/web.xml"));
137: }
138:
139: /**
140: * confirms that calls to getResource will
141: * adjust to a path missing the leading "/"
142: * @throws Exception
143: */
144: public void testGetResourceAsStreamFromFileSystemWithPathCorrection()
145: throws Exception {
146: context.setContextDirectory(new File(contextDirectory));
147: assertNotNull("resource was not found", context
148: .getResourceAsStream("WEB-INF/web.xml"));
149: }
150:
151: /**
152: * confirms that calls to getResource will
153: * adjust to a path missing the leading "/"
154: * @throws Exception
155: */
156: public void testGetResourceAsStreamFromClasspathWithPathCorrection()
157: throws Exception {
158: assertNotNull("resource was not found", context
159: .getResourceAsStream("WEB-INF/web.xml"));
160: }
161:
162: /**
163: * verifies that web.xml can be loaded
164: * using the filesystem. Assumes test is being run from
165: * the strutstestcase project root directory
166: * and that WEB-INF is in src/examples
167: * this is necessary because the other tests could be "fooled"
168: * by a file that was actually loaded by the classloader.
169: */
170: public void testGetResourceAsFileFromFileSystem() {
171: context.setContextDirectory(new File(contextDirectory));
172: File file = context.getResourceAsFile("/WEB-INF/web.xml");
173: assertNotNull("resource was not found", file);
174: assertTrue("resource was not found", file.exists());
175: }
176:
177: /**
178: * verifies that web.xml can be loaded
179: * using the filesystem. Assumes test is being run from
180: * the strutstestcase project root directory
181: * and that WEB-INF is in src/examples
182: * this is necessary because the other tests could be "fooled"
183: * by a file that was actually loaded by the classloader.
184: */
185: public void testGetResourceAsFileFromFileSystemWithRelativePath() {
186: File file = context
187: .getResourceAsFile("/src/examples/WEB-INF/web.xml");
188: assertNotNull("resource was not found", file);
189: assertTrue("resource was not found", file.exists());
190: }
191:
192: }
|