Example usage for org.springframework.util ObjectUtils nullSafeToString

List of usage examples for org.springframework.util ObjectUtils nullSafeToString

Introduction

In this page you can find the example usage for org.springframework.util ObjectUtils nullSafeToString.

Prototype

public static String nullSafeToString(@Nullable short[] array) 

Source Link

Document

Return a String representation of the contents of the specified array.

Usage

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

/**
 * Register classes in the supplied {@link GenericWebApplicationContext context}
 * from the classes in the supplied {@link WebMergedContextConfiguration}.
 *
 * <p>Each class must represent an <em>annotated class</em>. An
 * {@link AnnotatedBeanDefinitionReader} is used to register the appropriate
 * bean definitions.//w  w  w .  j a  va  2  s .c o  m
 *
 * @param context the context in which the annotated classes should be registered
 * @param webMergedConfig the merged configuration from which the classes should be retrieved
 *
 * @see AbstractGenericWebContextLoader#loadBeanDefinitions
 */
@Override
protected void loadBeanDefinitions(GenericWebApplicationContext context,
        WebMergedContextConfiguration webMergedConfig) {
    Class<?>[] annotatedClasses = webMergedConfig.getClasses();
    if (logger.isDebugEnabled()) {
        logger.debug("Registering annotated classes: " + ObjectUtils.nullSafeToString(annotatedClasses));
    }
    new AnnotatedBeanDefinitionReader(context).register(annotatedClasses);
}

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

@Test
public void assertContextConfigurationLocations() throws Exception {

    final ContextConfiguration contextConfig = this.testClass.getAnnotation(ContextConfiguration.class);
    final ContextLoader contextLoader = new GenericXmlContextLoader();
    final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
    final String[] processedLocations = contextLoader.processLocations(this.testClass, configuredLocations);

    if (logger.isDebugEnabled()) {
        logger.debug("----------------------------------------------------------------------");
        logger.debug("Configured locations: " + ObjectUtils.nullSafeToString(configuredLocations));
        logger.debug("Expected   locations: " + ObjectUtils.nullSafeToString(this.expectedLocations));
        logger.debug("Processed  locations: " + ObjectUtils.nullSafeToString(processedLocations));
    }/* w w w  .  j a v a2 s .  c o m*/

    assertArrayEquals("Verifying locations for test [" + this.testClass + "].", this.expectedLocations,
            processedLocations);
}

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

/**
 * Provide a String representation of the {@code @TestPropertySource}
 * attributes and declaring class.// w  ww  .j  a v  a 2 s. com
 */
@Override
public String toString() {
    return new ToStringCreator(this)//
            .append("declaringClass", declaringClass.getName())//
            .append("locations", ObjectUtils.nullSafeToString(locations))//
            .append("inheritLocations", inheritLocations)//
            .append("properties", ObjectUtils.nullSafeToString(properties))//
            .append("inheritProperties", inheritProperties)//
            .toString();
}

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

/**
 * Add the given <em>inlined properties</em> (in the form of <em>key-value</em>
 * pairs) to the supplied {@link ConfigurableEnvironment environment}.
 * <p>All key-value pairs will be added to the {@code Environment} as a
 * single {@link MapPropertySource} with the highest precedence.
 * <p>For details on the parsing of <em>inlined properties</em>, consult the
 * Javadoc for {@link #convertInlinedPropertiesToMap}.
 * @param environment the environment to update; never {@code null}
 * @param inlinedProperties the inlined properties to add to the environment;
 * potentially empty but never {@code null}
 * @since 4.1.5/*from www. ja  v  a2  s.  c  o m*/
 * @see MapPropertySource
 * @see #INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
 * @see TestPropertySource#properties
 * @see #convertInlinedPropertiesToMap
 */
public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment environment,
        String... inlinedProperties) {
    Assert.notNull(environment, "'environment' must not be null");
    Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null");
    if (!ObjectUtils.isEmpty(inlinedProperties)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Adding inlined properties to environment: "
                    + ObjectUtils.nullSafeToString(inlinedProperties));
        }
        MapPropertySource ps = (MapPropertySource) environment.getPropertySources()
                .get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
        if (ps == null) {
            ps = new MapPropertySource(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME, new LinkedHashMap<>());
            environment.getPropertySources().addFirst(ps);
        }
        ps.getSource().putAll(convertInlinedPropertiesToMap(inlinedProperties));
    }
}

From source file:org.springframework.test.context.web.AnnotationConfigWebContextLoader.java

/**
 * Register classes in the supplied {@linkplain GenericWebApplicationContext context}
 * from the classes in the supplied {@link WebMergedContextConfiguration}.
 * <p>Each class must represent an <em>annotated class</em>. An
 * {@link AnnotatedBeanDefinitionReader} is used to register the appropriate
 * bean definitions./*from www .java 2 s  . c  o  m*/
 * @param context the context in which the annotated classes should be registered
 * @param webMergedConfig the merged configuration from which the classes should be retrieved
 * @see AbstractGenericWebContextLoader#loadBeanDefinitions
 */
@Override
protected void loadBeanDefinitions(GenericWebApplicationContext context,
        WebMergedContextConfiguration webMergedConfig) {

    Class<?>[] annotatedClasses = webMergedConfig.getClasses();
    if (logger.isDebugEnabled()) {
        logger.debug("Registering annotated classes: " + ObjectUtils.nullSafeToString(annotatedClasses));
    }
    new AnnotatedBeanDefinitionReader(context).register(annotatedClasses);
}

From source file:org.springframework.test.context.web.AnnotationConfigWebContextLoader.java

/**
 * Ensure that the supplied {@link WebMergedContextConfiguration} does not
 * contain {@link MergedContextConfiguration#getLocations() locations}.
 * @since 4.0.4/*from   w  ww  .  j a  va 2  s  .  com*/
 * @see AbstractGenericWebContextLoader#validateMergedContextConfiguration
 */
@Override
protected void validateMergedContextConfiguration(WebMergedContextConfiguration webMergedConfig) {
    if (webMergedConfig.hasLocations()) {
        String msg = String.format(
                "Test class [%s] has been configured with @ContextConfiguration's 'locations' "
                        + "(or 'value') attribute %s, but %s does not support resource locations.",
                webMergedConfig.getTestClass().getName(),
                ObjectUtils.nullSafeToString(webMergedConfig.getLocations()), getClass().getSimpleName());
        logger.error(msg);
        throw new IllegalStateException(msg);
    }
}

From source file:org.springframework.test.util.ReflectionTestUtils.java

/**
 * Invoke the method with the given {@code name} on the supplied target
 * object with the supplied arguments./*from w w  w . ja  va 2s  .  co m*/
 * <p>This method traverses the class hierarchy in search of the desired
 * method. In addition, an attempt will be made to make non-{@code public}
 * methods <em>accessible</em>, thus allowing one to invoke {@code protected},
 * {@code private}, and <em>package-private</em> methods.
 * @param target the target object on which to invoke the specified method
 * @param name the name of the method to invoke
 * @param args the arguments to provide to the method
 * @return the invocation result, if any
 * @see MethodInvoker
 * @see ReflectionUtils#makeAccessible(Method)
 * @see ReflectionUtils#invokeMethod(Method, Object, Object[])
 * @see ReflectionUtils#handleReflectionException(Exception)
 */
@SuppressWarnings("unchecked")
@Nullable
public static <T> T invokeMethod(Object target, String name, Object... args) {
    Assert.notNull(target, "Target object must not be null");
    Assert.hasText(name, "Method name must not be empty");

    try {
        MethodInvoker methodInvoker = new MethodInvoker();
        methodInvoker.setTargetObject(target);
        methodInvoker.setTargetMethod(name);
        methodInvoker.setArguments(args);
        methodInvoker.prepare();

        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Invoking method '%s' on %s with arguments %s", name,
                    safeToString(target), ObjectUtils.nullSafeToString(args)));
        }

        return (T) methodInvoker.invoke();
    } catch (Exception ex) {
        ReflectionUtils.handleReflectionException(ex);
        throw new IllegalStateException("Should never get here");
    }
}