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();
}
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);
}
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);
}
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();
}
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();
}
Aggregations