1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.search.solr.internal.job

File DiffDocumentIteratorTest.java

 

Code metrics

6
58
10
2
178
126
14
0.24
5.8
5
1.4

Classes

Class Line # Actions
DiffDocumentIteratorTest 44 52 0% 8 3
0.951612995.2%
DiffDocumentIteratorTest.DocumentIteratorStub 46 6 0% 6 3
0.7575%
 

Contributing tests

This file is covered by 4 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.search.solr.internal.job;
21   
22    import static org.junit.Assert.*;
23    import static org.mockito.Mockito.*;
24   
25    import java.util.ArrayList;
26    import java.util.Collections;
27    import java.util.Iterator;
28    import java.util.List;
29   
30    import org.apache.commons.lang3.tuple.ImmutablePair;
31    import org.apache.commons.lang3.tuple.Pair;
32    import org.junit.Test;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.EntityReference;
35    import org.xwiki.model.reference.WikiReference;
36    import org.xwiki.search.solr.internal.job.DiffDocumentIterator.Action;
37   
38    /**
39    * Unit tests for {@link DiffDocumentIterator}.
40    *
41    * @version $Id: 56704b3971363f58cfd8b49ab3a5a1f6436df040 $
42    * @since 5.4.5
43    */
 
44    public class DiffDocumentIteratorTest
45    {
 
46    public static class DocumentIteratorStub<T> implements DocumentIterator<T>
47    {
48    private int size;
49   
50    private Iterator<Pair<DocumentReference, T>> iterator;
51   
 
52  4 toggle public DocumentIteratorStub(List<Pair<DocumentReference, T>> list)
53    {
54  4 size = list.size();
55  4 iterator = list.iterator();
56    }
57   
 
58  27 toggle @Override
59    public boolean hasNext()
60    {
61  27 return iterator.hasNext();
62    }
63   
 
64  9 toggle @Override
65    public Pair<DocumentReference, T> next()
66    {
67  9 return iterator.next();
68    }
69   
 
70  0 toggle @Override
71    public void remove()
72    {
73  0 iterator.remove();
74    }
75   
 
76  0 toggle @Override
77    public void setRootReference(EntityReference rootReference)
78    {
79    }
80   
 
81  2 toggle @Override
82    public long size()
83    {
84  2 return size;
85    }
86    }
87   
 
88  1 toggle @Test
89    public void iterate()
90    {
91  1 List<Pair<DocumentReference, String>> previous = new ArrayList<Pair<DocumentReference, String>>();
92  1 previous.add(new ImmutablePair<DocumentReference, String>(new DocumentReference("chess", "B", "M"), "2.3"));
93  1 previous.add(new ImmutablePair<DocumentReference, String>(new DocumentReference("chess", "E", "A"), "5.1"));
94  1 previous.add(new ImmutablePair<DocumentReference, String>(new DocumentReference("xwiki", "A", "S"), "1.1"));
95  1 DocumentIterator<String> previousIterator = new DocumentIteratorStub<String>(previous);
96   
97  1 List<Pair<DocumentReference, String>> next = new ArrayList<Pair<DocumentReference, String>>();
98  1 next.add(new ImmutablePair<DocumentReference, String>(new DocumentReference("chess", "B", "L"), "1.2"));
99  1 next.add(new ImmutablePair<DocumentReference, String>(new DocumentReference("chess", "B", "M"), "4.7"));
100  1 next.add(new ImmutablePair<DocumentReference, String>(new DocumentReference("xwiki", "A", "S"), "1.1"));
101  1 next.add(new ImmutablePair<DocumentReference, String>(new DocumentReference("xwiki", "B", "P"), "2.4"));
102  1 DocumentIterator<String> nextIterator = new DocumentIteratorStub<String>(next);
103   
104  1 DiffDocumentIterator<String> iterator = new DiffDocumentIterator<String>(previousIterator, nextIterator);
105   
106  1 assertEquals(4, iterator.size());
107   
108  1 List<Pair<DocumentReference, Action>> actualResult = new ArrayList<Pair<DocumentReference, Action>>();
109  6 while (iterator.hasNext()) {
110  5 actualResult.add(iterator.next());
111    }
112   
113  1 List<Pair<DocumentReference, Action>> expectedResult = new ArrayList<Pair<DocumentReference, Action>>();
114  1 expectedResult.add(new ImmutablePair<DocumentReference, Action>(next.get(0).getKey(), Action.ADD));
115  1 expectedResult.add(new ImmutablePair<DocumentReference, Action>(next.get(1).getKey(), Action.UPDATE));
116  1 expectedResult.add(new ImmutablePair<DocumentReference, Action>(previous.get(1).getKey(), Action.DELETE));
117  1 expectedResult.add(new ImmutablePair<DocumentReference, Action>(next.get(2).getKey(), Action.SKIP));
118  1 expectedResult.add(new ImmutablePair<DocumentReference, Action>(next.get(3).getKey(), Action.ADD));
119   
120  1 assertEquals(expectedResult, actualResult);
121    }
122   
 
123  1 toggle @Test
124    public void deleteAll()
125    {
126  1 List<Pair<DocumentReference, String>> previous = new ArrayList<Pair<DocumentReference, String>>();
127  1 previous.add(new ImmutablePair<DocumentReference, String>(new DocumentReference("wiki", "A", "B"), "3.1"));
128  1 previous.add(new ImmutablePair<DocumentReference, String>(new DocumentReference("wiki", "X", "Y"), "5.2"));
129  1 DocumentIterator<String> previousIterator = new DocumentIteratorStub<String>(previous);
130   
131  1 List<Pair<DocumentReference, String>> next = Collections.emptyList();
132  1 DocumentIterator<String> nextIterator = new DocumentIteratorStub<String>(next);
133   
134  1 DiffDocumentIterator<String> iterator = new DiffDocumentIterator<String>(previousIterator, nextIterator);
135   
136  1 List<Pair<DocumentReference, Action>> actualResult = new ArrayList<Pair<DocumentReference, Action>>();
137  3 while (iterator.hasNext()) {
138  2 actualResult.add(iterator.next());
139    }
140   
141  1 List<Pair<DocumentReference, Action>> expectedResult = new ArrayList<Pair<DocumentReference, Action>>();
142  1 expectedResult.add(new ImmutablePair<DocumentReference, Action>(previous.get(0).getKey(), Action.DELETE));
143  1 expectedResult.add(new ImmutablePair<DocumentReference, Action>(previous.get(1).getKey(), Action.DELETE));
144  1 assertEquals(expectedResult, actualResult);
145    }
146   
 
147  1 toggle @SuppressWarnings("unchecked")
148    @Test
149    public void setRootReference()
150    {
151  1 DocumentIterator<String> previous = mock(DocumentIterator.class, "previous");
152  1 DocumentIterator<String> next = mock(DocumentIterator.class, "next");
153  1 DiffDocumentIterator<String> iterator = new DiffDocumentIterator<String>(previous, next);
154   
155  1 WikiReference rootReference = new WikiReference("foo");
156  1 iterator.setRootReference(rootReference);
157   
158  1 verify(previous).setRootReference(rootReference);
159  1 verify(next).setRootReference(rootReference);
160    }
161   
 
162  1 toggle @SuppressWarnings("unchecked")
163    @Test
164    public void remove()
165    {
166  1 DocumentIterator<String> previous = mock(DocumentIterator.class, "previous");
167  1 DocumentIterator<String> next = mock(DocumentIterator.class, "next");
168  1 DiffDocumentIterator<String> iterator = new DiffDocumentIterator<String>(previous, next);
169  1 try {
170  1 iterator.remove();
171  0 fail();
172    } catch (Exception e) {
173  1 if (!(e instanceof UnsupportedOperationException)) {
174  0 fail();
175    }
176    }
177    }
178    }