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

File XObjectEventGeneratorListenerTest.java

 

Code metrics

0
48
9
1
204
123
9
0.19
5.33
9
1

Classes

Class Line # Actions
XObjectEventGeneratorListenerTest 49 48 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 8 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.bridge.event.DocumentCreatedEvent;
26    import org.xwiki.bridge.event.DocumentDeletedEvent;
27    import org.xwiki.bridge.event.DocumentUpdatedEvent;
28    import org.xwiki.component.manager.ComponentLookupException;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.observation.ObservationManager;
31    import org.xwiki.observation.event.Event;
32    import org.xwiki.test.mockito.MockitoComponentMockingRule;
33   
34    import com.xpn.xwiki.doc.XWikiDocument;
35    import com.xpn.xwiki.objects.BaseObject;
36    import com.xpn.xwiki.test.MockitoOldcoreRule;
37    import com.xpn.xwiki.test.reference.ReferenceComponentList;
38   
39    import static org.mockito.ArgumentMatchers.eq;
40    import static org.mockito.ArgumentMatchers.same;
41    import static org.mockito.Mockito.verify;
42   
43    /**
44    * Validate {@link XObjectEventGeneratorListener}.
45    *
46    * @version $Id: 8a5e5c1f68653341677748ba10f3c0624f23cb97 $
47    */
48    @ReferenceComponentList
 
49    public class XObjectEventGeneratorListenerTest
50    {
51    public MockitoComponentMockingRule<XObjectEventGeneratorListener> mocker =
52    new MockitoComponentMockingRule<XObjectEventGeneratorListener>(XObjectEventGeneratorListener.class);
53   
54    @Rule
55    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule(mocker);
56   
57    private ObservationManager mockObservation;
58   
59    private XWikiDocument document;
60   
61    private XWikiDocument documentOrigin;
62   
63    private BaseObject xobject;
64   
 
65  8 toggle @Before
66    public void before() throws Exception
67    {
68  8 this.document = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
69  8 this.documentOrigin = new XWikiDocument(this.document.getDocumentReference());
70  8 this.document.setOriginalDocument(this.documentOrigin);
71   
72  8 this.xobject = new BaseObject();
73  8 this.xobject.setXClassReference(this.document.getDocumentReference());
74   
75  8 this.mockObservation = this.mocker.getInstance(ObservationManager.class);
76    }
77   
 
78  1 toggle @Test
79    public void testAddDocument() throws ComponentLookupException
80    {
81  1 this.document.addXObject(this.xobject);
82   
83  1 final Event event = new XObjectAddedEvent(this.xobject.getReference());
84   
85  1 this.mocker.getComponentUnderTest().onEvent(new DocumentCreatedEvent(this.document.getDocumentReference()),
86    this.document, this.oldcore.getXWikiContext());
87   
88    // Make sure the listener generated a xobject added event
89  1 verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
90    }
91   
 
92  1 toggle @Test
93    public void testDeleteDocument() throws ComponentLookupException
94    {
95  1 this.documentOrigin.addXObject(this.xobject);
96   
97  1 final Event event = new XObjectDeletedEvent(this.xobject.getReference());
98   
99  1 this.mocker.getComponentUnderTest().onEvent(new DocumentDeletedEvent(this.document.getDocumentReference()),
100    this.document, this.oldcore.getXWikiContext());
101   
102    // Make sure the listener generated a xobject deleted event
103  1 verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
104    }
105   
 
106  1 toggle @Test
107    public void testModifiedDocumentXObjectAdded() throws ComponentLookupException
108    {
109  1 this.document.addXObject(this.xobject);
110   
111  1 final Event event = new XObjectAddedEvent(this.xobject.getReference());
112   
113  1 this.mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(this.document.getDocumentReference()),
114    this.document, this.oldcore.getXWikiContext());
115   
116    // Make sure the listener generated a xobject added event
117  1 verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
118    }
119   
 
120  1 toggle @Test
121    public void testModifiedDocumentXObjectDeleted() throws ComponentLookupException
122    {
123  1 this.documentOrigin.addXObject(this.xobject);
124   
125  1 final Event event = new XObjectDeletedEvent(this.xobject.getReference());
126   
127  1 this.mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(this.document.getDocumentReference()),
128    this.document, this.oldcore.getXWikiContext());
129   
130    // Make sure the listener generated a xobject deleted event
131  1 verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
132    }
133   
 
134  1 toggle @Test
135    public void testModifiedDocumentXObjectModified() throws ComponentLookupException
136    {
137  1 this.document.addXObject(this.xobject);
138  1 this.documentOrigin.addXObject(this.xobject.clone());
139   
140  1 this.xobject.setStringValue("newproperty", "newvalue");
141   
142  1 final Event event = new XObjectUpdatedEvent(this.xobject.getReference());
143   
144  1 this.mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(this.document.getDocumentReference()),
145    this.document, this.oldcore.getXWikiContext());
146   
147    // Make sure the listener generated a xobject updated event
148  1 verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
149    }
150   
 
151  1 toggle @Test
152    public void testModifiedDocumentXObjectPropertyAdded() throws ComponentLookupException
153    {
154  1 this.document.addXObject(this.xobject);
155  1 this.documentOrigin.addXObject(this.xobject.clone());
156   
157  1 this.xobject.setStringValue("newproperty", "newvalue");
158   
159  1 final Event event = new XObjectPropertyAddedEvent(this.xobject.getField("newproperty").getReference());
160   
161  1 this.mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(this.document.getDocumentReference()),
162    this.document, this.oldcore.getXWikiContext());
163   
164    // Make sure the listener generated a xobject property added event
165  1 verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
166    }
167   
 
168  1 toggle @Test
169    public void testModifiedDocumentXObjectPropertyDeleted() throws ComponentLookupException
170    {
171  1 this.document.addXObject(this.xobject.clone());
172  1 this.documentOrigin.addXObject(this.xobject);
173   
174  1 this.xobject.setStringValue("deletedproperty", "deletedvalue");
175   
176  1 final Event event = new XObjectPropertyDeletedEvent(this.xobject.getField("deletedproperty").getReference());
177   
178  1 this.mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(this.document.getDocumentReference()),
179    this.document, this.oldcore.getXWikiContext());
180   
181    // Make sure the listener generated a xobject deleted event
182  1 verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
183    }
184   
 
185  1 toggle @Test
186    public void testModifiedDocumentXObjectPropertyModified() throws ComponentLookupException
187    {
188  1 this.xobject.setStringValue("updatedproperty", "propertyvalue");
189   
190  1 BaseObject xobjectModified = this.xobject.clone();
191  1 xobjectModified.setStringValue("updatedproperty", "propertyvaluemodified");
192   
193  1 this.document.addXObject(this.xobject);
194  1 this.documentOrigin.addXObject(xobjectModified);
195   
196  1 final Event event = new XObjectPropertyUpdatedEvent(this.xobject.getField("updatedproperty").getReference());
197   
198  1 this.mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(this.document.getDocumentReference()),
199    this.document, this.oldcore.getXWikiContext());
200   
201    // Make sure the listener generated a xobject property updated event
202  1 verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
203    }
204    }