From 6000c1f4274d9bb1b2ef9c0154b771ab59831163 Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Sun, 25 Jun 2023 23:09:40 +0800 Subject: [PATCH] [test] test case update --- .../stream/core/bean/BeanHelper.java | 22 +- .../mybatisplus/JsonFieldHandlerTest.java | 230 ++++++++++++- .../mybatisplus/QueryConditionTest.java | 304 ------------------ 3 files changed, 241 insertions(+), 315 deletions(-) delete mode 100644 stream-plugin/stream-plugin-mybatis-plus/src/test/java/org/dromara/streamquery/stream/plugin/mybatisplus/QueryConditionTest.java diff --git a/stream-core/src/main/java/org/dromara/streamquery/stream/core/bean/BeanHelper.java b/stream-core/src/main/java/org/dromara/streamquery/stream/core/bean/BeanHelper.java index 0f2f93438..ea671c915 100644 --- a/stream-core/src/main/java/org/dromara/streamquery/stream/core/bean/BeanHelper.java +++ b/stream-core/src/main/java/org/dromara/streamquery/stream/core/bean/BeanHelper.java @@ -125,20 +125,25 @@ public static String getGetterName(String propertyName) { * @param 对象类型 */ public static R copyProperties(T source, R target) { - if (source == null || target ==null) { + if (source == null || target == null) { return target; } Class sourceType = SerFunc., Class>cast().apply(source.getClass()); - Map, SerBiCons>> sourcePropertyGetterSetterMap = LambdaHelper.getPropertyGetterSetterMap(sourceType); + Map, SerBiCons>> sourcePropertyGetterSetterMap = + LambdaHelper.getPropertyGetterSetterMap(sourceType); Class targetType = SerFunc., Class>cast().apply(target.getClass()); - Map, SerBiCons>> targetPropertyGetterSetterMap = LambdaHelper.getPropertyGetterSetterMap(targetType); + Map, SerBiCons>> targetPropertyGetterSetterMap = + LambdaHelper.getPropertyGetterSetterMap(targetType); - for (Map.Entry, SerBiCons>> sourceEntry : sourcePropertyGetterSetterMap.entrySet()) { + for (Map.Entry, SerBiCons>> sourceEntry : + sourcePropertyGetterSetterMap.entrySet()) { String property = sourceEntry.getKey(); - Map.Entry, SerBiCons> sourceGetterSetter = sourceEntry.getValue(); - Map.Entry, SerBiCons> targetGetterSetter = targetPropertyGetterSetterMap.get(property); + Map.Entry, SerBiCons> sourceGetterSetter = + sourceEntry.getValue(); + Map.Entry, SerBiCons> targetGetterSetter = + targetPropertyGetterSetterMap.get(property); if (targetGetterSetter == null) { continue; @@ -150,7 +155,9 @@ public static R copyProperties(T source, R target) { LambdaExecutable sourceGetterLambda = LambdaHelper.resolve(sourceGetter); LambdaExecutable targetGetterLambda = LambdaHelper.resolve(targetGetter); - if (!Opp.of(sourceGetterLambda.getReturnType()).map(Type::getTypeName).equals(Opp.of(targetGetterLambda.getReturnType()).map(Type::getTypeName))) { + if (!Opp.of(sourceGetterLambda.getReturnType()) + .map(Type::getTypeName) + .equals(Opp.of(targetGetterLambda.getReturnType()).map(Type::getTypeName))) { continue; } @@ -161,7 +168,6 @@ public static R copyProperties(T source, R target) { return target; } - public static R copyProperties(T source, Class targetType) { R target = ReflectHelper.newInstance(targetType); if (Objects.isNull(source)) { diff --git a/stream-plugin/stream-plugin-mybatis-plus/src/test/java/org/dromara/streamquery/stream/plugin/mybatisplus/JsonFieldHandlerTest.java b/stream-plugin/stream-plugin-mybatis-plus/src/test/java/org/dromara/streamquery/stream/plugin/mybatisplus/JsonFieldHandlerTest.java index dd9e03fe9..f29a50e4b 100644 --- a/stream-plugin/stream-plugin-mybatis-plus/src/test/java/org/dromara/streamquery/stream/plugin/mybatisplus/JsonFieldHandlerTest.java +++ b/stream-plugin/stream-plugin-mybatis-plus/src/test/java/org/dromara/streamquery/stream/plugin/mybatisplus/JsonFieldHandlerTest.java @@ -18,6 +18,7 @@ import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.test.autoconfigure.MybatisPlusTest; @@ -34,8 +35,14 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import java.io.Serializable; +import java.util.List; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.*; + /** - * @author VampireAchao + * @author VampireAchao Cason * @since 2023/4/14 */ @MybatisPlusTest @@ -67,7 +74,219 @@ void test() { Assertions.assertEquals("阿超", dbUser.getName().getNickname()); } - public static class JsonFieldHandler extends AbstractJsonFieldHandler { + @Test + void eqTest() { + Name name = new Name(); + name.setUsername("VampireAchao"); + name.setNickname("阿超"); + + UserInfoWithJsonName user = new UserInfoWithJsonName(); + user.setName(name); + Database.saveFewSql(Lists.of(user)); + Database.updateFewSql(Lists.of(user)); + + QueryCondition wrapper = + QueryCondition.query(UserInfoWithJsonName.class).eq(UserInfoWithJsonName::getName, name); + val list = Database.list(wrapper); + + assertEquals(1, list.size(), "Query should return exactly one result"); + assertEquals( + name, list.get(0).getName(), "Returned user's name should match the expected name"); + } + + @Test + void activeEqTest() { + Name name = new Name(); + name.setUsername("Cason"); + name.setNickname("JAY"); + + UserInfoWithJsonName user = new UserInfoWithJsonName(); + user.setName(name); + + Database.saveFewSql(Lists.of(user)); + + QueryCondition wrapper = + QueryCondition.query(UserInfoWithJsonName.class) + .activeEq(UserInfoWithJsonName::getName, name); + + val list = Database.list(wrapper); + + assertEquals(1, list.size(), "Query should return exactly one result"); + assertEquals( + name.getUsername(), + list.get(0).getName().getUsername(), + "Returned user's username should match the expected username"); + } + + @Test + void orTest() { + Name name1 = new Name(); + name1.setUsername("Cason"); + name1.setNickname("JAY"); + + Name name2 = new Name(); + name2.setUsername("Alice"); + name2.setNickname("AL"); + + Name name3 = new Name(); + name3.setUsername("Bob"); + name3.setNickname("BB"); + + UserInfoWithJsonName user1 = new UserInfoWithJsonName(); + user1.setName(name1); + + UserInfoWithJsonName user2 = new UserInfoWithJsonName(); + user2.setName(name2); + + UserInfoWithJsonName user3 = new UserInfoWithJsonName(); + user3.setName(name3); + + Database.saveFewSql(Lists.of(user1, user2, user3)); + + QueryCondition wrapper = + (QueryCondition) + QueryCondition.query(UserInfoWithJsonName.class) + .in(UserInfoWithJsonName::getName, Lists.of(name1, name3)) + .or(i -> i.eq(UserInfoWithJsonName::getName, user2.getName())); + + val list = Database.list(wrapper); + + assertEquals(3, list.size(), "Query should return exactly two results"); + + List usernames = + list.stream().map(user -> user.getName().getUsername()).collect(Collectors.toList()); + + assertTrue( + usernames.contains(name1.getUsername()), + "Returned users should contain the first expected username"); + assertTrue( + usernames.contains(name3.getUsername()), + "Returned users should contain the third expected username"); + assertTrue( + usernames.contains(name2.getUsername()), + "Returned users should not contain the second username"); + } + + @Test + void InTest() { + Name name1 = new Name(); + name1.setUsername("Cason"); + name1.setNickname("JAY"); + + Name name2 = new Name(); + name2.setUsername("Alice"); + name2.setNickname("AL"); + + Name name3 = new Name(); + name3.setUsername("Bob"); + name3.setNickname("BB"); + + UserInfoWithJsonName user1 = new UserInfoWithJsonName(); + user1.setName(name1); + + UserInfoWithJsonName user2 = new UserInfoWithJsonName(); + user2.setName(name2); + + UserInfoWithJsonName user3 = new UserInfoWithJsonName(); + user3.setName(name3); + + Database.saveFewSql(Lists.of(user1, user2, user3)); + + QueryCondition wrapper = + QueryCondition.query(UserInfoWithJsonName.class) + .activeIn(UserInfoWithJsonName::getName, Lists.of(name1, name3)); + + val list = Database.list(wrapper); + + assertEquals(2, list.size(), "Query should return exactly two results"); + + List usernames = + list.stream().map(user -> user.getName().getUsername()).collect(Collectors.toList()); + + assertTrue( + usernames.contains(name1.getUsername()), + "Returned users should contain the first expected username"); + assertTrue( + usernames.contains(name3.getUsername()), + "Returned users should contain the third expected username"); + assertFalse( + usernames.contains(name2.getUsername()), + "Returned users should not contain the second username"); + } + + @Test + void activeInTest() { + Name name1 = new Name(); + name1.setUsername("Cason"); + name1.setNickname("JAY"); + + Name name2 = new Name(); + name2.setUsername("Alice"); + name2.setNickname("AL"); + + Name name3 = new Name(); + name3.setUsername("Bob"); + name3.setNickname("BB"); + + UserInfoWithJsonName user1 = new UserInfoWithJsonName(); + user1.setName(name1); + + UserInfoWithJsonName user2 = new UserInfoWithJsonName(); + user2.setName(name2); + + UserInfoWithJsonName user3 = new UserInfoWithJsonName(); + user3.setName(name3); + + Database.saveFewSql(Lists.of(user1, user2, user3)); + + QueryCondition wrapper = + QueryCondition.query(UserInfoWithJsonName.class) + .activeIn(UserInfoWithJsonName::getName, Lists.of(name1, name3)); + + val list = Database.list(wrapper); + + assertEquals(2, list.size(), "Query should return exactly two results"); + + List usernames = + list.stream().map(user -> user.getName().getUsername()).collect(Collectors.toList()); + + assertTrue( + usernames.contains(name1.getUsername()), + "Returned users should contain the first expected username"); + assertTrue( + usernames.contains(name3.getUsername()), + "Returned users should contain the third expected username"); + assertFalse( + usernames.contains(name2.getUsername()), + "Returned users should not contain the second username"); + } + + @Test + void selectTest() { + Name name = new Name(); + name.setUsername("VampireAchao"); + name.setNickname("阿超"); + + UserInfoWithJsonName user = new UserInfoWithJsonName(); + user.setName(name); + Database.saveFewSql(Lists.of(user)); + Database.updateFewSql(Lists.of(user)); + + LambdaQueryWrapper wrapper = + QueryCondition.query(UserInfoWithJsonName.class) + .select(UserInfoWithJsonName::getName) + .eq(UserInfoWithJsonName::getName, name); + val list = Database.list(wrapper); + assertEquals(1, list.size(), "Query should return exactly one result"); + + UserInfoWithJsonName dbUser = list.get(0); + assertEquals( + user.getName().getUsername(), dbUser.getName().getUsername(), "Username should match"); + assertEquals( + user.getName().getNickname(), dbUser.getName().getNickname(), "Nickname should match"); + } + + public static class JsonFieldHandler extends AbstractJsonFieldHandler { ObjectMapper objectMapper = new ObjectMapper(); @@ -94,8 +313,13 @@ static class UserInfoWithJsonName { } @Data - static class Name { + static class Name implements Serializable, Comparable { private String username; private String nickname; + + @Override + public int compareTo(Name o) { + return username.compareTo(o.username); + } } } diff --git a/stream-plugin/stream-plugin-mybatis-plus/src/test/java/org/dromara/streamquery/stream/plugin/mybatisplus/QueryConditionTest.java b/stream-plugin/stream-plugin-mybatis-plus/src/test/java/org/dromara/streamquery/stream/plugin/mybatisplus/QueryConditionTest.java deleted file mode 100644 index f3a145df6..000000000 --- a/stream-plugin/stream-plugin-mybatis-plus/src/test/java/org/dromara/streamquery/stream/plugin/mybatisplus/QueryConditionTest.java +++ /dev/null @@ -1,304 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.dromara.streamquery.stream.plugin.mybatisplus; - -import com.baomidou.mybatisplus.annotation.TableField; -import com.baomidou.mybatisplus.annotation.TableName; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.test.autoconfigure.MybatisPlusTest; -import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.Data; -import lombok.SneakyThrows; -import lombok.val; -import org.apache.ibatis.session.SqlSessionFactory; -import org.dromara.streamquery.stream.core.collection.Lists; -import org.dromara.streamquery.stream.core.lambda.function.SerSupp; -import org.dromara.streamquery.stream.plugin.mybatisplus.engine.handler.AbstractJsonFieldHandler; -import org.dromara.streamquery.stream.plugin.mybatisplus.engine.mapper.IGenerateMapper; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; - -import java.io.Serializable; -import java.util.List; -import java.util.stream.Collectors; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * @author Cason - * @date 2023-06-08 21:32 - */ -@MybatisPlusTest -public class QueryConditionTest { - @BeforeEach - void init(@Autowired SqlSessionFactory sqlSessionFactory) { - Database.buildMapper(sqlSessionFactory.getConfiguration(), UserInfoWithJsonName.class); - } - - @Test - void eqTest() { - Name name = new Name(); - name.setUsername("VampireAchao"); - name.setNickname("阿超"); - - UserInfoWithJsonName user = new UserInfoWithJsonName(); - user.setName(name); - Database.saveFewSql(Lists.of(user)); - Database.updateFewSql(Lists.of(user)); - - QueryCondition wrapper = - QueryCondition.query(UserInfoWithJsonName.class).eq(UserInfoWithJsonName::getName, name); - val list = Database.list(wrapper); - - assertEquals(1, list.size(), "Query should return exactly one result"); - assertEquals( - name, list.get(0).getName(), "Returned user's name should match the expected name"); - } - - @Test - void activeEqTest() { - Name name = new Name(); - name.setUsername("Cason"); - name.setNickname("JAY"); - - UserInfoWithJsonName user = new UserInfoWithJsonName(); - user.setName(name); - - Database.saveFewSql(Lists.of(user)); - - QueryCondition wrapper = - QueryCondition.query(UserInfoWithJsonName.class) - .activeEq(UserInfoWithJsonName::getName, name); - - val list = Database.list(wrapper); - - assertEquals(1, list.size(), "Query should return exactly one result"); - assertEquals( - name.getUsername(), - list.get(0).getName().getUsername(), - "Returned user's username should match the expected username"); - } - - @Test - void orTest() { - Name name1 = new Name(); - name1.setUsername("Cason"); - name1.setNickname("JAY"); - - Name name2 = new Name(); - name2.setUsername("Alice"); - name2.setNickname("AL"); - - Name name3 = new Name(); - name3.setUsername("Bob"); - name3.setNickname("BB"); - - UserInfoWithJsonName user1 = new UserInfoWithJsonName(); - user1.setName(name1); - - UserInfoWithJsonName user2 = new UserInfoWithJsonName(); - user2.setName(name2); - - UserInfoWithJsonName user3 = new UserInfoWithJsonName(); - user3.setName(name3); - - Database.saveFewSql(Lists.of(user1, user2, user3)); - - QueryCondition wrapper = - (QueryCondition) - QueryCondition.query(UserInfoWithJsonName.class) - .in(UserInfoWithJsonName::getName, Lists.of(name1, name3)) - .or(i -> i.eq(UserInfoWithJsonName::getName, user2.getName())); - - val list = Database.list(wrapper); - - assertEquals(3, list.size(), "Query should return exactly two results"); - - List usernames = - list.stream().map(user -> user.getName().getUsername()).collect(Collectors.toList()); - - assertTrue( - usernames.contains(name1.getUsername()), - "Returned users should contain the first expected username"); - assertTrue( - usernames.contains(name3.getUsername()), - "Returned users should contain the third expected username"); - assertTrue( - usernames.contains(name2.getUsername()), - "Returned users should not contain the second username"); - } - - @Test - void InTest() { - Name name1 = new Name(); - name1.setUsername("Cason"); - name1.setNickname("JAY"); - - Name name2 = new Name(); - name2.setUsername("Alice"); - name2.setNickname("AL"); - - Name name3 = new Name(); - name3.setUsername("Bob"); - name3.setNickname("BB"); - - UserInfoWithJsonName user1 = new UserInfoWithJsonName(); - user1.setName(name1); - - UserInfoWithJsonName user2 = new UserInfoWithJsonName(); - user2.setName(name2); - - UserInfoWithJsonName user3 = new UserInfoWithJsonName(); - user3.setName(name3); - - Database.saveFewSql(Lists.of(user1, user2, user3)); - - QueryCondition wrapper = - QueryCondition.query(UserInfoWithJsonName.class) - .activeIn(UserInfoWithJsonName::getName, Lists.of(name1, name3)); - - val list = Database.list(wrapper); - - assertEquals(2, list.size(), "Query should return exactly two results"); - - List usernames = - list.stream().map(user -> user.getName().getUsername()).collect(Collectors.toList()); - - assertTrue( - usernames.contains(name1.getUsername()), - "Returned users should contain the first expected username"); - assertTrue( - usernames.contains(name3.getUsername()), - "Returned users should contain the third expected username"); - assertFalse( - usernames.contains(name2.getUsername()), - "Returned users should not contain the second username"); - } - - @Test - void activeInTest() { - Name name1 = new Name(); - name1.setUsername("Cason"); - name1.setNickname("JAY"); - - Name name2 = new Name(); - name2.setUsername("Alice"); - name2.setNickname("AL"); - - Name name3 = new Name(); - name3.setUsername("Bob"); - name3.setNickname("BB"); - - UserInfoWithJsonName user1 = new UserInfoWithJsonName(); - user1.setName(name1); - - UserInfoWithJsonName user2 = new UserInfoWithJsonName(); - user2.setName(name2); - - UserInfoWithJsonName user3 = new UserInfoWithJsonName(); - user3.setName(name3); - - Database.saveFewSql(Lists.of(user1, user2, user3)); - - QueryCondition wrapper = - QueryCondition.query(UserInfoWithJsonName.class) - .activeIn(UserInfoWithJsonName::getName, Lists.of(name1, name3)); - - val list = Database.list(wrapper); - - assertEquals(2, list.size(), "Query should return exactly two results"); - - List usernames = - list.stream().map(user -> user.getName().getUsername()).collect(Collectors.toList()); - - assertTrue( - usernames.contains(name1.getUsername()), - "Returned users should contain the first expected username"); - assertTrue( - usernames.contains(name3.getUsername()), - "Returned users should contain the third expected username"); - assertFalse( - usernames.contains(name2.getUsername()), - "Returned users should not contain the second username"); - } - - @Test - void selectTest() { - Name name = new Name(); - name.setUsername("VampireAchao"); - name.setNickname("阿超"); - - UserInfoWithJsonName user = new UserInfoWithJsonName(); - user.setName(name); - Database.saveFewSql(Lists.of(user)); - Database.updateFewSql(Lists.of(user)); - - LambdaQueryWrapper wrapper = - QueryCondition.query(UserInfoWithJsonName.class) - .select(UserInfoWithJsonName::getName) - .eq(UserInfoWithJsonName::getName, name); - val list = Database.list(wrapper); - assertEquals(1, list.size(), "Query should return exactly one result"); - - UserInfoWithJsonName dbUser = list.get(0); - assertEquals( - user.getName().getUsername(), dbUser.getName().getUsername(), "Username should match"); - assertEquals( - user.getName().getNickname(), dbUser.getName().getNickname(), "Nickname should match"); - } - - public static class JsonFieldHandler extends AbstractJsonFieldHandler { - - ObjectMapper objectMapper = new ObjectMapper(); - - @Override - public Object parse(String json, TableInfo tableInfo, TableFieldInfo fieldInfo) { - Class fieldType = fieldInfo.getField().getType(); - return ((SerSupp) (() -> objectMapper.readValue(json, fieldType))).get(); - } - - @Override - @SneakyThrows - public String toJson(Object obj, TableInfo tableInfo, TableFieldInfo fieldInfo) { - return objectMapper.writeValueAsString(obj); - } - } - - @Data - @TableName(value = "user_info", autoResultMap = true) - static class UserInfoWithJsonName implements IGenerateMapper { - private Long id; - - @TableField(typeHandler = JsonFieldHandler.class) - private Name name; - } - - @Data - static class Name implements Serializable, Comparable { - private String username; - private String nickname; - - @Override - public int compareTo(Name o) { - - return this.username.compareTo(o.username); - } - } -}