use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ScriptExportsCacheTest method expireCacheIfNeeded_one_expired_clears_all.
@Test
void expireCacheIfNeeded_one_expired_clears_all() throws Exception {
final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("some.app"), "some0.js");
final Resource resource = mock(Resource.class);
when(resource.getTimestamp()).thenReturn(1L);
when(resourceLookup.apply(resourceKey)).thenReturn(resource);
final ResourceKey resourceKeyExtra = ResourceKey.from(ApplicationKey.from("some.app"), "some1.js");
final Resource resourceExtra = mock(Resource.class);
when(resourceExtra.getTimestamp()).thenReturn(2L, 3L);
when(resourceLookup.apply(resourceKeyExtra)).thenReturn(resourceExtra);
final ScriptExportsCache scriptExportsCache = new ScriptExportsCache(RunMode.DEV, resourceLookup, expiredCallback);
scriptExportsCache.getOrCompute(resourceKey, requireFunction);
scriptExportsCache.getOrCompute(resourceKeyExtra, requireFunction);
scriptExportsCache.expireCacheIfNeeded();
scriptExportsCache.getOrCompute(resourceKey, requireFunction);
verify(resourceExtra, times(2)).getTimestamp();
verify(resourceLookup, times(2)).apply(resourceKey);
verify(requireFunction, times(2)).apply(resource);
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ScriptExportsCacheTest method expireCacheIfNeeded_has_no_effect_in_prod.
@Test
void expireCacheIfNeeded_has_no_effect_in_prod() throws Exception {
final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("some.app"), "main.js");
final Resource resource = mock(Resource.class);
when(resource.getTimestamp()).thenReturn(1L, 2L);
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);
scriptExportsCache.expireCacheIfNeeded();
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);
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ScriptRuntimeImplTest method runDisposers.
@Test
void runDisposers() {
final ApplicationKey applicationKey = ApplicationKey.from("myApp");
when(scriptExecutorFactory.apply(applicationKey)).thenReturn(scriptExecutor);
final ScriptRuntimeImpl scriptRuntime = new ScriptRuntimeImpl(scriptExecutorFactory);
final ResourceKey resourceKey = ResourceKey.from(applicationKey, "/main.js");
scriptRuntime.execute(resourceKey);
scriptRuntime.runDisposers(applicationKey);
verify(scriptExecutor, Mockito.times(1)).runDisposers();
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ScriptRuntimeImplTest method runDisposers_no_script_executor.
@Test
void runDisposers_no_script_executor() {
final ApplicationKey applicationKey = ApplicationKey.from("myApp");
when(scriptExecutorFactory.apply(applicationKey)).thenReturn(scriptExecutor);
final ScriptRuntimeImpl scriptRuntime = new ScriptRuntimeImpl(scriptExecutorFactory);
final ResourceKey resourceKey = ResourceKey.from(applicationKey, "/main.js");
scriptRuntime.execute(resourceKey);
scriptRuntime.invalidate(applicationKey);
scriptRuntime.runDisposers(applicationKey);
verify(scriptExecutor, Mockito.never()).runDisposers();
}
use of com.enonic.xp.resource.ResourceKey in project xp by enonic.
the class ScriptRuntimeImplTest method execute.
@Test
void execute() {
final ApplicationKey applicationKey = ApplicationKey.from("myApp");
when(scriptExecutorFactory.apply(applicationKey)).thenReturn(scriptExecutor);
final ScriptRuntimeImpl scriptRuntime = new ScriptRuntimeImpl(scriptExecutorFactory);
final ResourceKey resourceKey = ResourceKey.from(applicationKey, "/main.js");
scriptRuntime.execute(resourceKey);
verify(scriptExecutor, Mockito.times(1)).executeMain(resourceKey);
}
Aggregations