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

File DefaultJobProgressTest.java

 

Code metrics

0
170
13
1
425
249
13
0.08
13.08
13
1

Classes

Class Line # Actions
DefaultJobProgressTest 40 170 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 12 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.job.internal;
21   
22    import org.junit.Assert;
23    import org.junit.Before;
24    import org.junit.Rule;
25    import org.junit.Test;
26    import org.xwiki.job.event.status.EndStepProgressEvent;
27    import org.xwiki.job.event.status.PopLevelProgressEvent;
28    import org.xwiki.job.event.status.PushLevelProgressEvent;
29    import org.xwiki.job.event.status.StartStepProgressEvent;
30    import org.xwiki.job.event.status.StepProgressEvent;
31    import org.xwiki.observation.ObservationManager;
32    import org.xwiki.observation.internal.DefaultObservationManager;
33    import org.xwiki.test.AllLogRule;
34    import org.xwiki.test.annotation.ComponentList;
35    import org.xwiki.test.mockito.MockitoComponentManagerRule;
36   
37    import static org.junit.Assert.assertEquals;
38   
39    @ComponentList(DefaultObservationManager.class)
 
40    public class DefaultJobProgressTest
41    {
42    @Rule
43    public MockitoComponentManagerRule mocker = new MockitoComponentManagerRule();
44   
45    @Rule
46    public AllLogRule log = new AllLogRule();
47   
48    private ObservationManager observation;
49   
50    private DefaultJobProgress progress;
51   
 
52  12 toggle @Before
53    public void before() throws Exception
54    {
55  12 this.observation = mocker.getInstance(ObservationManager.class);
56  12 this.progress = new DefaultJobProgress();
57  12 this.observation.addListener(this.progress);
58    }
59   
 
60  1 toggle @Test
61    public void testProgressSteps()
62    {
63  1 Assert.assertEquals(0, Double.compare(0D, this.progress.getOffset()));
64  1 Assert.assertEquals(0, Double.compare(0D, this.progress.getCurrentLevelOffset()));
65   
66  1 this.observation.notify(new PushLevelProgressEvent(4), null, null);
67   
68  1 this.observation.notify(new StartStepProgressEvent(), null, null);
69   
70  1 Assert.assertEquals(0D, this.progress.getOffset(), 0D);
71  1 Assert.assertEquals(0D, this.progress.getCurrentLevelOffset(), 0D);
72   
73  1 this.observation.notify(new StartStepProgressEvent(), null, null);
74   
75  1 Assert.assertEquals(0.25D, this.progress.getOffset(), 0D);
76  1 Assert.assertEquals(0.25D, this.progress.getCurrentLevelOffset(), 0D);
77   
78  1 this.observation.notify(new PushLevelProgressEvent(2), null, null);
79   
80  1 this.observation.notify(new StartStepProgressEvent(), null, null);
81   
82  1 Assert.assertEquals(0.25D, this.progress.getOffset(), 0D);
83  1 Assert.assertEquals(0.0D, this.progress.getCurrentLevelOffset(), 0D);
84   
85  1 this.observation.notify(new StartStepProgressEvent(), null, null);
86   
87  1 Assert.assertEquals(0.375D, this.progress.getOffset(), 0D);
88  1 Assert.assertEquals(0.5D, this.progress.getCurrentLevelOffset(), 0D);
89   
90  1 this.observation.notify(new PopLevelProgressEvent(), null, null);
91   
92  1 Assert.assertEquals(0.5D, this.progress.getOffset(), 0D);
93  1 Assert.assertEquals(0.5D, this.progress.getCurrentLevelOffset(), 0D);
94    }
95   
 
96  1 toggle @Test
97    public void testStepProgressEvent()
98    {
99  1 Assert.assertEquals(0, Double.compare(0D, this.progress.getOffset()));
100  1 Assert.assertEquals(0, Double.compare(0D, this.progress.getCurrentLevelOffset()));
101   
102  1 this.observation.notify(new PushLevelProgressEvent(4), null, null);
103   
104  1 Assert.assertEquals(0D, this.progress.getOffset(), 0D);
105  1 Assert.assertEquals(0D, this.progress.getCurrentLevelOffset(), 0D);
106   
107  1 this.observation.notify(new StepProgressEvent(), null, null);
108   
109  1 Assert.assertEquals(0.25D, this.progress.getOffset(), 0D);
110  1 Assert.assertEquals(0.25D, this.progress.getCurrentLevelOffset(), 0D);
111   
112  1 this.observation.notify(new PushLevelProgressEvent(2), null, null);
113   
114  1 Assert.assertEquals(0.25D, this.progress.getOffset(), 0D);
115  1 Assert.assertEquals(0.0D, this.progress.getCurrentLevelOffset(), 0D);
116   
117  1 this.observation.notify(new StepProgressEvent(), null, null);
118   
119  1 Assert.assertEquals(0.375D, this.progress.getOffset(), 0D);
120  1 Assert.assertEquals(0.5D, this.progress.getCurrentLevelOffset(), 0D);
121   
122  1 this.observation.notify(new PopLevelProgressEvent(), null, null);
123   
124  1 Assert.assertEquals(0.5D, this.progress.getOffset(), 0D);
125  1 Assert.assertEquals(0.5D, this.progress.getCurrentLevelOffset(), 0D);
126    }
127   
128    /**
129    * Tests that the offset is 1 when the progress is done.
130    */
 
131  1 toggle @Test
132    public void testProgressDone()
133    {
134  1 Assert.assertEquals(0D, this.progress.getOffset(), 0D);
135  1 Assert.assertEquals(0D, this.progress.getCurrentLevelOffset(), 0D);
136   
137  1 this.observation.notify(new PushLevelProgressEvent(1), null, null);
138  1 this.observation.notify(new PopLevelProgressEvent(), null, null);
139   
140  1 Assert.assertEquals(1D, this.progress.getOffset(), 0D);
141  1 Assert.assertEquals(1D, this.progress.getCurrentLevelOffset(), 0D);
142    }
143   
 
144  1 toggle @Test
145    public void testPopDontMoveToNextStep()
146    {
147  1 Assert.assertEquals(0D, this.progress.getOffset(), 0D);
148  1 Assert.assertEquals(0D, this.progress.getCurrentLevelOffset(), 0D);
149   
150  1 this.observation.notify(new PushLevelProgressEvent(2), null, null);
151   
152  1 this.observation.notify(new PushLevelProgressEvent(1), null, null);
153  1 this.observation.notify(new StepProgressEvent(), null, null);
154  1 this.observation.notify(new PopLevelProgressEvent(), null, null);
155   
156  1 Assert.assertEquals(.5D, this.progress.getOffset(), 0D);
157  1 Assert.assertEquals(.5D, this.progress.getCurrentLevelOffset(), 0D);
158   
159  1 this.observation.notify(new StepProgressEvent(), null, null);
160   
161  1 Assert.assertEquals(.5D, this.progress.getOffset(), 0D);
162  1 Assert.assertEquals(.5D, this.progress.getCurrentLevelOffset(), 0D);
163   
164  1 this.observation.notify(new StepProgressEvent(), null, null);
165   
166  1 Assert.assertEquals(1D, this.progress.getOffset(), 0D);
167  1 Assert.assertEquals(1D, this.progress.getCurrentLevelOffset(), 0D);
168   
169  1 this.observation.notify(new PopLevelProgressEvent(), null, null);
170   
171  1 Assert.assertEquals(1D, this.progress.getOffset(), 0D);
172  1 Assert.assertEquals(1D, this.progress.getCurrentLevelOffset(), 0D);
173    }
174   
175    // Bulletproofing
176   
 
177  1 toggle @Test
178    public void testMoveToNextStepInRoot()
179    {
180  1 assertEquals(0, this.progress.getRootStep().getChildren().size());
181   
182    // Move to first step
183  1 this.observation.notify(new StartStepProgressEvent(), null, null);
184   
185    // Move to second step
186  1 this.observation.notify(new StartStepProgressEvent(), null, null);
187   
188  1 assertEquals(2, this.progress.getRootStep().getChildren().size());
189    }
190   
 
191  1 toggle @Test
192    public void testMoreStepsThanExpected()
193    {
194    // Expect 1 step
195  1 this.observation.notify(new PushLevelProgressEvent(1), null, null);
196   
197    // First step
198  1 this.observation.notify(new StartStepProgressEvent(), null, null);
199   
200  1 Assert.assertEquals(0D, this.progress.getOffset(), 0D);
201  1 Assert.assertEquals(0D, this.progress.getCurrentLevelOffset(), 0D);
202  1 assertEquals(1, this.progress.getRootStep().getChildren().size());
203   
204    // Second step
205  1 this.observation.notify(new StartStepProgressEvent(), null, null);
206   
207  1 Assert.assertEquals(1D, this.progress.getOffset(), 0D);
208  1 Assert.assertEquals(1D, this.progress.getCurrentLevelOffset(), 0D);
209  1 assertEquals(2, this.progress.getRootStep().getChildren().size());
210   
211    // Third step
212  1 this.observation.notify(new StartStepProgressEvent(), null, null);
213   
214  1 Assert.assertEquals(1D, this.progress.getOffset(), 0D);
215  1 assertEquals(3, this.progress.getRootStep().getChildren().size());
216   
217  1 this.observation.notify(new PopLevelProgressEvent(), null, null);
218   
219  1 Assert.assertEquals(1D, this.progress.getOffset(), 0D);
220  1 assertEquals(3, this.progress.getRootStep().getChildren().size());
221    }
222   
 
223  1 toggle @Test
224    public void testUnknownNumberOfSteps()
225    {
226    // Unknown number of steps
227  1 this.observation.notify(new PushLevelProgressEvent(), null, null);
228   
229    // First step
230  1 this.observation.notify(new StartStepProgressEvent(), null, null);
231   
232  1 Assert.assertEquals(0D, this.progress.getOffset(), 0D);
233  1 Assert.assertEquals(0D, this.progress.getCurrentLevelOffset(), 0D);
234  1 assertEquals(1, this.progress.getRootStep().getChildren().size());
235   
236    // Second step
237  1 this.observation.notify(new StartStepProgressEvent(), null, null);
238   
239  1 Assert.assertEquals(0.5D, this.progress.getOffset(), 0D);
240  1 Assert.assertEquals(0.5D, this.progress.getCurrentLevelOffset(), 0D);
241  1 assertEquals(2, this.progress.getRootStep().getChildren().size());
242   
243    // Third step
244  1 this.observation.notify(new StartStepProgressEvent(), null, null);
245   
246  1 assertEquals(3, this.progress.getRootStep().getChildren().size());
247   
248    // Fourth step
249  1 this.observation.notify(new StartStepProgressEvent(), null, null);
250   
251  1 Assert.assertEquals(0.75D, this.progress.getOffset(), 0D);
252  1 Assert.assertEquals(0.75D, this.progress.getCurrentLevelOffset(), 0D);
253  1 assertEquals(4, this.progress.getRootStep().getChildren().size());
254   
255  1 this.observation.notify(new PopLevelProgressEvent(), null, null);
256   
257  1 Assert.assertEquals(1D, this.progress.getOffset(), 0D);
258  1 assertEquals(4, this.progress.getRootStep().getChildren().size());
259    }
260   
 
261  1 toggle @Test
262    public void testPushLevelOnClosedStep()
263    {
264  1 this.observation.notify(new PushLevelProgressEvent(2), null, null);
265   
266    // Finish first step
267  1 this.observation.notify(new PushLevelProgressEvent(1), null, null);
268  1 this.observation.notify(new PopLevelProgressEvent(), null, null);
269   
270  1 assertEquals(1, this.progress.getRootStep().getChildren().size());
271   
272    // Forget the StepProgressEvent
273   
274    // All sub-steps to second step
275  1 this.observation.notify(new PushLevelProgressEvent(1), null, null);
276  1 this.observation.notify(new PopLevelProgressEvent(), null, null);
277   
278  1 assertEquals(2, this.progress.getRootStep().getChildren().size());
279    }
280   
 
281  1 toggle @Test
282    public void testPopLevelOnWrongSource()
283    {
284  1 Assert.assertEquals(0D, this.progress.getOffset(), 0D);
285  1 Assert.assertEquals(0D, this.progress.getCurrentLevelOffset(), 0D);
286   
287  1 this.observation.notify(new PushLevelProgressEvent(1), "source1", null);
288  1 this.observation.notify(new PopLevelProgressEvent(), "source2", null);
289   
290  1 assertEquals("Could not find any matching step level for source [source2]. Ignoring PopLevelProgressEvent.",
291    this.log.getMessage(0));
292   
293  1 Assert.assertEquals(0D, this.progress.getOffset(), 0D);
294  1 Assert.assertEquals(0D, this.progress.getCurrentLevelOffset(), 0D);
295    }
296   
 
297  1 toggle @Test
298    public void testPopLevelOnParentLevelSource()
299    {
300  1 Assert.assertEquals(0D, this.progress.getOffset(), 0D);
301  1 Assert.assertEquals(0D, this.progress.getCurrentLevelOffset(), 0D);
302   
303  1 Object source1 = "source1";
304  1 Object source11 = "source11";
305   
306  1 this.observation.notify(new PushLevelProgressEvent(2), source1, null);
307   
308  1 this.observation.notify(new PushLevelProgressEvent(2), source11, null);
309   
310    // First step in source11 level
311  1 this.observation.notify(new StartStepProgressEvent(), null, null);
312   
313  1 Assert.assertEquals(0D, this.progress.getOffset(), 0D);
314  1 Assert.assertEquals(0D, this.progress.getCurrentLevelOffset(), 0D);
315   
316    // Second step in source11 level
317  1 this.observation.notify(new StartStepProgressEvent(), null, null);
318   
319  1 Assert.assertEquals(0.25D, this.progress.getOffset(), 0D);
320  1 Assert.assertEquals(0.5D, this.progress.getCurrentLevelOffset(), 0D);
321   
322    // "Forget" to pop source11 level
323   
324    // Automatically close all levels until it matches source1
325  1 this.observation.notify(new PopLevelProgressEvent(), source1, null);
326   
327  1 Assert.assertEquals(1D, this.progress.getOffset(), 0D);
328  1 Assert.assertEquals(1D, this.progress.getCurrentLevelOffset(), 0D);
329    }
330   
 
331  1 toggle @Test
332    public void testEndStepOnParentStepSource()
333    {
334  1 Assert.assertEquals(0D, this.progress.getOffset(), 0D);
335  1 Assert.assertEquals(0D, this.progress.getCurrentLevelOffset(), 0D);
336   
337  1 Object source1 = "source1";
338  1 Object source11 = "source11";
339   
340    // Root level
341  1 this.observation.notify(new PushLevelProgressEvent(1), null, null);
342   
343    // First step in root level
344  1 this.observation.notify(new StartStepProgressEvent(), source1, null);
345   
346    // Level 1
347  1 this.observation.notify(new PushLevelProgressEvent(2), source1, null);
348   
349    // Level 11
350  1 this.observation.notify(new PushLevelProgressEvent(2), source11, null);
351   
352    // First step in source11 level
353  1 this.observation.notify(new StartStepProgressEvent(), null, null);
354   
355  1 Assert.assertEquals(0D, this.progress.getOffset(), 0D);
356  1 Assert.assertEquals(0D, this.progress.getCurrentLevelOffset(), 0D);
357   
358    // Second step in source11 level
359  1 this.observation.notify(new StartStepProgressEvent(), null, null);
360   
361  1 Assert.assertEquals(0.25D, this.progress.getOffset(), 0D);
362  1 Assert.assertEquals(0.5D, this.progress.getCurrentLevelOffset(), 0D);
363   
364    // "Forget" to pop source11 level
365   
366    // Automatically close all levels until it matches source1
367  1 this.observation.notify(new EndStepProgressEvent(), source1, null);
368   
369  1 Assert.assertEquals(1D, this.progress.getOffset(), 0D);
370  1 Assert.assertEquals(1D, this.progress.getCurrentLevelOffset(), 0D);
371    }
372   
 
373  1 toggle @Test
374    public void testStartStepFromDifferentSource()
375    {
376  1 Object source1 = "source1";
377  1 Object source11 = "source11";
378  1 Object source12 = "source12";
379  1 Object source1b = "source1b";
380   
381    // Root level
382  1 this.observation.notify(new PushLevelProgressEvent(), source1, null);
383   
384    // Start first step in source1 level
385  1 this.observation.notify(new StartStepProgressEvent(), source1, null);
386   
387  1 assertEquals(1, this.progress.getRootStep().getChildren().size());
388   
389    // Start first step in source11 level
390  1 this.observation.notify(new StartStepProgressEvent(), source11, null);
391   
392  1 assertEquals(1, this.progress.getRootStep().getChildren().size());
393  1 assertEquals(1, this.progress.getRootStep().getChildren().get(0).getChildren().size());
394   
395    // Close the step
396  1 this.observation.notify(new EndStepProgressEvent(), source11, null);
397   
398    // Start first step in source12 level
399  1 this.observation.notify(new StartStepProgressEvent(), source12, null);
400   
401  1 assertEquals(1, this.progress.getRootStep().getChildren().size());
402  1 assertEquals(2, this.progress.getRootStep().getChildren().get(0).getChildren().size());
403   
404    // Close the step
405  1 this.observation.notify(new EndStepProgressEvent(), source12, null);
406   
407    // Start second step in source1 level
408  1 this.observation.notify(new StartStepProgressEvent(), source1, null);
409   
410  1 assertEquals(2, this.progress.getRootStep().getChildren().size());
411  1 assertEquals(2, this.progress.getRootStep().getChildren().get(0).getChildren().size());
412  1 assertEquals(0, this.progress.getRootStep().getChildren().get(1).getChildren().size());
413   
414    // Close the step
415  1 this.observation.notify(new EndStepProgressEvent(), source1, null);
416   
417    // Start third step in source1 level (but with a different source)
418  1 this.observation.notify(new StartStepProgressEvent(), source1b, null);
419   
420  1 assertEquals(3, this.progress.getRootStep().getChildren().size());
421  1 assertEquals(2, this.progress.getRootStep().getChildren().get(0).getChildren().size());
422  1 assertEquals(0, this.progress.getRootStep().getChildren().get(1).getChildren().size());
423  1 assertEquals(0, this.progress.getRootStep().getChildren().get(2).getChildren().size());
424    }
425    }