Search in sources :

Example 21 with Resource

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

the class AssetHandlerTest method testRootResourceFound.

@Test
public void testRootResourceFound() throws Exception {
    final Resource resource = addResource("demo:/assets/css/main.css");
    final WebResponse res = this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertNotNull(res);
    assertEquals(HttpStatus.OK, res.getStatus());
    assertEquals(MediaType.CSS_UTF_8.withoutParameters(), res.getContentType());
    assertSame(resource, res.getBody());
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) MockResource(com.enonic.xp.resource.MockResource) Resource(com.enonic.xp.resource.Resource) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 22 with Resource

use of com.enonic.xp.resource.Resource 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 23 with Resource

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

Example 24 with Resource

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

the class ScriptExportsCache method getOrCompute.

public Object getOrCompute(final ResourceKey key, final Function<Resource, Object> requireFunction) throws InterruptedException, TimeoutException {
    ScriptExportEntry cached = cache.get(key);
    if (cached != null) {
        return cached.value;
    }
    if (lock.tryLock(5, TimeUnit.MINUTES)) {
        try {
            cached = cache.get(key);
            if (cached != null) {
                return cached.value;
            }
            final Resource resource = resourceLookup.apply(key);
            final Object value = requireFunction.apply(resource);
            cache.put(key, new ScriptExportEntry(resource, value));
            return value;
        } finally {
            lock.unlock();
        }
    } else {
        throw new TimeoutException();
    }
}
Also used : Resource(com.enonic.xp.resource.Resource) TimeoutException(java.util.concurrent.TimeoutException)

Example 25 with Resource

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

the class ProcessingCache method doProcess.

private ProcessingEntry doProcess(final ResourceProcessor<?, ?> processor) {
    final ResourceKey resourceKey = processor.toResourceKey();
    final Resource resource = this.loader.apply(resourceKey);
    final Object value = processor.process(resource);
    if (value == null) {
        return null;
    }
    return new ProcessingEntry(resourceKey, value, resource.getTimestamp());
}
Also used : Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey)

Aggregations

Resource (com.enonic.xp.resource.Resource)28 ResourceKey (com.enonic.xp.resource.ResourceKey)21 Test (org.junit.jupiter.api.Test)14 PortalResponse (com.enonic.xp.portal.PortalResponse)7 ContentService (com.enonic.xp.content.ContentService)6 PortalRequest (com.enonic.xp.portal.PortalRequest)6 ResourceService (com.enonic.xp.resource.ResourceService)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 ApplicationKey (com.enonic.xp.app.ApplicationKey)5 Branch (com.enonic.xp.branch.Branch)4 ContentId (com.enonic.xp.content.ContentId)4 ContentPath (com.enonic.xp.content.ContentPath)4 PropertyTree (com.enonic.xp.data.PropertyTree)4 DescriptorKey (com.enonic.xp.page.DescriptorKey)4 RenderMode (com.enonic.xp.portal.RenderMode)4 ErrorHandlerScript (com.enonic.xp.portal.impl.error.ErrorHandlerScript)4 ErrorHandlerScriptFactory (com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory)4 MockResource (com.enonic.xp.resource.MockResource)4 RunMode (com.enonic.xp.server.RunMode)4 Site (com.enonic.xp.site.Site)4