1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.parser.reference

File DefaultLinkReferenceParserTest.java

 

Code metrics

0
94
2
1
194
129
2
0.02
47
2
1

Classes

Class Line # Actions
DefaultLinkReferenceParserTest 44 94 0% 2 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.rendering.internal.parser.reference;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.xwiki.rendering.listener.reference.DocumentResourceReference;
26    import org.xwiki.rendering.listener.reference.InterWikiResourceReference;
27    import org.xwiki.rendering.listener.reference.ResourceReference;
28    import org.xwiki.rendering.listener.reference.ResourceType;
29    import org.xwiki.rendering.parser.ResourceReferenceParser;
30    import org.xwiki.rendering.wiki.WikiModel;
31    import org.xwiki.test.annotation.AllComponents;
32    import org.xwiki.test.mockito.MockitoComponentManagerRule;
33   
34    import static org.mockito.Mockito.when;
35    import static org.junit.Assert.*;
36   
37    /**
38    * Integration tests for {@link DefaultLinkReferenceParser} in the context of XWiki.
39    *
40    * @version $Id: b0681159e18eb14bac4276891cfec4195dd2d086 $
41    * @since 2.6M1
42    */
43    @AllComponents
 
44    public class DefaultLinkReferenceParserTest
45    {
46    @Rule
47    public final MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
48   
49    private ResourceReferenceParser parser;
50   
 
51  1 toggle @Before
52    public void setUp() throws Exception
53    {
54  1 this.parser = this.componentManager.getInstance(ResourceReferenceParser.class, "link");
55    }
56   
 
57  1 toggle @Test
58    public void parseWhenInWikiMode() throws Exception
59    {
60    // Create a Mock WikiModel implementation so that the link parser works in wiki mode
61  1 WikiModel mockWikiModel = this.componentManager.registerMockComponent(WikiModel.class);
62   
63  1 ResourceReference reference = this.parser.parse("");
64  1 assertEquals("", reference.getReference());
65  1 assertFalse(reference.isTyped());
66  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
67  1 assertEquals("Typed = [false] Type = [doc] Reference = []", reference.toString());
68   
69  1 when(mockWikiModel.isDocumentAvailable(new DocumentResourceReference("existingpage"))).thenReturn(true);
70  1 reference = this.parser.parse("existingpage");
71  1 assertEquals("existingpage", reference.getReference());
72  1 assertFalse(reference.isTyped());
73  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
74  1 assertEquals("Typed = [false] Type = [doc] Reference = [existingpage]", reference.toString());
75   
76  1 reference = this.parser.parse("unexistingpage");
77  1 assertEquals("unexistingpage", reference.getReference());
78  1 assertFalse(reference.isTyped());
79  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
80  1 assertEquals("Typed = [false] Type = [doc] Reference = [unexistingpage]", reference.toString());
81   
82  1 reference = this.parser.parse("space.unexistingpage");
83  1 assertEquals("space.unexistingpage", reference.getReference());
84  1 assertFalse(reference.isTyped());
85  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
86  1 assertEquals("Typed = [false] Type = [doc] Reference = [space.unexistingpage]", reference.toString());
87   
88  1 reference = this.parser.parse("http://xwiki.org");
89  1 assertEquals("http://xwiki.org", reference.getReference());
90  1 assertFalse(reference.isTyped());
91  1 assertEquals(ResourceType.URL, reference.getType());
92  1 assertEquals("Typed = [false] Type = [url] Reference = [http://xwiki.org]", reference.toString());
93   
94    // Verify mailto: URI is recognized
95  1 reference = this.parser.parse("mailto:john@smith.com?subject=test");
96  1 assertEquals("john@smith.com?subject=test", reference.getReference());
97  1 assertTrue(reference.isTyped());
98  1 assertEquals(ResourceType.MAILTO, reference.getType());
99  1 assertEquals("Typed = [true] Type = [mailto] Reference = [john@smith.com?subject=test]",
100    reference.toString());
101   
102    // Verify attach: URI is recognized
103  1 reference = this.parser.parse("attach:some:content");
104  1 assertEquals("some:content", reference.getReference());
105  1 assertTrue(reference.isTyped());
106  1 assertEquals(ResourceType.ATTACHMENT, reference.getType());
107  1 assertEquals("Typed = [true] Type = [attach] Reference = [some:content]", reference.toString());
108   
109    // Verify that unknown URIs are ignored
110    // Note: In this example we point to a document and we consider that myxwiki is the wiki name and
111    // http://xwiki.org is the page name
112  1 reference = this.parser.parse("mywiki:http://xwiki.org");
113  1 assertEquals("mywiki:http://xwiki.org", reference.getReference());
114  1 assertFalse(reference.isTyped());
115  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
116  1 assertEquals("Typed = [false] Type = [doc] Reference = [mywiki:http://xwiki.org]",
117    reference.toString());
118   
119    // Verify doc links work
120  1 reference = this.parser.parse("doc:wiki:space.page");
121  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
122  1 assertEquals("wiki:space.page", reference.getReference());
123  1 assertEquals("Typed = [true] Type = [doc] Reference = [wiki:space.page]", reference.toString());
124  1 assertTrue(reference.isTyped());
125   
126    // Verify space links work
127  1 reference = this.parser.parse("space:wiki:space");
128  1 assertEquals(ResourceType.SPACE, reference.getType());
129  1 assertEquals("wiki:space", reference.getReference());
130  1 assertEquals("Typed = [true] Type = [space] Reference = [wiki:space]", reference.toString());
131  1 assertTrue(reference.isTyped());
132   
133    // Verify InterWiki links work
134  1 reference = this.parser.parse("interwiki:alias:content");
135  1 assertEquals(ResourceType.INTERWIKI, reference.getType());
136  1 assertEquals("content", reference.getReference());
137  1 assertTrue(reference.isTyped());
138  1 assertEquals("alias", ((InterWikiResourceReference) reference).getInterWikiAlias());
139  1 assertEquals("Typed = [true] Type = [interwiki] Reference = [content] "
140    + "Parameters = [[interWikiAlias] = [alias]]", reference.toString());
141   
142    // Verify that an invalid InterWiki link is considered as Document link
143  1 reference = this.parser.parse("interwiki:invalid_since_doesnt_have_colon");
144  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
145  1 assertEquals("interwiki:invalid_since_doesnt_have_colon", reference.getReference());
146  1 assertFalse(reference.isTyped());
147  1 assertEquals("Typed = [false] Type = [doc] Reference = [interwiki:invalid_since_doesnt_have_colon]",
148    reference.toString());
149   
150    // Verify typed URLs
151  1 reference = this.parser.parse("url:http://xwiki.org");
152  1 assertEquals(ResourceType.URL, reference.getType());
153  1 assertTrue(reference.isTyped());
154  1 assertEquals("http://xwiki.org", reference.getReference());
155  1 assertEquals("Typed = [true] Type = [url] Reference = [http://xwiki.org]", reference.toString());
156   
157    // Verify query string and anchors have no meaning in link reference to documents.
158  1 reference = this.parser.parse("Hello World?no=queryString#notAnAnchor");
159  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
160  1 assertEquals("Hello World?no=queryString#notAnAnchor", reference.getReference());
161  1 assertFalse(reference.isTyped());
162  1 assertNull(((DocumentResourceReference) reference).getAnchor());
163  1 assertNull(((DocumentResourceReference) reference).getQueryString());
164  1 assertEquals("Typed = [false] Type = [doc] Reference = [Hello World?no=queryString#notAnAnchor]",
165    reference.toString());
166   
167    // Verify that the interwiki separator from XWiki Syntax 2.0 has not meaning in link references to documents
168  1 reference = this.parser.parse("page@alias");
169  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
170  1 assertFalse(reference.isTyped());
171  1 assertEquals("page@alias", reference.getReference());
172  1 assertEquals("Typed = [false] Type = [doc] Reference = [page@alias]", reference.toString());
173   
174    // Verify path link types
175  1 reference = this.parser.parse("path:/some/path");
176  1 assertEquals(ResourceType.PATH, reference.getType());
177  1 assertTrue(reference.isTyped());
178  1 assertEquals("/some/path", reference.getReference());
179  1 assertEquals("Typed = [true] Type = [path] Reference = [/some/path]", reference.toString());
180   
181    // Verify UNC link types
182  1 reference = this.parser.parse("unc:\\\\myserver\\myshare\\mydoc.txt");
183  1 assertEquals(ResourceType.UNC, reference.getType());
184  1 assertTrue(reference.isTyped());
185  1 assertEquals("\\\\myserver\\myshare\\mydoc.txt", reference.getReference());
186  1 assertEquals("Typed = [true] Type = [unc] Reference = [\\\\myserver\\myshare\\mydoc.txt]",
187    reference.toString());
188   
189    // Verify that reference escapes are left as is by the link parser
190  1 reference = this.parser.parse("pa\\.ge");
191  1 assertEquals(ResourceType.DOCUMENT, reference.getType());
192  1 assertEquals("pa\\.ge", reference.getReference());
193    }
194    }