private Expression parseTemplate(String expressionString, ParserContext context) throws ParseException { if (expressionString.isEmpty()) { return new LiteralExpression(""); } Expression[] expressions = parseExpressions(expressionString, context); if (expressions.length == 1) { return expressions[0]; } else { return new CompositeStringExpression(expressionString, expressions); } }
@Test public void testGetValue() throws Exception { LiteralExpression lEx = new LiteralExpression("somevalue"); checkString("somevalue", lEx.getValue()); checkString("somevalue", lEx.getValue(String.class)); EvaluationContext ctx = new StandardEvaluationContext(); checkString("somevalue", lEx.getValue(ctx)); checkString("somevalue", lEx.getValue(ctx, String.class)); checkString("somevalue", lEx.getValue(new Rooty())); checkString("somevalue", lEx.getValue(new Rooty(), String.class)); checkString("somevalue", lEx.getValue(ctx, new Rooty())); checkString("somevalue", lEx.getValue(ctx, new Rooty(),String.class)); assertEquals("somevalue", lEx.getExpressionString()); assertFalse(lEx.isWritable(new StandardEvaluationContext())); assertFalse(lEx.isWritable(new Rooty())); assertFalse(lEx.isWritable(new StandardEvaluationContext(), new Rooty())); }
@Test public void testGetValueType() throws Exception { LiteralExpression lEx = new LiteralExpression("somevalue"); assertEquals(String.class, lEx.getValueType()); assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext())); assertEquals(String.class, lEx.getValueType(new Rooty())); assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext(), new Rooty())); assertEquals(String.class, lEx.getValueTypeDescriptor().getType()); assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext()).getType()); assertEquals(String.class, lEx.getValueTypeDescriptor(new Rooty()).getType()); assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext(), new Rooty()).getType()); }
@Test public void testSetValue() { try { LiteralExpression lEx = new LiteralExpression("somevalue"); lEx.setValue(new StandardEvaluationContext(), "flibble"); fail("Should have got an exception that the value cannot be set"); } catch (EvaluationException ee) { // success, not allowed - whilst here, check the expression value in the exception assertEquals("somevalue", ee.getExpressionString()); } try { LiteralExpression lEx = new LiteralExpression("somevalue"); lEx.setValue(new Rooty(), "flibble"); fail("Should have got an exception that the value cannot be set"); } catch (EvaluationException ee) { // success, not allowed - whilst here, check the expression value in the exception assertEquals("somevalue", ee.getExpressionString()); } try { LiteralExpression lEx = new LiteralExpression("somevalue"); lEx.setValue(new StandardEvaluationContext(), new Rooty(), "flibble"); fail("Should have got an exception that the value cannot be set"); } catch (EvaluationException ee) { // success, not allowed - whilst here, check the expression value in the exception assertEquals("somevalue", ee.getExpressionString()); } }
@Test @MongoDbAvailable public void testWithCollectionNameExpressionSpecified() { Message<String> message = MessageBuilder.withPayload("").build(); MongoDbOutboundGateway gateway = createGateway(); gateway.setQueryExpression(new LiteralExpression("{name : 'Xavi'}")); gateway.setExpectSingleResult(true); gateway.setCollectionNameExpression(new LiteralExpression("anotherCollection")); gateway.afterPropertiesSet(); Object result = gateway.handleRequestMessage(message); assertNull(result); LiteralExpression collectionNameExpression = (LiteralExpression) TestUtils.getPropertyValue(gateway, "collectionNameExpression"); assertNotNull(collectionNameExpression); assertEquals("anotherCollection", collectionNameExpression.getValue()); }
@Override @Nullable public <T> T getValue(EvaluationContext context, @Nullable Class<T> expectedResultType) throws EvaluationException { Object value = getValue(context); return ExpressionUtils.convertTypedValue(context, new TypedValue(value), expectedResultType); }
@Test public void validateWithExternalTemplate() { RedisStoreWritingMessageHandler withExternalTemplate = TestUtils.getPropertyValue(context.getBean("withExternalTemplate.adapter"), "handler", RedisStoreWritingMessageHandler.class); assertEquals("pepboys", ((LiteralExpression) TestUtils.getPropertyValue(withExternalTemplate, "keyExpression")).getExpressionString()); assertEquals("PROPERTIES", (TestUtils.getPropertyValue(withExternalTemplate, "collectionType")).toString()); assertSame(redisTemplate, TestUtils.getPropertyValue(withExternalTemplate, "redisTemplate")); }
@Override @Nullable public <T> T getValue(EvaluationContext context, Object rootObject, @Nullable Class<T> desiredResultType) throws EvaluationException { Object value = getValue(context, rootObject); return ExpressionUtils.convertTypedValue(context, new TypedValue(value), desiredResultType); }
@Test @RedisAvailable public void testMapToMapWithProvidedKey() { RedisMap<String, String> redisMap = new DefaultRedisMap<String, String>("pepboys", this.redisTemplate); assertEquals(0, redisMap.size()); Map<String, String> pepboys = new HashMap<String, String>(); pepboys.put("1", "Manny"); pepboys.put("2", "Moe"); pepboys.put("3", "Jack"); Message<Map<String, String>> message = MessageBuilder.withPayload(pepboys).build(); this.mapToMapAChannel.send(message); assertEquals("Manny", redisMap.get("1")); assertEquals("Moe", redisMap.get("2")); assertEquals("Jack", redisMap.get("3")); RedisStoreWritingMessageHandler handler = this.beanFactory.getBean("mapToMapA.handler", RedisStoreWritingMessageHandler.class); assertEquals("pepboys", TestUtils.getPropertyValue(handler, "keyExpression", LiteralExpression.class).getExpressionString()); assertEquals("'foo'", TestUtils.getPropertyValue(handler, "mapKeyExpression", SpelExpression.class).getExpressionString()); }
expressions.add(new LiteralExpression(expressionString.substring(startIdx, prefixIndex))); expressions.add(new LiteralExpression(expressionString.substring(startIdx))); startIdx = expressionString.length();
@Override @Nullable public <T> T getValue(@Nullable Class<T> expectedResultType) throws EvaluationException { Object value = getValue(); return ExpressionUtils.convertTypedValue(null, new TypedValue(value), expectedResultType); }
@Test public void validateWithStringTemplate() throws Exception { RedisStoreWritingMessageHandler withStringTemplate = context.getBean("withStringTemplate.handler", RedisStoreWritingMessageHandler.class); assertEquals("pepboys", ((LiteralExpression) TestUtils.getPropertyValue(withStringTemplate, "keyExpression")).getExpressionString()); assertEquals("PROPERTIES", (TestUtils.getPropertyValue(withStringTemplate, "collectionType")).toString()); assertTrue(TestUtils.getPropertyValue(withStringTemplate, "redisTemplate") instanceof StringRedisTemplate); Object handler = TestUtils.getPropertyValue(context.getBean("withStringTemplate.adapter"), "handler"); assertTrue(AopUtils.isAopProxy(handler)); assertSame(((Advised) handler).getTargetSource().getTarget(), withStringTemplate); assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors[0].advice"), Matchers.instanceOf(RequestHandlerRetryAdvice.class)); assertEquals("true", TestUtils.getPropertyValue(withStringTemplate, "zsetIncrementScoreExpression", Expression.class).getExpressionString()); }
private Expression parseTemplate(String expressionString, ParserContext context) throws ParseException { if (expressionString.isEmpty()) { return new LiteralExpression(""); } Expression[] expressions = parseExpressions(expressionString, context); if (expressions.length == 1) { return expressions[0]; } else { return new CompositeStringExpression(expressionString, expressions); } }
@Override @Nullable public <T> T getValue(Object rootObject, @Nullable Class<T> desiredResultType) throws EvaluationException { Object value = getValue(rootObject); return ExpressionUtils.convertTypedValue(null, new TypedValue(value), desiredResultType); }
@Test public void validateWithStringObjectTemplate() { RedisStoreWritingMessageHandler withStringObjectTemplate = TestUtils.getPropertyValue(context.getBean("withStringObjectTemplate.adapter"), "handler", RedisStoreWritingMessageHandler.class); assertEquals("pepboys", ((LiteralExpression) TestUtils.getPropertyValue(withStringObjectTemplate, "keyExpression")).getExpressionString()); assertEquals("PROPERTIES", (TestUtils.getPropertyValue(withStringObjectTemplate, "collectionType")).toString()); assertFalse(TestUtils.getPropertyValue(withStringObjectTemplate, "redisTemplate") instanceof StringRedisTemplate); assertTrue(TestUtils.getPropertyValue(withStringObjectTemplate, "redisTemplate.keySerializer") instanceof StringRedisSerializer); assertTrue(TestUtils.getPropertyValue(withStringObjectTemplate, "redisTemplate.hashKeySerializer") instanceof StringRedisSerializer); assertTrue(TestUtils.getPropertyValue(withStringObjectTemplate, "redisTemplate.valueSerializer") instanceof JdkSerializationRedisSerializer); assertTrue(TestUtils.getPropertyValue(withStringObjectTemplate, "redisTemplate.hashValueSerializer") instanceof JdkSerializationRedisSerializer); }
expressions.add(new LiteralExpression(expressionString.substring(startIdx, prefixIndex))); expressions.add(new LiteralExpression(expressionString.substring(startIdx))); startIdx = expressionString.length();
@Override @Nullable public <T> T getValue(Object rootObject, @Nullable Class<T> desiredResultType) throws EvaluationException { Object value = getValue(rootObject); return ExpressionUtils.convertTypedValue(null, new TypedValue(value), desiredResultType); }
/** * @param remoteTimeout the remoteTimeout to set */ public void setRemoteTimeout(long remoteTimeout) { this.remoteTimeoutExpression = new LiteralExpression("" + remoteTimeout); }
@Override @Nullable public <T> T getValue(EvaluationContext context, Object rootObject, @Nullable Class<T> desiredResultType) throws EvaluationException { Object value = getValue(context, rootObject); return ExpressionUtils.convertTypedValue(context, new TypedValue(value), desiredResultType); }
/** * Specify the full path to the remote directory. * @param remoteDirectory The remote directory. */ public void setRemoteDirectory(String remoteDirectory) { this.remoteDirectoryExpression = new LiteralExpression(remoteDirectory); }