1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.macro.ctsreport

File CTSDataMacro.java

 

Coverage histogram

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

Code metrics

2
21
3
1
139
77
4
0.19
7
3
1.33

Classes

Class Line # Actions
CTSDataMacro 57 21 0% 4 4
0.8461538684.6%
 

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.internal.macro.ctsreport;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
24    import java.util.List;
25    import java.util.Map;
26    import java.util.Set;
27   
28    import javax.inject.Inject;
29    import javax.inject.Named;
30    import javax.inject.Singleton;
31    import javax.script.ScriptContext;
32   
33    import org.apache.commons.lang3.tuple.Pair;
34    import org.slf4j.Logger;
35    import org.xwiki.component.annotation.Component;
36    import org.xwiki.rendering.block.Block;
37    import org.xwiki.rendering.block.XDOM;
38    import org.xwiki.rendering.macro.AbstractNoParameterMacro;
39    import org.xwiki.rendering.macro.MacroContentParser;
40    import org.xwiki.rendering.macro.MacroExecutionException;
41    import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
42    import org.xwiki.rendering.renderer.BlockRenderer;
43    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
44    import org.xwiki.rendering.transformation.MacroTransformationContext;
45    import org.xwiki.script.ScriptContextManager;
46   
47    /**
48    * Parses CTS JUnit Results and make the generated data structures available in the Script Context for Script macros
49    * to use.
50    *
51    * @version $Id: f5f7dbe4c756fb15415cc7f0e762756eaeae282f $
52    * @since 4.1M2
53    */
54    @Component
55    @Named("ctsdata")
56    @Singleton
 
57    public class CTSDataMacro extends AbstractNoParameterMacro
58    {
59    /**
60    * The description of the macro.
61    */
62    private static final String DESCRIPTION =
63    "Parses XWiki Rendering Compatibility Test Suite (CTS) JUnit Results";
64   
65    /**
66    * The description of the macro content.
67    */
68    private static final String CONTENT_DESCRIPTION = "Textual results of CTS execution, one result per line";
69   
70    /**
71    * The logger to log.
72    */
73    @Inject
74    private Logger logger;
75   
76    @Inject
77    private ScriptContextManager scriptContextManager;
78   
79    /**
80    * Used to parse the macro content since it can contain wiki markup.
81    */
82    @Inject
83    private MacroContentParser contentParser;
84   
85    /**
86    * Used to render the macro content as text.
87    */
88    @Inject
89    @Named("plain/1.0")
90    private BlockRenderer plainTextRenderer;
91   
92    /**
93    * Create and initialize the descriptor of the macro.
94    */
 
95  1 toggle public CTSDataMacro()
96    {
97  1 super("CTS Data", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION));
98  1 setDefaultCategory(DEFAULT_CATEGORY_DEVELOPMENT);
99    }
100   
 
101  0 toggle @Override
102    public boolean supportsInlineMode()
103    {
104  0 return true;
105    }
106   
 
107  1 toggle @Override
108    public List<Block> execute(Object unusedParameters, String content, MacroTransformationContext context)
109    throws MacroExecutionException
110    {
111    // We consider the content as containing wiki syntax so we parse it and render it with the Plain text parser.
112  1 XDOM xdom = this.contentParser.parse(content, context, true, false);
113  1 DefaultWikiPrinter printer = new DefaultWikiPrinter();
114  1 this.plainTextRenderer.render(xdom, printer);
115  1 String parsedContent = printer.toString();
116   
117    // Parse the results
118  1 TestParser parser = new TestParser();
119  1 List<Result> results = new ArrayList<Result>();
120  1 for (String resultLine : parsedContent.split("[\\r\\n]+")) {
121  4 results.add(parser.parse(resultLine));
122    }
123   
124    // Bind 2 variables in the Script Context so that they can be used by Script macros
125  1 ScriptContext scriptContext = this.scriptContextManager.getCurrentScriptContext();
126  1 if (scriptContext != null) {
127  1 ResultExtractor extractor = new ResultExtractor();
128  1 Set<String> testNames = extractor.extractByTestName(results);
129  1 scriptContext.setAttribute("ctsTestNames", testNames, ScriptContext.ENGINE_SCOPE);
130  1 Map<String, Pair<Set<Test>, Set<Test>>> tests = extractor.extractBySyntax(results);
131  1 extractor.normalize(testNames, tests);
132  1 scriptContext.setAttribute("ctsTests", tests, ScriptContext.ENGINE_SCOPE);
133    } else {
134  0 this.logger.warn("Script Context not found in the Execution Context. CTS Data variable not bound!");
135    }
136   
137  1 return Collections.emptyList();
138    }
139    }