Search in sources :

Example 56 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class VfsResourceReferenceSerializerTest method serializeWhenNoSpecificSchemeSerializer.

@Test
public void serializeWhenNoSpecificSchemeSerializer() throws Exception {
    VfsResourceReference reference = new VfsResourceReference(URI.create("somescheme:wiki:space.page@attachment"), "path1/path2/test.txt");
    ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("vfs", "somescheme:wiki:space.page@attachment", "path1", "path2", "test.txt"));
    URLNormalizer<ExtendedURL> normalizer = this.mocker.getInstance(new DefaultParameterizedType(null, URLNormalizer.class, ExtendedURL.class), "contextpath");
    when(normalizer.normalize(extendedURL)).thenReturn(extendedURL);
    assertEquals("/vfs/somescheme%3Awiki%3Aspace.page%40attachment/path1/path2/test.txt", this.mocker.getComponentUnderTest().serialize(reference).toString());
}
Also used : URLNormalizer(org.xwiki.url.URLNormalizer) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Test(org.junit.Test)

Example 57 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class PathConverterTest method convertWhenError.

@Test
public void convertWhenError() throws Exception {
    ResourceReferenceSerializer<VfsResourceReference, URI> serializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, URI.class), "truevfs");
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:Sandbox.WebHome@my.zip"), "a/b/c");
    when(serializer.serialize(reference)).thenThrow(new SerializeResourceReferenceException("error"));
    try {
        this.mocker.getComponentUnderTest().convert(new DefaultParameterizedType(null, Path.class), "attach:Sandbox.WebHome@my.zip/a/b/c");
        fail("Should have thrown an exception here");
    } catch (ConversionException expected) {
        assertEquals("Failed to convert [attach:Sandbox.WebHome@my.zip/a/b/c] to a Path object", expected.getMessage());
    }
}
Also used : TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ConversionException(org.xwiki.properties.converter.ConversionException) SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) URI(java.net.URI) Test(org.junit.Test)

Example 58 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class PathConverterTest method convertWhenNoPermission.

@Test
public void convertWhenNoPermission() throws Exception {
    ResourceReferenceSerializer<VfsResourceReference, URI> serializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, URI.class), "truevfs");
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:Sandbox.WebHome@my.zip"), "a/b/c");
    when(serializer.serialize(reference)).thenReturn(URI.create("attach://xwiki:Sandbox.WebHome/my.zip/a/b/c"));
    VfsPermissionChecker permissionChecker = this.mocker.getInstance(VfsPermissionChecker.class, "cascading");
    doThrow(new VfsException("unauthorized")).when(permissionChecker).checkPermission(reference);
    try {
        this.mocker.getComponentUnderTest().convert(new DefaultParameterizedType(null, Path.class), "attach:Sandbox.WebHome@my.zip/a/b/c");
        fail("Should have thrown an exception here");
    } catch (ConversionException expected) {
        assertEquals("Failed to convert [attach:Sandbox.WebHome@my.zip/a/b/c] to a Path object", expected.getMessage());
        assertEquals("VfsException: unauthorized", ExceptionUtils.getRootCauseMessage(expected));
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ConversionException(org.xwiki.properties.converter.ConversionException) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsPermissionChecker(org.xwiki.vfs.VfsPermissionChecker) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) URI(java.net.URI) Test(org.junit.Test)

Example 59 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class URIVfsResourceReferenceSerializer method serialize.

@Override
public URI serialize(VfsResourceReference reference) throws SerializeResourceReferenceException, UnsupportedResourceReferenceException {
    URI resultURI;
    try {
        ResourceReferenceSerializer<VfsResourceReference, URI> serializer = this.componentManagerProvider.get().getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, URI.class), String.format("truevfs/%s", reference.getURI().getScheme()));
        resultURI = serializer.serialize(reference);
    } catch (ComponentLookupException e) {
        // No serializer exist, we just don't perform any conversion!
        resultURI = reference.toURI();
    }
    return resultURI;
}
Also used : ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) URI(java.net.URI)

Example 60 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class DefaultEditorManager method getEditor.

@Override
public <D> Editor<D> getEditor(Type dataType, String hint) {
    DefaultParameterizedType editorType = new DefaultParameterizedType(null, Editor.class, dataType);
    ComponentManager componentManager = this.componentManagerProvider.get();
    if (componentManager.hasComponent(editorType, hint)) {
        try {
            return componentManager.getInstance(editorType, hint);
        } catch (ComponentLookupException e) {
            throw new RuntimeException(String.format("Failed to look up the [%s] editor with hint [%s]", dataType.getTypeName(), hint), e);
        }
    } else {
        // No such editor component found.
        return null;
    }
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Aggregations

DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)104 Test (org.junit.Test)44 Before (org.junit.Before)32 Provider (javax.inject.Provider)27 DocumentReference (org.xwiki.model.reference.DocumentReference)24 XWikiContext (com.xpn.xwiki.XWikiContext)19 ComponentManager (org.xwiki.component.manager.ComponentManager)19 ExecutionContext (org.xwiki.context.ExecutionContext)17 ExtendedURL (org.xwiki.url.ExtendedURL)15 Execution (org.xwiki.context.Execution)14 HashMap (java.util.HashMap)13 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)13 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)12 ResourceReferenceSerializer (org.xwiki.resource.ResourceReferenceSerializer)10 VfsResourceReference (org.xwiki.vfs.VfsResourceReference)10 Properties (java.util.Properties)9 MimeBodyPartFactory (org.xwiki.mail.MimeBodyPartFactory)9 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)8 XWiki (com.xpn.xwiki.XWiki)7 File (java.io.File)7