| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 40 |
|
|
| 41 |
|
@version |
| 42 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (55) |
Complexity: 3 |
Complexity Density: 0.06 |
|
| 43 |
|
public class StatsUtilTest extends AbstractBridgedXWikiComponentTestCase |
| 44 |
|
{ |
| 45 |
|
private Mock mockXWiki; |
| 46 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 47 |
2 |
@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 |
|
@link |
| 59 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 60 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (40) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
| 75 |
1 |
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 |
|
} |