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

File XObjectEventGeneratorListenerTest.java

 

Code metrics

0
48
9
1
229
148
9
0.19
5.33
9
1

Classes

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