Skip to content

Commit 1c74527

Browse files
committed
Polishing.
Add missing property editors to inject requested operations. See #2828
1 parent 9bdbe12 commit 1c74527

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.core;
17+
18+
import java.beans.PropertyEditorSupport;
19+
20+
/**
21+
* PropertyEditor allowing for easy injection of {@link ClusterOperations} from {@link RedisOperations}.
22+
*
23+
* @author Mark Paluch
24+
*/
25+
class ClusterOperationsEditor extends PropertyEditorSupport {
26+
27+
public void setValue(Object value) {
28+
29+
if (value instanceof RedisOperations<?, ?> redisOperations) {
30+
super.setValue(redisOperations.opsForCluster());
31+
} else {
32+
throw new IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class);
33+
}
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.core;
17+
18+
import java.beans.PropertyEditorSupport;
19+
20+
/**
21+
* PropertyEditor allowing for easy injection of {@link HyperLogLogOperations} from {@link RedisOperations}.
22+
*
23+
* @author Mark Paluch
24+
*/
25+
class HyperLogLogOperationsEditor extends PropertyEditorSupport {
26+
27+
public void setValue(Object value) {
28+
29+
if (value instanceof RedisOperations<?, ?> redisOperations) {
30+
super.setValue(redisOperations.opsForHyperLogLog());
31+
} else {
32+
throw new IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class);
33+
}
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.core;
17+
18+
import java.beans.PropertyEditorSupport;
19+
20+
/**
21+
* PropertyEditor allowing for easy injection of {@link StreamOperations} from {@link RedisOperations}.
22+
*
23+
* @author Mark Paluch
24+
*/
25+
class StreamOperationsEditor extends PropertyEditorSupport {
26+
27+
public void setValue(Object value) {
28+
29+
if (value instanceof RedisOperations<?, ?> redisOperations) {
30+
super.setValue(redisOperations.opsForStream());
31+
} else {
32+
throw new IllegalArgumentException("Editor supports only conversion of type " + RedisOperations.class);
33+
}
34+
}
35+
}

src/test/java/org/springframework/data/redis/config/NamespaceIntegrationTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import jakarta.annotation.Resource;
21+
2022
import org.junit.jupiter.api.Test;
2123

2224
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.data.redis.core.HashOperations;
26+
import org.springframework.data.redis.core.StreamOperations;
2327
import org.springframework.data.redis.core.StringRedisTemplate;
28+
import org.springframework.data.redis.core.ValueOperations;
2429
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
2530
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2631

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.config;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import jakarta.annotation.Resource;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.data.redis.core.ClusterOperations;
25+
import org.springframework.data.redis.core.GeoOperations;
26+
import org.springframework.data.redis.core.HashOperations;
27+
import org.springframework.data.redis.core.HyperLogLogOperations;
28+
import org.springframework.data.redis.core.SetOperations;
29+
import org.springframework.data.redis.core.StreamOperations;
30+
import org.springframework.data.redis.core.ValueOperations;
31+
import org.springframework.data.redis.core.ZSetOperations;
32+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
33+
34+
/**
35+
* Integration tests to obtain various template resources through {@code @Resource} injection.
36+
*
37+
* @author Mark Paluch
38+
*/
39+
@SpringJUnitConfig(locations = "namespace.xml")
40+
class PropertyEditorSupportIntegrationTests {
41+
42+
@Resource(name = "redisTemplate") ClusterOperations<String, String> cluster;
43+
@Resource(name = "redisTemplate") GeoOperations<String, String> geo;
44+
@Resource(name = "redisTemplate") HashOperations<String, String, String> hash;
45+
@Resource(name = "redisTemplate") HyperLogLogOperations<String, String> hll;
46+
@Resource(name = "redisTemplate") SetOperations<String, String> set;
47+
@Resource(name = "redisTemplate") StreamOperations<String, String, String> stream;
48+
@Resource(name = "redisTemplate") ValueOperations<String, String> value;
49+
@Resource(name = "redisTemplate") ZSetOperations<String, String> zSet;
50+
51+
@Test // GH-2828, GH-2825
52+
void shouldInjectResources() {
53+
54+
assertThat(cluster).isNotNull();
55+
assertThat(geo).isNotNull();
56+
assertThat(hash).isNotNull();
57+
assertThat(hll).isNotNull();
58+
assertThat(set).isNotNull();
59+
assertThat(stream).isNotNull();
60+
assertThat(value).isNotNull();
61+
assertThat(zSet).isNotNull();
62+
}
63+
}

0 commit comments

Comments
 (0)