Search in sources :

Example 11 with Configuration

use of org.openstreetmap.atlas.utilities.configuration.Configuration in project atlas-checks by osmlab.

the class TagTest method testConfiguration.

/**
 * Private function used by all test cases to easily test the configuration instead of rewriting
 * the code constantly.
 *
 * @param config
 *            The {@link Configuration} is string form that is being test.
 * @param numberOfFlags
 *            The number of expected flags to be processed based on the provided configuration
 */
private void testConfiguration(final String config, final int numberOfFlags) {
    final Atlas atlas = setup.getAtlas();
    final Configuration configuration = ConfigurationResolver.inlineConfiguration(config);
    final List<CheckFlag> flags = Iterables.asList(new BaseTestCheck(configuration).flags(atlas));
    // will return the number of flags for the number of objects because no restrictions
    Assert.assertEquals(numberOfFlags, flags.size());
}
Also used : Atlas(org.openstreetmap.atlas.geography.atlas.Atlas) Configuration(org.openstreetmap.atlas.utilities.configuration.Configuration) CheckFlag(org.openstreetmap.atlas.checks.flag.CheckFlag) BaseTestCheck(org.openstreetmap.atlas.checks.base.checks.BaseTestCheck)

Example 12 with Configuration

use of org.openstreetmap.atlas.utilities.configuration.Configuration in project atlas-checks by osmlab.

the class CheckResourceLoader method loadChecks.

/**
 * Loads checks that are enabled by some other means, defined by {@code isEnabled}
 *
 * @param isEnabled
 *            {@link Predicate} used to determine if a check is enabled
 * @param configuration
 *            {@link Configuration} used to loadChecks {@link CheckResourceLoader}
 * @param <T>
 *            check type
 * @return a {@link Set} of checks
 */
@SuppressWarnings("unchecked")
public <T extends Check> Set<T> loadChecks(final Predicate<Class> isEnabled, final Configuration configuration) {
    final Set<T> checks = new HashSet<>();
    final Time time = Time.now();
    try {
        final ClassPath classPath = ClassPath.from(Thread.currentThread().getContextClassLoader());
        packages.forEach(packageName -> classPath.getTopLevelClassesRecursive(packageName).forEach(classInfo -> {
            final Class<?> checkClass = classInfo.load();
            if (checkType.isAssignableFrom(checkClass) && !Modifier.isAbstract(checkClass.getModifiers()) && isEnabled.test(checkClass)) {
                try {
                    Object check;
                    try {
                        check = checkClass.getConstructor(Configuration.class).newInstance(configuration);
                    } catch (final InvocationTargetException oops) {
                        throw new CoreException("Unable to create a configurable instance of {}", checkClass.getSimpleName(), oops);
                    } catch (final NoSuchMethodException oops) {
                        check = checkClass.newInstance();
                    }
                    if (check != null) {
                        checks.add((T) check);
                    }
                } catch (final ClassCastException | InstantiationException | IllegalAccessException oops) {
                    logger.error("Failed to instantiate {}, ignoring. Reason: {}", checkClass.getName(), oops.getMessage());
                }
            }
        }));
    } catch (final IOException oops) {
        throw new CoreException("Failed to discover {} classes on classpath", checkType.getSimpleName());
    }
    logger.info("Loaded {} {} in {}", checks.size(), checkType.getSimpleName(), time.elapsedSince());
    return checks;
}
Also used : Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) CoreException(org.openstreetmap.atlas.exception.CoreException) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet) List(java.util.List) Iterables(org.openstreetmap.atlas.utilities.collections.Iterables) Configuration(org.openstreetmap.atlas.utilities.configuration.Configuration) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) Time(org.openstreetmap.atlas.utilities.time.Time) ClassPath(com.google.common.reflect.ClassPath) Collections(java.util.Collections) MultiMap(org.openstreetmap.atlas.utilities.maps.MultiMap) ClassPath(com.google.common.reflect.ClassPath) Configuration(org.openstreetmap.atlas.utilities.configuration.Configuration) Time(org.openstreetmap.atlas.utilities.time.Time) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CoreException(org.openstreetmap.atlas.exception.CoreException) HashSet(java.util.HashSet)

Example 13 with Configuration

use of org.openstreetmap.atlas.utilities.configuration.Configuration in project atlas-checks by osmlab.

the class CheckResourceLoader method getConfigurationForCountry.

/**
 * Get configuration for a specific country, overriding for country specific overrides and group
 * specific overrides
 *
 * @param country
 *            country string
 * @return {@link Configuration}
 */
public Configuration getConfigurationForCountry(final String country) {
    Configuration specializedConfiguration = this.configuration.configurationForKeyword(country);
    final List<String> groups = this.countryGroups.get(country);
    if (groups != null) {
        for (final String group : groups) {
            specializedConfiguration = specializedConfiguration.configurationForKeyword(group);
        }
    }
    return specializedConfiguration;
}
Also used : Configuration(org.openstreetmap.atlas.utilities.configuration.Configuration)

Aggregations

Configuration (org.openstreetmap.atlas.utilities.configuration.Configuration)13 Test (org.junit.Test)6 List (java.util.List)4 CoreException (org.openstreetmap.atlas.exception.CoreException)4 Iterables (org.openstreetmap.atlas.utilities.collections.Iterables)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 BaseTestCheck (org.openstreetmap.atlas.checks.base.checks.BaseTestCheck)3 CheckResourceLoaderTestCheck (org.openstreetmap.atlas.checks.base.checks.CheckResourceLoaderTestCheck)3 Atlas (org.openstreetmap.atlas.geography.atlas.Atlas)3 StringList (org.openstreetmap.atlas.utilities.collections.StringList)3 CommandMap (org.openstreetmap.atlas.utilities.runtime.CommandMap)3 Objects (java.util.Objects)2