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

File LookaheadChainingListenerTest.java

 

Code metrics

0
29
6
2
109
65
6
0.21
4.83
3
1

Classes

Class Line # Actions
LookaheadChainingListenerTest 35 24 0% 1 0
1.0100%
LookaheadChainingListenerTest.TestChainingListener 37 5 0% 5 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.rendering.listener.chaining;
21   
22    import java.util.Collections;
23    import java.util.Map;
24   
25    import org.junit.Assert;
26    import org.junit.Test;
27    import org.xwiki.rendering.listener.MetaData;
28   
29    /**
30    * Unit tests for {@link LookaheadChainingListener}.
31    *
32    * @version $Id: 649bcc6f44e97d746c0b0ac0f0bbd5b6dc00d574 $
33    * @since 1.8RC1
34    */
 
35    public class LookaheadChainingListenerTest
36    {
 
37    public class TestChainingListener extends AbstractChainingListener
38    {
39    public int calls = 0;
40   
 
41  1 toggle public TestChainingListener(ListenerChain listenerChain)
42    {
43  1 setListenerChain(listenerChain);
44    }
45   
 
46  1 toggle @Override
47    public void beginDocument(MetaData metadata)
48    {
49  1 this.calls++;
50    }
51   
 
52  2 toggle @Override
53    public void beginParagraph(Map<String, String> parameters)
54    {
55  2 this.calls++;
56    }
57   
 
58  1 toggle @Override
59    public void endDocument(MetaData metadata)
60    {
61  1 this.calls++;
62    }
63   
 
64  1 toggle @Override
65    public void endParagraph(Map<String, String> parameters)
66    {
67  1 this.calls++;
68    }
69    }
70   
 
71  1 toggle @Test
72    public void testLookahead()
73    {
74  1 ListenerChain chain = new ListenerChain();
75  1 LookaheadChainingListener listener = new LookaheadChainingListener(chain, 2);
76  1 chain.addListener(listener);
77  1 TestChainingListener testListener = new TestChainingListener(chain);
78  1 chain.addListener(testListener);
79   
80    // The begin document flushes
81  1 listener.beginDocument(MetaData.EMPTY);
82  1 Assert.assertEquals(1, testListener.calls);
83   
84    // 1st lookahead, nothing is sent to the test listener
85  1 listener.beginParagraph(Collections.<String, String>emptyMap());
86  1 Assert.assertEquals(1, testListener.calls);
87  1 Assert.assertEquals(EventType.BEGIN_PARAGRAPH, listener.getNextEvent().eventType);
88  1 Assert.assertNull(listener.getNextEvent(2));
89   
90    // 2nd lookahead, nothing is sent to the test listener
91  1 listener.beginParagraph(Collections.<String, String>emptyMap());
92  1 Assert.assertEquals(1, testListener.calls);
93  1 Assert.assertEquals(EventType.BEGIN_PARAGRAPH, listener.getNextEvent().eventType);
94  1 Assert.assertEquals(EventType.BEGIN_PARAGRAPH, listener.getNextEvent(2).eventType);
95  1 Assert.assertNull(listener.getNextEvent(3));
96   
97    // 3rd events, the first begin paragraph is sent
98  1 listener.endParagraph(Collections.<String, String>emptyMap());
99  1 Assert.assertEquals(2, testListener.calls);
100  1 Assert.assertEquals(EventType.BEGIN_PARAGRAPH, listener.getNextEvent().eventType);
101  1 Assert.assertEquals(EventType.END_PARAGRAPH, listener.getNextEvent(2).eventType);
102  1 Assert.assertNull(listener.getNextEvent(3));
103   
104    // The end document flushes
105  1 listener.endDocument(MetaData.EMPTY);
106  1 Assert.assertEquals(5, testListener.calls);
107  1 Assert.assertNull(listener.getNextEvent());
108    }
109    }