Search in sources :

Example 11 with ResourceKey

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);
}
Also used : Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 12 with ResourceKey

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);
}
Also used : Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 13 with ResourceKey

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();
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 14 with ResourceKey

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();
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 15 with ResourceKey

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);
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceKey (com.enonic.xp.resource.ResourceKey)76 Test (org.junit.jupiter.api.Test)49 Resource (com.enonic.xp.resource.Resource)21 ApplicationKey (com.enonic.xp.app.ApplicationKey)12 ResourceService (com.enonic.xp.resource.ResourceService)12 ScriptExports (com.enonic.xp.script.ScriptExports)11 Application (com.enonic.xp.app.Application)9 UrlResource (com.enonic.xp.resource.UrlResource)9 URL (java.net.URL)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 PortalRequest (com.enonic.xp.portal.PortalRequest)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 ApplicationService (com.enonic.xp.app.ApplicationService)7 AssetUrlParams (com.enonic.xp.portal.url.AssetUrlParams)7 ScriptAsyncService (com.enonic.xp.script.impl.async.ScriptAsyncService)7 ScriptRuntimeFactoryImpl (com.enonic.xp.script.impl.standard.ScriptRuntimeFactoryImpl)7 PortalResponse (com.enonic.xp.portal.PortalResponse)6 PortalScriptServiceImpl (com.enonic.xp.portal.impl.script.PortalScriptServiceImpl)6 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)6 PropertyTree (com.enonic.xp.data.PropertyTree)5