1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.api

File XWikiTest.java

 

Code metrics

2
53
7
1
175
126
8
0.15
7.57
7
1.14

Classes

Class Line # Actions
XWikiTest 52 53 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 4 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 com.xpn.xwiki.api;
21   
22    import java.util.Calendar;
23    import java.util.Collections;
24    import java.util.HashMap;
25    import java.util.Map;
26    import java.util.Random;
27   
28    import org.jmock.Mock;
29    import org.jmock.core.Invocation;
30    import org.jmock.core.stub.CustomStub;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.rendering.syntax.Syntax;
33   
34    import com.xpn.xwiki.XWikiConfig;
35    import com.xpn.xwiki.XWikiContext;
36    import com.xpn.xwiki.XWikiException;
37    import com.xpn.xwiki.doc.XWikiDocument;
38    import com.xpn.xwiki.doc.XWikiDocumentArchive;
39    import com.xpn.xwiki.store.XWikiHibernateStore;
40    import com.xpn.xwiki.store.XWikiHibernateVersioningStore;
41    import com.xpn.xwiki.store.XWikiStoreInterface;
42    import com.xpn.xwiki.store.XWikiVersioningStoreInterface;
43    import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;
44    import com.xpn.xwiki.user.api.XWikiRightService;
45    import com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl;
46   
47    /**
48    * Unit tests for {@link com.xpn.xwiki.api.XWiki}.
49    *
50    * @version $Id: d794c6c5e69344e34b0d611ec0ff54cb193bcfe7 $
51    */
 
52    public class XWikiTest extends AbstractBridgedXWikiComponentTestCase
53    {
54    public static final Random rand = new Random(Calendar.getInstance().getTimeInMillis());
55   
56    private com.xpn.xwiki.XWiki xwiki;
57   
58    private Document apiDocument;
59   
60    private XWiki apiXWiki;
61   
62    private Mock mockXWikiStore;
63   
64    private Mock mockXWikiVersioningStore;
65   
66    private Mock mockXWikiRightService;
67   
68    private Map<String, XWikiDocument> docs = new HashMap<String, XWikiDocument>();
69   
 
70  4 toggle @Override
71    protected void setUp() throws Exception
72    {
73  4 super.setUp();
74  4 this.xwiki = new com.xpn.xwiki.XWiki();
75  4 getContext().setWiki(this.xwiki);
76  4 this.xwiki.setConfig(new XWikiConfig());
77   
78  4 this.apiXWiki = new XWiki(this.xwiki, getContext());
79   
80  4 this.mockXWikiStore =
81    mock(XWikiHibernateStore.class, new java.lang.Class[] {com.xpn.xwiki.XWiki.class, XWikiContext.class},
82    new java.lang.Object[] {this.xwiki, getContext()});
83  4 this.mockXWikiStore.stubs().method("loadXWikiDoc").will(
84    new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc")
85    {
 
86  16 toggle @Override
87    public java.lang.Object invoke(Invocation invocation) throws Throwable
88    {
89  16 XWikiDocument shallowDoc = (XWikiDocument) invocation.parameterValues.get(0);
90  16 if (XWikiTest.this.docs.containsKey(shallowDoc.getName())) {
91  6 return XWikiTest.this.docs.get(shallowDoc.getName());
92    } else {
93  10 return shallowDoc;
94    }
95    }
96    });
97  4 this.mockXWikiStore.stubs().method("saveXWikiDoc").will(
98    new CustomStub("Implements XWikiStoreInterface.saveXWikiDoc")
99    {
 
100  7 toggle @Override
101    public java.lang.Object invoke(Invocation invocation) throws Throwable
102    {
103  7 XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
104  7 document.setNew(false);
105  7 document.setStore((XWikiStoreInterface) XWikiTest.this.mockXWikiStore.proxy());
106  7 document.setId(rand.nextLong());
107  7 XWikiTest.this.docs.put(document.getName(), document);
108  7 return null;
109    }
110    });
111  4 this.mockXWikiStore.stubs().method("getTranslationList").will(returnValue(Collections.EMPTY_LIST));
112   
113  4 this.mockXWikiVersioningStore =
114    mock(XWikiHibernateVersioningStore.class, new java.lang.Class[] {com.xpn.xwiki.XWiki.class,
115    XWikiContext.class}, new java.lang.Object[] {this.xwiki, getContext()});
116  4 this.mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will(
117    returnValue(new XWikiDocumentArchive()));
118  4 this.mockXWikiVersioningStore.stubs().method("saveXWikiDocArchive").will(returnValue(null));
119   
120  4 this.mockXWikiRightService =
121    mock(XWikiRightServiceImpl.class, new java.lang.Class[] {}, new java.lang.Object[] {});
122  4 this.mockXWikiRightService.stubs().method("hasAccessLevel").will(returnValue(true));
123  4 this.mockXWikiRightService.stubs().method("hasProgrammingRights").will(returnValue(true));
124   
125  4 this.xwiki.setStore((XWikiStoreInterface) this.mockXWikiStore.proxy());
126  4 this.xwiki.setVersioningStore((XWikiVersioningStoreInterface) this.mockXWikiVersioningStore.proxy());
127  4 this.xwiki.setRightService((XWikiRightService) this.mockXWikiRightService.proxy());
128   
129  4 getContext().setUser("Redtail");
130  4 this.apiDocument =
131    new Document(new XWikiDocument(new DocumentReference("Wiki", "MilkyWay", "Fidis")), getContext());
132  4 this.apiDocument.getDocument().setCreator("c" + getContext().getUser());
133  4 this.apiDocument.getDocument().setAuthor("a" + getContext().getUser());
134  4 this.apiDocument.save();
135  4 getContext().setUser("Earth");
136    }
137   
 
138  1 toggle public void testAuthorIsntChangedAfterDocumentCopy() throws XWikiException
139    {
140  1 String copyName = "Lyre";
141  1 this.apiXWiki.copyDocument(this.apiDocument.getDocumentReference().getName(), copyName);
142  1 Document copy = this.apiXWiki.getDocument(copyName);
143   
144  1 assertEquals("XWiki.Earth", copy.getAuthor());
145    }
146   
 
147  1 toggle public void testCreatorIsntChangedAfterDocumentCopy() throws XWikiException
148    {
149  1 String copyName = "Sirius";
150  1 this.apiXWiki.copyDocument(this.apiDocument.getDocumentReference().getName(), copyName);
151  1 Document copy = this.apiXWiki.getDocument(copyName);
152   
153  1 assertEquals("XWiki.Earth", copy.getCreator());
154    }
155   
 
156  1 toggle public void testCreationDateAfterDocumentCopy() throws XWikiException
157    {
158  1 String copyName = this.apiDocument.getDocumentReference().getName() + "Copy";
159  1 long startTime = (Calendar.getInstance().getTimeInMillis() / 1000) * 1000;
160  1 this.apiXWiki.copyDocument(this.apiDocument.getDocumentReference().getName(), copyName);
161  1 long endTime = (Calendar.getInstance().getTimeInMillis() / 1000) * 1000;
162  1 long copyCreationTime = this.apiXWiki.getDocument(copyName).getCreationDate().getTime();
163   
164  1 assertTrue(startTime <= copyCreationTime && copyCreationTime <= endTime);
165    }
166   
 
167  1 toggle public void testGetAvailableRendererSyntax()
168    {
169  1 assertEquals(Syntax.PLAIN_1_0, this.apiXWiki.getAvailableRendererSyntax("plain", "1.0"));
170  1 assertEquals(Syntax.PLAIN_1_0, this.apiXWiki.getAvailableRendererSyntax("Plain", "1.0"));
171  1 assertEquals(Syntax.PLAIN_1_0, this.apiXWiki.getAvailableRendererSyntax("plain", null));
172  1 assertNull(this.apiXWiki.getAvailableRendererSyntax("plai", "1.0"));
173  1 assertNull(this.apiXWiki.getAvailableRendererSyntax("plai", null));
174    }
175    }