All Downloads are FREE. Search and download functionalities are using the official Maven repository.

patterntesting.runtime.junit.internal.TestOn Maven / Gradle / Ivy

Go to download

PatternTesting Runtime (patterntesting-rt) is the runtime component for the PatternTesting framework. It provides the annotations and base classes for the PatternTesting testing framework (e.g. patterntesting-check, patterntesting-concurrent or patterntesting-exception) but can be also used standalone for classpath monitoring or profiling. It uses AOP and AspectJ to perform this feat.

There is a newer version: 2.4.0
Show newest version
/*
 * $Id: TestOn.java,v 1.15 2011/11/21 20:46:43 oboehm Exp $
 *
 * Copyright (c) 2010 by Oliver Boehm
 *
 * Licensed 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 orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (c)reated 23.04.2010 by oliver ([email protected])
 */

package patterntesting.runtime.junit.internal;

import java.net.*;

import org.apache.commons.lang.*;
import org.slf4j.*;

import patterntesting.runtime.net.Localhost;
import patterntesting.runtime.util.*;

/**
 * This is a local helper class for the RunTestOn and SkipTestOn annotation.
 * So most of the methods are package default because there is no need to
 * access from somewhere else.
 *
 * @author oliver
 * @since 1.0 (23.04.2010)
 */
public final class TestOn {

    private static final Logger log = LoggerFactory.getLogger(TestOn.class);
    private String osName = Environment.OS_NAME;
    private String osArch = Environment.OS_ARCH;
    private String userName = Environment.USER_NAME;
    /** Contains the reason of the last match. */
    private transient String reason;

    /**
     * For testing you can set the OS name.
     *
     * @param name the new os name
     */
    public void setOsName(final String name) {
        this.osName = name;
    }

    /**
     * For testing you can set the OS architector.
     *
     * @param arch the new os architector
     */
    public void setOsArch(final String arch) {
        this.osArch = arch;
    }

    /**
     * For testing you can set the user name.
     *
     * @param name the new user name
     */
    public void setUserName(final String name) {
        this.userName = name;
    }

    /**
     * Gets the reason.
     *
     * @return the reason
     */
    public String getReason() {
        return this.reason;
    }

    /**
     * Checks for reason.
     *
     * @return true, if reason is set
     */
    public boolean hasReason() {
        return this.reason != null;
    }

    /**
     * If the operating system(s) match and one of the other parameters (if
     * they contain values) this method will return true.
     *
     * @param value same as os parameter
     * @param os the operating system(s) which should match
     * @param arch the architectures
     * @param version the version(s) of the operating systems
     * @param host the host(s)
     * @param javaVersions the java version(s)
     * @param javaVendors the java vendor(s)
     * @param users the user(s)
     * @param systemProps the system properties
     * @return true if the parameter matches
     */
    public boolean matches(final String[] value, final String[] os,
            final String[] arch, final String[] version, final String[] host,
            final String[] javaVersions, final String[] javaVendors,
            final String[] users, final String[] systemProps) {
        String[] osValue = StringUtils.isEmpty(os[0]) ? value : os;
        return matches(osValue, arch, version, host, javaVersions, javaVendors, users, systemProps);
    }

    /**
     * If the operating system(s) match and one of the other parameters (if
     * they contain values) this method will return true.
     *
     * @param os the operating system(s) which should match
     * @param arch the architectures
     * @param version the version(s) of the operating systems
     * @param host the host(s)
     * @param javaVersions the java version(s)
     * @param javaVendors the java vendor(s)
     * @param users the user(s)
     * @param systemProps the system properties
     * @return true if the parameter matches
     */
    public boolean matches(final String[] os,
            final String[] arch, final String[] version, final String[] host,
            final String[] javaVersions, final String[] javaVendors,
            final String[] users, final String[] systemProps) {
        boolean valueGiven = false;
        if (!StringUtils.isEmpty(os[0])) {
            if (!matches(os, osName)) {
                return false;
            }
            valueGiven = true;
        }
        if (!StringUtils.isEmpty(arch[0])) {
            if (!matches(arch, osArch)) {
                return false;
            }
            valueGiven = true;
        }
        if (!StringUtils.isEmpty(version[0])) {
            if (!matches(version, Environment.OS_VERSION)) {
                return false;
            }
            valueGiven = true;
        }
        if (!StringUtils.isEmpty(javaVersions[0])) {
            if (!matches(javaVersions, Environment.JAVA_VERSION)) {
                return false;
            }
            valueGiven = true;
        }
        if (!StringUtils.isEmpty(javaVendors[0])) {
            if (!matches(javaVendors, Environment.JAVA_VENDOR)) {
                return false;
            }
            valueGiven = true;
        }
        if (!StringUtils.isEmpty(users[0])) {
            if (!matches(users, userName)) {
                return false;
            }
            valueGiven = true;
        }
        if (!StringUtils.isEmpty(systemProps[0])) {
            if (!Environment.matchesOneOf(systemProps)) {
                return false;
            }
            valueGiven = true;
        }
        // this call is expensive so we shift it to the end
        if (!StringUtils.isEmpty(host[0])) {
            if (!runsOn(host)) {
                return false;
            }
            valueGiven = true;
        }
        if (!valueGiven) {
            throw new IllegalArgumentException("no value(s) given");
            //return true;
        }
        reason = (StringUtils.isEmpty(os[0]) ? "" : osName)
                + (StringUtils.isEmpty(arch[0]) ? "" : osArch)
                + (StringUtils.isEmpty(version[0]) ? "" : Environment.OS_VERSION)
                + (StringUtils.isEmpty(javaVersions[0]) ? "" : "JDK " + Environment.JAVA_VERSION)
                + (StringUtils.isEmpty(javaVendors[0]) ? "" : Environment.JAVA_VENDOR + " as vendor")
                + (StringUtils.isEmpty(users[0]) ? "" : Environment.JAVA_VENDOR + " as user")
                + (StringUtils.isEmpty(systemProps[0]) ? "" : Converter.toString(systemProps))
                + (StringUtils.isEmpty(host[0]) ? "" : "host " + Converter.toString(host))
                + " detected";
        return true;
    }

    private static boolean matches(final String[] names, final String name) {
        for (int i = 0; i < names.length; i++) {
            if (matches(names[i], name)) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * If the given pattern parameter contains an asterisk ("*") it is
     * considered as pattern. Otherwise only the beginning with name
     * must match.
     *
     * @param pattern the pattern
     * @param name the name
     * @return true, if successful
     * @since 1.1
     */
    private static boolean matches(final String pattern, final String name) {
        if (name.startsWith(pattern)) {
            return true;
        }
        if (pattern.contains("*") || (pattern.contains("?"))) {
            String regex = wildcardToRegex(pattern);
            return name.matches(regex);
        }
        return false;
    }

    private static boolean runsOn(final String[] hosts) {
        try {
            InetAddress localhost = InetAddress.getLocalHost();
            if (ArrayUtils.contains(hosts, localhost.getHostAddress())
                    || matches(hosts, localhost.getHostName())) {
                return true;
            }
            return Localhost.matches(hosts);
        } catch (UnknownHostException e) {
            log.debug("can't get local InetAddress - using localhost", e);
            return runsOnLocalhost(hosts);
        }
    }

    private static boolean runsOnLocalhost(final String[] hosts) {
        return ArrayUtils.contains(hosts, "127.0.0.1")
                || StringHelper.containsIgnoreCase(hosts, "localhost"); //NOPMD
    }

    /**
     * Wildcard to regex.
     *
     * @param wildcard the wildcard
     * @return the string
     * @see "http://www.rgagnon.com/javadetails/java-0515.html"
     */
    private static String wildcardToRegex(final String wildcard) {
        StringBuffer s = new StringBuffer(wildcard.length());
        s.append('^');
        for (int i = 0, is = wildcard.length(); i < is; i++) {
            char c = wildcard.charAt(i);
            switch(c) {
                case '*':
                    s.append(".*");
                    break;
                case '?':
                    s.append(".");
                    break;
                    // escape special regexp-characters
                case '(': case ')': case '[': case ']': case '$':
                case '^': case '.': case '{': case '}': case '|':
                case '\\':
                    s.append("\\");
                    s.append(c);
                    break;
                default:
                    s.append(c);
                    break;
            }
        }
        s.append('$');
        return(s.toString());
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy