package com.baomidou.mybatisplus.test.plugins.paginationInterceptor;

import java.io.Reader;
import java.sql.Connection;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.test.plugins.RandomUtils;
import com.baomidou.mybatisplus.test.plugins.paginationInterceptor.entity.PageUser;
import com.baomidou.mybatisplus.test.plugins.paginationInterceptor.service.PageUserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/plugins/paginationInterceptor.xml" })
public class PaginationInterceptorTest {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	@Autowired
	private PageUserService pageUserService;

	@Before
	public void setUp() throws Exception {
		SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession();
		Connection conn = session.getConnection();
		Reader reader = Resources.getResourceAsReader("com/baomidou/mybatisplus/test/plugins/paginationInterceptor/CreateDB.sql");
		ScriptRunner runner = new ScriptRunner(conn);
		runner.setLogWriter(null);
		runner.runScript(reader);
		reader.close();
		session.close();
	}

	@Test
	public void pageTest() {
		// 随机当前页和分页大小
		int size = RandomUtils.nextInt(1, 50);
		int current = RandomUtils.nextInt(1, 200 / size);
		System.err.println("当前页为:" + current + " 分页大小为" + size);
		//最基础分页
		
		Page<PageUser> page1 = new Page<PageUser>(current, size);
		Page<PageUser> result1 = pageUserService.selectPage(page1);
		Assert.assertTrue(!result1.getRecords().isEmpty());
		//带OrderBy
		
		Page<PageUser> page2 = new Page<PageUser>(current, size,"name");
		Page<PageUser> result2 = pageUserService.selectPage(page2);
		Assert.assertTrue(!result2.getRecords().isEmpty());
		//倒叙
		Page<PageUser> page3 = new Page<PageUser>(current, size);
		page3.setAsc(false);
		Page<PageUser> result3 = pageUserService.selectPage(page3);
		Assert.assertTrue(!result3.getRecords().isEmpty());
	}
}

@@ -1146,16 +1146,58 @@
 rvice;%0A%0A
+%09private int current;%0A%09private int size;%0A%0A
 %09@Before
@@ -1633,45 +1633,8 @@
 ();%0A
-%09%7D%0A%0A%09@Test%0A%09public void pageTest() %7B%0A
 %09%09//
@@ -1647,20 +1647,16 @@
 %E5%92%8C%E5%88%86%E9%A1%B5%E5%A4%A7%E5%B0%8F%0A%09%09
-int 
 size = R
@@ -1684,20 +1684,16 @@
  50);%0A%09%09
-int 
 current 
@@ -1793,21 +1793,62 @@
 e);%0A
+%09%7D%0A%0A%09@Test%0A%09public void pageSimpleTest() %7B%0A
 %09%09//
+ 
 %E6%9C%80%E5%9F%BA%E7%A1%80%E5%88%86%E9%A1%B5%0A
-%09%09%0A
 %09%09Pa
@@ -2023,23 +2023,66 @@
 ));%0A
-%09%09//%E5%B8%A6OrderBy%0A%09%09
+%0A%09%7D%0A%0A%09@Test%0A%09public void pageOrderByTest() %7B%0A%09%09// %E5%B8%A6OrderBy
 %0A%09%09P
@@ -2136,16 +2136,17 @@
 t, size,
+ 
 %22name%22);
@@ -2266,16 +2266,31 @@
 ));%0A%09%09//
+ %E6%B2%A1%E6%9C%89orderby%E4%BD%86%E6%98%AF%E8%AE%BE%E7%BD%AE%E4%BA%86
 %E5%80%92%E5%8F%99%0A%09%09Pag
@@ -2483,13 +2483,489 @@
 pty());%0A
+%09%09// %E6%9C%89orderby%E8%AE%BE%E7%BD%AE%E4%BA%86%E5%80%92%E5%8F%99%0A%09%09Page%3CPageUser%3E page4 = new Page%3CPageUser%3E(current, size, %22name%22);%0A%09%09page3.setAsc(false);%0A%09%09Page%3CPageUser%3E result4 = pageUserService.selectPage(page4);%0A%09%09Assert.assertTrue(!result4.getRecords().isEmpty());%0A%09%7D%0A%0A%09@Test%0A%09public void pageCountTest() %7B%0A%09%09// %E8%AE%BE%E7%BD%AE%E4%B8%8Dcount%0A%09%09Page%3CPageUser%3E page = new Page%3CPageUser%3E(current, size);%0A%09%09page.setSearchCount(false);%0A%09%09Page%3CPageUser%3E result = pageUserService.selectPage(page);%0A%09%09Assert.assertTrue(result.getTotal() == 0);%0A
 %09%7D%0A%7D%0A

添加分页初步测试,不好测