Browse Source

优化test

zhouhao 7 years ago
parent
commit
d156c6d809
11 changed files with 539 additions and 21 deletions
  1. 22 0
      hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/pom.xml
  2. 99 0
      hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/AbstractTreeSortServiceTests.java
  3. 29 0
      hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/EnableCacheAllEvictTestService.java
  4. 29 0
      hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/EnableCacheAllEvictTreeTestService.java
  5. 28 0
      hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/EnableCacheTestService.java
  6. 150 0
      hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/EnableCacheTests.java
  7. 28 0
      hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/EnableCacheTreeTestService.java
  8. 74 16
      hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/GenericEntityServiceTest.java
  9. 41 0
      hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/SpringTestApplication.java
  10. 12 5
      hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/TestEntity.java
  11. 27 0
      hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/TestTreeEntityService.java

+ 22 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/pom.xml

@@ -56,5 +56,27 @@
             <artifactId>hsweb-commons-dao-api</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>javax.el</groupId>
+            <artifactId>javax.el-api</artifactId>
+            <version>3.0.0</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-concurrent-cache</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-aspects</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

+ 99 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/AbstractTreeSortServiceTests.java

@@ -0,0 +1,99 @@
+package org.hswebframework.web.service;
+
+import com.alibaba.fastjson.JSON;
+import org.hswebframework.web.commons.entity.factory.MapperEntityFactory;
+import org.hswebframework.web.commons.entity.param.QueryParamEntity;
+import org.hswebframework.web.dao.CrudDao;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.stubbing.Answer;
+
+import javax.validation.Validation;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.*;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.when;
+
+/**
+ * @author zhouhao
+ * @since 3.0
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class AbstractTreeSortServiceTests {
+
+    @InjectMocks
+    private TestTreeEntityService entityService = new TestTreeEntityService();
+
+    @Mock
+    private CrudDao<TestEntity, String> dao;
+
+    private List<TestEntity> entities = new ArrayList<>();
+
+    @Before
+    public void init() {
+        entityService.setEntityFactory(new MapperEntityFactory());
+
+        TestEntity entity = TestEntity.builder()
+                .age((byte) 10)
+                .enabled(true)
+                .name("test")
+                .build();
+        entity.setId("testId");
+
+        when(dao.query(any()))
+                .thenReturn(new ArrayList<>());
+
+        when(dao.count(any())).thenReturn(1);
+
+//        when(dao.update(any())).thenReturn(1);
+
+        when(dao.delete(any())).thenReturn(1);
+        when(dao.deleteByPk("test")).thenReturn(1);
+
+        //  doNothing().when(dao).insert(anyObject());
+        doAnswer(invocationOnMock -> {
+            TestEntity t = invocationOnMock.getArgumentAt(0, TestEntity.class);
+            entities.add(t);
+            return t.getId();
+        }).when(dao).insert(anyObject());
+
+        doAnswer(invocationOnMock -> {
+            TestEntity t = invocationOnMock.getArgumentAt(0, TestEntity.class);
+            entities.add(t);
+            return t.getId();
+        }).when(dao).update(anyObject());
+    }
+
+    @Test
+    public void testBatchInsert() {
+        String treeJson = "{'id':'1','parentId':'-1','name':'父节点','children':[" +
+                "{'id':'101','parentId':'1','name':'子节点1'}," +
+                "{'id':'102','parentId':'1','name':'子节点2'}" +
+                "]}";
+        TestEntity entity = JSON.parseObject(treeJson, TestEntity.class);
+
+        entityService.insert(entity);
+
+        Assert.assertEquals(entities.size(), 3);
+        entities.clear();
+
+        entityService.updateByPk(entity);
+        Assert.assertEquals(entities.size(), 3);
+        entities.clear();
+
+        entityService.updateBatch(Arrays.asList(entity));
+        Assert.assertEquals(entities.size(), 3);
+    }
+}

+ 29 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/EnableCacheAllEvictTestService.java

@@ -0,0 +1,29 @@
+package org.hswebframework.web.service;
+
+import org.hswebframework.web.dao.CrudDao;
+import org.hswebframework.web.id.IDGenerator;
+import org.springframework.cache.annotation.CacheConfig;
+
+/**
+ * @author zhouhao
+ * @since 1.0
+ */
+@CacheConfig(cacheNames = "test-2-entity")
+public class EnableCacheAllEvictTestService extends EnableCacheAllEvictGenericEntityService<TestEntity, String> {
+    private CrudDao<TestEntity, String> dao;
+
+    @Override
+    protected IDGenerator<String> getIDGenerator() {
+        return IDGenerator.MD5;
+    }
+
+    @Override
+    public CrudDao<TestEntity, String> getDao() {
+        return dao;
+    }
+
+    public void setDao(CrudDao<TestEntity, String> dao) {
+        this.dao = dao;
+    }
+
+}

+ 29 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/EnableCacheAllEvictTreeTestService.java

@@ -0,0 +1,29 @@
+package org.hswebframework.web.service;
+
+import org.hswebframework.web.dao.CrudDao;
+import org.hswebframework.web.id.IDGenerator;
+import org.springframework.cache.annotation.CacheConfig;
+
+/**
+ * @author zhouhao
+ * @since 1.0
+ */
+@CacheConfig(cacheNames = "test-2-tree-entity")
+public class EnableCacheAllEvictTreeTestService extends EnableCacheAllEvictTreeSortService<TestEntity, String> {
+    private CrudDao<TestEntity, String> dao;
+
+    @Override
+    protected IDGenerator<String> getIDGenerator() {
+        return IDGenerator.MD5;
+    }
+
+    @Override
+    public CrudDao<TestEntity, String> getDao() {
+        return dao;
+    }
+
+    public void setDao(CrudDao<TestEntity, String> dao) {
+        this.dao = dao;
+    }
+
+}

+ 28 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/EnableCacheTestService.java

@@ -0,0 +1,28 @@
+package org.hswebframework.web.service;
+
+import org.hswebframework.web.dao.CrudDao;
+import org.hswebframework.web.id.IDGenerator;
+import org.springframework.cache.annotation.CacheConfig;
+
+/**
+ * @author zhouhao
+ * @since 1.0
+ */
+@CacheConfig(cacheNames = "test-entity")
+public class EnableCacheTestService extends EnableCacheGenericEntityService<TestEntity, String> {
+    private CrudDao<TestEntity, String> dao;
+
+    @Override
+    protected IDGenerator<String> getIDGenerator() {
+        return IDGenerator.MD5;
+    }
+
+    @Override
+    public CrudDao<TestEntity, String> getDao() {
+        return dao;
+    }
+
+    public void setDao(CrudDao<TestEntity, String> dao) {
+        this.dao = dao;
+    }
+}

+ 150 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/EnableCacheTests.java

@@ -0,0 +1,150 @@
+package org.hswebframework.web.service;
+
+import org.hswebframework.web.commons.entity.factory.MapperEntityFactory;
+import org.hswebframework.web.dao.CrudDao;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.theories.suppliers.TestedOn;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.stubbing.Answer;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.ApplicationContext;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import javax.validation.Validation;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+/**
+ * @author zhouhao
+ * @since 3.0
+ */
+@SpringBootTest(classes = SpringTestApplication.class)
+@RunWith(value = SpringRunner.class)
+public class EnableCacheTests {
+
+    @Autowired
+    private EnableCacheTestService enableCacheTestService;
+
+    @Autowired
+    private EnableCacheAllEvictTestService     enableCacheAllEvictTestService;
+    @Autowired
+    private EnableCacheTreeTestService         enableCacheTreeTestService;
+    @Autowired
+    private EnableCacheAllEvictTreeTestService enableCacheAllEvictTreeTestService;
+
+    private AtomicInteger counter = new AtomicInteger();
+
+    @Before
+    public void init() {
+        CrudDao<TestEntity, String> dao = Mockito.mock(CrudDao.class);
+        enableCacheTestService.setEntityFactory(new MapperEntityFactory());
+
+        TestEntity entity = TestEntity.builder()
+                .age((byte) 10)
+                .enabled(true)
+                .name("test")
+                .build();
+        entity.setId("testId");
+
+        when(dao.query(any()))
+                .then((Answer<List<TestEntity>>) invocationOnMock -> {
+                    //模拟命中数据库
+                    counter.incrementAndGet();
+                    return new ArrayList<>(Arrays.asList(entity));
+                });
+
+        when(dao.count(any())).then((Answer<Integer>) invocationOnMock -> {
+            //模拟命中数据库
+            counter.incrementAndGet();
+            return 1;
+        });
+
+        when(dao.update(any())).thenReturn(1);
+
+        when(dao.delete(any())).thenReturn(1);
+        when(dao.deleteByPk(anyString())).thenReturn(1);
+
+        doNothing().when(dao).insert(anyObject());
+
+        enableCacheTestService.setDao(dao);
+
+        enableCacheAllEvictTestService.setDao(dao);
+
+        enableCacheTreeTestService.setDao(dao);
+
+        enableCacheAllEvictTreeTestService.setDao(dao);
+    }
+
+    @Test
+    public void testSimpleCacheEnableService() {
+        doTest(enableCacheTestService);
+    }
+
+    @Test
+    public void testEnableCacheAllEvictTestService() {
+        doTest(enableCacheAllEvictTestService);
+    }
+
+    @Test
+    public void testEnableCacheTreeTestService() {
+        doTest(enableCacheTreeTestService);
+    }
+
+    @Test
+    public void testEnableCacheAllEvictTreeTestService() {
+        doTest(enableCacheAllEvictTreeTestService);
+    }
+
+    public void doTest(CrudService<TestEntity, String> service) {
+        service.selectByPk("testId"); //db 1
+        service.selectByPk("testId");//cache
+        Assert.assertEquals(counter.get(), 1);
+
+        service.select(); //db 2
+        service.select();//cache
+        Assert.assertEquals(counter.get(), 2);
+
+        service.count(); //db 3
+        service.count(); //cache
+        Assert.assertEquals(counter.get(), 3);
+
+        service.updateByPk("testId", new TestEntity()); //evict cache
+
+        service.select(); //db 4
+        service.selectByPk("testId");//db 5
+        service.count();//db 6
+
+        service.select(); //cache
+        service.selectByPk("testId");//cache
+        service.count();//cache
+        Assert.assertEquals(counter.get(), 6);
+
+        service.deleteByPk("testId"); //evict cache
+        if (service instanceof TreeService){
+            //树结构会先查询后再执行删除
+            counter.decrementAndGet();
+        }
+        service.select(); //db 7
+        service.selectByPk("testId");//db 8
+        service.count();//db 9
+
+        service.select(); //cache
+        service.selectByPk("testId");//cache
+        service.count();//cache
+        Assert.assertEquals(counter.get(), 9);
+
+    }
+
+
+}

+ 28 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/EnableCacheTreeTestService.java

@@ -0,0 +1,28 @@
+package org.hswebframework.web.service;
+
+import org.hswebframework.web.dao.CrudDao;
+import org.hswebframework.web.id.IDGenerator;
+import org.springframework.cache.annotation.CacheConfig;
+
+/**
+ * @author zhouhao
+ * @since 1.0
+ */
+@CacheConfig(cacheNames = "test-tree-entity")
+public class EnableCacheTreeTestService extends EnableCacheTreeSortService<TestEntity, String> {
+    private CrudDao<TestEntity, String> dao;
+
+    @Override
+    protected IDGenerator<String> getIDGenerator() {
+        return IDGenerator.MD5;
+    }
+
+    @Override
+    public CrudDao<TestEntity, String> getDao() {
+        return dao;
+    }
+
+    public void setDao(CrudDao<TestEntity, String> dao) {
+        this.dao = dao;
+    }
+}

+ 74 - 16
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/GenericEntityServiceTest.java

@@ -1,23 +1,24 @@
 package org.hswebframework.web.service;
 
 import org.hswebframework.web.commons.entity.PagerResult;
+import org.hswebframework.web.commons.entity.factory.MapperEntityFactory;
 import org.hswebframework.web.commons.entity.param.QueryParamEntity;
 import org.hswebframework.web.dao.CrudDao;
+import org.hswebframework.web.validate.ValidationException;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
-import org.mockito.invocation.InvocationOnMock;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.mockito.stubbing.Answer;
 
+import javax.validation.Validation;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
-import static org.junit.Assert.*;
 import static org.mockito.Mockito.*;
 
 /**
@@ -37,39 +38,96 @@ public class GenericEntityServiceTest {
 
     @Before
     public void init() {
+        entityService.setEntityFactory(new MapperEntityFactory());
+        entityService.setValidator(Validation.buildDefaultValidatorFactory().getValidator());
 
-        when(dao.query(queryParamEntity)).then((Answer<List<TestEntity>>) invocationOnMock -> new ArrayList<>(Arrays.asList(TestEntity.builder()
+        TestEntity entity = TestEntity.builder()
                 .age((byte) 10)
                 .enabled(true)
                 .name("test")
-                .build())));
+                .build();
+        entity.setId("testId");
+
+        when(dao.query(any()))
+                .then((Answer<List<TestEntity>>) invocationOnMock -> new ArrayList<>(Arrays.asList(entity)));
+
+        when(dao.count(any())).thenReturn(1);
+
+        when(dao.update(any())).thenReturn(1);
+
+        when(dao.delete(any())).thenReturn(1);
+        when(dao.deleteByPk("test")).thenReturn(1);
+
+        doNothing().when(dao).insert(anyObject());
+
+    }
+
+    @Test
+    public void testSimple() {
+        Assert.assertEquals(entityService.getEntityType(), TestEntity.class);
 
-        when(dao.count(queryParamEntity)).thenReturn(1);
+        Assert.assertEquals(entityService.getEntityInstanceType(), TestEntity.class);
 
-        doAnswer(invocationOnMock -> {
-            Assert.assertNotEquals(invocationOnMock.getArguments().length, 1);
-            Assert.assertNotNull(invocationOnMock.getArguments()[0]);
-            return null;
-        }).when(dao).insert(anyObject());
+        Assert.assertEquals(entityService.getPrimaryKeyType(), String.class);
     }
 
     @Test
     public void testQuery() {
         PagerResult<TestEntity> result = entityService.selectPager(queryParamEntity);
-
         Assert.assertEquals(result.getTotal(), 1);
         Assert.assertEquals(result.getData().size(), 1);
 
+        TestEntity entity = entityService.selectByPk(result.getData().get(0).getId());
+        Assert.assertNotNull(entity);
+
+        List<TestEntity> testEntities = entityService.selectByPk(Arrays.asList(result.getData().get(0).getId()));
+        Assert.assertTrue(!testEntities.isEmpty());
+    }
+
+
+    @Test
+    public void testInsert() {
         TestEntity testEntity = TestEntity.builder()
                 .age((byte) 1)
                 .enabled(true)
-                .name("测试")
+//                .name("测试")
                 .build();
+        try {
+            entityService.insert(testEntity);
+            Assert.assertFalse(true);
+        } catch (ValidationException e) {
+            Assert.assertFalse(e.getResults().isEmpty());
+            Assert.assertEquals(e.getResults().get(0).getField(), "name");
+            testEntity.setId(null);
+        }
+        testEntity.setName("测试");
+        String id = entityService.insert(testEntity);
+        Assert.assertNotNull(id);
+    }
 
-        entityService.insert(testEntity);
-
-        Assert.assertNotNull(testEntity.getId());
+    @Test
+    public void testUpdate() {
+        TestEntity testEntity = TestEntity.builder()
+                .age((byte) 1)
+                .enabled(true)
+                .name("测试")
+                .build();
+        testEntity.setId("testEntity");
+
+        int i = entityService.updateByPk("testEntity", testEntity);
+        entityService.updateByPk(testEntity);
+        entityService.updateByPk(Arrays.asList(testEntity));
+        String id = entityService.saveOrUpdate(testEntity);
+        Assert.assertEquals(id, testEntity.getId());
+        Assert.assertEquals(i, 1);
+    }
 
-        System.out.println(result.getTotal());
+    @Test
+    public void testDelete() {
+        int i = entityService.deleteByPk("test");
+        Assert.assertEquals(i, 1);
+        i = entityService.deleteByPk("test2");
+        Assert.assertEquals(i, 0);
     }
+
 }

+ 41 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/SpringTestApplication.java

@@ -0,0 +1,41 @@
+package org.hswebframework.web.service;
+
+import org.hswebframework.web.dao.CrudDao;
+import org.junit.Before;
+import org.mockito.Mockito;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
+
+/**
+ * @author zhouhao
+ * @since 3.0
+ */
+@SpringBootApplication
+@EnableCaching
+@Configuration
+@EnableAspectJAutoProxy(proxyTargetClass = true)
+public class SpringTestApplication {
+
+    @Bean
+    public EnableCacheTestService enableCacheTestService() {
+        return new EnableCacheTestService();
+    }
+
+    @Bean
+    public EnableCacheAllEvictTestService enableCacheAllEvictTestService() {
+        return new EnableCacheAllEvictTestService();
+    }
+
+    @Bean
+    public EnableCacheTreeTestService enableCacheTreeTestService() {
+        return new EnableCacheTreeTestService();
+    }
+
+    @Bean
+    public EnableCacheAllEvictTreeTestService enableCacheAllEvictTreeTestService() {
+        return new EnableCacheAllEvictTreeTestService();
+    }
+}

+ 12 - 5
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/TestEntity.java

@@ -1,23 +1,30 @@
 package org.hswebframework.web.service;
 
-import lombok.AllArgsConstructor;
-import lombok.Builder;
-import lombok.Data;
-import lombok.NoArgsConstructor;
+import lombok.*;
+import org.hibernate.validator.constraints.NotBlank;
 import org.hswebframework.web.commons.entity.SimpleGenericEntity;
+import org.hswebframework.web.commons.entity.SimpleTreeSortSupportEntity;
+import org.hswebframework.web.validator.group.CreateGroup;
+
+import java.util.List;
 
 /**
  * @author zhouhao
  * @since 1.0
  */
+@EqualsAndHashCode(callSuper = true)
 @Data
 @Builder
 @NoArgsConstructor
 @AllArgsConstructor
-public class TestEntity extends SimpleGenericEntity<String> {
+public class TestEntity extends SimpleTreeSortSupportEntity<String> {
+
+    @NotBlank(groups = CreateGroup.class)
     private String name;
 
     private Byte age;
 
     private Boolean enabled;
+
+    private List<TestEntity> children;
 }

+ 27 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/test/java/org/hswebframework/web/service/TestTreeEntityService.java

@@ -0,0 +1,27 @@
+package org.hswebframework.web.service;
+
+import org.hswebframework.web.dao.CrudDao;
+import org.hswebframework.web.id.IDGenerator;
+
+/**
+ * @author zhouhao
+ * @since 3.0
+ */
+public class TestTreeEntityService extends AbstractTreeSortService<TestEntity, String> {
+
+    private CrudDao<TestEntity, String> dao;
+
+    @Override
+    protected IDGenerator<String> getIDGenerator() {
+        return IDGenerator.MD5;
+    }
+
+    @Override
+    public CrudDao<TestEntity, String> getDao() {
+        return dao;
+    }
+
+    public void setDao(CrudDao<TestEntity, String> dao) {
+        this.dao = dao;
+    }
+}