1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.watchlist.internal

File AutomaticWatchModeListenerTest.java

 

Code metrics

0
24
4
1
133
77
4
0.17
6
4
1

Classes

Class Line # Actions
AutomaticWatchModeListenerTest 57 24 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 org.xwiki.watchlist.internal;
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.WikiCreatingEvent;
27    import org.xwiki.context.Execution;
28    import org.xwiki.context.ExecutionContext;
29    import org.xwiki.context.internal.DefaultExecution;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.observation.EventListener;
32    import org.xwiki.observation.internal.DefaultObservationContext;
33    import org.xwiki.observation.internal.ObservationContextListener;
34    import org.xwiki.test.annotation.ComponentList;
35    import org.xwiki.test.mockito.MockitoComponentMockingRule;
36    import org.xwiki.watchlist.internal.api.AutomaticWatchMode;
37    import org.xwiki.watchlist.internal.api.WatchListStore;
38    import org.xwiki.watchlist.internal.api.WatchedElementType;
39   
40    import com.xpn.xwiki.XWiki;
41    import com.xpn.xwiki.XWikiContext;
42    import com.xpn.xwiki.doc.XWikiDocument;
43    import com.xpn.xwiki.internal.event.XARImportingEvent;
44   
45    import static org.mockito.ArgumentMatchers.any;
46    import static org.mockito.Mockito.mock;
47    import static org.mockito.Mockito.never;
48    import static org.mockito.Mockito.verify;
49    import static org.mockito.Mockito.when;
50   
51    /**
52    * Unit tests for {@link AutomaticWatchModeListener}.
53    *
54    * @version $Id: 7d1136b9423d33097bd5e10d6075211f15404244 $
55    */
56    @ComponentList({ObservationContextListener.class, DefaultExecution.class, DefaultObservationContext.class})
 
57    public class AutomaticWatchModeListenerTest
58    {
59    private WatchListStore mockStore;
60   
61    private EventListener observationContextListener;
62   
63    @Rule
64    public final MockitoComponentMockingRule<EventListener> mocker = new MockitoComponentMockingRule<EventListener>(
65    AutomaticWatchModeListener.class);
66   
 
67  3 toggle @Before
68    public void setUp() throws Exception
69    {
70  3 this.mockStore = mocker.getInstance(WatchListStore.class);
71   
72    // Make sure we have an Execution Context since the observationContextListener will store current events in it
73  3 Execution execution = mocker.getInstance(Execution.class);
74  3 execution.setContext(new ExecutionContext());
75   
76  3 this.observationContextListener = mocker.getInstance(EventListener.class, "ObservationContextListener");
77    }
78   
79    /**
80    * Verify that we don't do anything when the current event is inside a WikiCreatingEvent.
81    */
 
82  1 toggle @Test
83    public void onEventWhenInContextOfWikiCreatingEvent() throws Exception
84    {
85    // We simulate a WikiCreatingEvent in the Execution Context
86  1 this.observationContextListener.onEvent(new WikiCreatingEvent(), null, null);
87   
88  1 mocker.getComponentUnderTest().onEvent(new DocumentCreatedEvent(), null, null);
89   
90  1 verify(mockStore, never()).getAutomaticWatchMode(any());
91  1 verify(mockStore, never()).addWatchedElement(any(), any(), any(WatchedElementType.class));
92    }
93   
94    /**
95    * Verify that we don't do anything when the current event is inside a XARImportingEvent.
96    */
 
97  1 toggle @Test
98    public void onEventWhenInContextOXARImportingEvent() throws Exception
99    {
100    // We simulate a XARImportingEvent in the Execution Context
101  1 this.observationContextListener.onEvent(new XARImportingEvent(), null, null);
102   
103  1 mocker.getComponentUnderTest().onEvent(new DocumentCreatedEvent(), null, null);
104   
105  1 verify(mockStore, never()).getAutomaticWatchMode(any());
106  1 verify(mockStore, never()).addWatchedElement(any(), any(), any(WatchedElementType.class));
107    }
108   
 
109  1 toggle @Test
110    public void onEventWhenDocumentCreatedEvent() throws Exception
111    {
112  1 XWikiContext context = mock(XWikiContext.class);
113   
114  1 XWikiDocument document = mock(XWikiDocument.class);
115  1 DocumentReference documentReference = new DocumentReference("authorWiki", "authorSpace", "authorPage");
116  1 when(document.getContentAuthor()).thenReturn("content author");
117  1 when(document.getContentAuthorReference()).thenReturn(documentReference);
118  1 when(document.getPrefixedFullName()).thenReturn("authorSpace.authorPage");
119   
120  1 when(this.mockStore.getAutomaticWatchMode("content author")).thenReturn(AutomaticWatchMode.ALL);
121   
122  1 XWiki xwiki = mock(XWiki.class);
123  1 when(xwiki.exists(documentReference, context)).thenReturn(true);
124   
125  1 when(context.getWiki()).thenReturn(xwiki);
126   
127  1 mocker.getComponentUnderTest().onEvent(new DocumentCreatedEvent(), document, context);
128   
129    // Verify that the document is added to the watchlist
130  1 verify(this.mockStore).addWatchedElement("content author", "authorSpace.authorPage",
131    org.xwiki.watchlist.internal.api.WatchedElementType.DOCUMENT);
132    }
133    }