Search in sources :

Example 51 with ResourceKey

use of com.enonic.xp.resource.ResourceKey in project xp by enonic.

the class ScriptRuntimeTest method testRequire.

@Test
public void testRequire() {
    final ResourceKey script = ResourceKey.from("myapplication:/site/require/require-test.js");
    final ScriptExports exports = runTestScript(script);
    assertNotNull(exports);
}
Also used : ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 52 with ResourceKey

use of com.enonic.xp.resource.ResourceKey in project xp by enonic.

the class ScriptRuntimeTest method testExecuteExported_objectArg.

@Test
public void testExecuteExported_objectArg() {
    final ResourceKey script = ResourceKey.from("myapplication:/export-test.js");
    final ScriptExports exports = runTestScript(script);
    assertNotNull(exports);
    assertSame(script, exports.getScript());
    assertTrue(exports.hasMethod("helloObject"));
    assertEquals("Hello World!", exports.executeMethod("helloObject", (MapSerializable) gen -> gen.value("name", "World")).getValue());
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ResourceKey(com.enonic.xp.resource.ResourceKey) ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) ApplicationKey(com.enonic.xp.app.ApplicationKey) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Test(org.junit.jupiter.api.Test) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ScriptExports(com.enonic.xp.script.ScriptExports) MapSerializable(com.enonic.xp.script.serializer.MapSerializable) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 53 with ResourceKey

use of com.enonic.xp.resource.ResourceKey in project xp by enonic.

the class NewBeanScriptTest method testEmpty.

@Test
public void testEmpty() {
    final ResourceKey script = ResourceKey.from("myapplication:/bean/simple-test.js");
    final ScriptExports exports = runTestScript(script);
    assertNotNull(exports);
    assertSame(script, exports.getScript());
}
Also used : ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test) AbstractScriptTest(com.enonic.xp.script.impl.AbstractScriptTest)

Example 54 with ResourceKey

use of com.enonic.xp.resource.ResourceKey in project xp by enonic.

the class ScriptExportsCacheTest method expireCacheIfNeeded.

@Test
void expireCacheIfNeeded() 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.DEV, 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(2)).apply(resourceKey);
    verifyNoMoreInteractions(resourceLookup);
    verify(requireFunction, times(2)).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 55 with ResourceKey

use of com.enonic.xp.resource.ResourceKey in project xp by enonic.

the class RequireResolverTest method resolve_no_ext_json.

@Test
public void resolve_no_ext_json() throws Exception {
    touchFile("dummy/index.json");
    final ResourceKey key2 = resolve("/a/b/c.js", "/dummy");
    assertEquals("/dummy/index.json", key2.getPath());
}
Also used : 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