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

File PropertyClassTest.java

 

Code metrics

4
44
6
1
161
105
8
0.18
7.33
6
1.33

Classes

Class Line # Actions
PropertyClassTest 54 44 0% 8 3
0.944444494.4%
 

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.objects.classes;
21   
22    import java.util.Arrays;
23    import java.util.Random;
24    import java.util.concurrent.Callable;
25   
26    import org.junit.jupiter.api.BeforeEach;
27    import org.junit.jupiter.api.Test;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.model.reference.DocumentReferenceResolver;
30    import org.xwiki.model.reference.EntityReferenceSerializer;
31    import org.xwiki.rendering.syntax.Syntax;
32    import org.xwiki.security.authorization.AuthorExecutor;
33    import org.xwiki.test.junit5.mockito.MockComponent;
34   
35    import com.xpn.xwiki.doc.XWikiDocument;
36    import com.xpn.xwiki.objects.BaseObject;
37    import com.xpn.xwiki.test.MockitoOldcore;
38    import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
39    import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
40   
41    import static org.junit.jupiter.api.Assertions.assertEquals;
42    import static org.mockito.ArgumentMatchers.any;
43    import static org.mockito.ArgumentMatchers.eq;
44    import static org.mockito.Mockito.doReturn;
45    import static org.mockito.Mockito.when;
46   
47    /**
48    * Unit tests for the base {@link PropertyClass} class.
49    *
50    * @version $Id: 9ca5c7b57f871d221e87d3de187885b7dafdc517 $
51    * @since 2.4M2
52    */
53    @OldcoreTest
 
54    public class PropertyClassTest
55    {
56    @InjectMockitoOldcore
57    private MockitoOldcore oldCore;
58   
59    @MockComponent
60    private AuthorExecutor authorExecutor;
61   
62    private BaseClass xclass = new BaseClass();
63   
 
64  4 toggle @BeforeEach
65    public void before() throws Exception
66    {
67  4 DocumentReference classReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "Class");
68  4 XWikiDocument classDocument = new XWikiDocument(classReference);
69  4 classDocument.setSyntax(Syntax.XWIKI_2_1);
70  4 doReturn(classDocument).when(this.oldCore.getSpyXWiki()).getDocument(classReference,
71    this.oldCore.getXWikiContext());
72   
73  4 this.xclass.setOwnerDocument(classDocument);
74   
75  4 this.oldCore.getMocker().registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "compactwiki");
76  4 this.oldCore.getMocker().registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
77    }
78   
79    /** Test the {@link PropertyClass#compareTo(PropertyClass)} method. */
 
80  1 toggle @Test
81    public void testCompareTo()
82    {
83  1 PropertyClass one = new PropertyClass();
84  1 PropertyClass two = new PropertyClass();
85    // Random numbers to be used as property indexes.
86  1 int n1, n2;
87   
88  1 one.setName("first");
89  1 two.setName("second");
90   
91    // Since the test might randomly succeed, run it several times to be safer.
92  1 Random random = new Random();
93  21 for (int i = 0; i < 20; ++i) {
94  20 n1 = random.nextInt();
95  20 n2 = random.nextInt();
96  20 one.setNumber(n1);
97  20 two.setNumber(n2);
98   
99  20 if (n1 == n2) {
100  0 assertEquals(Math.signum(one.compareTo(two)), -1.0);
101  0 assertEquals(Math.signum(two.compareTo(one)), 1.0);
102    } else {
103  20 assertEquals(0, Float.compare(Math.signum(one.compareTo(two)), Math.signum(n1 - n2)));
104  20 assertEquals(0, Float.compare(Math.signum(two.compareTo(one)), Math.signum(n2 - n1)));
105    }
106    }
107   
108    // Also test that the comparison takes into account the name in case the two numbers are identical
109  1 one.setNumber(42);
110  1 two.setNumber(42);
111  1 assertEquals(Math.signum(one.compareTo(two)), -1.0);
112  1 assertEquals(Math.signum(two.compareTo(one)), 1.0);
113    }
114   
 
115  1 toggle @Test
116    public void displayCustomWithClassDisplayer() throws Exception
117    {
118  1 DocumentReference authorReference = new DocumentReference("wiki", "XWiki", "Alice");
119  1 this.xclass.getOwnerDocument().setAuthorReference(authorReference);
120   
121  1 displayCustomWithAuthor(authorReference, this.xclass.getDocumentReference());
122    }
123   
 
124  1 toggle @Test
125    public void displayCustomWithClassDisplayerAndClassIsNew() throws Exception
126    {
127  1 DocumentReference userReference = new DocumentReference("wiki", "XWiki", "Alice");
128  1 this.oldCore.getXWikiContext().setUserReference(userReference);
129   
130  1 displayCustomWithAuthor(userReference, this.xclass.getDocumentReference());
131    }
132   
 
133  1 toggle @Test
134    public void displayCustomWithClassDisplayerAndGuestAuthor() throws Exception
135    {
136  1 DocumentReference userReference = new DocumentReference("wiki", "XWiki", "Alice");
137  1 this.oldCore.getXWikiContext().setUserReference(userReference);
138   
139  1 this.xclass.getOwnerDocument().setNew(false);
140   
141  1 displayCustomWithAuthor(null, this.xclass.getDocumentReference());
142    }
143   
 
144  3 toggle private void displayCustomWithAuthor(DocumentReference authorReference, DocumentReference secureDocument)
145    throws Exception
146    {
147  3 when(this.authorExecutor.call(any(Callable.class), eq(authorReference), eq(secureDocument)))
148    .thenReturn("output");
149   
150  3 PropertyClass propertyClass = new PropertyClass();
151  3 propertyClass.setCustomDisplay("test");
152  3 propertyClass.setObject(this.xclass);
153   
154  3 StringBuffer buffer = new StringBuffer();
155   
156  3 propertyClass.displayCustom(buffer, "date", "Path.To.Class_0_", "edit", new BaseObject(),
157    this.oldCore.getXWikiContext());
158   
159  3 assertEquals("output", buffer.toString());
160    }
161    }