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

File AbstractTestWiki.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
60
22
6
380
220
25
0.42
2.73
3.67
1.14

Classes

Class Line # Actions
AbstractTestWiki 50 10 0% 2 2
0.8461538684.6%
AbstractTestWiki.ElementBuilder 93 0 - 0 0
-1.0 -
AbstractTestWiki.WikiBuilder 109 44 0% 19 5
0.923076992.3%
AbstractTestWiki.WikiBuilder.AbstractElementBuilder 115 0 0% 2 0
1.0100%
AbstractTestWiki.WikiBuilder.AbstractRightElementBuilder 130 4 0% 1 0
1.0100%
AbstractTestWiki.WikiBuilder.DeclareElementBuilders 160 2 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 18 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.security.authorization.testwikibuilding;
21   
22    import java.io.File;
23    import java.io.FileNotFoundException;
24    import java.net.URL;
25    import java.util.Formatter;
26    import java.util.HashMap;
27    import java.util.Map;
28    import java.util.Stack;
29   
30    import javax.xml.XMLConstants;
31    import javax.xml.parsers.SAXParser;
32    import javax.xml.parsers.SAXParserFactory;
33    import javax.xml.validation.Schema;
34    import javax.xml.validation.SchemaFactory;
35   
36    import org.xml.sax.Attributes;
37    import org.xml.sax.SAXException;
38    import org.xml.sax.helpers.DefaultHandler;
39   
40    import com.xpn.xwiki.XWikiContext;
41   
42    /**
43    * This class is used for building a mocked test setup for testing the authorization component.
44    *
45    * Superclass for mocked test wikis built from xml sources.
46    *
47    * @since 4.2
48    * @version $Id: 880b66ac795af312a6aa7b56989c37594ce54e2a $
49    */
 
50    public abstract class AbstractTestWiki
51    {
52   
53    /** The subdirectory in the classpath were the test wiki definitions will be found. */
54    private final static String TEST_WIKI_DEFINITIONS_DIRECTORY = "testwikis";
55   
56    /** The wiki description that is currently being parsed and built. */
57    private HasWikiContents currentWiki;
58   
59    /** The space description that is currently being parsed and built. */
60    private HasDocuments currentSpace;
61   
62    /**
63    * The object (wiki, space or document) that have access control objects that is currently being parsed and built is
64    * at the top of this stack.
65    */
66    private final Stack<HasAcl> currentRightsHolder = new Stack<HasAcl>();
67   
68    /**
69    * The object (wiki or group) that have users that is currently being parsed and built is at the top of this stack.
70    */
71    private final Stack<HasUsers> currentUsersHolder = new Stack<HasUsers>();
72   
73    /**
74    * Add a wiki definition.
75    *
76    * @param name The name of the wiki.
77    * @param owner The owner of the wiki.
78    * @param isMainWiki {@code true} if the wiki should be considered the main wiki. It is an error unless exactly one
79    * wiki in a test wiki setup is marked as the mainwiki.
80    * @param isReadOnly Wether the wiki is in read only mode.
81    */
82    protected abstract HasWikiContents addWiki(String name, String owner, boolean isMainWiki, boolean isReadOnly,
83    String alt);
84   
85    /**
86    * @return The mocked context for this test wiki setup.
87    */
88    public abstract XWikiContext getXWikiContext();
89   
90    /**
91    * Interface for xml sax parsing.
92    */
 
93    private interface ElementBuilder
94    {
95   
96    /**
97    * Indicate start of element.
98    * @param attributes The xml attributes.
99    */
100    void startElement(Attributes attributes);
101   
102    /** Indicate end of element. */
103    void endElement();
104    }
105   
106    /**
107    * A SAX handler for building a test wiki.
108    */
 
109    private class WikiBuilder extends DefaultHandler
110    {
111   
112    /**
113    * Abstract class for convenient inheritance.
114    */
 
115    private abstract class AbstractElementBuilder implements ElementBuilder
116    {
 
117  18 toggle @Override
118    public void startElement(Attributes attributes) {
119    }
120   
 
121  70 toggle @Override
122    public void endElement() {
123    }
124   
125    }
126   
127    /**
128    * Abstract class for building a right declaration.
129    */
 
130    private abstract class AbstractRightElementBuilder extends AbstractElementBuilder
131    {
132   
 
133  30 toggle @Override
134    public void startElement(Attributes attributes) {
135  30 String name = attributes.getValue("name");
136  30 String type = attributes.getValue("type");
137   
138  30 HasAcl rightsHolder = currentRightsHolder.peek();
139   
140  30 addRight(rightsHolder, name, type);
141    }
142   
143    /**
144    * Add a right to the rights holder.
145    * @param rightsHolder The object (wiki, space or page) that can hold rights objects.
146    * @param name The name (user- or group name) that should be assigned the right.
147    * @param type The type of right (view, edit, etc.)
148    */
149    protected abstract void addRight(HasAcl rightsHolder, String name, String type);
150    }
151   
152    /**
153    * The map of all element builders.
154    */
155    private final Map<String, ElementBuilder> elementBuilders = new HashMap<String, ElementBuilder>();
156   
157    /**
158    * Convenience class for declaring element builders.
159    */
 
160    private class DeclareElementBuilders {
161   
162    /**
163    * @param name The XML element name.
164    * @param elementBuilder The builder instance.
165    * @return this instance.
166    */
 
167  180 toggle public DeclareElementBuilders declare(String name, ElementBuilder elementBuilder)
168    {
169  180 elementBuilders.put(name, elementBuilder);
170  180 return this;
171    }
172   
173    }
174   
 
175  18 toggle {
176  18 new DeclareElementBuilders()
177    .declare(
178    "wikis",
179    new AbstractElementBuilder() {
180    })
181    .declare(
182    "wiki",
183    new ElementBuilder() {
184   
 
185  26 toggle @Override
186    public void startElement(Attributes attributes) {
187  26 String name = attributes.getValue("name");
188  26 String owner = attributes.getValue("owner");
189  26 boolean isMainWiki = "true".equals(attributes.getValue("mainWiki"));
190  26 boolean isReadOnly = "true".equals(attributes.getValue("readOnly"));
191  26 String alt = attributes.getValue("alt");
192  26 HasWikiContents wiki = addWiki(name, owner, isMainWiki, isReadOnly, alt);
193   
194  26 currentWiki = wiki;
195  26 currentRightsHolder.push(currentWiki);
196  26 currentUsersHolder.push(currentWiki);
197    }
198   
 
199  26 toggle @Override
200    public void endElement() {
201  26 currentWiki = null;
202  26 currentRightsHolder.pop();
203  26 currentUsersHolder.pop();
204    }
205    })
206    .declare(
207    "user",
208    new AbstractElementBuilder() {
209   
 
210  22 toggle @Override
211    public void startElement(Attributes attributes) {
212  22 String name = attributes.getValue("name");
213   
214  22 HasUsers usersHolder = currentUsersHolder.peek();
215  22 usersHolder.addUser(name);
216    }
217    })
218    .declare(
219    "group",
220    new ElementBuilder() {
221   
 
222  5 toggle @Override
223    public void startElement(Attributes attributes) {
224  5 String name = attributes.getValue("name");
225   
226  5 HasUsers group = currentWiki.addGroup(name);
227  5 currentUsersHolder.push(group);
228    }
229   
 
230  5 toggle @Override
231    public void endElement() {
232  5 currentUsersHolder.pop();
233    }
234    })
235    .declare(
236    "space",
237    new ElementBuilder() {
238   
 
239  22 toggle @Override
240    public void startElement(Attributes attributes) {
241  22 String name = attributes.getValue("name");
242  22 String alt = attributes.getValue("alt");
243   
244  22 currentSpace = currentWiki.addSpace(name, alt);
245  22 currentRightsHolder.push(currentSpace);
246    }
247   
 
248  22 toggle @Override
249    public void endElement() {
250  22 currentSpace = null;
251  22 currentRightsHolder.pop();
252    }
253    })
254    .declare(
255    "document",
256    new ElementBuilder() {
257   
 
258  24 toggle @Override
259    public void startElement(Attributes attributes) {
260  24 String name = attributes.getValue("name");
261  24 String creator = attributes.getValue("creator");
262  24 String alt = attributes.getValue("alt");
263   
264  24 if (creator == null) {
265  24 creator = "XWiki.Admin";
266    }
267   
268  24 HasAcl document = currentSpace.addDocument(name, creator, alt);
269  24 currentRightsHolder.push(document);
270    }
271   
 
272  24 toggle @Override
273    public void endElement() {
274  24 currentRightsHolder.pop();
275    }
276    })
277    .declare(
278    "allowUser",
279    new AbstractRightElementBuilder() {
280   
 
281  18 toggle @Override
282    public void addRight(HasAcl rightsHolder, String name, String type) {
283  18 rightsHolder.addAllowUser(name, type);
284    }
285    })
286    .declare(
287    "denyUser",
288    new AbstractRightElementBuilder() {
289   
 
290  2 toggle @Override
291    public void addRight(HasAcl rightsHolder, String name, String type) {
292  2 rightsHolder.addDenyUser(name, type);
293    }
294    })
295    .declare(
296    "allowGroup",
297    new AbstractRightElementBuilder() {
298   
 
299  10 toggle @Override
300    public void addRight(HasAcl rightsHolder, String name, String type) {
301  10 rightsHolder.addAllowGroup(name, type);
302    }
303    })
304    .declare(
305    "denyGroup",
306    new AbstractRightElementBuilder() {
307   
 
308  0 toggle @Override
309    public void addRight(HasAcl rightsHolder, String name, String type) {
310  0 rightsHolder.addDenyGroup(name, type);
311    }
312    });
313   
314    }
315   
 
316  147 toggle @Override
317    public void startElement(String uri,
318    String localName,
319    String qName,
320    Attributes attributes) throws SAXException
321    {
322  147 getElementBuilder(qName).startElement(attributes);
323    }
324   
 
325  147 toggle @Override
326    public void endElement(String uri, String localName, String qName)
327    throws SAXException {
328   
329  147 getElementBuilder(qName).endElement();
330    }
331   
332    /**
333    * @param qName The XML element name.
334    * @return The element builder for the element name.
335    * @throws SAXException if no element builder can be found for the element name.
336    */
 
337  294 toggle private ElementBuilder getElementBuilder(String qName)
338    throws SAXException
339    {
340  294 ElementBuilder elementBuilder = elementBuilders.get(qName);
341   
342  294 if (elementBuilder == null) {
343  0 throw new SAXException(new Formatter().format("Invalid element name: '%s'", qName).toString());
344    }
345   
346  294 return elementBuilder;
347    }
348    }
349   
350    /**
351    * Load a test wiki from an xml-file located in the appropriate subdirectory in the classpath ({@link
352    * TEST_WIKI_DEFINITIONS_DIRECTORY}).
353    *
354    * @param name The file name relative the test wiki subdirectory.
355    */
 
356  18 toggle protected void loadTestWiki(String name) throws Exception
357    {
358  18 URL testwikiUrl = ClassLoader.getSystemResource(TEST_WIKI_DEFINITIONS_DIRECTORY + File.separatorChar + name);
359   
360  18 if (testwikiUrl == null) {
361  0 throw new FileNotFoundException(name);
362    }
363   
364  18 URL schemaUrl = ClassLoader.getSystemResource("schemas" + File.separatorChar + "wikitest.xsd");
365   
366  18 Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaUrl);
367   
368  18 SAXParserFactory parserFactory = SAXParserFactory.newInstance();
369   
370  18 parserFactory.setSchema(schema);
371   
372  18 SAXParser parser = parserFactory.newSAXParser();
373   
374  18 String filename = testwikiUrl.toURI().toString();
375   
376  18 parser.parse(filename, new WikiBuilder());
377    }
378   
379   
380    }