1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.url.internal.standard

File IntegrationTest.java

 

Code metrics

0
26
4
1
170
115
4
0.15
6.5
4
1

Classes

Class Line # Actions
IntegrationTest 86 26 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.url.internal.standard;
21   
22    import java.net.URL;
23    import java.util.Arrays;
24    import java.util.Collections;
25   
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.xwiki.component.manager.ComponentManager;
30    import org.xwiki.component.util.DefaultParameterizedType;
31    import org.xwiki.context.Execution;
32    import org.xwiki.context.ExecutionContext;
33    import org.xwiki.model.EntityType;
34    import org.xwiki.model.ModelConfiguration;
35    import org.xwiki.model.internal.reference.DefaultEntityReferenceProvider;
36    import org.xwiki.model.internal.reference.DefaultReferenceEntityReferenceResolver;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.resource.ResourceReference;
39    import org.xwiki.resource.ResourceReferenceResolver;
40    import org.xwiki.resource.ResourceType;
41    import org.xwiki.resource.ResourceTypeResolver;
42    import org.xwiki.resource.entity.EntityResourceAction;
43    import org.xwiki.resource.entity.EntityResourceReference;
44    import org.xwiki.resource.internal.entity.EntityResourceActionLister;
45    import org.xwiki.resource.resources.ResourcesResourceReference;
46    import org.xwiki.resource.skins.SkinsResourceReference;
47    import org.xwiki.test.annotation.BeforeComponent;
48    import org.xwiki.test.annotation.ComponentList;
49    import org.xwiki.test.mockito.MockitoComponentManagerRule;
50    import org.xwiki.url.ExtendedURL;
51    import org.xwiki.url.URLConfiguration;
52    import org.xwiki.url.internal.DefaultResourceReferenceResolver;
53    import org.xwiki.url.internal.DefaultResourceTypeResolver;
54    import org.xwiki.url.internal.DefaultStringResourceTypeResolver;
55    import org.xwiki.url.internal.GenericResourceReferenceResolver;
56    import org.xwiki.url.internal.GenericStringResourceTypeResolver;
57    import org.xwiki.url.internal.standard.resources.ResourcesResourceReferenceResolver;
58    import org.xwiki.url.internal.standard.skins.SkinsResourceReferenceResolver;
59    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
60   
61    import static org.junit.Assert.assertEquals;
62    import static org.mockito.Mockito.when;
63   
64    /**
65    * Integration Tests for resolving URLs using the Standard URL Scheme.
66    *
67    * @version $Id: e58ee3bd7e7543e2de3adf638cace12192272f1f $
68    * @since 7.1M1
69    */
70    @ComponentList({
71    DefaultResourceTypeResolver.class,
72    DefaultResourceReferenceResolver.class,
73    GenericResourceReferenceResolver.class,
74    StandardExtendedURLResourceTypeResolver.class,
75    StandardExtendedURLResourceReferenceResolver.class,
76    DefaultStringResourceTypeResolver.class,
77    GenericStringResourceTypeResolver.class,
78    DomainWikiReferenceExtractor.class,
79    PathWikiReferenceExtractor.class,
80    DefaultEntityReferenceProvider.class,
81    DefaultReferenceEntityReferenceResolver.class,
82    StandardStringResourceTypeResolver.class,
83    ResourcesResourceReferenceResolver.class,
84    SkinsResourceReferenceResolver.class
85    })
 
86    public class IntegrationTest
87    {
88    @Rule
89    public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
90   
91    private ResourceTypeResolver<ExtendedURL> resourceTypeResolver;
92   
93    private ResourceReferenceResolver<ExtendedURL> resourceReferenceResolver;
94   
 
95  1 toggle @BeforeComponent
96    public void setUpComponents() throws Exception
97    {
98    // Isolate from xwiki configuration file
99  1 URLConfiguration urlConfiguration = this.componentManager.registerMockComponent(URLConfiguration.class);
100  1 when(urlConfiguration.getURLFormatId()).thenReturn("standard");
101   
102    // Isolate from xwiki configuration file
103  1 StandardURLConfiguration standardURLConfiguration = this.componentManager.registerMockComponent(
104    StandardURLConfiguration.class);
105  1 when(standardURLConfiguration.getEntityPathPrefix()).thenReturn("bin");
106  1 when(standardURLConfiguration.getWikiPathPrefix()).thenReturn("wiki");
107  1 when(standardURLConfiguration.isViewActionHidden()).thenReturn(false);
108   
109    // Isolate from xwiki configuration file
110  1 ModelConfiguration modelConfiguration = this.componentManager.registerMockComponent(ModelConfiguration.class);
111  1 when(modelConfiguration.getDefaultReferenceValue(EntityType.WIKI)).thenReturn("xwiki");
112   
113    // Isolate from xwiki's model
114  1 WikiDescriptorManager wikiDescriptorManager =
115    this.componentManager.registerMockComponent(WikiDescriptorManager.class);
116  1 when(wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki");
117   
118    // Isolate from Environment
119  1 EntityResourceActionLister actionLister =
120    this.componentManager.registerMockComponent(EntityResourceActionLister.class);
121  1 when(actionLister.listActions()).thenReturn(Arrays.asList("view"));
122   
123    // Simulate a configured Execution Context
124  1 Execution execution = this.componentManager.registerMockComponent(Execution.class);
125  1 when(execution.getContext()).thenReturn(new ExecutionContext());
126   
127    // For test simplicity consider that Context CM == CM
128  1 this.componentManager.registerComponent(ComponentManager.class, "context", this.componentManager);
129    }
130   
 
131  1 toggle @Before
132    public void setUp() throws Exception
133    {
134  1 this.resourceTypeResolver = this.componentManager.getInstance(
135    new DefaultParameterizedType(null, ResourceTypeResolver.class, ExtendedURL.class));
136  1 this.resourceReferenceResolver = this.componentManager.getInstance(
137    new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class));
138    }
139   
 
140  1 toggle @Test
141    public void extractResourceReference() throws Exception
142    {
143    // Entity Resource References
144  1 assertURL("http://localhost:8080/xwiki/bin/view/space/page", EntityResourceReference.TYPE,
145    new EntityResourceReference(new DocumentReference("xwiki", "space", "page"), EntityResourceAction.VIEW));
146  1 assertURL("http://localhost:8080/xwiki/wiki/mywiki/view/space/page", new ResourceType("wiki"),
147    new EntityResourceReference(new DocumentReference("mywiki", "space", "page"), EntityResourceAction.VIEW));
148   
149    // Resources Resource References
150  1 assertURL("http://localhost:8080/xwiki/resources/js/prototype/prototype.js", ResourcesResourceReference.TYPE,
151    new ResourcesResourceReference());
152   
153    // Skins Resource References
154  1 assertURL("http://localhost:8080/xwiki/skins/flamingo/logo.png", SkinsResourceReference.TYPE,
155    new SkinsResourceReference());
156   
157    }
158   
 
159  4 toggle private void assertURL(String url, ResourceType expectedType, ResourceReference expectedReference) throws Exception
160    {
161  4 ExtendedURL extendedURL = new ExtendedURL(new URL(url), "xwiki");
162  4 ResourceType resourceType =
163    this.resourceTypeResolver.resolve(extendedURL, Collections.<String, Object>emptyMap());
164  4 assertEquals(expectedType.getId(), resourceType.getId());
165   
166  4 ResourceReference reference = this.resourceReferenceResolver.resolve(
167    extendedURL, resourceType, Collections.<String, Object>emptyMap());
168  4 assertEquals(expectedReference, reference);
169    }
170    }