DATAKV-36

+ add initial cut of mapping package and HashMapper
This commit is contained in:
Costin Leau
2011-03-08 19:52:00 +02:00
parent 32998b9d82
commit 6496c37e46
6 changed files with 211 additions and 0 deletions

View File

@@ -115,6 +115,12 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils-core</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>

View File

@@ -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<T> {
Map<?, ?> toHash(T object);
T fromHash(Map<?, ?> hash);
}

View File

@@ -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<T> implements HashMapper<T> {
private final Class<T> type;
private final ObjectMapper mapper;
public JacksonHashMapper(Class<T> type) {
this(type, new ObjectMapper());
}
public JacksonHashMapper(Class<T> 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);
}
}

View File

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

View File

@@ -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<Object> 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)));
}
}

View File

@@ -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);
}
}