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

File AnnotationMaintainerTest.java

 

Code metrics

4
79
10
1
265
140
12
0.15
7.9
10
1.2

Classes

Class Line # Actions
AnnotationMaintainerTest 46 79 0% 12 2
0.9784946497.8%
 

Contributing tests

This file is covered by 49 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.annotation.maintainer;
21   
22    import static org.junit.Assert.assertEquals;
23    import static org.junit.Assert.assertNotNull;
24    import static org.junit.Assert.assertTrue;
25   
26    import java.io.IOException;
27    import java.util.ArrayList;
28    import java.util.Collection;
29    import java.util.List;
30   
31    import org.junit.Test;
32    import org.junit.runner.RunWith;
33    import org.junit.runners.Parameterized;
34    import org.junit.runners.Parameterized.Parameters;
35    import org.xwiki.annotation.Annotation;
36    import org.xwiki.annotation.AnnotationsMockSetup;
37    import org.xwiki.test.jmock.AbstractComponentTestCase;
38   
39    /**
40    * Tests the annotation maintainer that updates annotations when documents change.
41    *
42    * @version $Id: 864bb6bbebcda29d8d17f9e966dd2772558630e2 $
43    * @since 2.3M1
44    */
45    @RunWith(Parameterized.class)
 
46    public class AnnotationMaintainerTest extends AbstractComponentTestCase
47    {
48    /**
49    * Document description files to run this test for.
50    */
51    private static Collection<String[]> files = new ArrayList<String[]>();
52   
53    /**
54    * Mock document to run tests for.
55    */
56    protected String docName;
57   
58    /**
59    * The annotation maintainer under test.
60    */
61    protected AnnotationMaintainer annotationMaintainer;
62   
63    /**
64    * The setup for mocking components needed in annotation code.
65    */
66    protected AnnotationsMockSetup setup;
67   
 
68  1 toggle static {
69  1 addFileToTest("maintainer/correction/Correction1");
70  1 addFileToTest("maintainer/correction/Correction2");
71  1 addFileToTest("maintainer/correction/Correction3");
72  1 addFileToTest("maintainer/correction/Correction4");
73   
74  1 addFileToTest("maintainer/correction/multiple/CorrectionMultiple1");
75  1 addFileToTest("maintainer/correction/multiple/CorrectionMultiple2");
76  1 addFileToTest("maintainer/correction/multiple/CorrectionMultiple3");
77  1 addFileToTest("maintainer/correction/multiple/CorrectionMultiple4");
78  1 addFileToTest("maintainer/correction/multiple/CorrectionMultiple5");
79   
80  1 addFileToTest("maintainer/endpoint/Beginning1");
81  1 addFileToTest("maintainer/endpoint/Beginning2");
82  1 addFileToTest("maintainer/endpoint/Beginning3");
83  1 addFileToTest("maintainer/endpoint/Beginning4");
84  1 addFileToTest("maintainer/endpoint/Beginning5");
85   
86  1 addFileToTest("maintainer/endpoint/End1");
87  1 addFileToTest("maintainer/endpoint/End2");
88  1 addFileToTest("maintainer/endpoint/End3");
89  1 addFileToTest("maintainer/endpoint/End4");
90  1 addFileToTest("maintainer/endpoint/End5");
91   
92  1 addFileToTest("maintainer/endpoint/Endpoints1");
93  1 addFileToTest("maintainer/endpoint/Endpoints2");
94  1 addFileToTest("maintainer/endpoint/Endpoints3");
95  1 addFileToTest("maintainer/endpoint/Endpoints4");
96   
97    // TODO: the following tests, paragraph and altered, need to be reviewed since there are multiple issues with
98    // character based diff that cause them to yield different results for a difference of a single letter which can
99    // cause a matching point.
100  1 addFileToTest("maintainer/paragraph/Paragraph1");
101  1 addFileToTest("maintainer/alter/Altered1");
102    // TODO: enable when/if fixed: see comment in the file
103    // addFileToTest("maintainer/alter/Altered2");
104  1 addFileToTest("maintainer/alter/Altered3");
105  1 addFileToTest("maintainer/alter/Altered4");
106   
107    // tests for the uniqueness maintenance
108  1 addFileToTest("maintainer/uniqueness/Unique1");
109  1 addFileToTest("maintainer/uniqueness/Unique2");
110  1 addFileToTest("maintainer/uniqueness/Unique3");
111   
112  1 addFileToTest("maintainer/uniqueness/Duplicate1");
113  1 addFileToTest("maintainer/uniqueness/Duplicate2");
114  1 addFileToTest("maintainer/uniqueness/Duplicate3");
115  1 addFileToTest("maintainer/uniqueness/Duplicate4");
116  1 addFileToTest("maintainer/uniqueness/Duplicate5");
117  1 addFileToTest("maintainer/uniqueness/Duplicate6");
118  1 addFileToTest("maintainer/uniqueness/Duplicate7");
119  1 addFileToTest("maintainer/uniqueness/Duplicate8");
120  1 addFileToTest("maintainer/uniqueness/Duplicate9");
121   
122    // tests for the cases when annotation stays unchanged, regardless of the changes on the content, or the
123    // selection of the annotation. Should check that the updater doesn't even run on these annotations
124  1 addFileToTest("maintainer/unchanged/Unchanged1");
125  1 addFileToTest("maintainer/unchanged/Unchanged2");
126  1 addFileToTest("maintainer/unchanged/Unchanged3");
127  1 addFileToTest("maintainer/unchanged/Unchanged4");
128  1 addFileToTest("maintainer/unchanged/Unchanged5");
129   
130  1 addFileToTest("maintainer/spaces/Spaces1");
131  1 addFileToTest("maintainer/spaces/Spaces2");
132  1 addFileToTest("maintainer/spaces/Spaces3");
133  1 addFileToTest("maintainer/spaces/Spaces4");
134  1 addFileToTest("maintainer/spaces/Spaces5");
135    // TODO: add test cases here for the case when an annotation becomes non-unique on update, but the 2 differ by
136    // some spaces (spaceless they are the same, but with normalized spaces they are different. Maintainer will fail
137    // in this case)
138   
139    // TODO: add tests for the case of multiple annotations which are being changed
140    }
141   
142    /**
143    * Builds a maintainer test to test the passed document name.
144    *
145    * @param docName the name of the document (and mock file) to test
146    */
 
147  49 toggle public AnnotationMaintainerTest(String docName)
148    {
149  49 this.docName = docName;
150    }
151   
152    /**
153    * Adds a file to the list of files to run tests for.
154    *
155    * @param docName the name of the document / file to test
156    */
 
157  49 toggle protected static void addFileToTest(String docName)
158    {
159  49 files.add(new String[] {docName});
160    }
161   
162    /**
163    * @return list of corpus files to instantiate tests for
164    */
 
165  1 toggle @Parameters
166    public static Collection<String[]> data()
167    {
168  1 return files;
169    }
170   
 
171  49 toggle @Override
172    public void setUp() throws Exception
173    {
174  49 super.setUp();
175   
176  49 annotationMaintainer = getComponentManager().getInstance(AnnotationMaintainer.class);
177    }
178   
 
179  49 toggle @Override
180    protected void registerComponents() throws Exception
181    {
182  49 super.registerComponents();
183   
184    // register the IO mockups
185  49 setup = new AnnotationsMockSetup(getComponentManager(), new TestDocumentFactory());
186  49 setup.setupExpectations(docName);
187    }
188   
189    /**
190    * Tests the update of a document.
191    *
192    * @throws IOException if anything goes wrong mocking the documents
193    * @throws MaintainerServiceException if anything goes wrong maintaining the the document annotations
194    */
 
195  49 toggle @Test
196    public void testUpdate() throws IOException, MaintainerServiceException
197    {
198    // ignore the docName ftm, just test the marvelous setup
199  49 MockDocument doc = ((TestDocumentFactory) setup.getDocFactory()).getDocument(docName);
200    // TODO: this is not the place to put this code, but it's the most comfortable
201  49 copyOriginalSelections(doc);
202   
203  49 annotationMaintainer.updateAnnotations(docName, doc.getSource(), doc.getModifiedSource());
204   
205    // test the result
206  49 assertSameAnnotations(doc.getUpdatedAnnotations(), doc.getAnnotations());
207    }
208   
209    /**
210    * Helper method to test if the two passed annotation lists contain the same annotations.
211    *
212    * @param expected the expected list of annotations
213    * @param actual the actual list of annotations
214    */
 
215  49 toggle private void assertSameAnnotations(List<Annotation> expected, List<Annotation> actual)
216    {
217  49 assertTrue(expected.size() == actual.size());
218   
219  49 for (Annotation actualAnn : actual) {
220  49 Annotation expectedAnn = getAnnotation(actualAnn.getId(), expected);
221  49 assertNotNull(expectedAnn);
222  49 assertEquals(expectedAnn.getSelection(), actualAnn.getSelection());
223  49 assertEquals(expectedAnn.getSelectionLeftContext(), actualAnn.getSelectionLeftContext());
224  49 assertEquals(expectedAnn.getSelectionRightContext(), actualAnn.getSelectionRightContext());
225  49 assertEquals(expectedAnn.getState(), actualAnn.getState());
226  49 assertEquals(expectedAnn.getOriginalSelection(), actualAnn.getOriginalSelection());
227    }
228    }
229   
230    /**
231    * Helper function to get the annotation with the passed id from the passed list.
232    *
233    * @param annId the id of the searched annotation
234    * @param list the list of annotations where to search for the passed annotation
235    * @return the found annotation
236    */
 
237  77 toggle private Annotation getAnnotation(String annId, Collection<Annotation> list)
238    {
239  77 for (Annotation ann : list) {
240  77 if (ann.getId().equals(annId)) {
241  77 return ann;
242    }
243    }
244   
245  0 return null;
246    }
247   
248    /**
249    * Helper function to set the original selected contents in the updated annotations of this document from the
250    * original list of annotations.
251    *
252    * @param doc the document for which to set the updated annotations original selected contents
253    */
 
254  49 toggle private void copyOriginalSelections(MockDocument doc)
255    {
256    // set the original selections of updated annotations from the original annotations of the document
257  49 for (Annotation updatedAnn : doc.getUpdatedAnnotations()) {
258  49 if (updatedAnn.getState() != AnnotationState.UPDATED) {
259  21 continue;
260    }
261  28 Annotation originalAnn = getAnnotation(updatedAnn.getId(), doc.getAnnotations());
262  28 updatedAnn.setOriginalSelection(originalAnn.getSelection());
263    }
264    }
265    }