1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.observation

File ObservationContextTest.java

 

Code metrics

0
30
1
1
102
58
1
0.03
30
1
1

Classes

Class Line # Actions
ObservationContextTest 47 30 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.observation;
21   
22    import org.junit.jupiter.api.Test;
23    import org.xwiki.context.Execution;
24    import org.xwiki.context.ExecutionContext;
25    import org.xwiki.observation.event.BeginEvent;
26    import org.xwiki.observation.event.EndEvent;
27    import org.xwiki.observation.event.Event;
28    import org.xwiki.observation.internal.DefaultObservationContext;
29    import org.xwiki.observation.internal.DefaultObservationManager;
30    import org.xwiki.observation.internal.ObservationContextListener;
31    import org.xwiki.test.junit5.mockito.ComponentTest;
32    import org.xwiki.test.junit5.mockito.InjectMockComponents;
33    import org.xwiki.test.mockito.MockitoComponentManager;
34   
35    import static org.junit.jupiter.api.Assertions.assertFalse;
36    import static org.junit.jupiter.api.Assertions.assertTrue;
37    import static org.mockito.ArgumentMatchers.any;
38    import static org.mockito.Mockito.mock;
39    import static org.mockito.Mockito.when;
40   
41    /**
42    * Validate {@link DefaultObservationContext}.
43    *
44    * @version $Id: 9cbc6c388cc320775603153e2cdf9a6f62ce954c $
45    */
46    @ComponentTest
 
47    public class ObservationContextTest
48    {
49    @InjectMockComponents
50    private DefaultObservationContext observationContext;
51   
 
52  1 toggle @Test
53    public void test(MockitoComponentManager componentManager) throws Exception
54    {
55  1 componentManager.registerComponent(ObservationContextListener.class);
56  1 componentManager.registerComponent(DefaultObservationManager.class);
57   
58  1 ObservationManager manager = componentManager.getInstance(ObservationManager.class);
59  1 Execution execution = componentManager.getInstance(Execution.class);
60   
61  1 when(execution.getContext()).thenReturn(new ExecutionContext());
62   
63  1 BeginEvent beginEvent1 = mock(BeginEvent.class, "begin1");
64  1 BeginEvent beginEvent2 = mock(BeginEvent.class, "begin2");
65  1 EndEvent endEvent1 = mock(EndEvent.class, "end1");
66  1 EndEvent endEvent2 = mock(EndEvent.class, "end2");
67   
68  1 when(beginEvent1.matches(any(Event.class))).thenReturn(false);
69  1 when(beginEvent1.matches(beginEvent1)).thenReturn(true);
70   
71  1 when(beginEvent2.matches(any(Event.class))).thenReturn(false);
72  1 when(beginEvent2.matches(beginEvent2)).thenReturn(true);
73   
74  1 when(endEvent1.matches(any(Event.class))).thenReturn(false);
75  1 when(endEvent1.matches(endEvent1)).thenReturn(true);
76   
77  1 when(endEvent2.matches(any(Event.class))).thenReturn(false);
78  1 when(endEvent2.matches(endEvent2)).thenReturn(true);
79   
80  1 assertFalse(this.observationContext.isIn(beginEvent1));
81  1 assertFalse(this.observationContext.isIn(beginEvent2));
82   
83  1 manager.notify(beginEvent1, null);
84   
85  1 assertTrue(this.observationContext.isIn(beginEvent1));
86   
87  1 manager.notify(beginEvent2, null);
88   
89  1 assertTrue(this.observationContext.isIn(beginEvent1));
90  1 assertTrue(this.observationContext.isIn(beginEvent2));
91   
92  1 manager.notify(endEvent2, null);
93   
94  1 assertTrue(this.observationContext.isIn(beginEvent1));
95  1 assertFalse(this.observationContext.isIn(beginEvent2));
96   
97  1 manager.notify(endEvent1, null);
98   
99  1 assertFalse(this.observationContext.isIn(beginEvent1));
100  1 assertFalse(this.observationContext.isIn(beginEvent2));
101    }
102    }