1
2
3
4
5
6
7
8
9
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.Test;
40 import org.junit.jupiter.api.extension.ExtendWith;
41 import org.junit.jupiter.params.ParameterizedTest;
42 import org.junit.jupiter.params.provider.ValueSource;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.junit.jupiter.MockitoExtension;
46
47 import psiprobe.model.ApplicationResource;
48
49
50
51
52 @ExtendWith(MockitoExtension.class)
53 class Tomcat10ContainerAdapterTest {
54
55
56 @Mock
57 Context context;
58
59
60 @Mock
61 EmbeddedServletOptions options;
62
63
64
65
66 @Test
67 void createValve() {
68 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
69 Valve valve = adapter.createValve();
70 assertEquals("Tomcat10AgentValve[Container is null]", valve.toString());
71 }
72
73
74
75
76 @Test
77 void canBoundToNull() {
78 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
79 assertFalse(adapter.canBoundTo(null));
80 }
81
82
83
84
85 @ParameterizedTest
86 @ValueSource(strings = {"Apache Tomcat/10.1", "Apache Tomcat (TomEE)/10.0",
87 "NonStop(tm) Servlets For JavaServer Pages(tm) v10.1", "Vmware tc..../10.1"})
88 void canBoundTo(String container) {
89 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
90 assertTrue(adapter.canBoundTo(container));
91 }
92
93
94
95
96 @Test
97 void canBoundToOther() {
98 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
99 assertFalse(adapter.canBoundTo("Other"));
100 }
101
102
103
104
105 @Test
106 void filterMappings() {
107 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
108 FilterMap map = new FilterMap();
109 map.addServletName("psi-probe");
110 map.addURLPattern("/psi-probe");
111 assertEquals(2, adapter.getFilterMappings(map, "dispatcherMap", "filterClass").size());
112 }
113
114
115
116
117 @Test
118 void createJspCompilationContext() {
119 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
120 Mockito.when(this.options.getGeneratedJspPackageName()).thenReturn("org.apache.jsp");
121 JspCompilationContext jspContext = adapter.createJspCompilationContext("name", this.options,
122 null, null, ClassLoader.getSystemClassLoader());
123 assertEquals("org.apache.jsp.name", jspContext.getFQCN());
124 }
125
126
127
128
129 @Test
130 void addContextResourceLink() {
131 NamingResourcesImpl namingResources = Mockito.mock(NamingResourcesImpl.class);
132 Mockito.when(context.getNamingResources()).thenReturn(namingResources);
133 Mockito.when(namingResources.findResourceLinks())
134 .thenReturn(new ContextResourceLink[] {new ContextResourceLink()});
135
136 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
137 final List<ApplicationResource> list = new ArrayList<ApplicationResource>();
138 adapter.addContextResourceLink(context, list);
139 assertFalse(list.isEmpty());
140 }
141
142
143
144
145 @Test
146 void addContextResource() {
147 NamingResourcesImpl namingResources = Mockito.mock(NamingResourcesImpl.class);
148 Mockito.when(context.getNamingResources()).thenReturn(namingResources);
149 Mockito.when(namingResources.findResources())
150 .thenReturn(new ContextResource[] {new ContextResource()});
151
152 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
153 final List<ApplicationResource> list = new ArrayList<ApplicationResource>();
154 adapter.addContextResource(context, list);
155 assertFalse(list.isEmpty());
156 }
157
158
159
160
161 @Test
162 void applicationFilterMaps() {
163 Mockito.when(context.findFilterMaps()).thenReturn(new FilterMap[] {new FilterMap()});
164
165 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
166 assertEquals(0, adapter.getApplicationFilterMaps(context).size());
167 }
168
169
170
171
172 @Test
173 void applicationFilters() {
174 Mockito.when(context.findFilterDefs()).thenReturn(new FilterDef[] {new FilterDef()});
175
176 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
177 assertEquals(1, adapter.getApplicationFilters(context).size());
178 }
179
180
181
182
183 @Test
184 void applicationInitParams() {
185 Mockito.when(context.findApplicationParameters())
186 .thenReturn(new ApplicationParameter[] {new ApplicationParameter()});
187
188 ServletContext servletContext = Mockito.mock(ServletContext.class);
189 Mockito.when(context.getServletContext()).thenReturn(servletContext);
190
191 List<String> initParams = new ArrayList<>();
192 initParams.add("name");
193 Enumeration<String> initParameterNames = Collections.enumeration(initParams);
194 Mockito.when(servletContext.getInitParameterNames()).thenReturn(initParameterNames);
195
196 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
197 assertEquals(1, adapter.getApplicationInitParams(context).size());
198 }
199
200
201
202
203 @Test
204 void resourceExists() {
205 WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
206 Mockito.when(context.getResources()).thenReturn(webResourceRoot);
207
208 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
209 assertFalse(adapter.resourceExists("name", context));
210 }
211
212
213
214
215
216
217 @Test
218 void resourceStream() throws IOException {
219 WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
220 Mockito.when(context.getResources()).thenReturn(webResourceRoot);
221
222 WebResource webResource = Mockito.mock(WebResource.class);
223 Mockito.when(webResourceRoot.getResource("name")).thenReturn(webResource);
224
225 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
226 assertNull(adapter.getResourceStream("name", context));
227 }
228
229
230
231
232 @Test
233 void resourceAttributes() {
234 WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
235 Mockito.when(context.getResources()).thenReturn(webResourceRoot);
236
237 WebResource webResource = Mockito.mock(WebResource.class);
238 Mockito.when(webResourceRoot.getResource("name")).thenReturn(webResource);
239
240 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
241 assertNotNull(adapter.getResourceAttributes("name", context));
242 }
243
244
245
246
247 @Test
248 void getNamingToken() {
249 Mockito.when(context.getNamingToken()).thenReturn(new Object());
250
251 final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
252 assertNotNull(adapter.getNamingToken(context));
253 }
254
255 }