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

File DefaultValidationTest.java

 

Code metrics

12
47
7
1
172
102
13
0.28
6.71
7
1.86

Classes

Class Line # Actions
DefaultValidationTest 38 47 0% 13 19
0.712121271.2%
 

Contributing tests

This file is covered by 2433 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.test.webstandards.framework;
21   
22    import java.io.BufferedReader;
23    import java.io.ByteArrayInputStream;
24    import java.io.ByteArrayOutputStream;
25    import java.io.PrintStream;
26    import java.io.StringReader;
27    import java.util.List;
28   
29    import org.apache.commons.httpclient.HttpClient;
30    import org.xwiki.validator.ValidationError;
31    import org.xwiki.validator.Validator;
32   
33    /**
34    * Verifies that all pages in the default wiki are valid HTML documents.
35    *
36    * @version $Id: d3d0b0ddadc3845b256fa16a1d4c6ba3b897d19f $
37    */
 
38    public class DefaultValidationTest extends AbstractValidationTest
39    {
40    protected Validator validator;
41   
42    /**
43    * We save the stdout stream since we replace it with our own in order to verify that XWiki doesn't generated any
44    * error while validating documents and we fail the build if it does.
45    */
46    protected PrintStream stdout;
47   
48    /**
49    * The new stdout stream we're using to replace the default console output.
50    */
51    protected ByteArrayOutputStream out;
52   
53    /**
54    * We save the stderr stream since we replace it with our own in order to verify that XWiki doesn't generated any
55    * error while validating documents and we fail the build if it does.
56    */
57    protected PrintStream stderr;
58   
59    /**
60    * The new stderr stream we're using to replace the default console output.
61    */
62    protected ByteArrayOutputStream err;
63   
 
64  2433 toggle public DefaultValidationTest(Target target, HttpClient client, Validator validator, String credentials)
65    throws Exception
66    {
67  2433 super("testDocumentValidity", target, client, credentials);
68   
69  2433 this.validator = validator;
70    }
71   
 
72  2433 toggle @Override
73    protected void setUp() throws Exception
74    {
75  2433 super.setUp();
76   
77    // TODO Until we find a way to incrementally display the result of tests this stays
78  2433 System.out.println(getName());
79   
80    // We redirect the stdout and the stderr in order to detect (server-side) error/warning
81    // messages like the ones generated by the velocity parser
82  2433 this.stdout = System.out;
83  2433 this.out = new ByteArrayOutputStream();
84  2433 System.setOut(new PrintStream(this.out));
85  2433 this.stderr = System.err;
86  2433 this.err = new ByteArrayOutputStream();
87  2433 System.setErr(new PrintStream(this.err));
88    }
89   
 
90  2433 toggle @Override
91    protected void tearDown() throws Exception
92    {
93    // Restore original stdout and stderr streams.
94  2433 String output = this.out.toString();
95  2433 String errput = this.err.toString();
96   
97  2433 System.setOut(this.stdout);
98  2433 System.out.print(output);
99  2433 System.setErr(this.stderr);
100  2433 System.err.print(errput);
101   
102    // Detect server-side error/warning messages from the stdout
103  2433 assertFalse("Errors found in the stdout output", hasLogErrors(output));
104  2433 assertFalse("Warnings found in the stdout output", hasLogWarnings(output));
105   
106    // Detect server-side error/warning messages from the stderr
107  2433 assertFalse("Errors found in the stderr output", hasLogErrors(errput));
108  2433 assertFalse("Warnings found in the stderr output", hasLogWarnings(errput));
109   
110  2433 super.tearDown();
111    }
112   
 
113  12168 toggle @Override
114    public String getName()
115    {
116  12168 return "Validating " + this.validator.getName() + " validity for: " + this.target.getName() + " executed "
117  12168 + (credentials == null ? "as guest" : "with credentials " + credentials);
118    }
119   
 
120  2433 toggle public void testDocumentValidity() throws Exception
121    {
122  2433 byte[] responseBody = getResponseBody();
123   
124  2433 this.validator.setDocument(new ByteArrayInputStream(responseBody));
125  2433 List<ValidationError> errors = this.validator.validate();
126   
127  2433 StringBuffer message = new StringBuffer();
128  2433 message.append("Validation errors in " + this.target.getName());
129  2433 boolean hasError = false;
130  2433 for (ValidationError error : errors) {
131  79 if (error.getType() == ValidationError.Type.WARNING) {
132  79 if (error.getLine() >= 0) {
133  79 System.out.println("Warning at " + error.getLine() + ":" + error.getColumn() + " "
134    + error.getMessage());
135    } else {
136  0 System.out.println("Warning " + error.getMessage());
137    }
138    } else {
139  0 if (error.getLine() >= 0) {
140  0 message.append("\n" + error.toString() + " at line [" + error.getLine() + "] column ["
141    + error.getColumn() + "]");
142    } else {
143  0 message.append("\n" + error.toString());
144    }
145   
146  0 hasError = true;
147    }
148    }
149   
150  2433 if (hasError) {
151  0 System.err.println("");
152  0 System.err.println("Validated content:");
153  0 BufferedReader reader = new BufferedReader(new StringReader(new String(responseBody)));
154  0 int index = 1;
155  0 for (String line = reader.readLine(); line != null; line = reader.readLine(), ++index) {
156  0 System.err.println(index + "\t" + line);
157    }
158    }
159   
160  2433 assertFalse(message.toString(), hasError);
161    }
162   
 
163  4866 toggle protected boolean hasLogErrors(String output)
164    {
165  4866 return output.indexOf("ERROR") >= 0 || output.indexOf("ERR") >= 0;
166    }
167   
 
168  4866 toggle protected boolean hasLogWarnings(String output)
169    {
170  4866 return output.indexOf("WARNING") >= 0 || output.indexOf("WARN") >= 0;
171    }
172    }