Search in sources :

Example 6 with WebJarsResourceReference

use of org.xwiki.webjars.internal.WebJarsResourceReference in project xwiki-platform by xwiki.

the class WebJarsScriptServiceTest method computeURLWithVersion.

@Test
public void computeURLWithVersion() throws Exception {
    WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
    when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("math");
    WebJarsResourceReference resourceReference = new WebJarsResourceReference("wiki:math", Arrays.asList("ang:ular", "2.1.11", "angular.css"));
    // Test that colon is not interpreted as groupId/artifactId separator (for backwards compatibility).
    when(this.serializer.serialize(resourceReference)).thenReturn(new ExtendedURL(Arrays.asList("xwiki", "ang:ular", "2.1.11", "angular.css")));
    assertEquals("/xwiki/ang%3Aular/2.1.11/angular.css", this.mocker.getComponentUnderTest().url("ang:ular/2.1.11/angular.css"));
}
Also used : WebJarsResourceReference(org.xwiki.webjars.internal.WebJarsResourceReference) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) ExtendedURL(org.xwiki.url.ExtendedURL) Test(org.junit.Test)

Example 7 with WebJarsResourceReference

use of org.xwiki.webjars.internal.WebJarsResourceReference in project xwiki-platform by xwiki.

the class WebJarsScriptServiceTest method computeURLWithoutVersionAndNoExtensionMatchingWebJarId.

@Test
public void computeURLWithoutVersionAndNoExtensionMatchingWebJarId() throws Exception {
    WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
    when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("math");
    WebJarsResourceReference resourceReference = new WebJarsResourceReference("wiki:math", Arrays.asList("angular", "angular.css"));
    when(this.serializer.serialize(resourceReference)).thenReturn(new ExtendedURL(Arrays.asList("xwiki", "angular", "angular.css")));
    assertEquals("/xwiki/angular/angular.css", this.mocker.getComponentUnderTest().url("angular", "angular.css"));
}
Also used : WebJarsResourceReference(org.xwiki.webjars.internal.WebJarsResourceReference) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) ExtendedURL(org.xwiki.url.ExtendedURL) Test(org.junit.Test)

Example 8 with WebJarsResourceReference

use of org.xwiki.webjars.internal.WebJarsResourceReference in project xwiki-platform by xwiki.

the class WebJarsScriptServiceTest method computeURLWithParameters.

@Test
public void computeURLWithParameters() throws Exception {
    WebJarsResourceReference resourceReference = new WebJarsResourceReference("wiki:wiki", Arrays.asList("angular", "2.1.11", "angular.js"));
    resourceReference.addParameter("evaluate", "true");
    resourceReference.addParameter("list", Arrays.asList("one", "two"));
    ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("xwiki", "angular", "2.1.11", "angular.js"), resourceReference.getParameters());
    when(this.serializer.serialize(resourceReference)).thenReturn(extendedURL);
    Map<String, Object> params = new LinkedHashMap<>();
    params.put("version", "2.1.11");
    params.put("evaluate", true);
    params.put("list", new String[] { "one", "two" });
    assertEquals("/xwiki/angular/2.1.11/angular.js?evaluate=true&list=one&list=two", this.mocker.getComponentUnderTest().url("angular", "wiki:wiki", "angular.js", params));
}
Also used : WebJarsResourceReference(org.xwiki.webjars.internal.WebJarsResourceReference) ExtendedURL(org.xwiki.url.ExtendedURL) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 9 with WebJarsResourceReference

use of org.xwiki.webjars.internal.WebJarsResourceReference in project xwiki-platform by xwiki.

the class WebJarsScriptService method getResourceReference.

private WebJarsResourceReference getResourceReference(String artifactId, Object version, String namespace, String path, Map<String, Object> urlParams) {
    List<String> segments = new ArrayList<>();
    segments.add(artifactId);
    // given WebJar id.
    if (version != null) {
        segments.add((String) version);
    }
    segments.addAll(Arrays.asList(path.split(RESOURCE_SEPARATOR)));
    // suffix is specified and there is no query string (thus preventing RequireJS from requesting ".js.js").
    if (path.endsWith(".js") && urlParams.isEmpty()) {
        urlParams.put("r", "1");
    }
    WebJarsResourceReference resourceReference = new WebJarsResourceReference(resolveNamespace(namespace), segments);
    for (Map.Entry<String, Object> parameterEntry : urlParams.entrySet()) {
        resourceReference.addParameter(parameterEntry.getKey(), parameterEntry.getValue());
    }
    return resourceReference;
}
Also used : WebJarsResourceReference(org.xwiki.webjars.internal.WebJarsResourceReference) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with WebJarsResourceReference

use of org.xwiki.webjars.internal.WebJarsResourceReference in project xwiki-platform by xwiki.

the class WebJarResourceReferenceTest method testEqualsAndHashCode.

@Test
public void testEqualsAndHashCode() {
    WebJarsResourceReference reference1 = new WebJarsResourceReference("namespace", Arrays.asList("one", "two"));
    reference1.addParameter("key1", "value1");
    reference1.addParameter("key2", new String[] { "value2", "value3" });
    WebJarsResourceReference reference2 = new WebJarsResourceReference("namespace", Arrays.asList("one", "two"));
    reference2.addParameter("key1", "value1");
    reference2.addParameter("key2", new String[] { "value2", "value3" });
    WebJarsResourceReference reference3 = new WebJarsResourceReference("namespace", Arrays.asList("one", "two"));
    WebJarsResourceReference reference4 = new WebJarsResourceReference("namespace2", Arrays.asList("one", "two"));
    assertEquals(reference2, reference1);
    assertEquals(reference2.hashCode(), reference1.hashCode());
    assertFalse(reference3.equals(reference1));
    assertFalse(reference3.hashCode() == reference1.hashCode());
    assertFalse(reference4.equals(reference3));
    assertFalse(reference4.hashCode() == reference3.hashCode());
}
Also used : WebJarsResourceReference(org.xwiki.webjars.internal.WebJarsResourceReference) Test(org.junit.Test)

Aggregations

WebJarsResourceReference (org.xwiki.webjars.internal.WebJarsResourceReference)15 Test (org.junit.Test)13 ExtendedURL (org.xwiki.url.ExtendedURL)8 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 LinkedHashMap (java.util.LinkedHashMap)3 ExtensionId (org.xwiki.extension.ExtensionId)3 InstalledExtension (org.xwiki.extension.InstalledExtension)3 InstalledExtensionRepository (org.xwiki.extension.repository.InstalledExtensionRepository)3 Reader (java.io.Reader)2 VelocityEngine (org.xwiki.velocity.VelocityEngine)2 VelocityManager (org.xwiki.velocity.VelocityManager)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Map (java.util.Map)1 VelocityException (org.apache.velocity.exception.VelocityException)1 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 ResourceReferenceHandlerException (org.xwiki.resource.ResourceReferenceHandlerException)1 SerializeResourceReferenceException (org.xwiki.resource.SerializeResourceReferenceException)1