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

File XClassPropertyEventGeneratorListenerTest.java

 

Code metrics

0
27
6
1
176
114
6
0.22
4.5
6
1

Classes

Class Line # Actions
XClassPropertyEventGeneratorListenerTest 74 27 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 5 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.classes.BaseClass;
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 XClassPropertyEventGeneratorListener}.
57    *
58    * @version $Id: a94ff631998089a20e1493bfe9ed9cc4096567d2 $
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 XClassPropertyEventGeneratorListenerTest
75    {
76    public MockitoComponentMockingRule<XClassPropertyEventGeneratorListener> mocker =
77    new MockitoComponentMockingRule<XClassPropertyEventGeneratorListener>(
78    XClassPropertyEventGeneratorListener.class);
79   
80    @Rule
81    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule(mocker);
82   
83    private ObservationManager mockObservation;
84   
85    private XWikiDocument document;
86   
87    private XWikiDocument documentOrigin;
88   
89    private BaseClass xclass;
90   
91    private BaseClass xclassOrigin;
92   
 
93  5 toggle @Before
94    public void before() throws Exception
95    {
96  5 this.document = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
97  5 this.documentOrigin = new XWikiDocument(this.document.getDocumentReference());
98  5 this.document.setOriginalDocument(this.documentOrigin);
99   
100  5 this.xclass = this.document.getXClass();
101  5 this.xclassOrigin = this.documentOrigin.getXClass();
102   
103  5 this.mockObservation = this.mocker.getInstance(ObservationManager.class);
104    }
105   
 
106  1 toggle @Test
107    public void testAddDocument() throws ComponentLookupException
108    {
109  1 this.xclass.addTextField("property", "Property", 30);
110   
111  1 final Event event = new XClassPropertyAddedEvent(this.xclass.getField("property").getReference());
112   
113  1 this.mocker.getComponentUnderTest().onEvent(new DocumentCreatedEvent(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 testDeleteDocument() throws ComponentLookupException
122    {
123  1 this.xclassOrigin.addTextField("property", "Property", 30);
124   
125  1 final Event event = new XClassPropertyDeletedEvent(this.xclassOrigin.getField("property").getReference());
126   
127  1 this.mocker.getComponentUnderTest().onEvent(new DocumentDeletedEvent(this.document.getDocumentReference()),
128    this.document, this.oldcore.getXWikiContext());
129   
130    // Make sure the listener generated a xobject added 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 testModifiedDocumentXClassPropertyAdded() throws ComponentLookupException
136    {
137  1 this.xclass.addTextField("property", "Property", 30);
138   
139  1 final Event event = new XClassPropertyAddedEvent(this.xclass.getField("property").getReference());
140   
141  1 this.mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(this.document.getDocumentReference()),
142    this.document, this.oldcore.getXWikiContext());
143   
144    // Make sure the listener generated a xobject added event
145  1 verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
146    }
147   
 
148  1 toggle @Test
149    public void testModifiedDocumentXClassPropertyDeleted() throws ComponentLookupException
150    {
151  1 this.xclassOrigin.addTextField("property", "Property", 30);
152   
153  1 final Event event = new XClassPropertyDeletedEvent(this.xclassOrigin.getField("property").getReference());
154   
155  1 this.mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(this.document.getDocumentReference()),
156    this.document, this.oldcore.getXWikiContext());
157   
158    // Make sure the listener generated a xobject added event
159  1 verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
160    }
161   
 
162  1 toggle @Test
163    public void testModifiedDocumentXClassPropertyModified() throws ComponentLookupException
164    {
165  1 this.xclassOrigin.addTextField("property", "Property", 30);
166  1 this.xclass.addTextField("property", "New Property", 30);
167   
168  1 final Event event = new XClassPropertyUpdatedEvent(this.xclassOrigin.getField("property").getReference());
169   
170  1 this.mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(this.document.getDocumentReference()),
171    this.document, this.oldcore.getXWikiContext());
172   
173    // Make sure the listener generated a xobject added event
174  1 verify(this.mockObservation).notify(eq(event), same(this.document), same(this.oldcore.getXWikiContext()));
175    }
176    }