1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.eventstream

File EventAndFactoryTest.java

 

Code metrics

0
176
21
1
382
291
21
0.12
8.38
21
1

Classes

Class Line # Actions
EventAndFactoryTest 52 176 0% 21 0
1.0100%
 

Contributing tests

This file is covered by 17 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 org.xwiki.eventstream;
21   
22    import java.net.MalformedURLException;
23    import java.net.URL;
24    import java.util.Date;
25    import java.util.UUID;
26   
27    import org.junit.Assert;
28   
29    import org.jmock.Expectations;
30    import org.junit.Before;
31    import org.junit.Test;
32    import org.xwiki.bridge.DocumentAccessBridge;
33    import org.xwiki.context.Execution;
34    import org.xwiki.context.ExecutionContext;
35    import org.xwiki.eventstream.Event.Importance;
36    import org.xwiki.eventstream.internal.DefaultEventFactory;
37    import org.xwiki.model.EntityType;
38    import org.xwiki.model.reference.DocumentReference;
39    import org.xwiki.model.reference.EntityReferenceResolver;
40    import org.xwiki.model.reference.SpaceReference;
41    import org.xwiki.model.reference.WikiReference;
42    import org.xwiki.test.jmock.AbstractMockingComponentTestCase;
43    import org.xwiki.test.jmock.annotation.MockingRequirement;
44   
45    /**
46    * Tests for the {@link org.xwiki.eventstream.internal.DefaultEvent default event} and
47    * {@link org.xwiki.eventstream.internal.DefaultEventFactory default event factory}.
48    *
49    * @version $Id: 586a9a8c771f787898e12cbc85e1611a69fd6a58 $
50    */
51    @MockingRequirement(DefaultEventFactory.class)
 
52    public class EventAndFactoryTest extends AbstractMockingComponentTestCase
53    {
54    private EventFactory factory;
55   
56    Event defaultEvent;
57   
58    Event rawEvent;
59   
 
60  17 toggle @Before
61    public void configure() throws Exception
62    {
63  17 final DocumentAccessBridge mockDocumentAccessBridge =
64    getComponentManager().getInstance(DocumentAccessBridge.class);
65  17 getMockery().checking(new Expectations()
66    {
 
67  17 toggle {
68  17 allowing(mockDocumentAccessBridge).getCurrentUser();
69  17 will(returnValue("XWiki.Admin"));
70    }
71    });
72   
73  17 final ExecutionContext context = new ExecutionContext();
74  17 final Execution mockExecution = getComponentManager().getInstance(Execution.class);
75  17 getMockery().checking(new Expectations()
76    {
 
77  17 toggle {
78  17 allowing(mockExecution).getContext();
79  17 will(returnValue(context));
80    }
81    });
82   
83  17 final EntityReferenceResolver<String> mockResolver =
84    getComponentManager().getInstance(EntityReferenceResolver.TYPE_STRING);
85  17 getMockery().checking(new Expectations()
86    {
 
87  17 toggle {
88  17 allowing(mockResolver).resolve("XWiki.Admin", EntityType.DOCUMENT);
89  17 will(returnValue(new DocumentReference("xwiki", "XWiki", "Admin")));
90    }
91    });
92  17 this.factory = getComponentManager().getInstance(EventFactory.class);
93  17 this.defaultEvent = this.factory.createEvent();
94  17 this.rawEvent = this.factory.createRawEvent();
95    }
96   
 
97  1 toggle @Test
98    public void testId()
99    {
100  1 Assert.assertNotNull("Event ID not set on new event", this.defaultEvent.getId());
101  1 String id = UUID.randomUUID().toString();
102  1 this.defaultEvent.setId(id);
103  1 Assert.assertEquals("Event ID was not persisted on new event", id, this.defaultEvent.getId());
104   
105  1 Assert.assertNull(this.rawEvent.getId());
106  1 this.rawEvent.setId(id);
107  1 Assert.assertEquals("Event ID was not persisted on raw event", id, this.rawEvent.getId());
108   
109  1 this.rawEvent.setId(null);
110  1 Assert.assertNull(this.rawEvent.getId());
111    }
112   
 
113  1 toggle @Test
114    public void testGroupId()
115    {
116  1 Assert.assertNotNull("Group ID not set on new event", this.defaultEvent.getGroupId());
117  1 Assert.assertEquals("Consecutive events have different group identifiers", this.defaultEvent.getGroupId(),
118    this.factory.createEvent().getGroupId());
119  1 String id = UUID.randomUUID().toString();
120  1 this.defaultEvent.setGroupId(id);
121  1 Assert.assertEquals("Group ID was not persisted", id, this.defaultEvent.getGroupId());
122   
123  1 Assert.assertNull(this.rawEvent.getGroupId());
124  1 this.rawEvent.setGroupId(id);
125  1 Assert.assertEquals(id, this.rawEvent.getGroupId());
126   
127  1 this.rawEvent.setGroupId(null);
128  1 Assert.assertNull(this.rawEvent.getGroupId());
129    }
130   
 
131  1 toggle @Test
132    public void testType()
133    {
134  1 Assert.assertNull(this.defaultEvent.getType());
135  1 String type = "CommentAdded";
136  1 this.defaultEvent.setType(type);
137  1 Assert.assertEquals(type, this.defaultEvent.getType());
138   
139  1 Assert.assertNull(this.rawEvent.getType());
140  1 this.rawEvent.setType(type);
141  1 Assert.assertEquals(type, this.rawEvent.getType());
142   
143  1 this.rawEvent.setType(null);
144  1 Assert.assertNull(this.rawEvent.getType());
145    }
146   
 
147  1 toggle @Test
148    public void testDate()
149    {
150  1 Assert.assertNotNull(this.defaultEvent.getDate());
151  1 Assert.assertTrue(new Date().getTime() - this.defaultEvent.getDate().getTime() < 1000);
152  1 Date date = new Date();
153  1 this.defaultEvent.setDate(date);
154  1 Assert.assertEquals(date, this.defaultEvent.getDate());
155   
156  1 Assert.assertNull(this.rawEvent.getDate());
157  1 this.rawEvent.setDate(date);
158  1 Assert.assertEquals(date, this.rawEvent.getDate());
159   
160  1 this.rawEvent.setDate(null);
161  1 Assert.assertNull(this.rawEvent.getDate());
162    }
163   
 
164  1 toggle @Test
165    public void testImportance()
166    {
167  1 Assert.assertEquals(Importance.MEDIUM, this.defaultEvent.getImportance());
168  1 this.defaultEvent.setImportance(Importance.CRITICAL);
169  1 Assert.assertEquals(Importance.CRITICAL, this.defaultEvent.getImportance());
170   
171  1 Assert.assertEquals(Importance.MEDIUM, this.rawEvent.getImportance());
172  1 this.rawEvent.setImportance(Importance.BACKGROUND);
173  1 Assert.assertEquals(Importance.BACKGROUND, this.rawEvent.getImportance());
174   
175  1 this.rawEvent.setImportance(null);
176  1 Assert.assertEquals(Importance.MEDIUM, this.rawEvent.getImportance());
177    }
178   
 
179  1 toggle @Test
180    public void testApplication()
181    {
182  1 Assert.assertNull(this.defaultEvent.getApplication());
183  1 String app = "Comments";
184  1 this.defaultEvent.setApplication(app);
185  1 Assert.assertEquals(app, this.defaultEvent.getApplication());
186   
187  1 Assert.assertNull(this.rawEvent.getApplication());
188  1 this.rawEvent.setApplication(app);
189  1 Assert.assertEquals(app, this.rawEvent.getApplication());
190   
191  1 this.rawEvent.setApplication(null);
192  1 Assert.assertNull(this.rawEvent.getApplication());
193    }
194   
 
195  1 toggle @Test
196    public void testStream()
197    {
198  1 Assert.assertNull(this.defaultEvent.getStream());
199  1 String stream = "xwiki:XWiki.AdminGroup";
200  1 this.defaultEvent.setStream(stream);
201  1 Assert.assertEquals(stream, this.defaultEvent.getStream());
202   
203  1 Assert.assertNull(this.rawEvent.getStream());
204  1 this.rawEvent.setStream(stream);
205  1 Assert.assertEquals(stream, this.rawEvent.getStream());
206   
207  1 this.rawEvent.setStream(null);
208  1 Assert.assertNull(this.rawEvent.getStream());
209    }
210   
 
211  1 toggle @Test
212    public void testDocument()
213    {
214  1 Assert.assertNull(this.defaultEvent.getDocument());
215  1 DocumentReference ref = new DocumentReference("wiki", "Space", "Page");
216  1 this.defaultEvent.setDocument(ref);
217  1 Assert.assertEquals(ref, this.defaultEvent.getDocument());
218   
219  1 Assert.assertNull(this.rawEvent.getDocument());
220  1 this.rawEvent.setDocument(ref);
221  1 Assert.assertEquals(ref, this.rawEvent.getDocument());
222   
223  1 this.defaultEvent.setDocument(null);
224  1 Assert.assertNull(this.defaultEvent.getDocument());
225    }
226   
 
227  1 toggle @Test
228    public void testSpace()
229    {
230  1 Assert.assertNull(this.defaultEvent.getSpace());
231  1 DocumentReference doc = new DocumentReference("wiki1", "Space1", "Page");
232  1 SpaceReference space = new SpaceReference("Space2", new WikiReference("wiki2"));
233   
234  1 this.defaultEvent.setDocument(doc);
235  1 Assert.assertEquals(doc.getLastSpaceReference(), this.defaultEvent.getSpace());
236  1 Assert.assertEquals("Space1", this.defaultEvent.getSpace().getName());
237   
238  1 this.defaultEvent.setSpace(space);
239  1 Assert.assertEquals(space, this.defaultEvent.getSpace());
240  1 Assert.assertEquals("Space2", this.defaultEvent.getSpace().getName());
241   
242  1 this.defaultEvent.setSpace(null);
243  1 Assert.assertEquals(doc.getLastSpaceReference(), this.defaultEvent.getSpace());
244  1 this.defaultEvent.setDocument(null);
245  1 Assert.assertNull(this.defaultEvent.getSpace());
246   
247  1 Assert.assertNull(this.rawEvent.getSpace());
248    }
249   
 
250  1 toggle @Test
251    public void testWiki()
252    {
253  1 Assert.assertNull(this.defaultEvent.getWiki());
254  1 DocumentReference doc = new DocumentReference("wiki1", "Space1", "Page");
255  1 SpaceReference space = new SpaceReference("Space2", new WikiReference("wiki2"));
256  1 WikiReference wiki = new WikiReference("wiki3");
257   
258  1 this.defaultEvent.setDocument(doc);
259  1 Assert.assertEquals(doc.getWikiReference(), this.defaultEvent.getWiki());
260  1 Assert.assertEquals("wiki1", this.defaultEvent.getWiki().getName());
261   
262  1 this.defaultEvent.setSpace(space);
263  1 Assert.assertEquals(space.getRoot(), this.defaultEvent.getWiki());
264  1 Assert.assertEquals("wiki2", this.defaultEvent.getWiki().getName());
265   
266  1 this.defaultEvent.setWiki(wiki);
267  1 Assert.assertEquals(wiki, this.defaultEvent.getWiki());
268  1 Assert.assertEquals("wiki3", this.defaultEvent.getWiki().getName());
269   
270  1 this.defaultEvent.setWiki(null);
271  1 Assert.assertEquals(space.getRoot(), this.defaultEvent.getWiki());
272  1 this.defaultEvent.setSpace(null);
273  1 Assert.assertEquals(doc.getWikiReference(), this.defaultEvent.getWiki());
274  1 this.defaultEvent.setDocument(null);
275  1 Assert.assertNull(this.defaultEvent.getWiki());
276   
277  1 Assert.assertNull(this.rawEvent.getWiki());
278    }
279   
 
280  1 toggle @Test
281    public void testRelatedEntity()
282    {
283  1 Assert.assertNull(this.defaultEvent.getRelatedEntity());
284  1 DocumentReference ref = new DocumentReference("wiki", "Space", "Page");
285  1 this.defaultEvent.setRelatedEntity(ref);
286  1 Assert.assertEquals(ref, this.defaultEvent.getRelatedEntity());
287   
288  1 this.defaultEvent.setRelatedEntity(null);
289  1 Assert.assertNull(this.defaultEvent.getRelatedEntity());
290   
291  1 Assert.assertNull(this.rawEvent.getRelatedEntity());
292    }
293   
 
294  1 toggle @Test
295    public void testDocumentVersion()
296    {
297  1 Assert.assertNull(this.defaultEvent.getDocumentVersion());
298  1 String version = "4.2";
299  1 this.defaultEvent.setDocumentVersion(version);
300  1 Assert.assertEquals(version, this.defaultEvent.getDocumentVersion());
301   
302  1 this.defaultEvent.setDocumentVersion(null);
303  1 Assert.assertNull(this.defaultEvent.getDocumentVersion());
304   
305  1 Assert.assertNull(this.rawEvent.getDocumentVersion());
306    }
307   
 
308  1 toggle @Test
309    public void testDocumentTitle()
310    {
311  1 Assert.assertNull(this.defaultEvent.getDocumentTitle());
312  1 String title = "Welcome to your wiki";
313  1 this.defaultEvent.setDocumentTitle(title);
314  1 Assert.assertEquals(title, this.defaultEvent.getDocumentTitle());
315   
316  1 this.defaultEvent.setDocumentTitle(null);
317  1 Assert.assertNull(this.defaultEvent.getDocumentTitle());
318   
319  1 Assert.assertNull(this.rawEvent.getDocumentTitle());
320    }
321   
 
322  1 toggle @Test
323    public void testUser()
324    {
325  1 Assert.assertNotNull(this.defaultEvent.getUser());
326  1 DocumentReference user = new DocumentReference("xwiki", "XWiki", "Admin");
327  1 Assert.assertEquals(user, this.defaultEvent.getUser());
328   
329  1 user = new DocumentReference("wiki2", "XWiki", "jdoe");
330  1 this.defaultEvent.setUser(user);
331  1 Assert.assertEquals(user, this.defaultEvent.getUser());
332   
333  1 this.defaultEvent.setUser(null);
334  1 Assert.assertNull(this.defaultEvent.getUser());
335   
336  1 Assert.assertNull(this.rawEvent.getUser());
337  1 this.rawEvent.setUser(user);
338  1 Assert.assertEquals(user, this.rawEvent.getUser());
339    }
340   
 
341  1 toggle @Test
342    public void testURL() throws MalformedURLException
343    {
344  1 Assert.assertNull(this.defaultEvent.getUrl());
345  1 URL url = new URL("http://xwiki.org/xwiki/bin/Some/Page");
346  1 this.defaultEvent.setUrl(url);
347  1 Assert.assertEquals(url, this.defaultEvent.getUrl());
348   
349  1 this.defaultEvent.setUrl(null);
350  1 Assert.assertNull(this.defaultEvent.getUrl());
351   
352  1 Assert.assertNull(this.rawEvent.getUrl());
353    }
354   
 
355  1 toggle @Test
356    public void testTitle()
357    {
358  1 Assert.assertNull(this.defaultEvent.getTitle());
359  1 String title = "Deleted attachment file.png";
360  1 this.defaultEvent.setTitle(title);
361  1 Assert.assertEquals(title, this.defaultEvent.getTitle());
362   
363  1 this.defaultEvent.setTitle(null);
364  1 Assert.assertNull(this.defaultEvent.getTitle());
365   
366  1 Assert.assertNull(this.rawEvent.getTitle());
367    }
368   
 
369  1 toggle @Test
370    public void testBody()
371    {
372  1 Assert.assertNull(this.defaultEvent.getBody());
373  1 String body = "I **do** believe in fairies!";
374  1 this.defaultEvent.setBody(body);
375  1 Assert.assertEquals(body, this.defaultEvent.getBody());
376   
377  1 this.defaultEvent.setBody(null);
378  1 Assert.assertNull(this.defaultEvent.getBody());
379   
380  1 Assert.assertNull(this.rawEvent.getBody());
381    }
382    }