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

File TestDocumentFactory.java

 

Coverage histogram

../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

24
51
5
1
175
93
18
0.35
10.2
5
3.6

Classes

Class Line # Actions
TestDocumentFactory 38 51 0% 18 10
0.87587.5%
 

Contributing tests

This file is covered by 124 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;
21   
22    import java.io.BufferedReader;
23    import java.io.IOException;
24    import java.io.InputStreamReader;
25    import java.io.StringReader;
26    import java.util.ArrayList;
27    import java.util.HashMap;
28    import java.util.Map;
29   
30    import org.xwiki.annotation.maintainer.AnnotationState;
31   
32    /**
33    * Factory to create test documents from corpus files.
34    *
35    * @version $Id: 025f12b2288b77c6f883c10a888e35e5c1e6eef8 $
36    * @since 2.3M1
37    */
 
38    public class TestDocumentFactory
39    {
40    /**
41    * Loaded documents map.
42    */
43    protected Map<String, MockDocument> docs = new HashMap<String, MockDocument>();
44   
45    /**
46    * Default constructor.
47    */
 
48  173 toggle public TestDocumentFactory()
49    {
50   
51    }
52   
53    /**
54    * @param docName name of test document to get, as loaded from the corpus file with the same name. Note that a
55    * document will be loaded and cached until the {@link #reset()} method is called.
56    * @return the test document loaded from the corpus file
57    * @throws IOException if something goes wrong parsing the document file
58    */
 
59  558 toggle public MockDocument getDocument(String docName) throws IOException
60    {
61  558 MockDocument loadedDoc = docs.get(docName);
62  558 if (loadedDoc == null) {
63  124 loadedDoc = new MockDocument();
64  124 loadDocument(loadedDoc, docName);
65  124 docs.put(docName, loadedDoc);
66    }
67  558 return loadedDoc;
68    }
69   
70    /**
71    * Helper method to load a document from the corpus file with the same name.
72    *
73    * @param doc the document to load the file in
74    * @param docName the name of the document to load from the corpus file
75    * @throws IOException if something goes wrong parsing the file
76    */
 
77  173 toggle protected void loadDocument(MockDocument doc, String docName) throws IOException
78    {
79    // FIXME: this is pretty dirty, but it should work
80  173 doc.set("annotations", new ArrayList<Annotation>());
81    // get the file
82  173 BufferedReader reader =
83    new BufferedReader(new InputStreamReader(TestDocumentFactory.class.getResourceAsStream("/" + docName)));
84    // read line by line and
85  173 String line = null;
86  173 String currentKey = null;
87  173 StringBuffer currentValue = new StringBuffer();
88  ? while ((line = reader.readLine()) != null) {
89  3029 if (line.startsWith("#")) {
90    // comment, ignore
91  544 continue;
92    }
93  2485 if (line.startsWith(".")) {
94    // it's a key, parse its value
95    // if there exists a previous key, put its value in the document
96  712 if (currentKey != null) {
97  539 saveKeyToDoc(currentKey, currentValue.toString(), doc, docName);
98  539 currentValue.delete(0, currentValue.length());
99    }
100  712 currentKey = line.substring(1);
101    } else {
102  1773 if (currentValue.length() > 0) {
103  1061 currentValue.append("\n");
104    }
105  1773 currentValue.append(line);
106    }
107    }
108    // process last key + value as well
109  173 saveKeyToDoc(currentKey, currentValue.toString(), doc, docName);
110    }
111   
112    /**
113    * Helper function to save a parsed key in the configuration file to the mock document.
114    *
115    * @param currentKey the read key
116    * @param currentValue the value for the read key
117    * @param doc the mock document read from corpus
118    * @param docName the name of the document where the annotation is contained
119    * @throws IOException if there is any problem reading the annotation representation
120    */
 
121  663 toggle protected void saveKeyToDoc(String currentKey, String currentValue, MockDocument doc, String docName)
122    throws IOException
123    {
124  663 if (currentKey.equals("annotation")) {
125    // parse the annotation value
126  193 Annotation ann = parseAnnotation(currentValue, docName);
127  193 doc.getAnnotations().add(ann);
128  470 } else if (currentKey.indexOf(':') > 0) {
129    // the key contains a key and a syntax, parse the syntax and set it
130  0 int separatorIndex = currentKey.indexOf(':');
131  0 String key = currentKey.substring(0, separatorIndex);
132  0 String syntax = currentKey.substring(separatorIndex + 1);
133  0 doc.set(key, currentValue);
134  0 doc.set(key + "Syntax", syntax);
135    } else {
136  470 doc.set(currentKey, currentValue.toString());
137    }
138    }
139   
140    /**
141    * Parses an annotation from its string representation, as read from the corpus file.
142    *
143    * @param annotation the string representation of the annotation, as in the corpus file
144    * @param docName the name of the document where the annotation is created
145    * @return an {@link Annotation} object corresponding to the data in the {@code annotation} string
146    * @throws IOException if there is any problem reading the annotation representation
147    */
 
148  242 toggle protected Annotation parseAnnotation(String annotation, String docName) throws IOException
149    {
150  242 BufferedReader stringReader = new BufferedReader(new StringReader(annotation));
151    // FIXME: pretty dirty to parse by lines
152  242 String line = null;
153  242 String[] properties = new String[7];
154  242 int propIndex = 0;
155  ? while ((line = stringReader.readLine()) != null) {
156  1160 properties[propIndex] = line;
157  1160 propIndex++;
158    }
159  242 AnnotationState state = AnnotationState.SAFE;
160  242 try {
161  242 state = AnnotationState.valueOf(properties[6] != null ? properties[6] : "");
162    } catch (IllegalArgumentException e) {
163    // nothing, leave it to SAFE
164    }
165  242 Annotation ann = new Annotation(properties[0]);
166    // allow left context and right context properties to miss
167  242 String leftContext = properties[4] == null ? "" : properties[4];
168  242 String rightContext = properties[5] == null ? "" : properties[5];
169  242 ann.setSelection(properties[3], leftContext, rightContext);
170  242 ann.setAuthor(properties[1]);
171  242 ann.setState(state);
172  242 ann.set("annotation", properties[2]);
173  242 return ann;
174    }
175    }