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

File StatsUtilTest.java

 

Code metrics

0
52
3
1
144
89
3
0.06
17.33
3
1

Classes

Class Line # Actions
StatsUtilTest 43 52 0% 3 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 com.xpn.xwiki.stats.impl;
21   
22    import java.text.SimpleDateFormat;
23    import java.util.Arrays;
24    import java.util.Calendar;
25    import java.util.Collections;
26    import java.util.HashSet;
27   
28    import org.jmock.Mock;
29    import org.xwiki.model.reference.DocumentReference;
30   
31    import com.xpn.xwiki.XWiki;
32    import com.xpn.xwiki.XWikiException;
33    import com.xpn.xwiki.doc.XWikiDocument;
34    import com.xpn.xwiki.objects.BaseObject;
35    import com.xpn.xwiki.stats.impl.StatsUtil.PeriodType;
36    import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;
37   
38    /**
39    * Unit tests for the {@link StatsUtil} class.
40    *
41    * @version $Id: add9f514acd1e070170013b28fe0392959b71d4b $
42    */
 
43    public class StatsUtilTest extends AbstractBridgedXWikiComponentTestCase
44    {
45    private Mock mockXWiki;
46   
 
47  2 toggle @Override
48    protected void setUp() throws Exception
49    {
50  2 super.setUp();
51   
52  2 this.mockXWiki = mock(XWiki.class);
53   
54  2 getContext().setWiki((XWiki) this.mockXWiki.proxy());
55    }
56   
57    /**
58    * Test for the {@link StatsUtil#getPeriodAsInt(java.util.Date, PeriodType)}.
59    */
 
60  1 toggle public void testGetPeriodAsInt()
61    {
62  1 Calendar cal = Calendar.getInstance();
63   
64  1 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
65  1 String a = sdf.format(cal.getTime());
66  1 String b = StatsUtil.getPeriodAsInt(cal.getTime(), PeriodType.MONTH) + "";
67  1 assertEquals("Wrong month period format", a, b);
68   
69  1 sdf = new SimpleDateFormat("yyyyMMdd");
70  1 a = sdf.format(cal.getTime());
71  1 b = StatsUtil.getPeriodAsInt(cal.getTime(), PeriodType.DAY) + "";
72  1 assertEquals("Wrong day period format", a, b);
73    }
74   
 
75  1 toggle public void testGetFilteredUsers() throws XWikiException
76    {
77  1 this.mockXWiki.stubs().method("getXWikiPreference").will(returnValue(null));
78  1 this.mockXWiki.stubs().method("Param").will(returnValue(null));
79   
80  1 assertEquals(new HashSet<DocumentReference>(), StatsUtil.getRequestFilteredUsers(getContext()));
81   
82  1 this.mockXWiki.stubs().method("Param").will(returnValue("XWiki.XWikiGuest"));
83  1 this.mockXWiki.stubs().method("getDocument")
84    .will(returnValue(new XWikiDocument(new DocumentReference("xwiki", "XWiki", "XWikiGuest"))));
85   
86  1 assertEquals(
87    new HashSet<>(Collections.singletonList(new DocumentReference("xwiki", "XWiki", "XWikiGuest"))),
88    StatsUtil.getRequestFilteredUsers(getContext()));
89   
90  1 this.mockXWiki.stubs().method("Param").will(returnValue("XWiki.supeRadmin"));
91  1 this.mockXWiki.stubs().method("getDocument")
92    .will(returnValue(new XWikiDocument(new DocumentReference("xwiki", "XWiki", "supeRadmin"))));
93   
94  1 assertEquals(new HashSet<>(Collections.singletonList(new DocumentReference("xwiki", "XWiki", "supeRadmin"))),
95    StatsUtil.getRequestFilteredUsers(getContext()));
96   
97  1 this.mockXWiki.stubs().method("Param").will(returnValue("invalid"));
98  1 this.mockXWiki.stubs().method("getDocument").will(returnValue(
99    new XWikiDocument(new DocumentReference("xwiki", "Space", "invalid"))));
100   
101  1 assertEquals(new HashSet<DocumentReference>(), StatsUtil.getRequestFilteredUsers(getContext()));
102   
103  1 DocumentReference userReference = new DocumentReference("xwiki", "XWiki", "user");
104  1 final XWikiDocument userDoc = new XWikiDocument(userReference);
105  1 userDoc.setNew(false);
106  1 userDoc.addXObject(new DocumentReference("xwiki", "XWiki", "XWikiUsers"), new BaseObject());
107   
108  1 this.mockXWiki.stubs().method("Param").will(returnValue("XWiki.user"));
109  1 this.mockXWiki.stubs().method("getDocument").with(eq(userReference), ANYTHING).will(returnValue(userDoc));
110   
111  1 assertEquals(new HashSet<>(Collections.singletonList(userReference)),
112    StatsUtil.getRequestFilteredUsers(getContext()));
113   
114  1 DocumentReference user2Reference = new DocumentReference("xwiki", "XWiki", "user2");
115  1 final XWikiDocument user2Doc = new XWikiDocument(user2Reference);
116  1 user2Doc.setNew(false);
117  1 user2Doc.addXObject(new DocumentReference("xwiki", "XWiki", "XWikiUsers"), new BaseObject());
118   
119  1 this.mockXWiki.stubs().method("Param").will(returnValue("XWiki.user,XWiki.user2"));
120  1 this.mockXWiki.stubs().method("getDocument").with(eq(user2Reference), ANYTHING).will(returnValue(user2Doc));
121   
122  1 assertEquals(new HashSet<>(Arrays.asList(userReference, user2Reference)),
123    StatsUtil.getRequestFilteredUsers(getContext()));
124   
125  1 DocumentReference user3Reference = new DocumentReference("otherwiki", "XWiki", "user3");
126  1 final XWikiDocument user3Doc = new XWikiDocument(user3Reference);
127  1 user3Doc.setNew(false);
128  1 user3Doc.addXObject(new DocumentReference("otherwiki", "XWiki", "XWikiUsers"), new BaseObject());
129   
130  1 DocumentReference groupReference = new DocumentReference("otherwiki", "XWiki", "group");
131  1 final XWikiDocument groupDoc = new XWikiDocument(groupReference);
132  1 groupDoc.setNew(false);
133  1 BaseObject member = new BaseObject();
134  1 member.setStringValue("member", "user3");
135  1 groupDoc.addXObject(new DocumentReference("otherwiki", "XWiki", "XWikiGroups"), member);
136   
137  1 this.mockXWiki.stubs().method("Param").will(returnValue("XWiki.user,XWiki.user2,otherwiki:XWiki.group"));
138  1 this.mockXWiki.stubs().method("getDocument").with(eq(groupReference), ANYTHING).will(returnValue(groupDoc));
139  1 this.mockXWiki.stubs().method("getDocument").with(eq(user3Reference), ANYTHING).will(returnValue(user3Doc));
140   
141  1 assertEquals(new HashSet<>(Arrays.asList(userReference, user2Reference, user3Reference)),
142    StatsUtil.getRequestFilteredUsers(getContext()));
143    }
144    }