001/**
002 * Powerunit - A JDK1.8 test framework
003 * Copyright (C) 2014 Mathieu Boretti.
004 *
005 * This file is part of Powerunit
006 *
007 * Powerunit is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation, either version 3 of the License, or
010 * (at your option) any later version.
011 *
012 * Powerunit is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
019 */
020package ch.powerunit.surefire;
021
022import java.lang.reflect.Field;
023import java.lang.reflect.Method;
024import java.util.Collections;
025import java.util.HashSet;
026import java.util.Properties;
027import java.util.Set;
028
029import org.apache.maven.surefire.booter.ProviderParameterNames;
030import org.apache.maven.surefire.util.ScannerFilter;
031
032import ch.powerunit.Test;
033import ch.powerunit.TestDelegate;
034
035/**
036 * @author borettim
037 *
038 */
039public class PowerUnitProviderScannerFilter implements ScannerFilter {
040
041    private final Set<String> sgroups;
042
043    private final Set<String> sexcludedGroups;
044
045    public PowerUnitProviderScannerFilter(Properties parameters) {
046        String groups = parameters.getProperty(
047                ProviderParameterNames.TESTNG_GROUPS_PROP, "");
048        String excludedGroups = parameters.getProperty(
049                ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP, "");
050        Set<String> sgroups = new HashSet<String>();
051        Set<String> sexcludedGroups = new HashSet<String>();
052        for (String g : groups.split(",")) {
053            sgroups.add(g);
054        }
055        for (String g : excludedGroups.split(",")) {
056            sexcludedGroups.add(g);
057        }
058        this.sgroups = Collections.unmodifiableSet(sgroups);
059        this.sexcludedGroups = Collections.unmodifiableSet(sexcludedGroups);
060    }
061
062    @Override
063    public boolean accept(@SuppressWarnings("rawtypes") Class testClass) {
064        for (Method m : testClass.getDeclaredMethods()) {
065            if (m.isAnnotationPresent(Test.class)) {
066                return true;
067            }
068        }
069        for (Field f : testClass.getDeclaredFields()) {
070            if (f.isAnnotationPresent(TestDelegate.class)) {
071                return true;
072            }
073        }
074        return false;
075    }
076}