Example usage for org.springframework.beans.factory.config ConfigurableBeanFactory resolveEmbeddedValue

List of usage examples for org.springframework.beans.factory.config ConfigurableBeanFactory resolveEmbeddedValue

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config ConfigurableBeanFactory resolveEmbeddedValue.

Prototype

@Nullable
String resolveEmbeddedValue(String value);

Source Link

Document

Resolve the given embedded value, e.g.

Usage

From source file:org.springframework.test.context.junit.jupiter.AbstractExpressionEvaluatingCondition.java

private <A extends Annotation> boolean evaluateExpression(String expression, boolean loadContext,
        Class<A> annotationType, ExtensionContext context) {

    Assert.state(context.getElement().isPresent(), "No AnnotatedElement");
    AnnotatedElement element = context.getElement().get();
    GenericApplicationContext gac = null;
    ApplicationContext applicationContext;

    if (loadContext) {
        applicationContext = SpringExtension.getApplicationContext(context);
    } else {/*  w ww  . java  2s. c  om*/
        gac = new GenericApplicationContext();
        gac.refresh();
        applicationContext = gac;
    }

    if (!(applicationContext instanceof ConfigurableApplicationContext)) {
        if (logger.isWarnEnabled()) {
            String contextType = applicationContext.getClass().getName();
            logger.warn(String.format(
                    "@%s(\"%s\") could not be evaluated on [%s] since the test "
                            + "ApplicationContext [%s] is not a ConfigurableApplicationContext",
                    annotationType.getSimpleName(), expression, element, contextType));
        }
        return false;
    }

    ConfigurableBeanFactory configurableBeanFactory = ((ConfigurableApplicationContext) applicationContext)
            .getBeanFactory();
    BeanExpressionResolver expressionResolver = configurableBeanFactory.getBeanExpressionResolver();
    Assert.state(expressionResolver != null, "No BeanExpressionResolver");
    BeanExpressionContext beanExpressionContext = new BeanExpressionContext(configurableBeanFactory, null);

    Object result = expressionResolver.evaluate(configurableBeanFactory.resolveEmbeddedValue(expression),
            beanExpressionContext);

    if (gac != null) {
        gac.close();
    }

    if (result instanceof Boolean) {
        return (Boolean) result;
    } else if (result instanceof String) {
        String str = ((String) result).trim().toLowerCase();
        if ("true".equals(str)) {
            return true;
        }
        Assert.state("false".equals(str),
                () -> String.format("@%s(\"%s\") on %s must evaluate to \"true\" or \"false\", not \"%s\"",
                        annotationType.getSimpleName(), expression, element, result));
        return false;
    } else {
        String message = String.format("@%s(\"%s\") on %s must evaluate to a String or a Boolean, not %s",
                annotationType.getSimpleName(), expression, element,
                (result != null ? result.getClass().getName() : "null"));
        throw new IllegalStateException(message);
    }
}

From source file:org.springframework.test.context.junit.jupiter.DisabledIfCondition.java

private boolean isDisabled(String expression, ExtensionContext extensionContext) {
    ApplicationContext applicationContext = SpringExtension.getApplicationContext(extensionContext);

    if (!(applicationContext instanceof ConfigurableApplicationContext)) {
        if (logger.isWarnEnabled()) {
            String contextType = (applicationContext != null ? applicationContext.getClass().getName()
                    : "null");
            logger.warn(String.format(
                    "@DisabledIf(\"%s\") could not be evaluated on [%s] since the test "
                            + "ApplicationContext [%s] is not a ConfigurableApplicationContext",
                    expression, extensionContext.getElement(), contextType));
        }/*from  w  w w . j  a v  a  2 s  . c  om*/
        return false;
    }

    ConfigurableBeanFactory configurableBeanFactory = ((ConfigurableApplicationContext) applicationContext)
            .getBeanFactory();
    BeanExpressionResolver expressionResolver = configurableBeanFactory.getBeanExpressionResolver();
    BeanExpressionContext beanExpressionContext = new BeanExpressionContext(configurableBeanFactory, null);

    Object result = expressionResolver.evaluate(configurableBeanFactory.resolveEmbeddedValue(expression),
            beanExpressionContext);

    Assert.state((result instanceof Boolean || result instanceof String),
            () -> String.format("@DisabledIf(\"%s\") must evaluate to a String or a Boolean, not %s",
                    expression, (result != null ? result.getClass().getName() : "null")));

    boolean disabled = (result instanceof Boolean && ((Boolean) result).booleanValue())
            || (result instanceof String && Boolean.parseBoolean((String) result));

    return disabled;
}