@@ -117,20 +117,28 @@ void getResourceWhenServletContextNotSet()
117117 exception .getMessage ());
118118 }
119119
120- @ Test
121- void getResourceOk () throws Exception
120+ @ ParameterizedTest
121+ @ ValueSource (booleans = { true , false })
122+ void getResourceOk (boolean initializeCache ) throws Exception
122123 {
123124 ServletContext servletContext = mock (ServletContext .class );
124125 this .environment .setServletContext (servletContext );
125126 String resourceName = "/test" ;
126127 when (servletContext .getResource (resourceName )).thenReturn (new URL ("file:/path/../test" ));
128+ if (initializeCache ) {
129+ this .environment .initializeCache ();
130+ }
127131
128132 URL expectedURL = new URL ("file:/test" );
129133 assertEquals (expectedURL , this .environment .getResource (resourceName ));
130134 verify (servletContext ).getResource (resourceName );
131- verify (this .cache ).set (resourceName , Optional .of (expectedURL ));
132- // As cache control returns false, the cache shouldn't be read.
133- verify (this .cache , never ()).get (any ());
135+ if (initializeCache ) {
136+ verify (this .cache ).set (resourceName , Optional .of (expectedURL ));
137+ // As cache control returns false, the cache shouldn't be read.
138+ verify (this .cache , never ()).get (any ());
139+ } else {
140+ verifyNoInteractions (this .cache );
141+ }
134142 }
135143
136144 @ Test
@@ -145,37 +153,55 @@ void getResourceAsStreamOk() throws Exception
145153 verifyNoInteractions (this .cache );
146154 }
147155
148- @ Test
149- void getResourceNotExisting () throws Exception
156+ @ ParameterizedTest
157+ @ ValueSource (booleans = { true , false })
158+ void getResourceNotExisting (boolean initializeCache )
150159 {
151- ServletContext servletContext = mock (ServletContext . class );
160+ ServletContext servletContext = mock ();
152161 this .environment .setServletContext (servletContext );
153162 String resourceName = "unknown resource" ;
163+ if (initializeCache ) {
164+ this .environment .initializeCache ();
165+ }
166+
154167 assertNull (this .environment .getResource (resourceName ));
155- verify (this .cache ).set (resourceName , Optional .empty ());
156- // As cache control returns false, the cache shouldn't be read.
157- verify (this .cache , never ()).get (any ());
168+ if (initializeCache ) {
169+ verify (this .cache ).set (resourceName , Optional .empty ());
170+ // As cache control returns false, the cache shouldn't be read.
171+ verify (this .cache , never ()).get (any ());
172+ } else {
173+ verifyNoInteractions (this .cache );
174+ }
158175 }
159176
160- @ Test
161- void getResourceWhenMalformedURLException () throws Exception
177+ @ ParameterizedTest
178+ @ ValueSource (booleans = { true , false })
179+ void getResourceWhenMalformedURLException (boolean initializeCache ) throws Exception
162180 {
163181 ServletContext servletContext = mock (ServletContext .class );
164182 String resourceName = "bad resource" ;
165183 when (servletContext .getResource (resourceName )).thenThrow (new MalformedURLException ("invalid url" ));
166184 this .environment .setServletContext (servletContext );
185+ if (initializeCache ) {
186+ this .environment .initializeCache ();
187+ }
167188 assertNull (this .environment .getResource (resourceName ));
168189 assertEquals ("Error getting resource [bad resource] because of invalid path format. Reason: [invalid url]" ,
169190 logCapture .getMessage (0 ));
170- verify (this .cache ).set (resourceName , Optional .empty ());
171- // As cache control returns false, the cache shouldn't be read.
172- verify (this .cache , never ()).get (any ());
191+ if (initializeCache ) {
192+ verify (this .cache ).set (resourceName , Optional .empty ());
193+ // As cache control returns false, the cache shouldn't be read.
194+ verify (this .cache , never ()).get (any ());
195+ } else {
196+ verifyNoInteractions (this .cache );
197+ }
173198 }
174199
175200 @ Test
176201 void getResourceNotExistingFromCache ()
177202 {
178203 String resourceName = "unknown resource" ;
204+ this .environment .initializeCache ();
179205 when (this .cacheControl .isCacheReadAllowed ()).thenReturn (true );
180206 when (this .cache .get (resourceName )).thenReturn (Optional .empty ());
181207 assertNull (this .environment .getResource (resourceName ));
@@ -188,6 +214,7 @@ void getResourceExistingFromCache()
188214 {
189215 String resourceName = "known resource" ;
190216 URL expectedURL = mock (URL .class );
217+ this .environment .initializeCache ();
191218 when (this .cacheControl .isCacheReadAllowed ()).thenReturn (true );
192219 when (this .cache .get (resourceName )).thenReturn (Optional .of (expectedURL ));
193220 assertEquals (expectedURL , this .environment .getResource (resourceName ));
@@ -230,8 +257,11 @@ void getResourceWithRecursiveCallInCacheInitialization(boolean cached) throws Ex
230257 ExecutorService executor = Executors .newFixedThreadPool (1 );
231258
232259 try {
233- CompletableFuture <URL > outerCall =
234- CompletableFuture .supplyAsync (() -> this .environment .getResource (resourceName ), executor );
260+ CompletableFuture <Void > initializeCall =
261+ CompletableFuture .supplyAsync (() -> {
262+ this .environment .initializeCache ();
263+ return null ;
264+ }, executor );
235265
236266 // Wait for the background thread to arrive in the cache creation call (but don't wait forever just to be
237267 // safe).
@@ -244,18 +274,16 @@ void getResourceWithRecursiveCallInCacheInitialization(boolean cached) throws Ex
244274 // Unblock the cache creation call.
245275 blockCreateCacheFuture .complete (null );
246276
247- // Ensure that the blocked call now got the value, too. Again, don't wait forever just in case.
248- URL actualOuterURL = outerCall .get (20 , TimeUnit .SECONDS );
249- if (cached ) {
250- assertEquals (cachedURL , actualOuterURL );
251- } else {
252- assertEquals (expectedURL , actualOuterURL );
253- }
277+ // Ensure that the blocked call now completed. Again, don't wait forever just in case.
278+ initializeCall .get (20 , TimeUnit .SECONDS );
254279
255- // Assert that the recursive call also got the URL.
280+ // Assert that the recursive call got the URL.
256281 assertEquals (expectedURL , recursiveURL .getValue ());
257282
258- // Only the outer call should have accessed the cache. If the cache didn't return the value, it should
283+ // Assert that we now get the cached URL.
284+ assertEquals (cached ? cachedURL : expectedURL , this .environment .getResource (resourceName ));
285+
286+ // Only the last call should have accessed the cache. If the cache didn't return the value, it should
259287 // have been stored.
260288 verify (this .cache ).get (resourceName );
261289 if (cached ) {
0 commit comments