use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ScriptExportsCacheTest method getOrCompute_calls_require_only_once.
@Test
void getOrCompute_calls_require_only_once() throws Exception {
final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("some.app"), "main.js");
final Resource resource = mock(Resource.class);
final Object value = new Object();
when(requireFunction.apply(resource)).thenReturn(value);
when(resourceLookup.apply(resourceKey)).thenReturn(resource);
final ScriptExportsCache scriptExportsCache = new ScriptExportsCache(RunMode.PROD, resourceLookup, expiredCallback);
final Object getOrCompute1 = scriptExportsCache.getOrCompute(resourceKey, requireFunction);
final Object getOrCompute2 = scriptExportsCache.getOrCompute(resourceKey, requireFunction);
assertAll(() -> assertSame(value, getOrCompute1), () -> assertSame(value, getOrCompute2));
verify(resourceLookup, times(1)).apply(resourceKey);
verifyNoMoreInteractions(resourceLookup);
verify(requireFunction, times(1)).apply(resource);
verifyNoMoreInteractions(requireFunction);
}
Aggregations