<commit_before>/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.test.integration.domain.mixed;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;

/**
 * @author <a href="<EMAIL>"><NAME></a>
 * @author <NAME>
 */
class OldVersionCopier {

    private static final String OLD_VERSIONS_DIR = "jboss.test.mixed.domain.dir";

    private final Version.AsVersion version;
    private final Path oldVersionsBaseDir;
    private final Path targetOldVersions = Paths.get("target/old-versions/");


    private OldVersionCopier(Version.AsVersion version, Path oldVersionsBaseDir) {
        this.version = version;
        this.oldVersionsBaseDir = oldVersionsBaseDir;
    }

    static Path getOldVersionDir(Version.AsVersion version) {

        OldVersionCopier copier = new OldVersionCopier(version, obtainOldVersionsDir());
        Path result = copier.getExpandedPath();
        if (Files.exists(result) && Files.isDirectory(result) && Files.exists(result.resolve("jboss-modules.jar"))) { //verify expanded version is proper
            return result;
        }
        return copier.expandAsInstance(version);
    }

    private static Path obtainOldVersionsDir() {
        String error = "System property '" + OLD_VERSIONS_DIR + "' must be set to a directory containing old versions";
        String oldVersionsDir = System.getProperty(OLD_VERSIONS_DIR);
        if (oldVersionsDir == null) {
            throw new RuntimeException(error);
        }
        Path file = Paths.get(oldVersionsDir);
        if (Files.notExists(file) || !Files.isDirectory(file)) {
            throw new RuntimeException(error);
        }
        return file;
    }

    private Path getExpandedPath() {
        return targetOldVersions.resolve(version.getFullVersionName());
    }

    private Path expandAsInstance(Version.AsVersion version) {
        createIfNotExists(targetOldVersions);

        Path file = oldVersionsBaseDir.resolve(version.getZipFileName());
        if (Files.notExists(file)) {
            throw new RuntimeException("Old version not found in " + file.toAbsolutePath().toString());
        }
        try {
            return expandAsInstance(file);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private Path expandAsInstance(final Path file) throws Exception {
        Path versionDir = getExpandedPath();
        createIfNotExists(versionDir);
        unzip(file, versionDir);
        return versionDir;
    }

    private static boolean shouldSkip(Path path) {
        String entryName = path.toString();
        if (entryName.endsWith("/docs/") || entryName.endsWith("/bundles/")) {
            return true;
        } else if (entryName.endsWith("/bin/") || entryName.endsWith("/welcome-content/")) {
            return true;
        } else if (entryName.endsWith("/eap/dir/")) { //console files
            return true;
        }
        return false;
    }

    private void createIfNotExists(Path file) {
        if (Files.notExists(file)) {
            try {
                Files.createDirectories(file);<commit_msg>[WFLY-9634] OldVersionCopier needs to create empty welcome-content/ directory, as 7.1.0 seems stricter about its existance<commit_after>/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.test.integration.domain.mixed;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;

/**
 * @author <a href="<EMAIL>"><NAME></a>
 * @author <NAME>
 */
class OldVersionCopier {

    private static final String OLD_VERSIONS_DIR = "jboss.test.mixed.domain.dir";

    private final Version.AsVersion version;
    private final Path oldVersionsBaseDir;
    private final Path targetOldVersions = Paths.get("target/old-versions/");


    private OldVersionCopier(Version.AsVersion version, Path oldVersionsBaseDir) {
        this.version = version;
        this.oldVersionsBaseDir = oldVersionsBaseDir;
    }

    static Path getOldVersionDir(Version.AsVersion version) {

        OldVersionCopier copier = new OldVersionCopier(version, obtainOldVersionsDir());
        Path result = copier.getExpandedPath();
        if (Files.exists(result) && Files.isDirectory(result) && Files.exists(result.resolve("jboss-modules.jar"))) { //verify expanded version is proper
            return result;
        }
        return copier.expandAsInstance(version);
    }

    private static Path obtainOldVersionsDir() {
        String error = "System property '" + OLD_VERSIONS_DIR + "' must be set to a directory containing old versions";
        String oldVersionsDir = System.getProperty(OLD_VERSIONS_DIR);
        if (oldVersionsDir == null) {
            throw new RuntimeException(error);
        }
        Path file = Paths.get(oldVersionsDir);
        if (Files.notExists(file) || !Files.isDirectory(file)) {
            throw new RuntimeException(error);
        }
        return file;
    }

    private Path getExpandedPath() {
        return targetOldVersions.resolve(version.getFullVersionName());
    }

    private Path expandAsInstance(Version.AsVersion version) {
        createIfNotExists(targetOldVersions);

        Path file = oldVersionsBaseDir.resolve(version.getZipFileName());
        if (Files.notExists(file)) {
            throw new RuntimeException("Old version not found in " + file.toAbsolutePath().toString());
        }
        try {
            return expandAsInstance(file);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private Path expandAsInstance(final Path file) throws Exception {
        Path versionDir = getExpandedPath();
        createIfNotExists(versionDir);
        unzip(file, versionDir);
        return versionDir;
    }

    private static boolean shouldSkip(Path path) {
        String entryName = path.toString();
        if (entryName.endsWith("/docs/") || entryName.endsWith("/bundles/")) {
            return true;
        } else if (entryName.endsWith("/bin/")) {
            return true;
        } else if (entryName.endsWith("/eap/dir/")) { //console files
            return true;
        } else if (entryName.contains("/welcome-content/") && !entryName.endsWith("/welcome-content/")) {
            //Create the directory but don't include any files
            return true;
        }
        return false;
    }

    private void createIfNotExists(Path file) {
        if (Files.notExists(file)) {
            try {
                Files.createDirectories(file);
{'fraction_non_alphanumeric': 0.09706075893836368, 'fraction_numerical': 0.004496600131607809, 'mean_word_length': 3.1169300225733636, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '4235825', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2441, 'n_words': 758}