1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.internal.parser.xwiki20

File XWiki20LinkReferenceParserTest.java

 

Code metrics

0
92
5
1
227
161
5
0.05
18.4
5
1

Classes

Class Line # Actions
XWiki20LinkReferenceParserTest 68 92 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 3 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.rendering.internal.parser.xwiki20;
21   
22    import javax.inject.Provider;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.component.manager.ComponentManager;
28    import org.xwiki.component.util.DefaultParameterizedType;
29    import org.xwiki.rendering.internal.parser.reference.DefaultUntypedLinkReferenceParser;
30    import org.xwiki.rendering.internal.parser.reference.type.AttachmentResourceReferenceTypeParser;
31    import org.xwiki.rendering.internal.parser.reference.type.DocumentResourceReferenceTypeParser;
32    import org.xwiki.rendering.internal.parser.reference.type.MailtoResourceReferenceTypeParser;
33    import org.xwiki.rendering.internal.parser.reference.type.SpaceResourceReferenceTypeParser;
34    import org.xwiki.rendering.internal.parser.reference.type.URLResourceReferenceTypeParser;
35    import org.xwiki.rendering.listener.reference.DocumentResourceReference;
36    import org.xwiki.rendering.listener.reference.InterWikiResourceReference;
37    import org.xwiki.rendering.listener.reference.ResourceReference;
38    import org.xwiki.rendering.listener.reference.ResourceType;
39    import org.xwiki.rendering.parser.ResourceReferenceParser;
40    import org.xwiki.rendering.wiki.WikiModel;
41    import org.xwiki.test.annotation.BeforeComponent;
42    import org.xwiki.test.annotation.ComponentList;
43    import org.xwiki.test.mockito.MockitoComponentManagerRule;
44   
45    import static org.junit.Assert.assertEquals;
46    import static org.junit.Assert.assertFalse;
47    import static org.junit.Assert.assertNull;
48    import static org.junit.Assert.assertTrue;
49    import static org.mockito.Mockito.when;
50   
51    /**
52    * Unit tests for {@link XWiki20LinkReferenceParser}.
53    *
54    * @version $Id: c5e4ff2162e270e60d61b4bf7b5a0c94bb49da6a $
55    * @since 2.5RC1
56    */
57    //@formatter:off
58    @ComponentList({
59    XWiki20LinkReferenceParser.class,
60    URLResourceReferenceTypeParser.class,
61    MailtoResourceReferenceTypeParser.class,
62    AttachmentResourceReferenceTypeParser.class,
63    DefaultUntypedLinkReferenceParser.class,
64    DocumentResourceReferenceTypeParser.class,
65    SpaceResourceReferenceTypeParser.class
66    })
67    //@formatter:on
 
68    public class XWiki20LinkReferenceParserTest
69    {
70    @Rule
71    public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
72   
73    protected ResourceReferenceParser parser;
74   
 
75  3 toggle @BeforeComponent
76    public void setUpComponents() throws Exception
77    {
78    // Create a Mock WikiModel implementation so that the link parser works in wiki mode
79  3 this.componentManager.registerMockComponent(WikiModel.class);
80   
81  3 Provider<ComponentManager> contextComponentManagerProvider = this.componentManager.registerMockComponent(
82    new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
83  3 when(contextComponentManagerProvider.get()).thenReturn(this.componentManager);
84    }
85   
 
86  3 toggle @Before
87    public void setUp() throws Exception
88    {
89  3 this.parser = this.componentManager.getInstance(ResourceReferenceParser.class, "xwiki/2.0/link");
90    }
91   
 
92  1 toggle @Test
93    public void testParseLinksWhenInWikiModeCommon() throws Exception
94    {
95  1 ResourceReference reference = this.parser.parse("");
96  1 assertEquals("", reference.getReference());
97  1 assertFalse(reference.isTyped());
98  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
99  1 assertEquals("Typed = [false] Type = [doc] Reference = []", reference.toString());
100   
101  1 reference = this.parser.parse("Hello World");
102  1 assertEquals("Hello World", reference.getReference());
103  1 assertFalse(reference.isTyped());
104  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
105  1 assertEquals("Typed = [false] Type = [doc] Reference = [Hello World]", reference.toString());
106   
107  1 reference = this.parser.parse("http://xwiki.org");
108  1 assertEquals("http://xwiki.org", reference.getReference());
109  1 assertFalse(reference.isTyped());
110  1 assertEquals(ResourceType.URL, reference.getType());
111  1 assertEquals("Typed = [false] Type = [url] Reference = [http://xwiki.org]", reference.toString());
112   
113    // Verify mailto: URI is recognized
114  1 reference = this.parser.parse("mailto:john@smith.com?subject=test");
115  1 assertEquals("john@smith.com?subject=test", reference.getReference());
116  1 assertTrue(reference.isTyped());
117  1 assertEquals(ResourceType.MAILTO, reference.getType());
118  1 assertEquals("Typed = [true] Type = [mailto] Reference = [john@smith.com?subject=test]",
119    reference.toString());
120   
121    // Verify attach: URI is recognized
122  1 reference = this.parser.parse("attach:some:content");
123  1 assertEquals("some:content", reference.getReference());
124  1 assertTrue(reference.isTyped());
125  1 assertEquals(ResourceType.ATTACHMENT, reference.getType());
126  1 assertEquals("Typed = [true] Type = [attach] Reference = [some:content]", reference.toString());
127   
128    // Verify that unknown URIs are ignored
129    // Note: In this example we point to a document and we consider that myxwiki is the wiki name and
130    // http://xwiki.org is the page name
131  1 reference = this.parser.parse("mywiki:http://xwiki.org");
132  1 assertEquals("mywiki:http://xwiki.org", reference.getReference());
133  1 assertFalse(reference.isTyped());
134  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
135  1 assertEquals("Typed = [false] Type = [doc] Reference = [mywiki:http://xwiki.org]", reference.toString());
136    }
137   
 
138  1 toggle @Test
139    public void testParseLinksWhenInWikiMode() throws Exception
140    {
141    // Test Query Strings in links to document
142  1 ResourceReference reference = this.parser.parse("Hello World?xredirect=../whatever");
143  1 assertEquals("Hello World", reference.getReference());
144  1 assertEquals("xredirect=../whatever", ((DocumentResourceReference) reference).getQueryString());
145  1 assertFalse(reference.isTyped());
146  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
147  1 assertEquals("Typed = [false] Type = [doc] Reference = [Hello World] "
148    + "Parameters = [[queryString] = [xredirect=../whatever]]", reference.toString());
149   
150  1 reference = this.parser.parse("HelloWorld?xredirect=http://xwiki.org");
151  1 assertEquals("HelloWorld", reference.getReference());
152  1 assertEquals("xredirect=http://xwiki.org", ((DocumentResourceReference) reference).getQueryString());
153  1 assertFalse(reference.isTyped());
154  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
155  1 assertEquals("Typed = [false] Type = [doc] Reference = [HelloWorld] "
156    + "Parameters = [[queryString] = [xredirect=http://xwiki.org]]", reference.toString());
157   
158    // Test Anchors in links to documents
159  1 reference = this.parser.parse("#anchor");
160  1 assertEquals("anchor", ((DocumentResourceReference) reference).getAnchor());
161  1 assertFalse(reference.isTyped());
162  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
163  1 assertEquals("Typed = [false] Type = [doc] Reference = [] Parameters = [[anchor] = [anchor]]",
164    reference.toString());
165   
166  1 reference = this.parser.parse("Hello#anchor");
167  1 assertEquals("Hello", reference.getReference());
168  1 assertEquals("anchor", ((DocumentResourceReference) reference).getAnchor());
169  1 assertFalse(reference.isTyped());
170  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
171  1 assertEquals("Typed = [false] Type = [doc] Reference = [Hello] Parameters = [[anchor] = [anchor]]",
172    reference.toString());
173   
174    // Test InterWiki links
175  1 reference = this.parser.parse("HelloWorld#anchor?param1=1&param2=2@wikipedia");
176  1 assertEquals("HelloWorld#anchor?param1=1&param2=2", reference.getReference());
177  1 assertEquals("wikipedia", ((InterWikiResourceReference) reference).getInterWikiAlias());
178  1 assertTrue(reference.isTyped());
179  1 assertEquals(ResourceType.INTERWIKI, reference.getType());
180  1 assertEquals("Typed = [true] Type = [interwiki] Reference = [HelloWorld#anchor?param1=1&param2=2] "
181    + "Parameters = [[interWikiAlias] = [wikipedia]]", reference.toString());
182   
183    // Verify in XWiki Syntax 2.0 the "doc" prefix is not meaningful
184  1 reference = this.parser.parse("doc:whatever");
185  1 assertEquals("doc:whatever", reference.getReference());
186  1 assertFalse(reference.isTyped());
187  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
188  1 assertEquals("Typed = [false] Type = [doc] Reference = [doc:whatever]", reference.toString());
189    }
190   
 
191  1 toggle @Test
192    public void testParseLinksWithEscapes() throws Exception
193    {
194  1 ResourceReference reference = this.parser.parse("\\.\\#notanchor");
195  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
196  1 assertEquals("\\.#notanchor", reference.getReference());
197  1 assertNull(((DocumentResourceReference) reference).getAnchor());
198   
199  1 reference = this.parser.parse("page\\?notquerystring");
200  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
201  1 assertEquals("page?notquerystring", reference.getReference());
202  1 assertNull(((DocumentResourceReference) reference).getQueryString());
203   
204    // Verify that \ can be escaped and that escaped chars in query string, and anchors are escaped
205  1 reference = this.parser.parse("page\\\\#anchor\\\\?querystring\\\\");
206  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
207  1 assertEquals("page\\\\", reference.getReference());
208  1 assertEquals("anchor\\", ((DocumentResourceReference) reference).getAnchor());
209  1 assertEquals("querystring\\", ((DocumentResourceReference) reference).getQueryString());
210   
211  1 reference = this.parser.parse("pa\\.ge\\?query\\#anchor\\@notinterwiki");
212  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
213  1 assertEquals("pa\\.ge?query#anchor@notinterwiki", reference.getReference());
214   
215    // Verify that \ can be escaped and that escaped chars in query string, anchors and InterWiki aliases are
216    // escaped.
217  1 reference = this.parser.parse("page\\\\#anchor\\\\?querystring\\\\@alias\\\\");
218  1 assertEquals(ResourceType.INTERWIKI, reference.getType());
219  1 assertEquals("page\\#anchor\\?querystring\\", reference.getReference());
220  1 assertEquals("alias\\", ((InterWikiResourceReference) reference).getInterWikiAlias());
221   
222  1 reference = this.parser.parse("something\\\\@inter\\@wikilink");
223  1 assertEquals(ResourceType.INTERWIKI, reference.getType());
224  1 assertEquals("something\\", reference.getReference());
225  1 assertEquals("inter@wikilink", ((InterWikiResourceReference) reference).getInterWikiAlias());
226    }
227    }