Search in sources :

Example 26 with DirectFieldAccessor

use of cn.taketoday.beans.DirectFieldAccessor in project today-framework by TAKETODAY.

the class ScriptTemplateViewTests method detectScriptTemplateConfigWithEngine.

@Test
public void detectScriptTemplateConfigWithEngine() {
    InvocableScriptEngine engine = mock(InvocableScriptEngine.class);
    this.configurer.setEngine(engine);
    this.configurer.setRenderObject("Template");
    this.configurer.setRenderFunction("render");
    this.configurer.setContentType(MediaType.TEXT_PLAIN_VALUE);
    this.configurer.setCharset(StandardCharsets.ISO_8859_1);
    this.configurer.setSharedEngine(true);
    DirectFieldAccessor accessor = new DirectFieldAccessor(view);
    this.view.setApplicationContext(this.wac);
    assertThat(accessor.getPropertyValue("engine")).isEqualTo(engine);
    assertThat(accessor.getPropertyValue("renderObject")).isEqualTo("Template");
    assertThat(accessor.getPropertyValue("renderFunction")).isEqualTo("render");
    assertThat(accessor.getPropertyValue("contentType")).isEqualTo(MediaType.TEXT_PLAIN_VALUE);
    assertThat(accessor.getPropertyValue("charset")).isEqualTo(StandardCharsets.ISO_8859_1);
    assertThat(accessor.getPropertyValue("sharedEngine")).asInstanceOf(BOOLEAN).isTrue();
}
Also used : DirectFieldAccessor(cn.taketoday.beans.DirectFieldAccessor) Test(org.junit.jupiter.api.Test)

Example 27 with DirectFieldAccessor

use of cn.taketoday.beans.DirectFieldAccessor in project today-framework by TAKETODAY.

the class ScriptTemplateViewTests method detectScriptTemplateConfigWithEngineName.

@Test
public void detectScriptTemplateConfigWithEngineName() {
    this.configurer.setEngineName("nashorn");
    this.configurer.setRenderObject("Template");
    this.configurer.setRenderFunction("render");
    DirectFieldAccessor accessor = new DirectFieldAccessor(view);
    this.view.setApplicationContext(this.wac);
    assertThat(accessor.getPropertyValue("engineName")).isEqualTo("nashorn");
    assertThat(accessor.getPropertyValue("engine")).isNotNull();
    assertThat(accessor.getPropertyValue("renderObject")).isEqualTo("Template");
    assertThat(accessor.getPropertyValue("renderFunction")).isEqualTo("render");
    assertThat(accessor.getPropertyValue("contentType")).isEqualTo(MediaType.TEXT_HTML_VALUE);
    assertThat(accessor.getPropertyValue("charset")).isEqualTo(StandardCharsets.UTF_8);
}
Also used : DirectFieldAccessor(cn.taketoday.beans.DirectFieldAccessor) Test(org.junit.jupiter.api.Test)

Example 28 with DirectFieldAccessor

use of cn.taketoday.beans.DirectFieldAccessor in project today-framework by TAKETODAY.

the class ScriptTemplateViewTests method customEngineAndRenderFunction.

@Test
public void customEngineAndRenderFunction() {
    ScriptEngine engine = mock(InvocableScriptEngine.class);
    given(engine.get("key")).willReturn("value");
    this.view.setEngine(engine);
    this.view.setRenderFunction("render");
    this.view.setApplicationContext(this.wac);
    engine = this.view.getEngine();
    assertThat(engine).isNotNull();
    assertThat(engine.get("key")).isEqualTo("value");
    DirectFieldAccessor accessor = new DirectFieldAccessor(view);
    assertThat(accessor.getPropertyValue("renderObject")).isNull();
    assertThat(accessor.getPropertyValue("renderFunction")).isEqualTo("render");
    assertThat(accessor.getPropertyValue("charset")).isEqualTo(StandardCharsets.UTF_8);
}
Also used : DirectFieldAccessor(cn.taketoday.beans.DirectFieldAccessor) ScriptEngine(javax.script.ScriptEngine) Test(org.junit.jupiter.api.Test)

Example 29 with DirectFieldAccessor

use of cn.taketoday.beans.DirectFieldAccessor in project today-framework by TAKETODAY.

the class EnableRetryTests method testExpression.

@Test
public void testExpression() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
    ExpressionService service = context.getBean(ExpressionService.class);
    service.service1();
    assertEquals(3, service.getCount());
    try {
        service.service2();
        fail("expected exception");
    } catch (RuntimeException e) {
        assertEquals("this cannot be retried", e.getMessage());
    }
    assertEquals(4, service.getCount());
    service.service3();
    assertEquals(9, service.getCount());
    RetryConfiguration config = context.getBean(RetryConfiguration.class);
    AnnotationAwareRetryOperationsInterceptor advice = (AnnotationAwareRetryOperationsInterceptor) new DirectFieldAccessor(config).getPropertyValue("advice");
    @SuppressWarnings("unchecked") Map<Object, Map<Method, MethodInterceptor>> delegates = (Map<Object, Map<Method, MethodInterceptor>>) new DirectFieldAccessor(advice).getPropertyValue("delegates");
    MethodInterceptor interceptor = delegates.get(target(service)).get(ExpressionService.class.getDeclaredMethod("service3"));
    RetryTemplate template = (RetryTemplate) new DirectFieldAccessor(interceptor).getPropertyValue("retryOperations");
    DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template);
    ExponentialBackOffPolicy backOff = (ExponentialBackOffPolicy) templateAccessor.getPropertyValue("backOffPolicy");
    assertEquals(1, backOff.getInitialInterval());
    assertEquals(5, backOff.getMaxInterval());
    assertEquals(1.1, backOff.getMultiplier(), 0.1);
    SimpleRetryPolicy retryPolicy = (SimpleRetryPolicy) templateAccessor.getPropertyValue("retryPolicy");
    assertEquals(5, retryPolicy.getMaxAttempts());
    service.service4();
    assertEquals(11, service.getCount());
    service.service5();
    assertEquals(12, service.getCount());
    context.close();
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) ExponentialBackOffPolicy(cn.taketoday.retry.backoff.ExponentialBackOffPolicy) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Method(java.lang.reflect.Method) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) DirectFieldAccessor(cn.taketoday.beans.DirectFieldAccessor) Map(java.util.Map) Test(org.junit.Test)

Example 30 with DirectFieldAccessor

use of cn.taketoday.beans.DirectFieldAccessor in project today-framework by TAKETODAY.

the class CircuitBreakerTests method vanilla.

@Test
public void vanilla() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
    Service service = context.getBean(Service.class);
    assertTrue(AopUtils.isAopProxy(service));
    try {
        service.service();
        fail("Expected exception");
    } catch (Exception e) {
    }
    assertFalse((Boolean) service.getContext().getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN));
    try {
        service.service();
        fail("Expected exception");
    } catch (Exception e) {
    }
    assertFalse((Boolean) service.getContext().getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN));
    try {
        service.service();
        fail("Expected exception");
    } catch (Exception e) {
    }
    assertTrue((Boolean) service.getContext().getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN));
    assertEquals(3, service.getCount());
    try {
        service.service();
        fail("Expected exception");
    } catch (Exception e) {
    }
    // Not called again once circuit is open
    assertEquals(3, service.getCount());
    service.expressionService();
    assertEquals(4, service.getCount());
    Advised advised = (Advised) service;
    Advisor advisor = advised.getAdvisors()[0];
    Map<?, ?> delegates = (Map<?, ?>) new DirectFieldAccessor(advisor).getPropertyValue("advice.delegates");
    assertTrue(delegates.size() == 1);
    Map<?, ?> methodMap = (Map<?, ?>) delegates.values().iterator().next();
    MethodInterceptor interceptor = (MethodInterceptor) methodMap.get(Service.class.getDeclaredMethod("expressionService"));
    DirectFieldAccessor accessor = new DirectFieldAccessor(interceptor);
    assertEquals(8, accessor.getPropertyValue("retryOperations.retryPolicy.delegate.maxAttempts"));
    assertEquals(19000L, accessor.getPropertyValue("retryOperations.retryPolicy.openTimeout"));
    assertEquals(20000L, accessor.getPropertyValue("retryOperations.retryPolicy.resetTimeout"));
    assertEquals("#root instanceof RuntimeExpression", accessor.getPropertyValue("retryOperations.retryPolicy.delegate.expression.expression"));
    context.close();
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Advised(cn.taketoday.aop.framework.Advised) DirectFieldAccessor(cn.taketoday.beans.DirectFieldAccessor) Advisor(cn.taketoday.aop.Advisor) Map(java.util.Map) Test(org.junit.Test)

Aggregations

DirectFieldAccessor (cn.taketoday.beans.DirectFieldAccessor)46 Test (org.junit.jupiter.api.Test)34 ScriptEngine (javax.script.ScriptEngine)6 Test (org.junit.Test)6 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)4 ThreadPoolTaskScheduler (cn.taketoday.scheduling.concurrent.ThreadPoolTaskScheduler)4 TemplateEngine (groovy.text.TemplateEngine)4 MarkupTemplateEngine (groovy.text.markup.MarkupTemplateEngine)4 Method (java.lang.reflect.Method)4 Map (java.util.Map)4 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)4 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)3 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)3 Advisor (cn.taketoday.aop.Advisor)2 Advised (cn.taketoday.aop.framework.Advised)2 LifecycleProcessor (cn.taketoday.context.LifecycleProcessor)2 StaticApplicationContext (cn.taketoday.context.support.StaticApplicationContext)2 MediaType (cn.taketoday.http.MediaType)2 ExponentialBackOffPolicy (cn.taketoday.retry.backoff.ExponentialBackOffPolicy)2 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)2