1
2
3
4
5
6
7
8
9
10
11 package psiprobe.mappers;
12
13 import com.opensymphony.module.sitemesh.Config;
14 import com.opensymphony.module.sitemesh.Decorator;
15 import com.opensymphony.module.sitemesh.DecoratorMapper;
16 import com.opensymphony.module.sitemesh.Page;
17
18 import jakarta.servlet.http.HttpServletRequest;
19
20 import java.util.Properties;
21
22 import org.junit.jupiter.api.Assertions;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.junit.jupiter.MockitoExtension;
29
30
31
32
33 @ExtendWith(MockitoExtension.class)
34 class AjaxDecoratorMapperTest {
35
36
37 AjaxDecoratorMapper mapper;
38
39
40 @Mock
41 Config config;
42
43
44 Properties properties;
45
46
47 @Mock
48 DecoratorMapper decoratorMapper;
49
50
51 @Mock
52 HttpServletRequest request;
53
54
55 @Mock
56 Page page;
57
58
59
60
61 @BeforeEach
62 void before() {
63 mapper = new AjaxDecoratorMapper();
64 properties = new Properties();
65 }
66
67
68
69
70
71
72 @Test
73 void ajaxDecoratorMapperTest() throws InstantiationException {
74 properties.setProperty("ajaxExtension", ".ajax");
75 mapper.init(config, properties, decoratorMapper);
76
77 Mockito.when(request.getAttribute("jakarta.servlet.error.request_uri"))
78 .thenReturn("https://localhost:8443/probe");
79 Mockito.when(request.getServletPath()).thenReturn("probe/ws");
80
81
82 var decorator = Mockito.mock(Decorator.class);
83 Mockito.when(decoratorMapper.getDecorator(request, page)).thenReturn(decorator);
84
85 Assertions.assertNotNull(mapper.getDecorator(request, page));
86 }
87
88
89
90
91
92
93 @Test
94 void testWithoutAjaxExtensionProperty() throws InstantiationException {
95 mapper.init(config, properties, decoratorMapper);
96
97 Mockito.when(request.getAttribute("jakarta.servlet.error.request_uri"))
98 .thenReturn("https://localhost:8443/probe");
99 Mockito.when(request.getServletPath()).thenReturn("probe/ws");
100
101
102 var decorator = Mockito.mock(Decorator.class);
103 Mockito.when(decoratorMapper.getDecorator(request, page)).thenReturn(decorator);
104
105 Assertions.assertNotNull(mapper.getDecorator(request, page));
106 }
107
108
109
110
111
112
113 @Test
114 void testNullRequestUri() throws InstantiationException {
115 properties.setProperty("ajaxExtension", ".ajax");
116 mapper.init(config, properties, decoratorMapper);
117
118 Mockito.when(request.getAttribute("jakarta.servlet.error.request_uri")).thenReturn(null);
119 Mockito.when(request.getServletPath()).thenReturn("probe/ws");
120
121
122 var decorator = Mockito.mock(Decorator.class);
123 Mockito.when(decoratorMapper.getDecorator(request, page)).thenReturn(decorator);
124
125 Assertions.assertNotNull(mapper.getDecorator(request, page));
126 }
127
128
129
130
131
132
133 @Test
134 void returnsNullForAjaxServletPath() throws InstantiationException {
135 properties.setProperty("ajaxExtension", ".ajax");
136 mapper.init(config, properties, decoratorMapper);
137
138 Mockito.when(request.getAttribute("jakarta.servlet.error.request_uri")).thenReturn(null);
139 Mockito.when(request.getServletPath()).thenReturn("/foo.ajax");
140
141 Assertions.assertNull(mapper.getDecorator(request, page));
142 }
143
144
145
146
147
148
149 @Test
150 void returnsNullForAjaxErrorUri() throws InstantiationException {
151 properties.setProperty("ajaxExtension", ".ajax");
152 mapper.init(config, properties, decoratorMapper);
153
154 Mockito.when(request.getAttribute("jakarta.servlet.error.request_uri"))
155 .thenReturn("/error/test.ajax");
156
157 Assertions.assertNull(mapper.getDecorator(request, page));
158 }
159
160
161
162
163
164
165 @Test
166 void returnsNullForAjaxErrorUriWithQueryString() throws InstantiationException {
167 properties.setProperty("ajaxExtension", ".ajax");
168 mapper.init(config, properties, decoratorMapper);
169
170 Mockito.when(request.getAttribute("jakarta.servlet.error.request_uri"))
171 .thenReturn("/error/test.ajax?param=1");
172
173 Assertions.assertNull(mapper.getDecorator(request, page));
174 }
175
176
177
178
179
180
181 @Test
182 void callsSuperForNonAjaxRequest() throws InstantiationException {
183 properties.setProperty("ajaxExtension", ".ajax");
184 mapper.init(config, properties, decoratorMapper);
185
186 Mockito.when(request.getAttribute("jakarta.servlet.error.request_uri")).thenReturn(null);
187 Mockito.when(request.getServletPath()).thenReturn("/foo.html");
188
189 var decorator = Mockito.mock(Decorator.class);
190 Mockito.when(decoratorMapper.getDecorator(request, page)).thenReturn(decorator);
191
192 Assertions.assertEquals(decorator, mapper.getDecorator(request, page));
193 }
194
195
196
197
198
199
200 @Test
201 void usesCustomAjaxExtension() throws InstantiationException {
202 properties.setProperty("ajaxExtension", ".customajax");
203 mapper.init(config, properties, decoratorMapper);
204
205 Mockito.when(request.getAttribute("jakarta.servlet.error.request_uri"))
206 .thenReturn("/foo.customajax");
207
208 Assertions.assertNull(mapper.getDecorator(request, page));
209 }
210
211 }