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

File DefaultIOTargetServiceTest.java

 

Code metrics

0
139
25
1
344
264
25
0.18
5.56
25
1

Classes

Class Line # Actions
DefaultIOTargetServiceTest 46 139 0% 25 0
1.0100%
 

Contributing tests

This file is covered by 11 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.annotation.io.internal;
21   
22    import org.jmock.Expectations;
23    import org.junit.Test;
24    import org.xwiki.annotation.io.IOTargetService;
25    import org.xwiki.bridge.DocumentAccessBridge;
26    import org.xwiki.bridge.DocumentModelBridge;
27    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
28    import org.xwiki.component.manager.ComponentManager;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.model.reference.DocumentReferenceResolver;
31    import org.xwiki.rendering.syntax.Syntax;
32    import org.xwiki.rendering.syntax.SyntaxType;
33    import org.xwiki.test.jmock.AbstractComponentTestCase;
34   
35    import com.xpn.xwiki.web.Utils;
36   
37    import static org.junit.Assert.assertEquals;
38   
39    /**
40    * Tests the default implementation of {@link IOTargetService}, and integration with target resolvers, up to the
41    * document access bridge access.
42    *
43    * @version $Id: f9744d3001fbffb3833656889f7bdb3ca9f53620 $
44    * @since 2.3M1
45    */
 
46    public class DefaultIOTargetServiceTest extends AbstractComponentTestCase
47    {
48    /**
49    * Tested io target service.
50    */
51    private IOTargetService ioTargetService;
52   
53    /**
54    * Mock for the document access bridge.
55    */
56    private DocumentAccessBridge dabMock;
57   
58    /**
59    * Mock for DocumentReferenceResolver<String> used by BaseObjectReference
60    */
61    private DocumentReferenceResolver<String> classResolver;
62   
 
63  11 toggle @Override
64    protected void registerComponents() throws Exception
65    {
66  11 super.registerComponents();
67   
68    // register the dab
69  11 this.dabMock = registerMockComponent(DocumentAccessBridge.class);
70  11 getMockery().checking(new Expectations()
71    {
 
72  11 toggle {
73  11 allowing(dabMock).getCurrentUserReference();
74    }
75    });
76  11 this.classResolver = registerMockComponent(DocumentReferenceResolver.TYPE_STRING);
77   
78    // We don't care about multi CM
79  11 DefaultComponentDescriptor<ComponentManager> componentDescriptor = new DefaultComponentDescriptor<>();
80  11 componentDescriptor.setRoleType(ComponentManager.class);
81  11 componentDescriptor.setRoleHint("context");
82  11 getComponentManager().registerComponent(componentDescriptor, getComponentManager());
83    }
84   
 
85  11 toggle @Override
86    public void setUp() throws Exception
87    {
88  11 super.setUp();
89   
90    // get the default io target service
91  11 ioTargetService = getComponentManager().getInstance(IOTargetService.class);
92  11 Utils.setComponentManager(getComponentManager());
93    }
94   
 
95  1 toggle @Test
96    public void testGettersWhenTargetIsTypedDocument() throws Exception
97    {
98  1 final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
99  1 getMockery().checking(new Expectations()
100    {
 
101  1 toggle {
102  1 allowing(dabMock).getTranslatedDocumentInstance(new DocumentReference("wiki", "Space", "Page"));
103  1 will(returnValue(dmb));
104  1 oneOf(dmb).getContent();
105  1 will(returnValue("defcontent"));
106  1 oneOf(dmb).getSyntax();
107  1 will(returnValue(new Syntax(SyntaxType.XWIKI,"2.0")));
108    }
109    });
110   
111  1 String reference = "DOCUMENT://wiki:Space.Page";
112  1 assertEquals("defcontent", ioTargetService.getSource(reference));
113  1 assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
114    }
115   
 
116  1 toggle @Test
117    public void testGettersWhenTargetIsNonTypedDocument() throws Exception
118    {
119  1 final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
120  1 getMockery().checking(new Expectations()
121    {
 
122  1 toggle {
123  1 allowing(dabMock).getTranslatedDocumentInstance(new DocumentReference("wiki", "Space", "Page"));
124  1 will(returnValue(dmb));
125  1 oneOf(dmb).getContent();
126  1 will(returnValue("defcontent"));
127  1 oneOf(dmb).getSyntax();
128  1 will(returnValue(new Syntax(SyntaxType.XWIKI,"2.0")));
129    }
130    });
131   
132  1 String reference = "wiki:Space.Page";
133  1 assertEquals("defcontent", ioTargetService.getSource(reference));
134  1 assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
135    }
136   
 
137  1 toggle @Test
138    public void testGettersWhenTargetIsNonTypedRelativeDocument() throws Exception
139    {
140  1 final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
141  1 getMockery().checking(new Expectations()
142    {
 
143  1 toggle {
144    // default resolver should be used
145  1 allowing(dabMock).getTranslatedDocumentInstance(new DocumentReference("xwiki", "Space", "Page"));
146  1 will(returnValue(dmb));
147  1 oneOf(dmb).getContent();
148  1 will(returnValue("defcontent"));
149  1 oneOf(dmb).getSyntax();
150  1 will(returnValue(new Syntax(SyntaxType.XWIKI,"2.0")));
151    }
152    });
153   
154  1 String reference = "Space.Page";
155  1 assertEquals("defcontent", ioTargetService.getSource(reference));
156  1 assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
157    }
158   
 
159  1 toggle @Test
160    public void testGettersWhenTargetIsTypedRelativeDocument() throws Exception
161    {
162  1 final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
163  1 getMockery().checking(new Expectations()
164    {
 
165  1 toggle {
166    // default resolver should be used
167  1 allowing(dabMock).getTranslatedDocumentInstance(new DocumentReference("xwiki", "Space", "Page"));
168  1 will(returnValue(dmb));
169  1 oneOf(dmb).getContent();
170  1 will(returnValue("defcontent"));
171  1 oneOf(dmb).getSyntax();
172  1 will(returnValue(new Syntax(SyntaxType.XWIKI,"2.0")));
173    }
174    });
175   
176  1 String reference = "DOCUMENT://Space.Page";
177  1 assertEquals("defcontent", ioTargetService.getSource(reference));
178  1 assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
179    }
180   
 
181  1 toggle @Test
182    public void testGettersWhenTargetIsTypedSpace() throws Exception
183    {
184  1 final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
185  1 getMockery().checking(new Expectations()
186    {
 
187  1 toggle {
188    // default resolver should be used
189  1 oneOf(dabMock).getDocumentContent("SPACE://wiki:Space");
190  1 will(returnValue("defcontent"));
191  1 oneOf(dabMock).getDocumentSyntaxId("SPACE://wiki:Space");
192  1 will(returnValue("xwiki/2.0"));
193    }
194    });
195   
196    // expect source ref to be used as is, as it doesn't parse to something acceptable
197  1 String reference = "SPACE://wiki:Space";
198  1 assertEquals("defcontent", ioTargetService.getSource(reference));
199  1 assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
200    }
201   
 
202  1 toggle @Test
203    public void testGettersWhenTargetIsEmptyString() throws Exception
204    {
205  1 final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
206  1 getMockery().checking(new Expectations()
207    {
 
208  1 toggle {
209    // default resolver should be used. Note that this will fail if default values change, not very well
210    // isolated
211  1 allowing(dabMock).getTranslatedDocumentInstance(new DocumentReference("xwiki", "Main", "WebHome"));
212  1 will(returnValue(dmb));
213  1 oneOf(dmb).getContent();
214  1 will(returnValue("defcontent"));
215  1 oneOf(dmb).getSyntax();
216  1 will(returnValue(new Syntax(SyntaxType.XWIKI,"2.0")));
217    }
218    });
219   
220    // expect source ref to be used as is, as it doesn't parse to something acceptable
221  1 String reference = "";
222  1 assertEquals("defcontent", ioTargetService.getSource(reference));
223  1 assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
224    }
225   
 
226  1 toggle @Test
227    public void testGetterWhenTargetIsTypedIndexedObjectProperty() throws Exception
228    {
229  1 final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
230  1 getMockery().checking(new Expectations()
231    {
 
232  1 toggle {
233  1 allowing(classResolver).resolve("XWiki.Class", new DocumentReference("wiki", "Space", "Page"));
234  1 will(returnValue(new DocumentReference("wiki", "XWiki", "Class")));
235  1 oneOf(dabMock).getProperty(new DocumentReference("wiki", "Space", "Page"),
236    new DocumentReference("wiki", "XWiki", "Class"), 1, "property");
237  1 will(returnValue("defcontent"));
238  1 oneOf(dabMock).getTranslatedDocumentInstance(new DocumentReference("wiki", "Space", "Page"));
239  1 will(returnValue(dmb));
240  1 oneOf(dmb).getSyntax();
241  1 will(returnValue(new Syntax(SyntaxType.XWIKI,"2.0")));
242    }
243    });
244   
245  1 String reference = "OBJECT_PROPERTY://wiki:Space.Page^XWiki.Class[1].property";
246  1 assertEquals("defcontent", ioTargetService.getSource(reference));
247  1 assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
248    }
249   
 
250  1 toggle @Test
251    public void testGetterWhenTargetIsTypedDefaultObjectProperty() throws Exception
252    {
253  1 final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
254  1 getMockery().checking(new Expectations()
255    {
 
256  1 toggle {
257  1 allowing(classResolver).resolve("XWiki.Class", new DocumentReference("wiki", "Space", "Page"));
258  1 will(returnValue(new DocumentReference("wiki", "XWiki", "Class")));
259  1 oneOf(dabMock).getProperty(new DocumentReference("wiki", "Space", "Page"),
260    new DocumentReference("wiki", "XWiki", "Class"), "property");
261  1 will(returnValue("defcontent"));
262  1 oneOf(dabMock).getTranslatedDocumentInstance(new DocumentReference("wiki", "Space", "Page"));
263  1 will(returnValue(dmb));
264  1 oneOf(dmb).getSyntax();
265  1 will(returnValue(new Syntax(SyntaxType.XWIKI,"2.0")));
266    }
267    });
268   
269  1 String reference = "OBJECT_PROPERTY://wiki:Space.Page^XWiki.Class.property";
270  1 assertEquals("defcontent", ioTargetService.getSource(reference));
271  1 assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
272    }
273   
 
274  1 toggle @Test
275    public void testGetterWhenTargetIsTypedObjectPropertyInRelativeDocument() throws Exception
276    {
277  1 final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
278  1 getMockery().checking(new Expectations()
279    {
 
280  1 toggle {
281  1 allowing(classResolver).resolve("XWiki.Class", new DocumentReference("xwiki", "Main", "Page"));
282  1 will(returnValue(new DocumentReference("xwiki", "XWiki", "Class")));
283  1 oneOf(dabMock).getProperty(new DocumentReference("xwiki", "Main", "Page"),
284    new DocumentReference("xwiki", "XWiki", "Class"), "property");
285  1 will(returnValue("defcontent"));
286  1 oneOf(dabMock).getTranslatedDocumentInstance(new DocumentReference("xwiki", "Main", "Page"));
287  1 will(returnValue(dmb));
288  1 oneOf(dmb).getSyntax();
289  1 will(returnValue(new Syntax(SyntaxType.XWIKI,"2.0")));
290    }
291    });
292   
293  1 String reference = "OBJECT_PROPERTY://Page^XWiki.Class.property";
294  1 assertEquals("defcontent", ioTargetService.getSource(reference));
295  1 assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
296    }
297   
 
298  1 toggle @Test
299    public void testGetterWhenTargetIsNonTypedObjectProperty() throws Exception
300    {
301  1 final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
302  1 getMockery().checking(new Expectations()
303    {
 
304  1 toggle {
305    // target will be parsed as document, because document is the default
306  1 allowing(dabMock).getTranslatedDocumentInstance(new DocumentReference("wiki", "Space.Page^XWiki.Class", "property"));
307  1 will(returnValue(dmb));
308  1 oneOf(dmb).getContent();
309  1 will(returnValue("defcontent"));
310  1 oneOf(dmb).getSyntax();
311  1 will(returnValue(new Syntax(SyntaxType.XWIKI,"2.0")));
312    }
313    });
314   
315  1 String reference = "wiki:Space\\.Page^XWiki\\.Class.property";
316  1 assertEquals("defcontent", ioTargetService.getSource(reference));
317  1 assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
318    }
319   
 
320  1 toggle @Test
321    public void testGetterWhenTargetIsTypedIndexedRelativeObjectProperty() throws Exception
322    {
323  1 final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
324  1 getMockery().checking(new Expectations()
325    {
 
326  1 toggle {
327    // this will fail if defaults fail, not very well isolated
328  1 allowing(classResolver).resolve("Classes.Class", new DocumentReference("xwiki", "Main", "WebHome"));
329  1 will(returnValue(new DocumentReference("xwiki", "Classes", "Class")));
330  1 oneOf(dabMock).getProperty(new DocumentReference("xwiki", "Main", "WebHome"),
331    new DocumentReference("xwiki", "Classes", "Class"), 3, "property");
332  1 will(returnValue("defcontent"));
333  1 oneOf(dabMock).getTranslatedDocumentInstance(new DocumentReference("xwiki", "Main", "WebHome"));
334  1 will(returnValue(dmb));
335  1 oneOf(dmb).getSyntax();
336  1 will(returnValue(new Syntax(SyntaxType.XWIKI,"2.0")));
337    }
338    });
339   
340  1 String reference = "OBJECT_PROPERTY://Classes.Class[3].property";
341  1 assertEquals("defcontent", ioTargetService.getSource(reference));
342  1 assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
343    }
344    }