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

File XWiki20ImageReferenceParserTest.java

 

Code metrics

0
19
4
1
112
69
4
0.21
4.75
4
1

Classes

Class Line # Actions
XWiki20ImageReferenceParserTest 56 19 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 2 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.type.AttachmentResourceReferenceTypeParser;
30    import org.xwiki.rendering.internal.parser.reference.type.URLResourceReferenceTypeParser;
31    import org.xwiki.rendering.listener.reference.ResourceReference;
32    import org.xwiki.rendering.listener.reference.ResourceType;
33    import org.xwiki.rendering.parser.ResourceReferenceParser;
34    import org.xwiki.rendering.wiki.WikiModel;
35    import org.xwiki.test.annotation.BeforeComponent;
36    import org.xwiki.test.annotation.ComponentList;
37    import org.xwiki.test.mockito.MockitoComponentManagerRule;
38   
39    import static org.junit.Assert.assertEquals;
40    import static org.junit.Assert.assertFalse;
41    import static org.mockito.Mockito.when;
42   
43    /**
44    * Unit tests for {@link XWiki20ImageReferenceParser}.
45    *
46    * @version $Id: 2ab5325ba020a788f7b26d8951506770b0609d76 $
47    * @since 2.5RC1
48    */
49    //@formatter:off
50    @ComponentList({
51    XWiki20ImageReferenceParser.class,
52    URLResourceReferenceTypeParser.class,
53    AttachmentResourceReferenceTypeParser.class
54    })
55    //@formatter:on
 
56    public class XWiki20ImageReferenceParserTest
57    {
58    @Rule
59    public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
60   
61    private ResourceReferenceParser parser;
62   
 
63  2 toggle @BeforeComponent
64    public void setUpComponents() throws Exception
65    {
66    // Create a Mock WikiModel implementation so that the image parser works in wiki mode
67  2 this.componentManager.registerMockComponent(WikiModel.class);
68   
69  2 Provider<ComponentManager> contextComponentManagerProvider = this.componentManager.registerMockComponent(
70    new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
71  2 when(contextComponentManagerProvider.get()).thenReturn(this.componentManager);
72    }
73   
 
74  2 toggle @Before
75    public void setUp() throws Exception
76    {
77  2 this.parser = this.componentManager.getInstance(ResourceReferenceParser.class, "xwiki/2.0/image");
78    }
79   
 
80  1 toggle @Test
81    public void testParseImagesCommon() throws Exception
82    {
83    // Verify that non-typed image referencing an attachment works.
84  1 ResourceReference reference = this.parser.parse("wiki:space.page@filename");
85  1 assertEquals(ResourceType.ATTACHMENT, reference.getType());
86  1 assertEquals("wiki:space.page@filename", reference.getReference());
87  1 assertEquals("Typed = [false] Type = [attach] Reference = [wiki:space.page@filename]",
88    reference.toString());
89  1 assertFalse(reference.isTyped());
90   
91    // Verify that non-typed image referencing a URL works.
92  1 reference = this.parser.parse("http://server/path/to/image");
93  1 assertEquals(ResourceType.URL, reference.getType());
94  1 assertEquals("http://server/path/to/image", reference.getReference());
95  1 assertEquals("Typed = [false] Type = [url] Reference = [http://server/path/to/image]",
96    reference.toString());
97  1 assertFalse(reference.isTyped());
98   
99    }
100   
 
101  1 toggle @Test
102    public void testParseImages() throws Exception
103    {
104    // Verify that "attach:" prefix isn't taken into account in XWiki Syntax 2.0.
105  1 ResourceReference reference = this.parser.parse("attach:wiki:space.page@filename");
106  1 assertEquals(ResourceType.ATTACHMENT, reference.getType());
107  1 assertEquals("attach:wiki:space.page@filename", reference.getReference());
108  1 assertFalse(reference.isTyped());
109  1 assertEquals("Typed = [false] Type = [attach] Reference = [attach:wiki:space.page@filename]",
110    reference.toString());
111    }
112    }