Create parser pool interface and move current pool to an implementation of this inter...
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / idp / IdpTestCase.java
1 /*
2  * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package edu.internet2.middleware.shibboleth.idp;
17
18 import java.io.File;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21
22 import junit.framework.TestCase;
23
24 import com.mockrunner.mock.web.MockFilterConfig;
25 import com.mockrunner.mock.web.MockHttpServletRequest;
26 import com.mockrunner.mock.web.MockHttpServletResponse;
27 import com.mockrunner.mock.web.MockServletContext;
28 import com.mockrunner.mock.web.WebMockObjectFactory;
29 import com.mockrunner.servlet.ServletTestModule;
30
31 import edu.internet2.middleware.shibboleth.utils.FileUtils;
32
33 /**
34  * Base class for Shibboleth IdP MockObject unit tests.
35  * 
36  * @author Will Norris (wnorris@memphis.edu)
37  */
38 public abstract class IdpTestCase extends TestCase {
39
40         // The Factory creates the Request, Response, Session, etc.
41         WebMockObjectFactory factory = new WebMockObjectFactory();
42
43         // The TestModule runs the Servlet and Filter methods in the simulated
44         // container
45         ServletTestModule testModule = new ServletTestModule(factory);
46
47         // Now simulated Servlet API objects
48         MockServletContext servletContext = factory.getMockServletContext();
49
50         MockFilterConfig filterConfig = factory.getMockFilterConfig();
51
52         MockHttpServletResponse response = factory.getMockResponse();
53
54         MockHttpServletRequest request = factory.getMockRequest();
55
56         protected void setUp() throws Exception {
57                 super.setUp();
58
59                 // ServletContext (argument to Filters and Servlets)
60                 servletContext.setServletContextName("Shibboleth Test Context");
61                 servletContext.setInitParameter("IdPConfigFile", new File(tmpIdpHome()
62                                 + "/etc/idp.xml").toURL().toString());
63                 // testModule.setServlet(sso);
64         }
65
66         /**
67          * Start the IdP servlet using the given config directory and reset request
68          * and response objects
69          * 
70          * @param configDir
71          *            this directory will be copied to IDP_HOME/etc
72          * @throws IOException
73          */
74         void resetServlet(String configDir) throws IOException {
75                 // setup config directory and initialize servlet
76                 prepareConfigDir(new File(configDir));
77                 IdPConfigLoader.reset();
78                 testModule.createServlet(IdPResponder.class);
79
80                 // reset request and response objects
81                 request.clearAttributes();
82                 request.clearParameters();
83                 response.reset();
84         }
85
86         /**
87          * Copy configDir to IDP_HOME/etc. Any instances of the string "$IDP_HOME$"
88          * in the file idp.xml will be replaced with the current IdP home directory
89          * 
90          * @param configDir
91          * @throws IOException
92          */
93         private void prepareConfigDir(File configDir) throws IOException {
94                 try {
95                         FileUtils.forceDelete(new File(tmpIdpHome() + "/etc"));
96                 } catch (FileNotFoundException fnf) {
97                         // directory doesn't exist... no big deal
98                 }
99
100                 FileUtils.copyDirectory(configDir, new File(tmpIdpHome() + "/etc"));
101                 new File(tmpIdpHome() + "/logs").mkdir();
102
103                 FileUtils.replaceString(new File(tmpIdpHome() + "/etc/idp.xml"),
104                                 "\\$IDP_HOME\\$", new File(tmpIdpHome()).toURL().toString());
105         }
106
107         /**
108          * Get a temporary directory to be used as IDP_HOME during testing.
109          * 
110          * @return
111          */
112         private String tmpIdpHome() {
113                 // TODO: ideally this should check for a TMP environment variable, or at
114                 // least return a platform appropriate directory. Fortunately, /tmp is
115                 // properly converted to C:\tmp in Windows,
116                 // so the following should still work across platforms
117                 return "/tmp/shibboleth-idp";
118         }
119
120         /**
121          * Test two SAML response bodies for equality. Because many items in a SAML
122          * response are generated at runtime (such as ResponseID, IssueInstant,
123          * etc), an exact string match is not possible. To handle this, the expected
124          * string should be a regular expression which will be used to match against
125          * the received string. Any extra whitespace and any whitespace between XML
126          * tags will be ignored.
127          * 
128          * @param expected
129          *            regular expression used to match against the received string
130          * @param received
131          *            HTTP body of received response
132          * @return
133          */
134         boolean responsesAreEqual(String expected, String received) {
135                 // ignore extra whitespace
136                 String exp = expected.replaceAll("\\s+", " ");
137                 String rec = received.replaceAll("\\s+", " ");
138
139                 // ignore whitespace between tags
140                 exp = exp.replaceAll("\\s*(>|<)\\s*", "$1");
141                 rec = rec.replaceAll("\\s*(>|<)\\s*", "$1");
142
143                 // System.out.println("exp = " + exp);
144                 // System.out.println("rec = " + rec);
145
146                 return rec.matches(exp);
147         }
148
149 }