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

File CacheEntryListenerTest.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

10
30
13
3
213
100
19
0.63
2.31
4.33
1.46

Classes

Class Line # Actions
CacheEntryListenerTest 30 17 0% 11 9
0.689655269%
CacheEntryListenerTest.EventType 35 0 - 0 0
-1.0 -
CacheEntryListenerTest.EventWaiter 159 13 0% 8 7
0.708333370.8%
 

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 org.xwiki.cache.tests;
21   
22    import org.xwiki.cache.event.CacheEntryEvent;
23    import org.xwiki.cache.event.CacheEntryListener;
24   
25    /**
26    * Class used to test cache event management.
27    *
28    * @version $Id: 754feafef880b40ea737d575ada617c554a15b55 $
29    */
 
30    public class CacheEntryListenerTest implements CacheEntryListener<Object>
31    {
32    /**
33    *
34    */
 
35    public enum EventType
36    {
37    /**
38    * Add event.
39    */
40    ADD,
41   
42    /**
43    * Modify event.
44    */
45    MODIFY,
46   
47    /**
48    * Remove event.
49    */
50    REMOVE
51    }
52   
53    /**
54    * Event object received with last insertion.
55    */
56    private CacheEntryEvent<Object> addedEvent;
57   
58    /**
59    * Event object received with last modification.
60    */
61    private CacheEntryEvent<Object> modifiedEvent;
62   
63    /**
64    * Event object received with last remove.
65    */
66    private CacheEntryEvent<Object> removedEvent;
67   
68    /**
69    * @return event object received with last insertion.
70    */
 
71  4 toggle public CacheEntryEvent<Object> getAddedEvent()
72    {
73  4 return this.addedEvent;
74    }
75   
76    /**
77    * Set add event to null.
78    */
 
79  0 toggle public void reinitAddEvent()
80    {
81  0 this.addedEvent = null;
82    }
83   
84    /**
85    * @return event object received with last modification.
86    */
 
87  4 toggle public CacheEntryEvent<Object> getModifiedEvent()
88    {
89  4 return this.modifiedEvent;
90    }
91   
92    /**
93    * Set modified event to null.
94    */
 
95  0 toggle public void reinitModifiedEvent()
96    {
97  0 this.modifiedEvent = null;
98    }
99   
100    /**
101    * @return event object received with last remove.
102    */
 
103  7 toggle public CacheEntryEvent<Object> getRemovedEvent()
104    {
105  7 return this.removedEvent;
106    }
107   
108    /**
109    * Set removed event to null.
110    */
 
111  0 toggle public void reinitRemovedEvent()
112    {
113  0 this.removedEvent = null;
114    }
115   
 
116  7 toggle @Override
117    public void cacheEntryAdded(CacheEntryEvent<Object> event)
118    {
119  7 this.addedEvent = event;
120    }
121   
 
122  1 toggle @Override
123    public void cacheEntryModified(CacheEntryEvent<Object> event)
124    {
125  1 this.modifiedEvent = event;
126    }
127   
 
128  6 toggle @Override
129    public void cacheEntryRemoved(CacheEntryEvent<Object> event)
130    {
131  6 this.removedEvent = event;
132    }
133   
134    /**
135    * @param eventType event type.
136    * @return wait until it receive a entry removed event.
137    * @throws InterruptedException error
138    */
 
139  3 toggle public boolean waitForEntryEvent(EventType eventType) throws InterruptedException
140    {
141  3 EventWaiter eventWaiter = new EventWaiter(eventType);
142   
143  3 Thread thread = new Thread(eventWaiter);
144  3 thread.start();
145  3 thread.join(100000);
146   
147  3 if (thread.isAlive()) {
148  0 eventWaiter.stop();
149   
150  0 return false;
151    }
152   
153  3 return true;
154    }
155   
156    /**
157    * Event waiter.
158    */
 
159    class EventWaiter implements Runnable
160    {
161    /**
162    * Indicate that the thread should continue to run or have to stop.
163    */
164    private boolean run = true;
165   
166    /**
167    * The event type.
168    */
169    private EventType eventType;
170   
171    /**
172    * @param eventType the event type.
173    */
 
174  3 toggle EventWaiter(EventType eventType)
175    {
176  3 this.eventType = eventType;
177    }
178   
179    /**
180    * Stop.
181    */
 
182  0 toggle void stop()
183    {
184  0 this.run = false;
185    }
186   
 
187  3 toggle @Override
188    public void run()
189    {
190  3 CacheEntryEvent<Object> event;
191   
192  9 while (this.run) {
193  9 if (this.eventType == EventType.ADD) {
194  0 event = addedEvent;
195  9 } else if (this.eventType == EventType.MODIFY) {
196  0 event = modifiedEvent;
197    } else {
198  9 event = removedEvent;
199    }
200   
201  9 if (event != null) {
202  3 break;
203    }
204   
205  6 try {
206  6 Thread.sleep(500);
207    } catch (InterruptedException e) {
208    // ignore
209    }
210    }
211    }
212    }
213    }