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

File BaseObjectTest.java

 

Code metrics

2
73
7
1
189
129
8
0.11
10.43
7
1.14

Classes

Class Line # Actions
BaseObjectTest 44 73 0% 8 2
0.975609897.6%
 

Contributing tests

This file is covered by 7 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.objects;
21   
22    import java.util.List;
23   
24    import org.junit.Assert;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.logging.LogLevel;
28    import org.xwiki.logging.event.LogEvent;
29    import org.xwiki.model.EntityType;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.model.reference.EntityReference;
32   
33    import com.xpn.xwiki.doc.merge.MergeConfiguration;
34    import com.xpn.xwiki.doc.merge.MergeResult;
35    import com.xpn.xwiki.test.MockitoOldcoreRule;
36    import com.xpn.xwiki.test.reference.ReferenceComponentList;
37   
38    /**
39    * Unit tests for the {@link BaseObject} class.
40    *
41    * @version $Id: 63537e566def83c3eb66135525918db5238fe6b4 $
42    */
43    @ReferenceComponentList
 
44    public class BaseObjectTest
45    {
46    @Rule
47    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
48   
 
49  1 toggle @Test
50    public void testSetDocumentReference() throws Exception
51    {
52  1 BaseObject baseObject = new BaseObject();
53   
54  1 DocumentReference reference = new DocumentReference("wiki", "space", "page");
55  1 baseObject.setDocumentReference(reference);
56   
57  1 Assert.assertEquals(reference, baseObject.getDocumentReference());
58    }
59   
 
60  1 toggle @Test
61    public void testSetName() throws Exception
62    {
63  1 String database = this.oldcore.getXWikiContext().getWikiId();
64  1 BaseObject baseObject = new BaseObject();
65   
66  1 baseObject.setName("space.page");
67   
68  1 Assert.assertEquals(database, baseObject.getDocumentReference().getWikiReference().getName());
69  1 Assert.assertEquals("space", baseObject.getDocumentReference().getLastSpaceReference().getName());
70  1 Assert.assertEquals("page", baseObject.getDocumentReference().getName());
71    }
72   
 
73  1 toggle @Test
74    public void testSetNameAloneWithChangingContext() throws Exception
75    {
76  1 String database = this.oldcore.getXWikiContext().getWikiId();
77  1 BaseObject baseObject = new BaseObject();
78   
79  1 baseObject.setName("space.page");
80   
81  1 try {
82  1 this.oldcore.getXWikiContext().setWikiId("otherwiki");
83   
84  1 Assert.assertEquals(database, baseObject.getDocumentReference().getWikiReference().getName());
85  1 Assert.assertEquals("space", baseObject.getDocumentReference().getLastSpaceReference().getName());
86  1 Assert.assertEquals("page", baseObject.getDocumentReference().getName());
87   
88  1 baseObject.setName("otherspace.otherpage");
89    } finally {
90  1 this.oldcore.getXWikiContext().setWikiId(database);
91    }
92   
93  1 Assert.assertEquals(database, baseObject.getDocumentReference().getWikiReference().getName());
94  1 Assert.assertEquals("otherspace", baseObject.getDocumentReference().getLastSpaceReference().getName());
95  1 Assert.assertEquals("otherpage", baseObject.getDocumentReference().getName());
96   
97  1 baseObject = new BaseObject();
98  1 try {
99  1 this.oldcore.getXWikiContext().setWikiId("otherwiki");
100  1 baseObject.setName("space.page");
101    } finally {
102  1 this.oldcore.getXWikiContext().setWikiId(database);
103    }
104   
105  1 Assert.assertEquals("otherwiki", baseObject.getDocumentReference().getWikiReference().getName());
106  1 Assert.assertEquals("space", baseObject.getDocumentReference().getLastSpaceReference().getName());
107  1 Assert.assertEquals("page", baseObject.getDocumentReference().getName());
108   
109  1 baseObject.setName("otherspace.otherpage");
110   
111  1 Assert.assertEquals("otherwiki", baseObject.getDocumentReference().getWikiReference().getName());
112  1 Assert.assertEquals("otherspace", baseObject.getDocumentReference().getLastSpaceReference().getName());
113  1 Assert.assertEquals("otherpage", baseObject.getDocumentReference().getName());
114    }
115   
 
116  1 toggle @Test
117    public void getReference()
118    {
119  1 BaseObject baseObject = new BaseObject();
120   
121  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
122  1 baseObject.setDocumentReference(documentReference);
123  1 DocumentReference classReference = new DocumentReference("wiki", "space", "class");
124  1 baseObject.setXClassReference(classReference);
125   
126  1 Assert.assertEquals(new BaseObjectReference(classReference, baseObject.getNumber(), documentReference),
127    baseObject.getReference());
128    }
129   
 
130  1 toggle @Test
131    public void setXClassReference()
132    {
133  1 BaseObject baseObject = new BaseObject();
134   
135  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
136  1 baseObject.setDocumentReference(documentReference);
137  1 DocumentReference classReference = new DocumentReference("otherwiki", "space", "class");
138  1 baseObject.setXClassReference(classReference);
139   
140  1 Assert.assertEquals(new DocumentReference("wiki", "space", "class"), baseObject.getXClassReference());
141  1 Assert.assertEquals(new EntityReference("class", EntityType.DOCUMENT, new EntityReference("space",
142    EntityType.SPACE)), baseObject.getRelativeXClassReference());
143    }
144   
 
145  1 toggle @Test
146    public void testMerge()
147    {
148  1 BaseObject previousObject = new BaseObject();
149  1 previousObject.setStringValue("str", "value");
150  1 BaseObject nextObject = new BaseObject();
151  1 nextObject.setStringValue("str", "newvalue");
152  1 BaseObject currentObject = new BaseObject();
153  1 currentObject.setStringValue("str", "value");
154   
155  1 MergeConfiguration mergeConfiguration = new MergeConfiguration();
156  1 MergeResult mergeResult = new MergeResult();
157   
158  1 currentObject
159    .merge(previousObject, nextObject, mergeConfiguration, this.oldcore.getXWikiContext(), mergeResult);
160   
161  1 List<LogEvent> errors = mergeResult.getLog().getLogsFrom(LogLevel.WARN);
162  1 if (errors.size() > 0) {
163  0 Assert.fail("Found error or warning during the merge (" + errors.get(0) + ")");
164    }
165   
166  1 Assert.assertEquals("newvalue", currentObject.getStringValue("str"));
167    }
168   
 
169  1 toggle @Test
170    public void testHashCode()
171    {
172  1 final int number = 101;
173   
174  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "document");
175  1 DocumentReference classReference = new DocumentReference("wiki", "space", "class");
176   
177  1 BaseObject o1 = new BaseObject();
178  1 o1.setDocumentReference(documentReference);
179  1 o1.setXClassReference(classReference);
180  1 BaseObject o2 = new BaseObject();
181  1 o2.setDocumentReference(documentReference);
182  1 o2.setXClassReference(classReference);
183   
184  1 o1.setNumber(number);
185  1 o2.setNumber(number);
186   
187  1 Assert.assertEquals(o1.hashCode(), o2.hashCode());
188    }
189    }