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());
}
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);
}
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);
}
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();
}
}
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());
}
Aggregations