Example usage for org.springframework.util Assert notEmpty

List of usage examples for org.springframework.util Assert notEmpty

Introduction

In this page you can find the example usage for org.springframework.util Assert notEmpty.

Prototype

public static void notEmpty(@Nullable Map<?, ?> map, Supplier<String> messageSupplier) 

Source Link

Document

Assert that a Map contains entries; that is, it must not be null and must contain at least one entry.

Usage

From source file:org.springframework.test.context.ContextLoaderUtils.java

/**
 * Resolve the list of merged {@code ApplicationContextInitializer} classes for the
 * supplied list of {@code ContextConfigurationAttributes}.
 *
 * <p>Note that the {@link ContextConfiguration#inheritInitializers inheritInitializers}
 * flag of {@link ContextConfiguration @ContextConfiguration} will be taken into
 * consideration. Specifically, if the {@code inheritInitializers} flag is set to
 * {@code true} for a given level in the class hierarchy represented by the provided
 * configuration attributes, context initializer classes defined at the given level
 * will be merged with those defined in higher levels of the class hierarchy.
 *
 * @param configAttributesList the list of configuration attributes to process; must
 * not be {@code null} or <em>empty</em>; must be ordered <em>bottom-up</em>
 * (i.e., as if we were traversing up the class hierarchy)
 * @return the set of merged context initializer classes, including those from
 * superclasses if appropriate (never {@code null})
 * @since 3.2//from w w  w.  j a  v  a 2  s. c  o  m
 */
static Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> resolveInitializerClasses(
        List<ContextConfigurationAttributes> configAttributesList) {
    Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be empty");

    final Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> initializerClasses = //
            new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();

    for (ContextConfigurationAttributes configAttributes : configAttributesList) {
        if (logger.isTraceEnabled()) {
            logger.trace(
                    String.format("Processing context initializers for context configuration attributes %s",
                            configAttributes));
        }

        initializerClasses.addAll(Arrays.asList(configAttributes.getInitializers()));

        if (!configAttributes.isInheritInitializers()) {
            break;
        }
    }

    return initializerClasses;
}

From source file:org.springframework.test.context.ContextLoaderUtils.java

/**
 * Build the {@link MergedContextConfiguration merged context configuration} for
 * the supplied {@link Class testClass} and {@code defaultContextLoaderClassName},
 * taking into account context hierarchies declared via
 * {@link ContextHierarchy @ContextHierarchy} and
 * {@link ContextConfiguration @ContextConfiguration}.
 *
 * @param testClass the test class for which the {@code MergedContextConfiguration}
 * should be built (must not be {@code null})
 * @param defaultContextLoaderClassName the name of the default {@code ContextLoader}
 * class to use (may be {@code null})/*from   ww  w. ja v a 2 s. c  om*/
 * @param cacheAwareContextLoaderDelegate the cache-aware context loader delegate to
 * be passed to the {@code MergedContextConfiguration} constructor
 * @return the merged context configuration
 * @see #buildContextHierarchyMap(Class)
 * @see #buildMergedContextConfiguration(Class, List, String, MergedContextConfiguration, CacheAwareContextLoaderDelegate)
 */
@SuppressWarnings("javadoc")
static MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass,
        String defaultContextLoaderClassName, CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) {

    if (testClass.isAnnotationPresent(ContextHierarchy.class)) {
        Map<String, List<ContextConfigurationAttributes>> hierarchyMap = buildContextHierarchyMap(testClass);

        MergedContextConfiguration parentConfig = null;
        MergedContextConfiguration mergedConfig = null;

        for (List<ContextConfigurationAttributes> list : hierarchyMap.values()) {
            List<ContextConfigurationAttributes> reversedList = new ArrayList<ContextConfigurationAttributes>(
                    list);
            Collections.reverse(reversedList);

            // Don't use the supplied testClass; instead ensure that we are
            // building the MCC for the actual test class that declared the
            // configuration for the current level in the context hierarchy.
            Assert.notEmpty(reversedList, "ContextConfigurationAttributes list must not be empty");
            Class<?> declaringClass = reversedList.get(0).getDeclaringClass();

            mergedConfig = buildMergedContextConfiguration(declaringClass, reversedList,
                    defaultContextLoaderClassName, parentConfig, cacheAwareContextLoaderDelegate);
            parentConfig = mergedConfig;
        }

        // Return the last level in the context hierarchy
        return mergedConfig;
    } else {
        return buildMergedContextConfiguration(testClass, resolveContextConfigurationAttributes(testClass),
                defaultContextLoaderClassName, null, cacheAwareContextLoaderDelegate);
    }
}

From source file:org.springframework.test.context.support.AbstractTestContextBootstrapper.java

/**
 * {@inheritDoc}//from   w  w  w.j a  va 2 s .  co  m
 */
@SuppressWarnings("unchecked")
@Override
public final MergedContextConfiguration buildMergedContextConfiguration() {
    Class<?> testClass = getBootstrapContext().getTestClass();
    CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = getCacheAwareContextLoaderDelegate();

    if (MetaAnnotationUtils.findAnnotationDescriptorForTypes(testClass, ContextConfiguration.class,
            ContextHierarchy.class) == null) {
        return buildDefaultMergedContextConfiguration(testClass, cacheAwareContextLoaderDelegate);
    }

    if (AnnotationUtils.findAnnotation(testClass, ContextHierarchy.class) != null) {
        Map<String, List<ContextConfigurationAttributes>> hierarchyMap = ContextLoaderUtils
                .buildContextHierarchyMap(testClass);
        MergedContextConfiguration parentConfig = null;
        MergedContextConfiguration mergedConfig = null;

        for (List<ContextConfigurationAttributes> list : hierarchyMap.values()) {
            List<ContextConfigurationAttributes> reversedList = new ArrayList<>(list);
            Collections.reverse(reversedList);

            // Don't use the supplied testClass; instead ensure that we are
            // building the MCC for the actual test class that declared the
            // configuration for the current level in the context hierarchy.
            Assert.notEmpty(reversedList, "ContextConfigurationAttributes list must not be empty");
            Class<?> declaringClass = reversedList.get(0).getDeclaringClass();

            mergedConfig = buildMergedContextConfiguration(declaringClass, reversedList, parentConfig,
                    cacheAwareContextLoaderDelegate, true);
            parentConfig = mergedConfig;
        }

        // Return the last level in the context hierarchy
        Assert.state(mergedConfig != null, "No merged context configuration");
        return mergedConfig;
    } else {
        return buildMergedContextConfiguration(testClass,
                ContextLoaderUtils.resolveContextConfigurationAttributes(testClass), null,
                cacheAwareContextLoaderDelegate, true);
    }
}

From source file:org.springframework.test.context.support.AbstractTestContextBootstrapper.java

/**
 * Build the {@link MergedContextConfiguration merged context configuration}
 * for the supplied {@link Class testClass}, context configuration attributes,
 * and parent context configuration./*from w  w  w  .j  a v a  2 s . c  o m*/
 * @param testClass the test class for which the {@code MergedContextConfiguration}
 * should be built (must not be {@code null})
 * @param configAttributesList the list of context configuration attributes for the
 * specified test class, ordered <em>bottom-up</em> (i.e., as if we were
 * traversing up the class hierarchy); never {@code null} or empty
 * @param parentConfig the merged context configuration for the parent application
 * context in a context hierarchy, or {@code null} if there is no parent
 * @param cacheAwareContextLoaderDelegate the cache-aware context loader delegate to
 * be passed to the {@code MergedContextConfiguration} constructor
 * @param requireLocationsClassesOrInitializers whether locations, classes, or
 * initializers are required; typically {@code true} but may be set to {@code false}
 * if the configured loader supports empty configuration
 * @return the merged context configuration
 * @see #resolveContextLoader
 * @see ContextLoaderUtils#resolveContextConfigurationAttributes
 * @see SmartContextLoader#processContextConfiguration
 * @see ContextLoader#processLocations
 * @see ActiveProfilesUtils#resolveActiveProfiles
 * @see ApplicationContextInitializerUtils#resolveInitializerClasses
 * @see MergedContextConfiguration
 */
private MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass,
        List<ContextConfigurationAttributes> configAttributesList,
        @Nullable MergedContextConfiguration parentConfig,
        CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate,
        boolean requireLocationsClassesOrInitializers) {

    Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be null or empty");

    ContextLoader contextLoader = resolveContextLoader(testClass, configAttributesList);
    List<String> locations = new ArrayList<>();
    List<Class<?>> classes = new ArrayList<>();
    List<Class<?>> initializers = new ArrayList<>();

    for (ContextConfigurationAttributes configAttributes : configAttributesList) {
        if (logger.isTraceEnabled()) {
            logger.trace(
                    String.format("Processing locations and classes for context configuration attributes %s",
                            configAttributes));
        }
        if (contextLoader instanceof SmartContextLoader) {
            SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader;
            smartContextLoader.processContextConfiguration(configAttributes);
            locations.addAll(0, Arrays.asList(configAttributes.getLocations()));
            classes.addAll(0, Arrays.asList(configAttributes.getClasses()));
        } else {
            String[] processedLocations = contextLoader.processLocations(configAttributes.getDeclaringClass(),
                    configAttributes.getLocations());
            locations.addAll(0, Arrays.asList(processedLocations));
            // Legacy ContextLoaders don't know how to process classes
        }
        initializers.addAll(0, Arrays.asList(configAttributes.getInitializers()));
        if (!configAttributes.isInheritLocations()) {
            break;
        }
    }

    Set<ContextCustomizer> contextCustomizers = getContextCustomizers(testClass,
            Collections.unmodifiableList(configAttributesList));

    Assert.state(
            !(requireLocationsClassesOrInitializers
                    && areAllEmpty(locations, classes, initializers, contextCustomizers)),
            () -> String.format(
                    "%s was unable to detect defaults, and no ApplicationContextInitializers "
                            + "or ContextCustomizers were declared for context configuration attributes %s",
                    contextLoader.getClass().getSimpleName(), configAttributesList));

    MergedTestPropertySources mergedTestPropertySources = TestPropertySourceUtils
            .buildMergedTestPropertySources(testClass);
    MergedContextConfiguration mergedConfig = new MergedContextConfiguration(testClass,
            StringUtils.toStringArray(locations), ClassUtils.toClassArray(classes),
            ApplicationContextInitializerUtils.resolveInitializerClasses(configAttributesList),
            ActiveProfilesUtils.resolveActiveProfiles(testClass), mergedTestPropertySources.getLocations(),
            mergedTestPropertySources.getProperties(), contextCustomizers, contextLoader,
            cacheAwareContextLoaderDelegate, parentConfig);

    return processMergedContextConfiguration(mergedConfig);
}

From source file:org.springframework.test.context.support.ApplicationContextInitializerUtils.java

/**
 * Resolve the set of merged {@code ApplicationContextInitializer} classes for the
 * supplied list of {@code ContextConfigurationAttributes}.
 *
 * <p>Note that the {@link ContextConfiguration#inheritInitializers inheritInitializers}
 * flag of {@link ContextConfiguration @ContextConfiguration} will be taken into
 * consideration. Specifically, if the {@code inheritInitializers} flag is set to
 * {@code true} for a given level in the class hierarchy represented by the provided
 * configuration attributes, context initializer classes defined at the given level
 * will be merged with those defined in higher levels of the class hierarchy.
 *
 * @param configAttributesList the list of configuration attributes to process; must
 * not be {@code null} or <em>empty</em>; must be ordered <em>bottom-up</em>
 * (i.e., as if we were traversing up the class hierarchy)
 * @return the set of merged context initializer classes, including those from
 * superclasses if appropriate (never {@code null})
 * @since 3.2/* w w w . j  a  v a2 s.  c  o m*/
 */
static Set<Class<? extends ApplicationContextInitializer<?>>> resolveInitializerClasses(
        List<ContextConfigurationAttributes> configAttributesList) {
    Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be empty");

    final Set<Class<? extends ApplicationContextInitializer<?>>> initializerClasses = //
            new HashSet<>();

    for (ContextConfigurationAttributes configAttributes : configAttributesList) {
        if (logger.isTraceEnabled()) {
            logger.trace(
                    String.format("Processing context initializers for context configuration attributes %s",
                            configAttributes));
        }

        initializerClasses.addAll(Arrays.asList(configAttributes.getInitializers()));

        if (!configAttributes.isInheritInitializers()) {
            break;
        }
    }

    return initializerClasses;
}

From source file:org.springframework.web.client.HttpMessageConverterExtractor.java

@SuppressWarnings("unchecked")
HttpMessageConverterExtractor(Type responseType, List<HttpMessageConverter<?>> messageConverters, Log logger) {
    Assert.notNull(responseType, "'responseType' must not be null");
    Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
    this.responseType = responseType;
    this.responseClass = (responseType instanceof Class ? (Class<T>) responseType : null);
    this.messageConverters = messageConverters;
    this.logger = logger;
}

From source file:org.springframework.web.client.RestTemplate.java

/**
 * Create a new instance of the {@link RestTemplate} using the given list of
 * {@link HttpMessageConverter} to use/*from  w w w.  j ava 2 s.  co m*/
 * @param messageConverters the list of {@link HttpMessageConverter} to use
 * @since 2.0
 */
public RestTemplate(List<HttpMessageConverter<?>> messageConverters) {
    Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
    this.messageConverters.addAll(messageConverters);
}

From source file:org.springframework.web.client.RestTemplate.java

/**
 * Set the message body converters to use.
 * <p>These converters are used to convert from and to HTTP requests and responses.
 *///from  www. j av  a  2  s.c  o m
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
    Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
    // Take getMessageConverters() List as-is when passed in here
    if (this.messageConverters != messageConverters) {
        this.messageConverters.clear();
        this.messageConverters.addAll(messageConverters);
    }
}

From source file:org.springframework.web.reactive.function.server.RequestPredicates.java

/**
 * Return a {@code RequestPredicate} that tests if the request's
 * {@linkplain ServerRequest.Headers#contentType() content type} is
 * {@linkplain MediaType#includes(MediaType) included} by any of the given media types.
 * @param mediaTypes the media types to match the request's content type against
 * @return a predicate that tests the request's content type against the given media types
 *//*from  ww w  . java 2s  . co  m*/
public static RequestPredicate contentType(MediaType... mediaTypes) {
    Assert.notEmpty(mediaTypes, "'mediaTypes' must not be empty");
    Set<MediaType> mediaTypeSet = new HashSet<>(Arrays.asList(mediaTypes));

    return headers(new Predicate<ServerRequest.Headers>() {
        @Override
        public boolean test(ServerRequest.Headers headers) {
            MediaType contentType = headers.contentType().orElse(MediaType.APPLICATION_OCTET_STREAM);
            boolean match = mediaTypeSet.stream().anyMatch(mediaType -> mediaType.includes(contentType));
            traceMatch("Content-Type", mediaTypeSet, contentType, match);
            return match;
        }

        @Override
        public String toString() {
            return String.format("Content-Type: %s", mediaTypeSet);
        }
    });
}

From source file:org.springframework.web.reactive.function.server.RequestPredicates.java

/**
 * Return a {@code RequestPredicate} that tests if the request's
 * {@linkplain ServerRequest.Headers#accept() accept} header is
 * {@linkplain MediaType#isCompatibleWith(MediaType) compatible} with any of the given media types.
 * @param mediaTypes the media types to match the request's accept header against
 * @return a predicate that tests the request's accept header against the given media types
 *///from   w ww .  j  ava2  s . c o  m
public static RequestPredicate accept(MediaType... mediaTypes) {
    Assert.notEmpty(mediaTypes, "'mediaTypes' must not be empty");
    Set<MediaType> mediaTypeSet = new HashSet<>(Arrays.asList(mediaTypes));

    return headers(new Predicate<ServerRequest.Headers>() {
        @Override
        public boolean test(ServerRequest.Headers headers) {
            List<MediaType> acceptedMediaTypes = headers.accept();
            if (acceptedMediaTypes.isEmpty()) {
                acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
            } else {
                MediaType.sortBySpecificityAndQuality(acceptedMediaTypes);
            }
            boolean match = acceptedMediaTypes.stream().anyMatch(
                    acceptedMediaType -> mediaTypeSet.stream().anyMatch(acceptedMediaType::isCompatibleWith));
            traceMatch("Accept", mediaTypeSet, acceptedMediaTypes, match);
            return match;
        }

        @Override
        public String toString() {
            return String.format("Accept: %s", mediaTypeSet);
        }
    });
}