<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.sling.junit.impl.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Collection;

import javax.servlet.http.HttpServletResponse;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.junit.Renderer;
import org.apache.sling.junit.TestSelector;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

/** HTML renderer for JUnit servlet */
@Component(immediate=false)
@Service
public class HtmlRenderer extends RunListener implements Renderer {

    public static final String EXTENSION = "html";
    private PrintWriter output;
    
    /** @inheritDoc */
    public boolean appliesTo(TestSelector s) {
        // This is our default renderer, applies to the empty
        // extension as well
        return EXTENSION.equals(s.getExtension()) || "".equals(s.getExtension()); 
    }
    
    /** @inheritDoc */
    public String getExtension() {
        return EXTENSION;
    }

    /** @inheritDoc */
    public void info(String cssClass, String str) {
        output.println("<p class='" + cssClass + "'>");
        HtmlFilter.escape(output, str);
        output.println("</p>");
    }
    
    /** @inheritDoc */
    public void list(String cssClass, Collection<String> data) {
        output.println("<ul class='testNames'>");
        for(String str : data) {
            output.println("<li>");
            HtmlFilter.escape(output, str);
            output.println("</li>");
        }
        output.println("</ul>");
    }
    
    /** @inheritDoc */
    public void title(int level, String title) {
        output.print("<h" + level + ">");
        HtmlFilter.escape(output, title);
        output.print("</h" + level + ">");
    }
    
    /** @inheritDoc */
    public void link(String info, String url, String method) {
        output.println("<div class='link'>");
        
        if("POST".equalsIgnoreCase(method)) {
            output.print("<form method='POST' action='");
            output.print(url);
            output.print("'>");
            output.print("<input type='submit' value='");
            HtmlFilter.escape(output, info);
            output.print("'/>");
            output.println("</form>");
        } else {
            output.print("<a href='");
            output.print(url);
            HtmlFilter.escape(output, info);
            output.print("'>");
            output.println("</a>");
        }
        
        output.println("</div>");
    }

    /** @inheritDoc */
    public void setup(HttpServletResponse response, String pageTitle) throws IOException, UnsupportedEncodingException {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        output = response.getWriter();
        output.println("<html><head>");
        output.println("<link rel='stylesheet' type='text/css' href='" + JUnitServlet.CSS + "'/>");
        output.print("<title>");
        HtmlFilter.escape(output, pageTitle);
        output.println("</title>");
        output.println("</head><body><h1>");
        HtmlFilter.escape(output, pageTitle);
        output.println("</h1>");
    }
    
    /** @inheritDoc */
    public void cleanup() {
        output.println("</body>");
        output.println("</html>");
    }
    
    /** @inheritDoc */
    public RunListener getRunListener() {
        return this;
    }
    
    @Override
    public void testFailure(Failure failure) throws Exception {
        super.testFailure(failure);
        output.print("<div class='failure'><h3>");
        output.print("TEST FAILED: ");
        HtmlFilter.escape(output, failure.getTestHeader());
        output.print("</h3><div class='failureDetails'>");
        HtmlFilter.escape(output, failure.toString());
        output.println("</div></div>");
    }

    @Override
    public void testFinished(Description description) throws Exception {
        super.testFinished(description);
        output.print("<p class='finished'>Test finished: ");
        HtmlFilter.escape(output, description.toString());
        output.println("</p>");
    }

    @Override
    public void testIgnored(Description description) throws Exception {
        super.testIgnored(description);
        output.print("<p class='ignored'><h3>TEST IGNORED</h3><b>");
        HtmlFilter.escape(output, description.toString());
        output.println("</b></p>");
    }

    private void counter(String name, String cssName, int value) {
        final String cssClass = cssName + (value > 0 ? "NonZero" : "Zero");<commit_msg>SLING-2270 Junit Core HtmlRenderer outputs invalid HTML Apply patch by <NAME> (Many thanks)<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.sling.junit.impl.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Collection;

import javax.servlet.http.HttpServletResponse;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.junit.Renderer;
import org.apache.sling.junit.TestSelector;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

/** HTML renderer for JUnit servlet */
@Component(immediate=false)
@Service
public class HtmlRenderer extends RunListener implements Renderer {

    public static final String EXTENSION = "html";
    private PrintWriter output;
    
    /** @inheritDoc */
    public boolean appliesTo(TestSelector s) {
        // This is our default renderer, applies to the empty
        // extension as well
        return EXTENSION.equals(s.getExtension()) || "".equals(s.getExtension()); 
    }
    
    /** @inheritDoc */
    public String getExtension() {
        return EXTENSION;
    }

    /** @inheritDoc */
    public void info(String cssClass, String str) {
        output.println("<p class='" + cssClass + "'>");
        HtmlFilter.escape(output, str);
        output.println("</p>");
    }
    
    /** @inheritDoc */
    public void list(String cssClass, Collection<String> data) {
        output.println("<ul class='testNames'>");
        for(String str : data) {
            output.println("<li>");
            HtmlFilter.escape(output, str);
            output.println("</li>");
        }
        output.println("</ul>");
    }
    
    /** @inheritDoc */
    public void title(int level, String title) {
        output.print("<h" + level + ">");
        HtmlFilter.escape(output, title);
        output.print("</h" + level + ">");
    }
    
    /** @inheritDoc */
    public void link(String info, String url, String method) {
        output.println("<div class='link'>");
        
        if("POST".equalsIgnoreCase(method)) {
            output.print("<form method='POST' action='");
            output.print(url);
            output.print("'>");
            output.print("<input type='submit' value='");
            HtmlFilter.escape(output, info);
            output.print("'/>");
            output.println("</form>");
        } else {
            output.print("<a href='");
            output.print(url);
            HtmlFilter.escape(output, info);
            output.print("'>");
            output.println("</a>");
        }
        
        output.println("</div>");
    }

    /** @inheritDoc */
    public void setup(HttpServletResponse response, String pageTitle) throws IOException, UnsupportedEncodingException {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        output = response.getWriter();
        output.println("<html><head>");
        output.println("<link rel='stylesheet' type='text/css' href='" + JUnitServlet.CSS + "'/>");
        output.print("<title>");
        HtmlFilter.escape(output, pageTitle);
        output.println("</title>");
        output.println("</head><body><h1>");
        HtmlFilter.escape(output, pageTitle);
        output.println("</h1>");
    }
    
    /** @inheritDoc */
    public void cleanup() {
        output.println("</body>");
        output.println("</html>");
    }
    
    /** @inheritDoc */
    public RunListener getRunListener() {
        return this;
    }
    
    @Override
    public void testFailure(Failure failure) throws Exception {
        super.testFailure(failure);
        output.print("<div class='failure'><h3>");
        output.print("TEST FAILED: ");
        HtmlFilter.escape(output, failure.getTestHeader());
        output.print("</h3><div class='failureDetails'>");
        HtmlFilter.escape(output, failure.toString());
        output.println("</div></div>");
    }

    @Override
    public void testFinished(Description description) throws Exception {
        super.testFinished(description);
        output.print("<p class='finished'>Test finished: ");
        HtmlFilter.escape(output, description.toString());
        output.println("</p></div>");
    }

    @Override
    public void testIgnored(Description description) throws Exception {
        super.testIgnored(description);
        output.print("<p class='ignored'><h3>TEST IGNORED</h3><b>");
        HtmlFilter.escape(output, description.toString());
        output.println("</b></p>");
    }

    private void counter(String name, String cssName, int value) {
        final String cssClass = cssName + (value > 0 ? "NonZero" : "Zero");
{'fraction_non_alphanumeric': 0.13937656753851665, 'fraction_numerical': 0.0025080616266571123, 'mean_word_length': 2.819705781731098, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6747923', 'n_tokens_mistral': 3022, 'n_tokens_neox': 2980, 'n_words': 749}