View Javadoc

1   package org.apache.continuum.webdav;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.commons.io.IOUtils;
24  import org.apache.continuum.buildagent.configuration.BuildAgentConfigurationService;
25  import org.apache.jackrabbit.webdav.DavSessionProvider;
26  import org.apache.jackrabbit.webdav.WebdavRequest;
27  import org.apache.jackrabbit.webdav.WebdavRequestImpl;
28  import org.codehaus.plexus.util.Base64;
29  import org.easymock.MockControl;
30  
31  import java.io.BufferedReader;
32  import java.io.IOException;
33  import java.io.UnsupportedEncodingException;
34  import java.security.Principal;
35  import java.util.Enumeration;
36  import java.util.Hashtable;
37  import java.util.Locale;
38  import java.util.Map;
39  import javax.servlet.RequestDispatcher;
40  import javax.servlet.ServletInputStream;
41  import javax.servlet.http.Cookie;
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpSession;
44  
45  public class ContinuumBuildAgentDavSessionProviderTest
46      extends TestCase
47  {
48      private DavSessionProvider sessionProvider;
49  
50      private WebdavRequest request;
51  
52      private MockControl buildAgentConfigurationServiceControl;
53  
54      private BuildAgentConfigurationService buildAgentConfigurationService;
55  
56      @Override
57      protected void setUp()
58          throws Exception
59      {
60          super.setUp();
61  
62          buildAgentConfigurationServiceControl = MockControl.
63              createControl( BuildAgentConfigurationService.class );
64          buildAgentConfigurationService =
65              (BuildAgentConfigurationService) buildAgentConfigurationServiceControl.getMock();
66  
67          sessionProvider = new ContinuumBuildAgentDavSessionProvider( buildAgentConfigurationService );
68          request = new WebdavRequestImpl( new HttpServletRequestMock(), null );
69  
70          buildAgentConfigurationServiceControl.expectAndReturn( buildAgentConfigurationService.getSharedSecretPassword(),
71                                                                 "secret", 2 );
72  
73          buildAgentConfigurationServiceControl.replay();
74  
75      }
76  
77      public void testAttachSession()
78          throws Exception
79      {
80          assertNull( request.getDavSession() );
81  
82          sessionProvider.attachSession( request );
83  
84          buildAgentConfigurationServiceControl.verify();
85  
86          assertNotNull( request.getDavSession() );
87      }
88  
89      public void testReleaseSession()
90          throws Exception
91      {
92          assertNull( request.getDavSession() );
93  
94          sessionProvider.attachSession( request );
95  
96          buildAgentConfigurationServiceControl.verify();
97  
98          assertNotNull( request.getDavSession() );
99  
100         sessionProvider.releaseSession( request );
101         assertNull( request.getDavSession() );
102     }
103 
104     @SuppressWarnings( "unchecked" )
105     private class HttpServletRequestMock
106         implements HttpServletRequest
107     {
108         public Object getAttribute( String arg0 )
109         {
110             throw new UnsupportedOperationException( "Not supported yet." );
111         }
112 
113         public Enumeration getAttributeNames()
114         {
115             throw new UnsupportedOperationException( "Not supported yet." );
116         }
117 
118         public String getCharacterEncoding()
119         {
120             throw new UnsupportedOperationException( "Not supported yet." );
121         }
122 
123         public int getContentLength()
124         {
125             throw new UnsupportedOperationException( "Not supported yet." );
126         }
127 
128         public String getContentType()
129         {
130             throw new UnsupportedOperationException( "Not supported yet." );
131         }
132 
133         public ServletInputStream getInputStream()
134             throws IOException
135         {
136             throw new UnsupportedOperationException( "Not supported yet." );
137         }
138 
139         public String getLocalAddr()
140         {
141             throw new UnsupportedOperationException( "Not supported yet." );
142         }
143 
144         public String getLocalName()
145         {
146             throw new UnsupportedOperationException( "Not supported yet." );
147         }
148 
149         public int getLocalPort()
150         {
151             throw new UnsupportedOperationException( "Not supported yet." );
152         }
153 
154         public Locale getLocale()
155         {
156             throw new UnsupportedOperationException( "Not supported yet." );
157         }
158 
159         public Enumeration getLocales()
160         {
161             throw new UnsupportedOperationException( "Not supported yet." );
162         }
163 
164         public String getParameter( String arg0 )
165         {
166             throw new UnsupportedOperationException( "Not supported yet." );
167         }
168 
169         public Map getParameterMap()
170         {
171             throw new UnsupportedOperationException( "Not supported yet." );
172         }
173 
174         public Enumeration getParameterNames()
175         {
176             throw new UnsupportedOperationException( "Not supported yet." );
177         }
178 
179         public String[] getParameterValues( String arg0 )
180         {
181             throw new UnsupportedOperationException( "Not supported yet." );
182         }
183 
184         public String getProtocol()
185         {
186             throw new UnsupportedOperationException( "Not supported yet." );
187         }
188 
189         public BufferedReader getReader()
190             throws IOException
191         {
192             throw new UnsupportedOperationException( "Not supported yet." );
193         }
194 
195         public String getRealPath( String arg0 )
196         {
197             throw new UnsupportedOperationException( "Not supported yet." );
198         }
199 
200         public String getRemoteAddr()
201         {
202             throw new UnsupportedOperationException( "Not supported yet." );
203         }
204 
205         public String getRemoteHost()
206         {
207             throw new UnsupportedOperationException( "Not supported yet." );
208         }
209 
210         public int getRemotePort()
211         {
212             throw new UnsupportedOperationException( "Not supported yet." );
213         }
214 
215         public RequestDispatcher getRequestDispatcher( String arg0 )
216         {
217             throw new UnsupportedOperationException( "Not supported yet." );
218         }
219 
220         public String getScheme()
221         {
222             return "";
223         }
224 
225         public String getServerName()
226         {
227             throw new UnsupportedOperationException( "Not supported yet." );
228         }
229 
230         public int getServerPort()
231         {
232             throw new UnsupportedOperationException( "Not supported yet." );
233         }
234 
235         public boolean isSecure()
236         {
237             throw new UnsupportedOperationException( "Not supported yet." );
238         }
239 
240         public void removeAttribute( String arg0 )
241         {
242             throw new UnsupportedOperationException( "Not supported yet." );
243         }
244 
245         public void setAttribute( String arg0, Object arg1 )
246         {
247             throw new UnsupportedOperationException( "Not supported yet." );
248         }
249 
250         public void setCharacterEncoding( String arg0 )
251             throws UnsupportedEncodingException
252         {
253             throw new UnsupportedOperationException( "Not supported yet." );
254         }
255 
256 
257         public String getAuthType()
258         {
259             throw new UnsupportedOperationException( "Not supported yet." );
260         }
261 
262         public String getContextPath()
263         {
264             return "/";
265         }
266 
267         public Cookie[] getCookies()
268         {
269             throw new UnsupportedOperationException( "Not supported yet." );
270         }
271 
272         public long getDateHeader( String arg0 )
273         {
274             throw new UnsupportedOperationException( "Not supported yet." );
275         }
276 
277         public String getHeader( String arg0 )
278         {
279             if ( arg0 != null && arg0.equalsIgnoreCase( "authorization" ) )
280             {
281                 return getAuthorizationHeader();
282             }
283 
284             return "";
285         }
286 
287         public Enumeration getHeaderNames()
288         {
289             throw new UnsupportedOperationException( "Not supported yet." );
290         }
291 
292         public Enumeration getHeaders( String arg0 )
293         {
294             Hashtable<String, String> hashTable = new Hashtable<String, String>();
295             hashTable.put( "Authorization", getAuthorizationHeader() );
296 
297             return hashTable.elements();
298         }
299 
300         public int getIntHeader( String arg0 )
301         {
302             throw new UnsupportedOperationException( "Not supported yet." );
303         }
304 
305         public String getMethod()
306         {
307             throw new UnsupportedOperationException( "Not supported yet." );
308         }
309 
310         public String getPathInfo()
311         {
312             throw new UnsupportedOperationException( "Not supported yet." );
313         }
314 
315         public String getPathTranslated()
316         {
317             throw new UnsupportedOperationException( "Not supported yet." );
318         }
319 
320         public String getQueryString()
321         {
322             throw new UnsupportedOperationException( "Not supported yet." );
323         }
324 
325         public String getRemoteUser()
326         {
327             throw new UnsupportedOperationException( "Not supported yet." );
328         }
329 
330         public String getRequestURI()
331         {
332             return "/";
333         }
334 
335         public StringBuffer getRequestURL()
336         {
337             throw new UnsupportedOperationException( "Not supported yet." );
338         }
339 
340         public String getRequestedSessionId()
341         {
342             throw new UnsupportedOperationException( "Not supported yet." );
343         }
344 
345         public String getServletPath()
346         {
347             throw new UnsupportedOperationException( "Not supported yet." );
348         }
349 
350         public HttpSession getSession( boolean arg0 )
351         {
352             throw new UnsupportedOperationException( "Not supported yet." );
353         }
354 
355         public HttpSession getSession()
356         {
357             throw new UnsupportedOperationException( "Not supported yet." );
358         }
359 
360         public Principal getUserPrincipal()
361         {
362             throw new UnsupportedOperationException( "Not supported yet." );
363         }
364 
365         public boolean isRequestedSessionIdFromCookie()
366         {
367             throw new UnsupportedOperationException( "Not supported yet." );
368         }
369 
370         public boolean isRequestedSessionIdFromURL()
371         {
372             throw new UnsupportedOperationException( "Not supported yet." );
373         }
374 
375         public boolean isRequestedSessionIdFromUrl()
376         {
377             throw new UnsupportedOperationException( "Not supported yet." );
378         }
379 
380         public boolean isRequestedSessionIdValid()
381         {
382             throw new UnsupportedOperationException( "Not supported yet." );
383         }
384 
385         public boolean isUserInRole( String arg0 )
386         {
387             throw new UnsupportedOperationException( "Not supported yet." );
388         }
389 
390         private String getAuthorizationHeader()
391         {
392             try
393             {
394                 String encodedPassword = IOUtils.toString( Base64.encodeBase64( ":secret".getBytes() ) );
395                 return "Basic " + encodedPassword;
396             }
397             catch ( IOException e )
398             {
399                 return "";
400             }
401         }
402     }
403 }