1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.webjars

File WebJarsScriptServiceTest.java

 

Code metrics

0
52
8
1
193
139
8
0.15
6.5
8
1

Classes

Class Line # Actions
WebJarsScriptServiceTest 51 52 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 7 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.webjars;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.LinkedHashMap;
25    import java.util.Map;
26   
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.component.util.DefaultParameterizedType;
31    import org.xwiki.extension.ExtensionId;
32    import org.xwiki.extension.InstalledExtension;
33    import org.xwiki.extension.repository.InstalledExtensionRepository;
34    import org.xwiki.resource.ResourceReference;
35    import org.xwiki.resource.ResourceReferenceSerializer;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37    import org.xwiki.url.ExtendedURL;
38    import org.xwiki.webjars.internal.WebJarsResourceReference;
39    import org.xwiki.webjars.script.WebJarsScriptService;
40    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
41   
42    import static org.junit.Assert.*;
43    import static org.mockito.Mockito.*;
44   
45    /**
46    * Unit tests for {@link org.xwiki.webjars.script.WebJarsScriptService}.
47    *
48    * @version $Id: 6ad538f414cf086368ff929d30f18bb73914e080 $
49    * @since 6.0M1
50    */
 
51    public class WebJarsScriptServiceTest
52    {
53    @Rule
54    public MockitoComponentMockingRule<WebJarsScriptService> mocker =
55    new MockitoComponentMockingRule<>(WebJarsScriptService.class);
56   
57    private ResourceReferenceSerializer<ResourceReference, ExtendedURL> serializer;
58   
 
59  7 toggle @Before
60    public void setUp() throws Exception
61    {
62  7 this.serializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class,
63    ResourceReference.class, ExtendedURL.class));
64    }
65   
 
66  1 toggle @Test
67    public void computeURLWithVersion() throws Exception
68    {
69  1 WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
70  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("math");
71   
72  1 WebJarsResourceReference resourceReference = new WebJarsResourceReference("wiki:math",
73    Arrays.asList("ang:ular", "2.1.11", "angular.css"));
74    // Test that colon is not interpreted as groupId/artifactId separator (for backwards compatibility).
75  1 when(this.serializer.serialize(resourceReference)).thenReturn(
76    new ExtendedURL(Arrays.asList("xwiki", "ang:ular", "2.1.11", "angular.css")));
77   
78  1 assertEquals("/xwiki/ang%3Aular/2.1.11/angular.css",
79    this.mocker.getComponentUnderTest().url("ang:ular/2.1.11/angular.css"));
80    }
81   
 
82  1 toggle @Test
83    public void computeURLWithoutVersion() throws Exception
84    {
85  1 WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
86  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("math");
87   
88  1 InstalledExtensionRepository installedExtensionRepository =
89    this.mocker.getInstance(InstalledExtensionRepository.class);
90  1 InstalledExtension extension = mock(InstalledExtension.class);
91  1 when(installedExtensionRepository.getInstalledExtension("org.webjars:angular", "wiki:math")).thenReturn(
92    extension);
93  1 when(extension.getId()).thenReturn(new ExtensionId("bar", "2.1.11"));
94   
95  1 WebJarsResourceReference resourceReference = new WebJarsResourceReference("wiki:math",
96    Arrays.asList("angular", "2.1.11", "angular.css"));
97  1 when(this.serializer.serialize(resourceReference)).thenReturn(
98    new ExtendedURL(Arrays.asList("xwiki", "angular", "2.1.11", "angular.css")));
99   
100  1 assertEquals("/xwiki/angular/2.1.11/angular.css",
101    this.mocker.getComponentUnderTest().url("angular", "angular.css"));
102    }
103   
 
104  1 toggle @Test
105    public void computeURLWithoutVersionAndNoExtensionMatchingWebJarId() throws Exception
106    {
107  1 WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
108  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("math");
109   
110  1 WebJarsResourceReference resourceReference =
111    new WebJarsResourceReference("wiki:math", Arrays.asList("angular", "angular.css"));
112  1 when(this.serializer.serialize(resourceReference)).thenReturn(
113    new ExtendedURL(Arrays.asList("xwiki", "angular", "angular.css")));
114   
115  1 assertEquals("/xwiki/angular/angular.css", this.mocker.getComponentUnderTest().url("angular", "angular.css"));
116    }
117   
 
118  1 toggle @Test
119    public void computeURLWithParameters() throws Exception
120    {
121  1 WebJarsResourceReference resourceReference = new WebJarsResourceReference("wiki:wiki",
122    Arrays.asList("angular", "2.1.11", "angular.js"));
123  1 resourceReference.addParameter("evaluate", "true");
124  1 resourceReference.addParameter("list", Arrays.asList("one", "two"));
125  1 ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("xwiki", "angular", "2.1.11", "angular.js"),
126    resourceReference.getParameters());
127  1 when(this.serializer.serialize(resourceReference)).thenReturn(extendedURL);
128   
129  1 Map<String, Object> params = new LinkedHashMap<>();
130  1 params.put("version", "2.1.11");
131  1 params.put("evaluate", true);
132  1 params.put("list", new String[] {"one", "two"});
133  1 assertEquals("/xwiki/angular/2.1.11/angular.js?evaluate=true&list=one&list=two",
134    this.mocker.getComponentUnderTest().url("angular", "wiki:wiki", "angular.js", params));
135    }
136   
 
137  1 toggle @Test
138    public void computeJavaScriptURLWithSuffixAndNoParameters() throws Exception
139    {
140  1 WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
141  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("math");
142   
143  1 WebJarsResourceReference resourceReference =
144    new WebJarsResourceReference("wiki:math", Arrays.asList("angular", "angular.js"));
145  1 resourceReference.addParameter("r", "1");
146  1 when(this.serializer.serialize(resourceReference)).thenReturn(
147    new ExtendedURL(Arrays.asList("xwiki", "angular", "angular.js"), resourceReference.getParameters()));
148   
149  1 assertEquals("/xwiki/angular/angular.js?r=1", this.mocker.getComponentUnderTest().url("angular", "angular.js"));
150    }
151   
 
152  1 toggle @Test
153    public void computeURLForBackwardCompatibilityWhenWikiIsSpecifiedAsParameter() throws Exception
154    {
155  1 InstalledExtensionRepository installedExtensionRepository =
156    this.mocker.getInstance(InstalledExtensionRepository.class);
157  1 InstalledExtension extension = mock(InstalledExtension.class);
158  1 when(installedExtensionRepository.getInstalledExtension("org.webjars:angular", "wiki:math")).thenReturn(
159    extension);
160  1 when(extension.getId()).thenReturn(new ExtensionId("bar", "2.1.11"));
161   
162  1 WebJarsResourceReference resourceReference = new WebJarsResourceReference("wiki:math",
163    Arrays.asList("angular", "2.1.11", "angular.css"));
164  1 when(this.serializer.serialize(resourceReference)).thenReturn(
165    new ExtendedURL(Arrays.asList("xwiki", "angular", "2.1.11", "angular.css")));
166   
167  1 assertEquals("/xwiki/angular/2.1.11/angular.css",
168    this.mocker.getComponentUnderTest().url("angular", "angular.css",
169    Collections.singletonMap("wiki", "math")));
170    }
171   
 
172  1 toggle @Test
173    public void computeURLWithParametersAndWikiIsNotSpecifiedInParameter() throws Exception
174    {
175  1 WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
176  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("math");
177   
178  1 InstalledExtensionRepository installedExtensionRepository =
179    this.mocker.getInstance(InstalledExtensionRepository.class);
180  1 InstalledExtension extension = mock(InstalledExtension.class);
181  1 when(installedExtensionRepository.getInstalledExtension("org.webjars:angular", "wiki:math")).thenReturn(
182    extension);
183  1 when(extension.getId()).thenReturn(new ExtensionId("bar", "2.1.11"));
184   
185  1 WebJarsResourceReference resourceReference = new WebJarsResourceReference("wiki:math",
186    Arrays.asList("angular", "2.1.11", "angular.css"));
187  1 when(this.serializer.serialize(resourceReference)).thenReturn(
188    new ExtendedURL(Arrays.asList("xwiki", "angular", "2.1.11", "angular.css")));
189   
190  1 assertEquals("/xwiki/angular/2.1.11/angular.css",
191    this.mocker.getComponentUnderTest().url("angular", "angular.css", Collections.<String, Object>emptyMap()));
192    }
193    }