diff --git a/pom.xml b/pom.xml index 6e446970fc..693f250ad8 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-redis - 2.2.0.BUILD-SNAPSHOT + 2.2.0.DATAREDIS-1030-SNAPSHOT Spring Data Redis diff --git a/src/main/java/org/springframework/data/redis/core/script/DefaultRedisScript.java b/src/main/java/org/springframework/data/redis/core/script/DefaultRedisScript.java index 43374983fe..c78a26a29e 100644 --- a/src/main/java/org/springframework/data/redis/core/script/DefaultRedisScript.java +++ b/src/main/java/org/springframework/data/redis/core/script/DefaultRedisScript.java @@ -75,7 +75,7 @@ public DefaultRedisScript(String script, @Nullable Class resultType) { * (non-Javadoc) * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ - public void afterPropertiesSet() throws Exception { + public void afterPropertiesSet() { Assert.state(this.scriptSource != null, "Either script, script location," + " or script source is required"); } diff --git a/src/main/java/org/springframework/data/redis/core/script/RedisScript.java b/src/main/java/org/springframework/data/redis/core/script/RedisScript.java index 0716fc100c..a4b53d9f68 100644 --- a/src/main/java/org/springframework/data/redis/core/script/RedisScript.java +++ b/src/main/java/org/springframework/data/redis/core/script/RedisScript.java @@ -15,22 +15,24 @@ */ package org.springframework.data.redis.core.script; +import org.springframework.core.io.Resource; import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** - * A script to be executed using the Redis scripting support available as of - * version 2.6 + * A script to be executed using the Redis scripting support available as + * of version 2.6 * * @author Jennifer Hickey * @author Christoph Strobl + * @author Mark Paluch * @param The script result type. Should be one of Long, Boolean, List, or deserialized value type. Can be - * {@litearl null} if the script returns a throw-away status (i.e "OK") + * {@literal null} if the script returns a throw-away status (i.e "OK") */ public interface RedisScript { /** - * @return The SHA1 of the script, used for executing Redis evalsha command. + * @return The SHA1 of the script, used for executing Redis {@code evalsha} command. */ String getSha1(); @@ -62,6 +64,9 @@ default boolean returnsRawValue() { * @since 2.0 */ static RedisScript of(String script) { + + Assert.notNull(script, "Script must not be null!"); + return new DefaultRedisScript<>(script); } @@ -73,11 +78,50 @@ static RedisScript of(String script) { * @return new instance of {@link RedisScript}. * @since 2.0 */ - static RedisScript of(String script, Class resultType) { + static RedisScript of(String script, Class resultType) { Assert.notNull(script, "Script must not be null!"); Assert.notNull(resultType, "ResultType must not be null!"); - return new DefaultRedisScript(script, resultType); + return new DefaultRedisScript<>(script, resultType); + } + + /** + * Creates new {@link RedisScript} from {@link Resource}. + * + * @param resource must not be {@literal null}. + * @return new instance of {@link RedisScript}. + * @since 2.2 + */ + static RedisScript of(Resource resource) { + + Assert.notNull(resource, "Resource must not be null!"); + + DefaultRedisScript script = new DefaultRedisScript<>(); + script.setLocation(resource); + script.afterPropertiesSet(); + + return script; + } + + /** + * Creates new {@link RedisScript} from {@link Resource}. + * + * @param resource must not be {@literal null}. + * @param resultType must not be {@literal null}. + * @return new instance of {@link RedisScript}. + * @since 2.2 + */ + static RedisScript of(Resource resource, Class resultType) { + + Assert.notNull(resource, "Resource must not be null!"); + Assert.notNull(resultType, "ResultType must not be null!"); + + DefaultRedisScript script = new DefaultRedisScript<>(); + script.setResultType(resultType); + script.setLocation(resource); + script.afterPropertiesSet(); + + return script; } } diff --git a/src/test/java/org/springframework/data/redis/core/script/DefaultRedisScriptTests.java b/src/test/java/org/springframework/data/redis/core/script/DefaultRedisScriptTests.java index 6db812600c..72730c267b 100644 --- a/src/test/java/org/springframework/data/redis/core/script/DefaultRedisScriptTests.java +++ b/src/test/java/org/springframework/data/redis/core/script/DefaultRedisScriptTests.java @@ -28,6 +28,7 @@ * * @author Jennifer Hickey * @author Christoph Strobl + * @author Mark Paluch */ public class DefaultRedisScriptTests { @@ -55,6 +56,14 @@ public void testGetScriptAsString() { assertThat(redisScript.getScriptAsString()).isEqualTo("return ARGS[1]"); } + @Test // DATAREDIS-1030 + public void testGetScriptAsStringFromResource() { + + RedisScript redisScript = RedisScript + .of(new ClassPathResource("org/springframework/data/redis/core/script/cas.lua")); + assertThat(redisScript.getScriptAsString()).startsWith("local current = redis.call('GET', KEYS[1])"); + } + @Test(expected = ScriptingException.class) public void testGetScriptAsStringError() {