Skip to content

Commit 82d09f7

Browse files
committed
Added a unit test for the Servlet 3.0 implementation of ServletRegistrationProvider
1 parent 39004c9 commit 82d09f7

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
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 org.ocpsoft.rewrite.servlet.impl;
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertNull;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.when;
23+
24+
import java.util.Arrays;
25+
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
import javax.servlet.ServletContext;
30+
31+
import org.junit.Test;
32+
import org.ocpsoft.rewrite.servlet.ServletRegistration;
33+
34+
@SuppressWarnings({ "unchecked", "rawtypes" })
35+
public class Servlet3ServletRegistrationProviderTest
36+
{
37+
38+
@Test
39+
public void testShouldReturnNullForOldServletVersions()
40+
{
41+
// GIVEN a Servlet 2.5 container
42+
ServletContext servletContext = mock(ServletContext.class);
43+
when(servletContext.getMajorVersion()).thenReturn(2);
44+
45+
// WHEN the provider is asked for the registrations
46+
Servlet3ServletRegistrationProvider provider = new Servlet3ServletRegistrationProvider();
47+
List<ServletRegistration> registrations = provider.getServletRegistrations(servletContext);
48+
49+
// THEN it must return null
50+
assertNull(registrations);
51+
52+
}
53+
54+
@Test
55+
public void testShouldReturnEmptyListForNoRegistrations()
56+
{
57+
// GIVEN a Servlet 3.0 container
58+
ServletContext servletContext = mock(ServletContext.class);
59+
when(servletContext.getMajorVersion()).thenReturn(3);
60+
61+
// AND no registrations
62+
when(servletContext.getServletRegistrations()).thenReturn(new HashMap());
63+
64+
// WHEN the provider is asked for the registrations
65+
Servlet3ServletRegistrationProvider provider = new Servlet3ServletRegistrationProvider();
66+
List<ServletRegistration> result = provider.getServletRegistrations(servletContext);
67+
68+
// THEN it must return an empty list
69+
assertNotNull(result);
70+
assertEquals(0, result.size());
71+
72+
}
73+
74+
@Test
75+
public void testShouldReturnCorrectRegistrationWithExistingRegistrations()
76+
{
77+
// GIVEN a Servlet 3.0 container
78+
ServletContext servletContext = mock(ServletContext.class);
79+
when(servletContext.getMajorVersion()).thenReturn(3);
80+
81+
// AND one registration
82+
javax.servlet.ServletRegistration registration = mock(javax.servlet.ServletRegistration.class);
83+
when(registration.getClassName()).thenReturn("com.example.MyServlet");
84+
when(registration.getMappings()).thenReturn(Arrays.asList("*.jsf", "/faces/*"));
85+
Map registrationMap = new HashMap();
86+
registrationMap.put("test", registration);
87+
when(servletContext.getServletRegistrations()).thenReturn(registrationMap);
88+
89+
// WHEN the provider is asked for the registrations
90+
Servlet3ServletRegistrationProvider provider = new Servlet3ServletRegistrationProvider();
91+
List<ServletRegistration> result = provider.getServletRegistrations(servletContext);
92+
93+
// THEN it must return the correct registration
94+
assertNotNull(result);
95+
assertEquals(1, result.size());
96+
assertEquals("com.example.MyServlet", result.get(0).getClassName());
97+
assertEquals(Arrays.asList("*.jsf", "/faces/*"), result.get(0).getMappings());
98+
99+
}
100+
101+
}

0 commit comments

Comments
 (0)