use of com.enonic.xp.resource.Resource in project xp by enonic.
the class TaskDescriptorLoaderTest method testLoad.
@Test
public void testLoad() {
final DescriptorKey descriptorKey = DescriptorKey.from("myapp1:task1");
final ResourceKey resourceKey = this.loader.toResource(descriptorKey);
assertEquals("myapp1:/tasks/task1/task1.xml", resourceKey.toString());
final Resource resource = this.resourceService.getResource(resourceKey);
final TaskDescriptor descriptor = this.loader.load(descriptorKey, resource);
assertEquals("MyTask", descriptor.getDescription());
FormItem formItem = descriptor.getConfig().getFormItem("param1");
assertEquals(" something ", formItem.toInput().getDefaultValue().getRootValue());
}
use of com.enonic.xp.resource.Resource in project xp by enonic.
the class WebAppHandlerTest method handle_serveAsset.
@Test
public void handle_serveAsset() throws Exception {
final Resource resource = mockResource("myapp:/assets/a/b.txt", "hello".getBytes());
this.request.setRawPath("/webapp/myapp/a/b.txt");
this.request.setMethod(HttpMethod.GET);
final WebResponse response = this.handler.doHandle(this.request, null, this.chain);
assertEquals(HttpStatus.OK, response.getStatus());
assertSame(resource, response.getBody());
}
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 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 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);
}
Aggregations