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.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   * The Class AjaxDecoratorMapperTest.
32   */
33  @ExtendWith(MockitoExtension.class)
34  class AjaxDecoratorMapperTest {
35  
36    /** The mapper. */
37    AjaxDecoratorMapper mapper;
38  
39    /** The config. */
40    @Mock
41    Config config;
42  
43    /** The properties. */
44    Properties properties;
45  
46    /** The decorator mapper. */
47    @Mock
48    DecoratorMapper decoratorMapper;
49  
50    /** The request. */
51    @Mock
52    HttpServletRequest request;
53  
54    /** The page. */
55    @Mock
56    Page page;
57  
58    /**
59     * Before.
60     */
61    @BeforeEach
62    void before() {
63      mapper = new AjaxDecoratorMapper();
64      properties = new Properties();
65    }
66  
67    /**
68     * Ajax decorator mapper test.
69     *
70     * @throws InstantiationException the instantiation exception
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      // Mock the decoratorMapper to return a non-null decorator
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     * Test without ajax extension property.
90     *
91     * @throws InstantiationException the instantiation exception
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     // Mock the decoratorMapper to return a non-null decorator
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    * Test null request uri.
110    *
111    * @throws InstantiationException the instantiation exception
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     // Mock the decoratorMapper to return a non-null decorator
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    * Returns null for ajax servlet path.
130    *
131    * @throws InstantiationException the instantiation exception
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    * Returns null for ajax error uri.
146    *
147    * @throws InstantiationException the instantiation exception
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    * Returns null for ajax error uri with query string.
162    *
163    * @throws InstantiationException the instantiation exception
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    * Calls super for non ajax request.
178    *
179    * @throws InstantiationException the instantiation exception
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    * Uses custom ajax extension.
197    *
198    * @throws InstantiationException the instantiation exception
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 }