View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe;
12  
13  import static org.junit.jupiter.api.Assertions.assertEquals;
14  import static org.junit.jupiter.api.Assertions.assertFalse;
15  import static org.junit.jupiter.api.Assertions.assertNotNull;
16  import static org.junit.jupiter.api.Assertions.assertNull;
17  import static org.junit.jupiter.api.Assertions.assertTrue;
18  
19  import jakarta.servlet.ServletContext;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Enumeration;
25  import java.util.List;
26  
27  import org.apache.catalina.Context;
28  import org.apache.catalina.Valve;
29  import org.apache.catalina.WebResource;
30  import org.apache.catalina.WebResourceRoot;
31  import org.apache.catalina.deploy.NamingResourcesImpl;
32  import org.apache.jasper.EmbeddedServletOptions;
33  import org.apache.jasper.JspCompilationContext;
34  import org.apache.tomcat.util.descriptor.web.ApplicationParameter;
35  import org.apache.tomcat.util.descriptor.web.ContextResource;
36  import org.apache.tomcat.util.descriptor.web.ContextResourceLink;
37  import org.apache.tomcat.util.descriptor.web.FilterDef;
38  import org.apache.tomcat.util.descriptor.web.FilterMap;
39  import org.junit.jupiter.api.Disabled;
40  import org.junit.jupiter.api.Test;
41  import org.junit.jupiter.api.extension.ExtendWith;
42  import org.junit.jupiter.params.ParameterizedTest;
43  import org.junit.jupiter.params.provider.ValueSource;
44  import org.mockito.Mock;
45  import org.mockito.Mockito;
46  import org.mockito.junit.jupiter.MockitoExtension;
47  
48  import psiprobe.model.ApplicationResource;
49  
50  /**
51   * The Class Tomcat11ContainerAdapterTest.
52   */
53  @ExtendWith(MockitoExtension.class)
54  class Tomcat11ContainerAdapterTest {
55  
56    /** The context. */
57    @Mock
58    Context context;
59  
60    // * The embedded servlet options. */
61    @Mock
62    EmbeddedServletOptions options;
63  
64    /**
65     * Creates the valve.
66     */
67    @Test
68    void createValve() {
69      final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
70      Valve valve = adapter.createValve();
71      assertEquals("Tomcat11AgentValve[Container is null]", valve.toString());
72    }
73  
74    /**
75     * Can bound to null.
76     */
77    @Test
78    void canBoundToNull() {
79      final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
80      assertFalse(adapter.canBoundTo(null));
81    }
82  
83    /**
84     * Can bound to tomcat11.
85     */
86    @Test
87    void canBoundToTomcat11() {
88      final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
89      assertTrue(adapter.canBoundTo("Apache Tomcat/11.0"));
90    }
91  
92    /**
93     * Can bound to tomee 11.0, nsjsp 11.0, vmware tc 11.0.
94     */
95    // TODO Not yet supported
96    @Disabled
97    @ParameterizedTest
98    @ValueSource(strings = {"Apache Tomcat (TomEE)/11.0",
99        "NonStop(tm) Servlets For JavaServer Pages(tm) v11.0", "Vmware tc..../11.0"})
100   void canBoundTo(String container) {
101     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
102     assertTrue(adapter.canBoundTo(container));
103   }
104 
105   /**
106    * Can bound to other.
107    */
108   @Test
109   void canBoundToOther() {
110     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
111     assertFalse(adapter.canBoundTo("Other"));
112   }
113 
114   /**
115    * Filter mappings.
116    */
117   @Test
118   void filterMappings() {
119     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
120     FilterMap map = new FilterMap();
121     map.addServletName("psi-probe");
122     map.addURLPattern("/psi-probe");
123     assertEquals(2, adapter.getFilterMappings(map, "dispatcherMap", "filterClass").size());
124   }
125 
126   /**
127    * Creates the jsp compilation context.
128    */
129   @Test
130   void createJspCompilationContext() {
131     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
132     Mockito.when(this.options.getGeneratedJspPackageName()).thenReturn("org.apache.jsp");
133     JspCompilationContext jspContext = adapter.createJspCompilationContext("name", this.options,
134         null, null, ClassLoader.getSystemClassLoader());
135     assertEquals("org.apache.jsp.name", jspContext.getFQCN());
136   }
137 
138   /**
139    * Adds the context resource link.
140    */
141   @Test
142   void addContextResourceLink() {
143     NamingResourcesImpl namingResources = Mockito.mock(NamingResourcesImpl.class);
144     Mockito.when(context.getNamingResources()).thenReturn(namingResources);
145     Mockito.when(namingResources.findResourceLinks())
146         .thenReturn(new ContextResourceLink[] {new ContextResourceLink()});
147 
148     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
149     final List<ApplicationResource> list = new ArrayList<ApplicationResource>();
150     adapter.addContextResourceLink(context, list);
151     assertFalse(list.isEmpty());
152   }
153 
154   /**
155    * Adds the context resource.
156    */
157   @Test
158   void addContextResource() {
159     NamingResourcesImpl namingResources = Mockito.mock(NamingResourcesImpl.class);
160     Mockito.when(context.getNamingResources()).thenReturn(namingResources);
161     Mockito.when(namingResources.findResources())
162         .thenReturn(new ContextResource[] {new ContextResource()});
163 
164     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
165     final List<ApplicationResource> list = new ArrayList<ApplicationResource>();
166     adapter.addContextResource(context, list);
167     assertFalse(list.isEmpty());
168   }
169 
170   /**
171    * Gets the application filter maps.
172    */
173   @Test
174   void applicationFilterMaps() {
175     Mockito.when(context.findFilterMaps()).thenReturn(new FilterMap[] {new FilterMap()});
176 
177     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
178     assertEquals(0, adapter.getApplicationFilterMaps(context).size());
179   }
180 
181   /**
182    * Application filters.
183    */
184   @Test
185   void applicationFilters() {
186     Mockito.when(context.findFilterDefs()).thenReturn(new FilterDef[] {new FilterDef()});
187 
188     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
189     assertEquals(1, adapter.getApplicationFilters(context).size());
190   }
191 
192   /**
193    * Application init params.
194    */
195   @Test
196   void applicationInitParams() {
197     Mockito.when(context.findApplicationParameters())
198         .thenReturn(new ApplicationParameter[] {new ApplicationParameter()});
199 
200     ServletContext servletContext = Mockito.mock(ServletContext.class);
201     Mockito.when(context.getServletContext()).thenReturn(servletContext);
202 
203     List<String> initParams = new ArrayList<>();
204     initParams.add("name");
205     Enumeration<String> initParameterNames = Collections.enumeration(initParams);
206     Mockito.when(servletContext.getInitParameterNames()).thenReturn(initParameterNames);
207 
208     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
209     assertEquals(1, adapter.getApplicationInitParams(context).size());
210   }
211 
212   /**
213    * Resource exists.
214    */
215   @Test
216   void resourceExists() {
217     WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
218     Mockito.when(context.getResources()).thenReturn(webResourceRoot);
219 
220     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
221     assertFalse(adapter.resourceExists("name", context));
222   }
223 
224   /**
225    * Resource stream.
226    *
227    * @throws IOException Signals that an I/O exception has occurred.
228    */
229   @Test
230   void resourceStream() throws IOException {
231     WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
232     Mockito.when(context.getResources()).thenReturn(webResourceRoot);
233 
234     WebResource webResource = Mockito.mock(WebResource.class);
235     Mockito.when(webResourceRoot.getResource("name")).thenReturn(webResource);
236 
237     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
238     assertNull(adapter.getResourceStream("name", context));
239   }
240 
241   /**
242    * Resource attributes.
243    */
244   @Test
245   void resourceAttributes() {
246     WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
247     Mockito.when(context.getResources()).thenReturn(webResourceRoot);
248 
249     WebResource webResource = Mockito.mock(WebResource.class);
250     Mockito.when(webResourceRoot.getResource("name")).thenReturn(webResource);
251 
252     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
253     assertNotNull(adapter.getResourceAttributes("name", context));
254   }
255 
256   /**
257    * Gets the naming token.
258    */
259   @Test
260   void getNamingToken() {
261     Mockito.when(context.getNamingToken()).thenReturn(new Object());
262 
263     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
264     assertNotNull(adapter.getNamingToken(context));
265   }
266 
267 }