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

File ObservationContextTest.java

 

Code metrics

0
30
1
1
100
56
1
0.03
30
1
1

Classes

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