`persist` method allow to store entity without id. `create` and `getOrCreate` methods are deprecated.

pull/653/head
Nikita
parent 733ac70c00
commit c6bb27b95a

@ -23,26 +23,32 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentMap;
import org.redisson.api.RExpirable;
import org.redisson.api.RExpirableAsync;
import org.redisson.api.RLiveObject;
import org.redisson.api.RLiveObjectService;
import org.redisson.api.RMap;
import org.redisson.api.RMapAsync;
import org.redisson.api.RObject;
import org.redisson.api.RObjectAsync;
import org.redisson.api.RedissonClient;
import org.redisson.api.annotation.REntity;
import org.redisson.api.annotation.RFieldAccessor;
import org.redisson.api.annotation.RId;
import org.redisson.client.codec.Codec;
import org.redisson.codec.CodecProvider;
import org.redisson.liveobject.LiveObjectTemplate;
import org.redisson.liveobject.core.AccessorInterceptor;
import org.redisson.liveobject.core.FieldAccessorInterceptor;
import org.redisson.liveobject.core.LiveObjectInterceptor;
import org.redisson.liveobject.core.RExpirableInterceptor;
import org.redisson.liveobject.core.RMapInterceptor;
import org.redisson.liveobject.core.RObjectInterceptor;
import org.redisson.liveobject.misc.Introspectior;
import org.redisson.codec.CodecProvider;
import org.redisson.liveobject.provider.ResolverProvider;
import org.redisson.liveobject.resolver.NamingScheme;
import org.redisson.liveobject.resolver.Resolver;
import org.redisson.misc.RedissonObjectFactory;
import jodd.bean.BeanCopy;
import jodd.bean.BeanUtil;
import jodd.util.ReflectUtil;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.field.FieldList;
@ -51,16 +57,6 @@ import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.FieldProxy;
import net.bytebuddy.matcher.ElementMatchers;
import org.redisson.api.RExpirable;
import org.redisson.api.RExpirableAsync;
import org.redisson.api.RMap;
import org.redisson.api.RMapAsync;
import org.redisson.api.RObjectAsync;
import org.redisson.api.annotation.RFieldAccessor;
import org.redisson.liveobject.core.FieldAccessorInterceptor;
import org.redisson.liveobject.core.RExpirableInterceptor;
import org.redisson.liveobject.core.RMapInterceptor;
import org.redisson.liveobject.core.RObjectInterceptor;
public class RedissonLiveObjectService implements RLiveObjectService {
@ -86,25 +82,39 @@ public class RedissonLiveObjectService implements RLiveObjectService {
// map.expire(timeToLive, timeUnit);
// return instance;
// }
public RMap<String, Object> getMap(Object proxied) {
return BeanUtil.declared.getProperty(proxied, "liveObjectLiveMap");
}
@Override
public <T> T create(Class<T> entityClass) {
validateClass(entityClass);
try {
Class<? extends T> proxyClass = getProxyClass(entityClass);
String idFieldName = getRIdFieldName(entityClass);
RId annotation = entityClass
.getDeclaredField(idFieldName)
.getAnnotation(RId.class);
Resolver resolver = resolverProvider.getResolver(entityClass,
annotation.generator(), annotation);
Object id = resolver.resolve(entityClass, annotation, idFieldName, redisson);
Object id = generateId(entityClass);
T proxied = instantiateLiveObject(proxyClass, id);
return asLiveObject(proxied).isExists() ? null : proxied;
if (!getMap(proxied).fastPut("redisson_live_object", "1")) {
throw new IllegalArgumentException("Object already exists");
}
return proxied;
} catch (Exception ex) {
unregisterClass(entityClass);
throw ex instanceof RuntimeException ? (RuntimeException) ex : new RuntimeException(ex);
}
}
private <T> Object generateId(Class<T> entityClass) throws NoSuchFieldException {
String idFieldName = getRIdFieldName(entityClass);
RId annotation = entityClass
.getDeclaredField(idFieldName)
.getAnnotation(RId.class);
Resolver resolver = resolverProvider.getResolver(entityClass,
annotation.generator(), annotation);
Object id = resolver.resolve(entityClass, annotation, idFieldName, redisson);
return id;
}
@Override
public <T, K> T get(Class<T> entityClass, K id) {
try {
@ -119,7 +129,9 @@ public class RedissonLiveObjectService implements RLiveObjectService {
@Override
public <T, K> T getOrCreate(Class<T> entityClass, K id) {
try {
return instantiateLiveObject(getProxyClass(entityClass), id);
T proxied = instantiateLiveObject(getProxyClass(entityClass), id);
getMap(proxied).fastPut("redisson_live_object", "1");
return proxied;
} catch (Exception ex) {
unregisterClass(entityClass);
throw ex instanceof RuntimeException ? (RuntimeException) ex : new RuntimeException(ex);
@ -131,10 +143,10 @@ public class RedissonLiveObjectService implements RLiveObjectService {
validateDetached(detachedObject);
Class<T> entityClass = (Class<T>) detachedObject.getClass();
try {
String idFieldName = getRIdFieldName(detachedObject.getClass());
Object id = BeanUtil.declared.getProperty(detachedObject, idFieldName);
Class<? extends T> proxyClass = getProxyClass(entityClass);
return instantiateLiveObject(proxyClass,
BeanUtil.pojo.getSimpleProperty(detachedObject,
getRIdFieldName(detachedObject.getClass())));
return instantiateLiveObject(proxyClass, id);
} catch (Exception ex) {
unregisterClass(entityClass);
throw ex instanceof RuntimeException ? (RuntimeException) ex : new RuntimeException(ex);
@ -150,16 +162,24 @@ public class RedissonLiveObjectService implements RLiveObjectService {
@Override
public <T> T persist(T detachedObject) {
String idFieldName = getRIdFieldName(detachedObject.getClass());
Object id = BeanUtil.declared.getProperty(detachedObject, idFieldName);
if (id == null) {
try {
id = generateId(detachedObject.getClass());
} catch (NoSuchFieldException e) {
throw new IllegalArgumentException(e);
}
BeanUtil.declared.setProperty(detachedObject, idFieldName, id);
}
T attachedObject = attach(detachedObject);
if (!asLiveObject(attachedObject).isExists()) {
RLiveObject liveObject = asLiveObject(attachedObject);
if (getMap(liveObject).fastPut("redisson_live_object", "1")) {
copy(detachedObject, attachedObject);
return attachedObject;
}
throw new IllegalStateException("This REntity already exists.");
}
private RMap<String, Object> getMap(Object proxied) {
return BeanUtil.declared.getProperty(proxied, "liveObjectLiveMap");
throw new IllegalArgumentException("This REntity already exists.");
}
@Override

@ -15,18 +15,11 @@
*/
package org.redisson.api;
//import org.redisson.core.RMap;
/**
*
* @author Rui Gu (https://github.com/jackygurui)
*/
public interface RLiveObject {
//
// /**
// * @return the liveObjectLiveMap
// */
// public RMap getLiveObjectLiveMap();
/**
* Returns the value of the field that has the RId annotation.

@ -27,22 +27,13 @@ package org.redisson.api;
public interface RLiveObjectService {
/**
* Find or create the entity from Redis with the id.
*
* The entityClass should have a field annotated with RId, and the
* entityClass itself should have REntity annotated. The type of the RId can
* be anything <b>except</b> the followings:
* <ol>
* <li>An array i.e. byte[], int[], Integer[], etc.</li>
* <li>or a RObject i.e. RedissonMap</li>
* <li>or a Class with REntity annotation.</li>
* </ol>
*
* Use {@link #persist(Object)} method instead
*
* @param entityClass Entity class
* @param <T> Entity type
* @return Always returns a proxied object. Even it does not exist in redis.
*/
@Deprecated
<T> T create(Class<T> entityClass);
/**
@ -67,17 +58,7 @@ public interface RLiveObjectService {
<T, K> T get(Class<T> entityClass, K id);
/**
* Find or create the entity from Redis with the id.
*
* The entityClass should have a field annotated with RId, and the
* entityClass itself should have REntity annotated. The type of the RId can
* be anything <b>except</b> the followings:
* <ol>
* <li>An array i.e. byte[], int[], Integer[], etc.</li>
* <li>or a RObject i.e. RedissonMap</li>
* <li>or a Class with REntity annotation.</li>
* </ol>
*
* Use {@link #persist(Object)} method instead
*
* @param entityClass Entity class
* @param id identifier
@ -85,6 +66,7 @@ public interface RLiveObjectService {
* @param <K> Key type
* @return Always returns a proxied object. Even it does not exist in redis.
*/
@Deprecated
<T, K> T getOrCreate(Class<T> entityClass, K id);
/**
@ -127,9 +109,6 @@ public interface RLiveObjectService {
* <b>NON NULL</b> field values to the redis server. Only when the it does
* not already exist.
*
* The class representing this object should have a field annotated with
* RId, and the object should hold a non null value in that field.
*
* If this object is not in redis then a new hash key will be created to
* store it.
*

@ -107,12 +107,12 @@ public class AccessorInterceptor {
}
String fieldName = getFieldName(method);
Class<?> idFieldType = me.getClass().getSuperclass().getDeclaredField(fieldName).getType();
Class<?> fieldType = me.getClass().getSuperclass().getDeclaredField(fieldName).getType();
if (isGetter(method, fieldName)) {
Object result = liveMap.get(fieldName);
if (result == null) {
Class<? extends RObject> mappedClass = getMappedClass(idFieldType);
Class<? extends RObject> mappedClass = getMappedClass(fieldType);
if (mappedClass != null) {
Codec fieldCodec = getFieldCodec(me.getClass().getSuperclass(), mappedClass, fieldName);
NamingScheme fieldNamingScheme = getFieldNamingScheme(me.getClass().getSuperclass(), fieldName, fieldCodec);
@ -135,23 +135,30 @@ public class AccessorInterceptor {
}
}
return result instanceof RedissonReference
? RedissonObjectFactory.fromReference(redisson, (RedissonReference) result, method.getReturnType())
: result;
if (result instanceof RedissonReference) {
return RedissonObjectFactory.fromReference(redisson, (RedissonReference) result);
}
return result;
}
if (isSetter(method, fieldName)) {
if (args[0] instanceof RLiveObject) {
Class<? extends Object> rEntity = args[0].getClass().getSuperclass();
Object arg = args[0];
if (arg.getClass().isAnnotationPresent(REntity.class)) {
throw new IllegalStateException("REntity object should be attached to Redisson first");
}
if (arg instanceof RLiveObject) {
RLiveObject liveObject = (RLiveObject) arg;
Class<? extends Object> rEntity = liveObject.getClass().getSuperclass();
REntity anno = rEntity.getAnnotation(REntity.class);
NamingScheme ns = anno.namingScheme()
.getDeclaredConstructor(Codec.class)
.newInstance(codecProvider.getCodec(anno, (Class) rEntity));
liveMap.fastPut(fieldName, new RedissonReference(rEntity,
ns.getName(rEntity, idFieldType, getREntityIdFieldName(args[0]),
((RLiveObject) args[0]).getLiveObjectId())));
ns.getName(rEntity, fieldType, getREntityIdFieldName(liveObject),
liveObject.getLiveObjectId())));
return me;
}
Object arg = args[0];
if (!(arg instanceof RObject)
&& (arg instanceof Collection || arg instanceof Map)
&& TransformationMode.ANNOTATION_BASED

@ -100,10 +100,6 @@ public class RedissonObjectFactory {
}
public static <T> T fromReference(RedissonClient redisson, RedissonReference rr) throws Exception {
return fromReference(redisson, rr, null);
}
public static <T> T fromReference(RedissonClient redisson, RedissonReference rr, Class<?> expected) throws Exception {
Class<? extends Object> type = rr.getType();
CodecProvider codecProvider = redisson.getConfig().getCodecProvider();
if (type != null) {
@ -113,7 +109,7 @@ public class RedissonObjectFactory {
NamingScheme ns = anno.namingScheme()
.getDeclaredConstructor(Codec.class)
.newInstance(codecProvider.getCodec(anno, type));
return (T) liveObjectService.getOrCreate(type, ns.resolveId(rr.getKeyName()));
return (T) liveObjectService.get(type, ns.resolveId(rr.getKeyName()));
}
List<Class<?>> interfaces = Arrays.asList(type.getInterfaces());
for (Class<?> iType : interfaces) {

@ -145,6 +145,9 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@RId
private String name;
private Map value;
public TestREntityWithMap() {
}
public TestREntityWithMap(String name) {
this.name = name;
@ -267,17 +270,21 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testBasics() {
RLiveObjectService s = redisson.getLiveObjectService();
TestREntity t = s.<TestREntity, String>getOrCreate(TestREntity.class, "1");
TestREntity t = new TestREntity("1");
t = s.persist(t);
assertEquals("1", t.getName());
assertTrue(!redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, String.class, "name", "1")).isExists());
assertTrue(redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, String.class, "name", "1")).isExists());
t.setName("3333");
assertEquals("3333", t.getName());
assertTrue(!redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, String.class, "name", "3333")).isExists());
assertTrue(redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, String.class, "name", "3333")).isExists());
t.setValue("111");
assertEquals("111", t.getValue());
assertTrue(redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, String.class, "name", "3333")).isExists());
assertTrue(!redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, String.class, "name", "1")).isExists());
assertEquals("111", redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, String.class, "name", "3333")).get("value"));
// ((RLiveObject) t).getLiveObjectLiveMap().put("value", "555");
// assertEquals("555", redisson.getMap(REntity.DefaultNamingScheme.INSTANCE.getName(TestREntity.class, "name", "3333")).get("value"));
// assertEquals("3333", ((RObject) t).getName());//field access takes priority over the implemented interface.
@ -286,47 +293,57 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testLiveObjectWithCollection() {
RLiveObjectService s = redisson.getLiveObjectService();
TestREntityWithMap t = s.<TestREntityWithMap, String>getOrCreate(TestREntityWithMap.class, "2");
TestREntityWithMap t = new TestREntityWithMap("2");
t = s.persist(t);
RMap<String, String> map = redisson.<String, String>getMap("testMap");
t.setValue(map);
map.put("field", "123");
assertEquals("123",
s.<TestREntityWithMap, String>getOrCreate(TestREntityWithMap.class, "2")
.getValue().get("field"));
s.getOrCreate(TestREntityWithMap.class, "2").getValue().put("field", "333");
assertEquals("333",
s.<TestREntityWithMap, String>getOrCreate(TestREntityWithMap.class, "2")
.getValue().get("field"));
TestREntityWithMap t2 = s.get(TestREntityWithMap.class, "2");
assertEquals("123", t2.getValue().get("field"));
TestREntityWithMap t3 = s.get(TestREntityWithMap.class, "2");
t3.getValue().put("field", "333");
t3 = s.get(TestREntityWithMap.class, "2");
assertEquals("333", t3.getValue().get("field"));
HashMap<String, String> map2 = new HashMap<>();
map2.put("field", "hello");
t.setValue(map2);
assertEquals("hello",
s.<TestREntityWithMap, String>getOrCreate(TestREntityWithMap.class, "2")
.getValue().get("field"));
t3 = s.get(TestREntityWithMap.class, "2");
assertEquals("hello", t3.getValue().get("field"));
}
@Test
public void testLiveObjectWithRObject() {
RLiveObjectService s = redisson.getLiveObjectService();
TestREntityWithRMap t = s.<TestREntityWithRMap, String>getOrCreate(TestREntityWithRMap.class, "2");
TestREntityWithRMap t = new TestREntityWithRMap("2");
t = s.persist(t);
RMap<String, String> map = redisson.<String, String>getMap("testMap");
t.setValue(map);
map.put("field", "123");
assertEquals("123",
s.<TestREntityWithRMap, String>getOrCreate(TestREntityWithRMap.class, "2")
s.<TestREntityWithRMap, String>get(TestREntityWithRMap.class, "2")
.getValue().get("field"));
s.getOrCreate(TestREntityWithRMap.class, "2").getValue().put("field", "333");
t = s.get(TestREntityWithRMap.class, "2");
t.getValue().put("field", "333");
assertEquals("333",
s.<TestREntityWithRMap, String>getOrCreate(TestREntityWithRMap.class, "2")
s.<TestREntityWithRMap, String>get(TestREntityWithRMap.class, "2")
.getValue().get("field"));
}
@Test
public void testLiveObjectWithNestedLiveObjectAsId() {
RLiveObjectService s = redisson.getLiveObjectService();
TestREntity t1 = s.<TestREntity, String>getOrCreate(TestREntity.class, "1");
TestREntity t1 = new TestREntity("1");
t1 = s.persist(t1);
try {
s.<TestREntityIdNested, TestREntity>getOrCreate(TestREntityIdNested.class, t1);
s.persist(new TestREntityIdNested(t1));
fail("Should not be here");
} catch (Exception e) {
assertEquals("Field with RId annotation cannot be a type of which class is annotated with REntity.", e.getMessage());
@ -336,17 +353,22 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testLiveObjectWithNestedLiveObjectAsValue() throws Exception {
RLiveObjectService s = redisson.getLiveObjectService();
TestREntityWithRMap t1 = s.<TestREntityWithRMap, String>getOrCreate(TestREntityWithRMap.class, "111");
TestREntityValueNested t2 = s.<TestREntityValueNested, String>getOrCreate(TestREntityValueNested.class, "122");
TestREntityWithRMap t1 = new TestREntityWithRMap("111");
t1 = s.persist(t1);
TestREntityValueNested t2 = new TestREntityValueNested("122");
t2 = s.persist(t2);
RMap<String, String> map = redisson.<String, String>getMap("32123");
t2.setValue(t1);
t2.getValue().setValue(map);
map.put("field", "123");
assertEquals("123",
s.<TestREntityWithRMap, String>getOrCreate(TestREntityWithRMap.class, "111")
s.get(TestREntityWithRMap.class, "111")
.getValue().get("field"));
assertEquals("123",
s.<TestREntityValueNested, String>getOrCreate(TestREntityValueNested.class, "122")
s.get(TestREntityValueNested.class, "122")
.getValue().getValue().get("field"));
}
@ -360,6 +382,9 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@RId
private Serializable id;
public TestClass() {
}
public TestClass(Serializable id) {
this.id = id;
}
@ -469,40 +494,47 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testSerializerable() {
RLiveObjectService service = redisson.getLiveObjectService();
TestClass t = service.getOrCreate(TestClass.class, "55555");
TestClass t = new TestClass("55555");
t = service.persist(t);
assertTrue(Objects.equals("55555", t.getId()));
t = service.getOrCreate(TestClass.class, 90909l);
t = new TestClass(90909l);
t = service.persist(t);
assertTrue(Objects.equals(90909l, t.getId()));
t = service.getOrCreate(TestClass.class, 90909);
t = new TestClass(90909);
t = service.persist(t);
assertTrue(Objects.equals(90909, t.getId()));
t = service.getOrCreate(TestClass.class, new ObjectId(9090909));
t = new TestClass(new ObjectId(9090909));
t = service.persist(t);
assertTrue(Objects.equals(new ObjectId(9090909), t.getId()));
t = service.getOrCreate(TestClass.class, new Byte("0"));
t = new TestClass(new Byte("0"));
t = service.persist(t);
assertEquals(new Byte("0"), Byte.valueOf(t.getId().toString()));
t = service.getOrCreate(TestClass.class, (byte) 90);
t = new TestClass((byte)90);
assertEquals((byte) 90, Byte.parseByte(t.getId().toString()));
t = service.getOrCreate(TestClass.class, Arrays.asList(1, 2, 3, 4));
t = new TestClass((Serializable)Arrays.asList(1, 2, 3, 4));
t = service.persist(t);
List<Integer> l = new ArrayList();
l.addAll(Arrays.asList(1, 2, 3, 4));
assertTrue(l.removeAll((List) t.getId()));
assertTrue(l.isEmpty());
try {
service.getOrCreate(TestClass.class, new int[]{1, 2, 3, 4, 5});
t = new TestClass(new int[]{1, 2, 3, 4, 5});
t = service.persist(t);
fail("Should not be here");
} catch (Exception e) {
assertEquals("RId value cannot be an array.", e.getMessage());
}
try {
service.getOrCreate(TestClass.class, new byte[]{1, 2, 3, 4, 5});
t = new TestClass(new byte[]{1, 2, 3, 4, 5});
t = service.persist(t);
fail("Should not be here");
} catch (Exception e) {
assertEquals("RId value cannot be an array.", e.getMessage());
@ -587,7 +619,9 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testAsLiveObject() {
RLiveObjectService service = redisson.getLiveObjectService();
TestClass instance = service.getOrCreate(TestClass.class, new ObjectId(100));
TestClass instance = new TestClass(new ObjectId(100));
instance = service.persist(instance);
RLiveObject liveObject = service.asLiveObject(instance);
assertEquals(new ObjectId(100), liveObject.getLiveObjectId());
try {
@ -633,20 +667,9 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
assertNull(service.get(TestClass.class, new ObjectId(100)));
TestClass ts = new TestClass(new ObjectId(100));
TestClass persisted = service.persist(ts);
assertNull(service.get(TestClass.class, new ObjectId(100)));
persisted.setCode("CODE");
assertNotNull(service.get(TestClass.class, new ObjectId(100)));
}
@Test
public void testGetOrCreate() {
RLiveObjectService service = redisson.getLiveObjectService();
assertNotNull(service.getOrCreate(TestClass.class, new ObjectId(100)));
TestClass ts = new TestClass(new ObjectId(100));
TestClass persisted = service.persist(ts);
assertNotNull(service.getOrCreate(TestClass.class, new ObjectId(100)));
persisted.setCode("CODE");
assertNotNull(service.getOrCreate(TestClass.class, new ObjectId(100)));
assertNotNull(service.get(TestClass.class, new ObjectId(100)));
}
@Test
@ -677,6 +700,9 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@RId(generator = DistributedAtomicLongIdGenerator.class)
private Long name;
public TestClassID1() {
}
public TestClassID1(Long name) {
this.name = name;
}
@ -693,6 +719,9 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@RId(generator = DistributedAtomicLongIdGenerator.class)
private Long name;
public TestClassID2() {
}
public TestClassID2(Long name) {
this.name = name;
}
@ -706,61 +735,66 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testCreate() {
RLiveObjectService service = redisson.getLiveObjectService();
TestClass ts = service.create(TestClass.class);
TestClass ts = new TestClass();
ts = service.persist(ts);
UUID uuid = UUID.fromString(ts.getId().toString());
assertEquals(4, uuid.version());
TestClassID1 tc1 = service.create(TestClassID1.class);
TestClassID1 tc1 = new TestClassID1();
tc1 = service.persist(tc1);
assertEquals(new Long(1), tc1.getName());
TestClassID2 tc2 = service.create(TestClassID2.class);
TestClassID2 tc2 = new TestClassID2();
tc2 = service.persist(tc2);
assertEquals(new Long(1), tc2.getName());
}
@Test
public void testTransformation() {
RLiveObjectService service = redisson.getLiveObjectService();
TestClass ts = service.create(TestClass.class);
TestClass ts = new TestClass();
ts = service.persist(ts);
HashMap<String, String> m = new HashMap();
HashMap<String, String> m = new HashMap<>();
ts.setContent(m);
assertFalse(HashMap.class.isAssignableFrom(ts.getContent().getClass()));
assertTrue(RMap.class.isAssignableFrom(ts.getContent().getClass()));
HashSet<String> s = new HashSet();
HashSet<String> s = new HashSet<>();
ts.setContent(s);
assertFalse(HashSet.class.isAssignableFrom(ts.getContent().getClass()));
assertTrue(RSet.class.isAssignableFrom(ts.getContent().getClass()));
TreeSet<String> ss = new TreeSet();
TreeSet<String> ss = new TreeSet<>();
ts.setContent(ss);
assertFalse(TreeSet.class.isAssignableFrom(ts.getContent().getClass()));
assertTrue(RSortedSet.class.isAssignableFrom(ts.getContent().getClass()));
ArrayList<String> al = new ArrayList();
ArrayList<String> al = new ArrayList<>();
ts.setContent(al);
assertFalse(ArrayList.class.isAssignableFrom(ts.getContent().getClass()));
assertTrue(RList.class.isAssignableFrom(ts.getContent().getClass()));
ConcurrentHashMap<String, String> chm = new ConcurrentHashMap();
ConcurrentHashMap<String, String> chm = new ConcurrentHashMap<>();
ts.setContent(chm);
assertFalse(ConcurrentHashMap.class.isAssignableFrom(ts.getContent().getClass()));
assertTrue(RMap.class.isAssignableFrom(ts.getContent().getClass()));
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(10);
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue<>(10);
ts.setContent(abq);
assertFalse(ArrayBlockingQueue.class.isAssignableFrom(ts.getContent().getClass()));
assertTrue(RBlockingQueue.class.isAssignableFrom(ts.getContent().getClass()));
ConcurrentLinkedQueue<String> clq = new ConcurrentLinkedQueue();
ConcurrentLinkedQueue<String> clq = new ConcurrentLinkedQueue<>();
ts.setContent(clq);
assertFalse(ConcurrentLinkedQueue.class.isAssignableFrom(ts.getContent().getClass()));
assertTrue(RQueue.class.isAssignableFrom(ts.getContent().getClass()));
LinkedBlockingDeque<String> lbdq = new LinkedBlockingDeque();
LinkedBlockingDeque<String> lbdq = new LinkedBlockingDeque<>();
ts.setContent(lbdq);
assertFalse(LinkedBlockingDeque.class.isAssignableFrom(ts.getContent().getClass()));
assertTrue(RBlockingDeque.class.isAssignableFrom(ts.getContent().getClass()));
LinkedList<String> ll = new LinkedList();
LinkedList<String> ll = new LinkedList<>();
ts.setContent(ll);
assertFalse(LinkedList.class.isAssignableFrom(ts.getContent().getClass()));
assertTrue(RDeque.class.isAssignableFrom(ts.getContent().getClass()));
@ -777,6 +811,9 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@RId
private Serializable id;
public TestClassNoTransformation() {
}
public TestClassNoTransformation(Serializable id) {
this.id = id;
}
@ -836,50 +873,51 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testNoTransformation() {
RLiveObjectService service = redisson.getLiveObjectService();
TestClassNoTransformation ts = service.create(TestClassNoTransformation.class);
TestClassNoTransformation ts = new TestClassNoTransformation();
ts = service.persist(ts);
HashMap<String, String> m = new HashMap();
HashMap<String, String> m = new HashMap<>();
ts.setContent(m);
assertTrue(HashMap.class.isAssignableFrom(ts.getContent().getClass()));
assertFalse(RMap.class.isAssignableFrom(ts.getContent().getClass()));
HashSet<String> s = new HashSet();
HashSet<String> s = new HashSet<>();
ts.setContent(s);
assertTrue(HashSet.class.isAssignableFrom(ts.getContent().getClass()));
assertFalse(RSet.class.isAssignableFrom(ts.getContent().getClass()));
TreeSet<String> ss = new TreeSet();
TreeSet<String> ss = new TreeSet<>();
ts.setContent(ss);
assertTrue(TreeSet.class.isAssignableFrom(ts.getContent().getClass()));
assertFalse(RSortedSet.class.isAssignableFrom(ts.getContent().getClass()));
ArrayList<String> al = new ArrayList();
ArrayList<String> al = new ArrayList<>();
ts.setContent(al);
assertTrue(ArrayList.class.isAssignableFrom(ts.getContent().getClass()));
assertFalse(RList.class.isAssignableFrom(ts.getContent().getClass()));
ConcurrentHashMap<String, String> chm = new ConcurrentHashMap();
ConcurrentHashMap<String, String> chm = new ConcurrentHashMap<>();
ts.setContent(chm);
assertTrue(ConcurrentHashMap.class.isAssignableFrom(ts.getContent().getClass()));
assertFalse(RMap.class.isAssignableFrom(ts.getContent().getClass()));
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(10);
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue<>(10);
abq.add("111");
ts.setContent(abq);
assertTrue(ArrayBlockingQueue.class.isAssignableFrom(ts.getContent().getClass()));
assertFalse(RBlockingQueue.class.isAssignableFrom(ts.getContent().getClass()));
ConcurrentLinkedQueue<String> clq = new ConcurrentLinkedQueue();
ConcurrentLinkedQueue<String> clq = new ConcurrentLinkedQueue<>();
ts.setContent(clq);
assertTrue(ConcurrentLinkedQueue.class.isAssignableFrom(ts.getContent().getClass()));
assertFalse(RQueue.class.isAssignableFrom(ts.getContent().getClass()));
LinkedBlockingDeque<String> lbdq = new LinkedBlockingDeque();
LinkedBlockingDeque<String> lbdq = new LinkedBlockingDeque<>();
ts.setContent(lbdq);
assertTrue(LinkedBlockingDeque.class.isAssignableFrom(ts.getContent().getClass()));
assertFalse(RBlockingDeque.class.isAssignableFrom(ts.getContent().getClass()));
LinkedList<String> ll = new LinkedList();
LinkedList<String> ll = new LinkedList<>();
ts.setContent(ll);
assertTrue(LinkedList.class.isAssignableFrom(ts.getContent().getClass()));
assertFalse(RDeque.class.isAssignableFrom(ts.getContent().getClass()));
@ -936,7 +974,8 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testExpirable() throws InterruptedException {
RLiveObjectService service = redisson.getLiveObjectService();
TestClass myObject = service.create(TestClass.class);
TestClass myObject = new TestClass();
myObject = service.persist(myObject);
myObject.setValue("123345");
assertTrue(service.asLiveObject(myObject).isExists());
service.asRExpirable(myObject).expire(1, TimeUnit.SECONDS);
@ -947,7 +986,9 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testMap() {
RLiveObjectService service = redisson.getLiveObjectService();
TestClass myObject = service.create(TestClass.class);
TestClass myObject = new TestClass();
myObject = service.persist(myObject);
myObject.setValue("123345");
assertEquals("123345", service.asRMap(myObject).get("value"));
service.asRMap(myObject).put("value", "9999");
@ -957,7 +998,8 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testRObject() {
RLiveObjectService service = redisson.getLiveObjectService();
TestClass myObject = service.create(TestClass.class);
TestClass myObject = new TestClass();
myObject = service.persist(myObject);
try {
((RObject) myObject).isExists();
} catch (Exception e) {
@ -1017,8 +1059,13 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testStoreInnerObject() {
ObjectWithList so = redisson.getLiveObjectService().create(ObjectWithList.class);
SimpleObject s = redisson.getLiveObjectService().create(SimpleObject.class);
RLiveObjectService service = redisson.getLiveObjectService();
ObjectWithList so = new ObjectWithList();
so = service.persist(so);
SimpleObject s = new SimpleObject();
s = service.persist(s);
so.setSo(s);
assertThat(s.getId()).isNotNull();
so.getObjects().add(s);
@ -1030,9 +1077,11 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testFieldWithoutIdSetter() {
SimpleObject so = redisson.getLiveObjectService().create(SimpleObject.class);
RLiveObjectService service = redisson.getLiveObjectService();
SimpleObject so = new SimpleObject();
so = service.persist(so);
so.setValue(10L);
so = redisson.getLiveObjectService().detach(so);
assertThat(so.getId()).isNotNull();
assertThat(so.getValue()).isEqualTo(10L);
@ -1044,7 +1093,11 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testCreateObjectsInRuntime() {
TestREntityWithMap so = redisson.getLiveObjectService().create(TestREntityWithMap.class);
RLiveObjectService service = redisson.getLiveObjectService();
TestREntityWithMap so = new TestREntityWithMap();
so = service.persist(so);
so.getValue().put("1", "2");
so = redisson.getLiveObjectService().detach(so);
@ -1061,7 +1114,9 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
@Test
public void testFieldAccessor() {
RLiveObjectService service = redisson.getLiveObjectService();
TestClass myObject = service.create(TestClass.class);
TestClass myObject = new TestClass();
myObject = service.persist(myObject);
myObject.setValue("123345");
assertEquals("123345", myObject.get("value"));
myObject.set("value", "9999");

@ -11,6 +11,7 @@ import org.redisson.api.RBucket;
import org.redisson.api.RBucketAsync;
import org.redisson.api.RBucketReactive;
import org.redisson.api.RLiveObject;
import org.redisson.api.RLiveObjectService;
import org.redisson.api.RMap;
import org.redisson.api.RScoredSortedSet;
import org.redisson.api.RSet;
@ -37,7 +38,10 @@ public class RedissonReferenceTest extends BaseTest {
((RedissonMapCache) b4.get()).fastPut(b1, b2, 1, TimeUnit.MINUTES);
assertEquals("b2", ((RBucket)((RedissonMapCache) b4.get()).get(b1)).getName());
RBucket<Object> b5 = redisson.getBucket("b5");
RedissonLiveObjectServiceTest.TestREntity rlo = redisson.getLiveObjectService().create(RedissonLiveObjectServiceTest.TestREntity.class);
RLiveObjectService service = redisson.getLiveObjectService();
RedissonLiveObjectServiceTest.TestREntity rlo = new RedissonLiveObjectServiceTest.TestREntity("123");
rlo = service.persist(rlo);
rlo.setName("t1");
rlo.setValue("t2");
b5.set(rlo);

Loading…
Cancel
Save