diff --git a/spring-data-redis/pom.xml b/spring-data-redis/pom.xml index 275cf39cd..2f683aefe 100644 --- a/spring-data-redis/pom.xml +++ b/spring-data-redis/pom.xml @@ -115,6 +115,12 @@ 1.3 test + + + commons-beanutils + commons-beanutils-core + 1.8.3 + junit diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/mapper/HashMapper.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/mapper/HashMapper.java new file mode 100644 index 000000000..783ebb85c --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/mapper/HashMapper.java @@ -0,0 +1,30 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis.mapper; + +import java.util.Map; + +/** + * Core mapping contract between Java types and Redis hashes/maps. + * + * @author Costin Leau + */ +public interface HashMapper { + + Map toHash(T object); + + T fromHash(Map hash); +} diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/mapper/JacksonHashMapper.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/mapper/JacksonHashMapper.java new file mode 100644 index 000000000..fceb20e8a --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/mapper/JacksonHashMapper.java @@ -0,0 +1,50 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis.mapper; + +import java.util.Map; + +import org.codehaus.jackson.map.ObjectMapper; + +/** + * Mapper based on Jackson library. + * + * @author Costin Leau + */ +public class JacksonHashMapper implements HashMapper { + + private final Class type; + private final ObjectMapper mapper; + + public JacksonHashMapper(Class type) { + this(type, new ObjectMapper()); + } + + public JacksonHashMapper(Class type, ObjectMapper mapper) { + this.type = type; + this.mapper = mapper; + } + + @Override + public T fromHash(Map hash) { + return mapper.convertValue(hash, type); + } + + @Override + public Map toHash(T object) { + return mapper.convertValue(object, Map.class); + } +} diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/core/SortTest.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/core/SortTest.java new file mode 100644 index 000000000..df6fd0b95 --- /dev/null +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/core/SortTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis.core; + + +import org.junit.After; +import org.junit.Before; +import org.springframework.data.keyvalue.redis.core.query.SortQueryBuilder; + +public class SortTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + public void testBasicDSL() throws Exception { + SortQueryBuilder.sort("list").build(); + } + +} diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/mapping/AbstractHashMapperTest.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/mapping/AbstractHashMapperTest.java new file mode 100644 index 000000000..de38b5663 --- /dev/null +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/mapping/AbstractHashMapperTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis.mapping; + +import static org.junit.Assert.*; + +import java.util.Map; + +import org.junit.Test; +import org.springframework.data.keyvalue.redis.Address; +import org.springframework.data.keyvalue.redis.Person; +import org.springframework.data.keyvalue.redis.mapper.HashMapper; + +/** + * @author Costin Leau + */ +public abstract class AbstractHashMapperTest { + protected abstract HashMapper mapperFor(Class t); + + private void test(Object o) { + HashMapper mapper = mapperFor(o.getClass()); + Map hash = mapper.toHash(o); + System.out.println("object hash " + hash.size() + " is " + hash); + assertEquals(o, mapper.fromHash(hash)); + } + + @Test(expected = Exception.class) + public void testBasicValues() throws Exception { + test("SomeStrangeString*&#@"); + test(123); + test(Integer.MAX_VALUE); + test(Long.MAX_VALUE); + test(Double.MIN_VALUE); + test(Float.MIN_VALUE); + test(Boolean.FALSE); + test(Thread.State.BLOCKED); + } + + @Test + public void testSimpleBean() throws Exception { + test(new Address("Broadway", 1)); + } + + @Test + public void testNestedBean() throws Exception { + test(new Person("George", "Enescu", 74, new Address("liveni", 19))); + } +} \ No newline at end of file diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/mapping/JacksonHashMapperTest.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/mapping/JacksonHashMapperTest.java new file mode 100644 index 000000000..c8e2d80f1 --- /dev/null +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/mapping/JacksonHashMapperTest.java @@ -0,0 +1,27 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.redis.mapping; + +import org.springframework.data.keyvalue.redis.mapper.HashMapper; +import org.springframework.data.keyvalue.redis.mapper.JacksonHashMapper; + +public class JacksonHashMapperTest extends AbstractHashMapperTest { + + @Override + protected HashMapper mapperFor(Class t) { + return new JacksonHashMapper(t); + } +}