From 82f54c778674928e0cea2887c913eb1b5b82d6b6 Mon Sep 17 00:00:00 2001 From: godotg Date: Mon, 1 Jul 2024 17:03:48 +0800 Subject: [PATCH] test[orm]: cache test --- .../zfoo/orm/cache/wrapper/EnhanceUtils.java | 2 +- .../com/zfoo/orm/accessor/DocumentTest.java | 41 ++++++++++ .../com/zfoo/orm/cache/EntityCachesTest.java | 38 ++++----- .../zfoo/orm/entity/wrapper/DoubleEntity.java | 81 ++++++++++++++++++ .../zfoo/orm/entity/wrapper/FloatEntity.java | 81 ++++++++++++++++++ .../zfoo/orm/entity/wrapper/IntEntity.java | 81 ++++++++++++++++++ .../zfoo/orm/entity/wrapper/LongEntity.java | 81 ++++++++++++++++++ .../orm/entity/wrapper/ObjectIdEntity.java | 82 +++++++++++++++++++ .../zfoo/orm/entity/wrapper/StringEntity.java | 81 ++++++++++++++++++ .../com/zfoo/protocol/util/FieldUtils.java | 2 +- 10 files changed, 547 insertions(+), 23 deletions(-) create mode 100644 orm/src/test/java/com/zfoo/orm/accessor/DocumentTest.java create mode 100644 orm/src/test/java/com/zfoo/orm/entity/wrapper/DoubleEntity.java create mode 100644 orm/src/test/java/com/zfoo/orm/entity/wrapper/FloatEntity.java create mode 100644 orm/src/test/java/com/zfoo/orm/entity/wrapper/IntEntity.java create mode 100644 orm/src/test/java/com/zfoo/orm/entity/wrapper/LongEntity.java create mode 100644 orm/src/test/java/com/zfoo/orm/entity/wrapper/ObjectIdEntity.java create mode 100644 orm/src/test/java/com/zfoo/orm/entity/wrapper/StringEntity.java diff --git a/orm/src/main/java/com/zfoo/orm/cache/wrapper/EnhanceUtils.java b/orm/src/main/java/com/zfoo/orm/cache/wrapper/EnhanceUtils.java index 3ffd2dc9..94120505 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/wrapper/EnhanceUtils.java +++ b/orm/src/main/java/com/zfoo/orm/cache/wrapper/EnhanceUtils.java @@ -54,7 +54,7 @@ public abstract class EnhanceUtils { } else if (idType == float.class || idType == Float.class) { return "((Float)$1).floatValue()"; } else if (idType == double.class || idType == Double.class) { - return "((Double)$1).floatValue()"; + return "((Double)$1).doubleValue()"; } else if (idType == String.class) { return "(String)$1"; } else { diff --git a/orm/src/test/java/com/zfoo/orm/accessor/DocumentTest.java b/orm/src/test/java/com/zfoo/orm/accessor/DocumentTest.java new file mode 100644 index 00000000..1a475c64 --- /dev/null +++ b/orm/src/test/java/com/zfoo/orm/accessor/DocumentTest.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2020 The zfoo 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 com.zfoo.orm.accessor; + +import com.mongodb.client.model.Filters; +import com.zfoo.orm.OrmContext; +import com.zfoo.orm.entity.UserEntity; +import com.zfoo.protocol.util.ThreadUtils; +import org.bson.Document; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author godotg + */ +@Ignore +public class DocumentTest { + + @Test + public void collectionTest() { + var context = new ClassPathXmlApplicationContext("application.xml"); + + var collection = OrmContext.getOrmManager().getCollection(UserEntity.class); + var result = collection.updateOne(Filters.eq("_id", 1), new Document("$inc", new Document("c", 1L))); + System.out.println(result); + ThreadUtils.sleep(Long.MAX_VALUE); + } + +} diff --git a/orm/src/test/java/com/zfoo/orm/cache/EntityCachesTest.java b/orm/src/test/java/com/zfoo/orm/cache/EntityCachesTest.java index ff86a2d3..e8a8f39a 100644 --- a/orm/src/test/java/com/zfoo/orm/cache/EntityCachesTest.java +++ b/orm/src/test/java/com/zfoo/orm/cache/EntityCachesTest.java @@ -36,7 +36,7 @@ public class EntityCachesTest { @Test - public void test() { + public void loadTest() { var context = new ClassPathXmlApplicationContext("application.xml"); // 每次运行前先删除数据库 @@ -65,33 +65,29 @@ public class EntityCachesTest { } @Test - public void testLoadOrInsert() { + public void loadOrCreateTest() { var context = new ClassPathXmlApplicationContext("application.xml"); + // 每次运行前先删除数据库 + var collection = OrmContext.getOrmManager().getCollection(UserEntity.class); + collection.drop(); + + @SuppressWarnings("unchecked") + var userEntityCaches = (IEntityCache) OrmContext.getOrmManager().getEntityCaches(UserEntity.class); + // 动态去拿到UserEntity的EntityCaches - @SuppressWarnings("unchecked") - var mailEntityCaches = (IEntityCache) OrmContext.getOrmManager().getEntityCaches(MailEntity.class); - for (var i = 1; i <= 10; i++) { - var entity = mailEntityCaches.loadOrCreate(String.valueOf(i)); - entity.setContent("msg:" + i); - mailEntityCaches.update(entity); + for (var j = 1; j <= 10; j++) { + var entity = userEntityCaches.loadOrCreate((long) j); + entity.setE(StringUtils.format("update-{}-{}", i, j)); + entity.setC(j); + userEntityCaches.update(entity); + } + + ThreadUtils.sleep(60 * TimeUtils.MILLIS_PER_SECOND); } - - ThreadUtils.sleep(60 * TimeUtils.MILLIS_PER_SECOND); - - mailEntityCaches.load("1"); } - @Test - public void collectionTest() { - var context = new ClassPathXmlApplicationContext("application.xml"); - - var collection = OrmContext.getOrmManager().getCollection(UserEntity.class); - var result = collection.updateOne(Filters.eq("_id", 1), new Document("$inc", new Document("c", 1L))); - System.out.println(result); - ThreadUtils.sleep(Long.MAX_VALUE); - } public void batchInsert() { var listUser = new ArrayList(); diff --git a/orm/src/test/java/com/zfoo/orm/entity/wrapper/DoubleEntity.java b/orm/src/test/java/com/zfoo/orm/entity/wrapper/DoubleEntity.java new file mode 100644 index 00000000..6a368cc9 --- /dev/null +++ b/orm/src/test/java/com/zfoo/orm/entity/wrapper/DoubleEntity.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2020 The zfoo 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 com.zfoo.orm.entity.wrapper; + +import com.zfoo.orm.anno.EntityCache; +import com.zfoo.orm.anno.Id; +import com.zfoo.orm.anno.Version; +import com.zfoo.orm.model.IEntity; + +/** + * @author godotg + */ +@EntityCache +public class DoubleEntity implements IEntity { + + @Id + private double id; + + private String message; + + @Version + private long version; + + public DoubleEntity() { + } + + public DoubleEntity(double id, String message, long version) { + this.id = id; + this.message = message; + this.version = version; + } + + @Override + public Double id() { + return id; + } + + public double getId() { + return id; + } + + public void setId(double id) { + this.id = id; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public long getVersion() { + return version; + } + + public void setVersion(long version) { + this.version = version; + } + + @Override + public String toString() { + return "DoubleEntity{" + + "id=" + id + + ", message='" + message + '\'' + + ", version=" + version + + '}'; + } +} diff --git a/orm/src/test/java/com/zfoo/orm/entity/wrapper/FloatEntity.java b/orm/src/test/java/com/zfoo/orm/entity/wrapper/FloatEntity.java new file mode 100644 index 00000000..47af84ce --- /dev/null +++ b/orm/src/test/java/com/zfoo/orm/entity/wrapper/FloatEntity.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2020 The zfoo 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 com.zfoo.orm.entity.wrapper; + +import com.zfoo.orm.anno.EntityCache; +import com.zfoo.orm.anno.Id; +import com.zfoo.orm.anno.Version; +import com.zfoo.orm.model.IEntity; + +/** + * @author godotg + */ +@EntityCache +public class FloatEntity implements IEntity { + + @Id + private float id; + + private String message; + + @Version + private long version; + + public FloatEntity() { + } + + public FloatEntity(float id, String message, long version) { + this.id = id; + this.message = message; + this.version = version; + } + + @Override + public Float id() { + return id; + } + + public float getId() { + return id; + } + + public void setId(float id) { + this.id = id; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public long getVersion() { + return version; + } + + public void setVersion(long version) { + this.version = version; + } + + @Override + public String toString() { + return "FloatEntity{" + + "id=" + id + + ", message='" + message + '\'' + + ", version=" + version + + '}'; + } +} diff --git a/orm/src/test/java/com/zfoo/orm/entity/wrapper/IntEntity.java b/orm/src/test/java/com/zfoo/orm/entity/wrapper/IntEntity.java new file mode 100644 index 00000000..234360d5 --- /dev/null +++ b/orm/src/test/java/com/zfoo/orm/entity/wrapper/IntEntity.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2020 The zfoo 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 com.zfoo.orm.entity.wrapper; + +import com.zfoo.orm.anno.*; +import com.zfoo.orm.model.IEntity; + +import java.util.List; + +/** + * @author godotg + */ +@EntityCache +public class IntEntity implements IEntity { + + @Id + private int id; + + private String message; + + @Version + private long version; + + public IntEntity() { + } + + public IntEntity(int id, String message, long version) { + this.id = id; + this.message = message; + this.version = version; + } + + @Override + public Integer id() { + return id; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public long getVersion() { + return version; + } + + public void setVersion(long version) { + this.version = version; + } + + @Override + public String toString() { + return "IntEntity{" + + "id=" + id + + ", message='" + message + '\'' + + ", version=" + version + + '}'; + } +} diff --git a/orm/src/test/java/com/zfoo/orm/entity/wrapper/LongEntity.java b/orm/src/test/java/com/zfoo/orm/entity/wrapper/LongEntity.java new file mode 100644 index 00000000..f4809843 --- /dev/null +++ b/orm/src/test/java/com/zfoo/orm/entity/wrapper/LongEntity.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2020 The zfoo 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 com.zfoo.orm.entity.wrapper; + +import com.zfoo.orm.anno.EntityCache; +import com.zfoo.orm.anno.Id; +import com.zfoo.orm.anno.Version; +import com.zfoo.orm.model.IEntity; + +/** + * @author godotg + */ +@EntityCache +public class LongEntity implements IEntity { + + @Id + private long id; + + private String message; + + @Version + private long version; + + public LongEntity() { + } + + public LongEntity(long id, String message, long version) { + this.id = id; + this.message = message; + this.version = version; + } + + @Override + public Long id() { + return id; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public long getVersion() { + return version; + } + + public void setVersion(long version) { + this.version = version; + } + + @Override + public String toString() { + return "LongEntity{" + + "id=" + id + + ", message='" + message + '\'' + + ", version=" + version + + '}'; + } +} diff --git a/orm/src/test/java/com/zfoo/orm/entity/wrapper/ObjectIdEntity.java b/orm/src/test/java/com/zfoo/orm/entity/wrapper/ObjectIdEntity.java new file mode 100644 index 00000000..03a212f4 --- /dev/null +++ b/orm/src/test/java/com/zfoo/orm/entity/wrapper/ObjectIdEntity.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2020 The zfoo 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 com.zfoo.orm.entity.wrapper; + +import com.zfoo.orm.anno.EntityCache; +import com.zfoo.orm.anno.Id; +import com.zfoo.orm.anno.Version; +import com.zfoo.orm.model.IEntity; +import org.bson.types.ObjectId; + +/** + * @author godotg + */ +@EntityCache +public class ObjectIdEntity implements IEntity { + + @Id + private ObjectId id; + + private String message; + + @Version + private long version; + + public ObjectIdEntity() { + } + + public ObjectIdEntity(ObjectId id, String message, long version) { + this.id = id; + this.message = message; + this.version = version; + } + + @Override + public ObjectId id() { + return id; + } + + public ObjectId getId() { + return id; + } + + public void setId(ObjectId id) { + this.id = id; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public long getVersion() { + return version; + } + + public void setVersion(long version) { + this.version = version; + } + + @Override + public String toString() { + return "ObjectIdEntity{" + + "id=" + id + + ", message='" + message + '\'' + + ", version=" + version + + '}'; + } +} diff --git a/orm/src/test/java/com/zfoo/orm/entity/wrapper/StringEntity.java b/orm/src/test/java/com/zfoo/orm/entity/wrapper/StringEntity.java new file mode 100644 index 00000000..e9391cfd --- /dev/null +++ b/orm/src/test/java/com/zfoo/orm/entity/wrapper/StringEntity.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2020 The zfoo 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 com.zfoo.orm.entity.wrapper; + +import com.zfoo.orm.anno.EntityCache; +import com.zfoo.orm.anno.Id; +import com.zfoo.orm.anno.Version; +import com.zfoo.orm.model.IEntity; + +/** + * @author godotg + */ +@EntityCache +public class StringEntity implements IEntity { + + @Id + private String id; + + private String message; + + @Version + private long version; + + public StringEntity() { + } + + public StringEntity(String id, String message, long version) { + this.id = id; + this.message = message; + this.version = version; + } + + @Override + public String id() { + return id; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public long getVersion() { + return version; + } + + public void setVersion(long version) { + this.version = version; + } + + @Override + public String toString() { + return "StringEntity{" + + "id=" + id + + ", message='" + message + '\'' + + ", version=" + version + + '}'; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/util/FieldUtils.java b/protocol/src/main/java/com/zfoo/protocol/util/FieldUtils.java index 25ad2ac9..c25c7513 100644 --- a/protocol/src/main/java/com/zfoo/protocol/util/FieldUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/util/FieldUtils.java @@ -44,7 +44,7 @@ public abstract class FieldUtils { clazz.getDeclaredMethod(methodName); return methodName; } catch (NoSuchMethodException e) { - throw new RunException("field:[{}] has no getMethod or isMethod in class:[{}]", field.getName(), clazz.getCanonicalName()); + throw new RunException("field:[{}] has no get or set method in class:[{}]", field.getName(), clazz.getCanonicalName()); } }