1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.event

File CommentEventGeneratorListenerTest.java

 

Code metrics

0
23
4
1
150
95
4
0.17
5.75
4
1

Classes

Class Line # Actions
CommentEventGeneratorListenerTest 66 23 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 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.internal.event;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.xwiki.component.manager.ComponentLookupException;
26    import org.xwiki.model.internal.DefaultModelConfiguration;
27    import org.xwiki.model.internal.reference.DefaultEntityReferenceProvider;
28    import org.xwiki.model.internal.reference.DefaultStringDocumentReferenceResolver;
29    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceResolver;
30    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceSerializer;
31    import org.xwiki.model.internal.reference.DefaultSymbolScheme;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.observation.ObservationManager;
34    import org.xwiki.observation.event.Event;
35    import org.xwiki.test.annotation.ComponentList;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37   
38    import com.xpn.xwiki.doc.XWikiDocument;
39    import com.xpn.xwiki.internal.model.reference.CurrentEntityReferenceProvider;
40    import com.xpn.xwiki.internal.model.reference.CurrentReferenceDocumentReferenceResolver;
41    import com.xpn.xwiki.internal.model.reference.CurrentReferenceEntityReferenceResolver;
42    import com.xpn.xwiki.objects.BaseObject;
43    import com.xpn.xwiki.objects.classes.BaseClass;
44    import com.xpn.xwiki.test.MockitoOldcoreRule;
45   
46    import static org.mockito.ArgumentMatchers.any;
47    import static org.mockito.ArgumentMatchers.same;
48    import static org.mockito.Mockito.verify;
49   
50    /**
51    * Validate {@link CommentEventGeneratorListener}.
52    *
53    * @version $Id: a591c3c085e880900004ed370bc2cb6eac306f08 $
54    */
55    @ComponentList({
56    CurrentReferenceDocumentReferenceResolver.class,
57    CurrentReferenceEntityReferenceResolver.class,
58    CurrentEntityReferenceProvider.class,
59    DefaultModelConfiguration.class,
60    DefaultStringEntityReferenceSerializer.class,
61    DefaultStringDocumentReferenceResolver.class,
62    DefaultStringEntityReferenceResolver.class,
63    DefaultEntityReferenceProvider.class,
64    DefaultSymbolScheme.class
65    })
 
66    public class CommentEventGeneratorListenerTest
67    {
68    public MockitoComponentMockingRule<CommentEventGeneratorListener> mocker =
69    new MockitoComponentMockingRule<CommentEventGeneratorListener>(CommentEventGeneratorListener.class);
70   
71    @Rule
72    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule(mocker);
73   
74    private ObservationManager mockObservation;
75   
76    private XWikiDocument commentXClassDocument;
77   
78    private BaseClass commentXClass;
79   
80    private BaseObject commentXObject;
81   
82    private XWikiDocument document;
83   
84    private XWikiDocument documentOrigin;
85   
 
86  3 toggle @Before
87    public void before() throws Exception
88    {
89  3 this.commentXClassDocument = new XWikiDocument(new DocumentReference("wiki", "XWiki", "XWikiComments"));
90  3 this.commentXClass = this.commentXClassDocument.getXClass();
91  3 this.commentXClass.addTextAreaField("comment", "comment", 60, 20);
92   
93  3 this.document = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
94  3 this.documentOrigin = new XWikiDocument(this.document.getDocumentReference());
95  3 this.document.setOriginalDocument(this.documentOrigin);
96   
97  3 this.commentXObject = new BaseObject();
98  3 this.commentXObject.setXClassReference(this.commentXClass.getDocumentReference());
99   
100  3 this.mockObservation = this.mocker.getInstance(ObservationManager.class);
101    }
102   
 
103  1 toggle @Test
104    public void testAddComment() throws ComponentLookupException
105    {
106  1 this.document.addXObject(this.commentXObject);
107   
108  1 final Event event = new CommentAddedEvent("wiki:space.page", "0");
109   
110  1 this.mocker.getComponentUnderTest().onEvent(new XObjectAddedEvent(this.commentXObject.getReference()), this.document,
111    this.oldcore.getXWikiContext());
112   
113    // Make sure the listener generated a comment added event
114  1 verify(this.mockObservation)
115    .notify(any(event.getClass()), same(document), same(this.oldcore.getXWikiContext()));
116    }
117   
 
118  1 toggle @Test
119    public void testDeleteComment() throws ComponentLookupException
120    {
121  1 this.documentOrigin.addXObject(this.commentXObject);
122   
123  1 final Event event = new CommentDeletedEvent("wiki:space.page", "0");
124   
125  1 this.mocker.getComponentUnderTest().onEvent(new XObjectDeletedEvent(this.commentXObject.getReference()),
126    this.document, this.oldcore.getXWikiContext());
127   
128    // Make sure the listener generated a comment deleted event
129  1 verify(this.mockObservation)
130    .notify(any(event.getClass()), same(document), same(this.oldcore.getXWikiContext()));
131    }
132   
 
133  1 toggle @Test
134    public void testModifiedComment() throws ComponentLookupException
135    {
136  1 this.document.addXObject(this.commentXObject);
137  1 this.documentOrigin.addXObject(this.commentXObject.clone());
138   
139  1 this.commentXObject.setStringValue("comment", "comment");
140   
141  1 final Event event = new CommentUpdatedEvent("wiki:space.page", "0");
142   
143  1 this.mocker.getComponentUnderTest().onEvent(new XObjectUpdatedEvent(this.commentXObject.getReference()),
144    this.document, this.oldcore.getXWikiContext());
145   
146    // Make sure the listener generated a comment updated event
147  1 verify(this.mockObservation)
148    .notify(any(event.getClass()), same(document), same(this.oldcore.getXWikiContext()));
149    }
150    }