<commit_before>/*
 * This is eMonocot, a global online biodiversity information resource.
 *
 * Copyright © 2011–2015 The Board of Trustees of the Royal Botanic Gardens, Kew and The University of Oxford
 *
 * eMonocot is free software: you can redistribute it and/or modify it under the terms of the
 * GNU Affero General Public License as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * eMonocot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * The complete text of the GNU Affero General Public License is in the source repository as the file
 * ‘COPYING’.  It is also available from <http://www.gnu.org/licenses/>.
 */
package org.emonocot.test;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.emonocot.portal.driver.WebDriverFacade;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.springframework.core.io.ClassPathResource;

/**
 *
 * @author ben
 *
 */
public class TakeScreenshotListener extends RunListener {

    /**
     * @param failure Set the failure
     */
    public final void testFailure(final Failure failure) {
        try {
            File file = ((TakesScreenshot) WebDriverFacade.getWebDriver())
                    .getScreenshotAs(OutputType.FILE);
            ClassPathResource root = new ClassPathResource("/");
            String screenshotName = File.separator + "screenshots"
                    + File.separator
                    + failure.getDescription().getClassName() + "-"
                    + failure.getDescription().getMethodName() + ".png";
            screenshotName = screenshotName.replace(" ", "_");
            screenshotName = screenshotName.replace(":", "");

            File screenshotDirectory = new File(root.getFile().getParentFile()
                    .getAbsolutePath()
                    + File.separator + "screenshots" + File.separator);
            if (!screenshotDirectory.exists()) {
                screenshotDirectory.mkdir();
            }
            File screenshot = new File(root.getFile().getParentFile()
                    .getAbsolutePath()
                    + screenshotName);
            screenshot.createNewFile();
            FileUtils.copyFile(file, screenshot);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}<commit_msg>Improved functional test screenshot writer to use better filenames, less likely to hit system limits.<commit_after>/*
 * This is eMonocot, a global online biodiversity information resource.
 *
 * Copyright © 2011–2015 The Board of Trustees of the Royal Botanic Gardens, Kew and The University of Oxford
 *
 * eMonocot is free software: you can redistribute it and/or modify it under the terms of the
 * GNU Affero General Public License as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * eMonocot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * The complete text of the GNU Affero General Public License is in the source repository as the file
 * ‘COPYING’.  It is also available from <http://www.gnu.org/licenses/>.
 */
package org.emonocot.test;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.emonocot.portal.driver.WebDriverFacade;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.springframework.core.io.ClassPathResource;

/**
 *
 * @author ben
 *
 */
public class TakeScreenshotListener extends RunListener {

    /**
     * @param failure Set the failure
     */
    public final void testFailure(final Failure failure) {
        String screenshotName = null;
        try {
            File file = ((TakesScreenshot) WebDriverFacade.getWebDriver())
                    .getScreenshotAs(OutputType.FILE);
            ClassPathResource root = new ClassPathResource("/");
            screenshotName = File.separator + "screenshots"
                    + File.separator
                    + failure.getDescription().getClassName() + "-"
                    + failure.getDescription().getMethodName() + ".png";
            screenshotName = screenshotName.replace(" ", "_");
            screenshotName = screenshotName.replace(":", "");
            screenshotName = screenshotName.replaceAll("_+", "_");

            File screenshotDirectory = new File(root.getFile().getParentFile()
                    .getAbsolutePath()
                    + File.separator + "screenshots" + File.separator);
            if (!screenshotDirectory.exists()) {
                screenshotDirectory.mkdir();
            }
            File screenshot = new File(root.getFile().getParentFile()
                    .getAbsolutePath()
                    + screenshotName);
            screenshot.createNewFile();
            FileUtils.copyFile(file, screenshot);
        } catch (IOException e) {
            System.err.println("Error writing screenshot "+screenshotName);
            e.printStackTrace();
        }
    }
}
{'fraction_non_alphanumeric': 0.09220733427362482, 'fraction_numerical': 0.003173483779971791, 'mean_word_length': 2.683766233766234, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, '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': '141669', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1404, 'n_words': 454}