package com.baomidou.mybatisplus.test.h2;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.sql.DataSource;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baomidou.mybatisplus.test.h2.entity.mapper.H2UserMetaobjMapper;
import com.baomidou.mybatisplus.test.h2.entity.persistent.H2UserMetaObj;

/**
 * <p>
 * 测试元对象填充器
 * </p>
 *
 * @author yuxiaobin
 * @date 2017/5/31
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:h2/spring-test-h2-metaobj.xml"})
public class H2MetaObjectHandlerTest extends H2Test {

    @Autowired
    private H2UserMetaobjMapper userMapper;

    @BeforeClass
    public static void initDB() throws SQLException, IOException {
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:h2/spring-test-h2-metaobj.xml");
        DataSource ds = (DataSource) context.getBean("dataSource");
        try (Connection conn = ds.getConnection()) {
            String createTableSql = readFile("user.ddl.sql");
            Statement stmt = conn.createStatement();
            stmt.execute(createTableSql);
            stmt.execute("truncate table h2user");
            executeSql(stmt, "user.insert.sql");
            conn.commit();
        }
    }

    @Test
    public void testMetaObjectHandler() {
        H2UserMetaObj user = new H2UserMetaObj();
        user.setName("metaobjtest");
        user.setVersion(1);
        user.setAge(12);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -1);
        user.setLastUpdatedDt(new Timestamp(cal.getTimeInMillis()));
        user.setDesc("abc");
        userMapper.insert(user);
        System.out.println("before update: getLastUpdatedDt=" + user.getLastUpdatedDt());
        Assert.assertNotNull(userMapper.selectById(user.getId()).getTestType());

        user.setName("999");
        userMapper.updateById(user);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        Long id = user.getId();
        H2UserMetaObj userDB = userMapper.selectById(id);

        //MyMetaObjectHandler.insertFill() : set default testType value=3
        Assert.assertEquals(3, userDB.getTestType().intValue());
        Assert.assertEquals("999", userDB.getName());

        Date lastUpdatedDt = userDB.getLastUpdatedDt();
        System.out.println("after update: testDate=" + lastUpdatedDt);
        String versionDateStr = sdf.format(lastUpdatedDt);
        //MyMetaObjectHandler.updateFill() : set lastUpdatedDt=currentTimestamp
        Assert.assertEquals(sdf.format(new Date()), versionDateStr);//before update: lastUpdatedDt=currentTimestamp-1day
    }

    @Test
    public void testMetaObjectHandlerNullInsert4Update() {
        H2UserMetaObj user = new H2UserMetaObj();
        user.setName("metaobjtest");
        user.setVersion(1);
        user.setAge(12);
        user.setDesc("abc");
        userMapper.insert(user);
        System.out.println("before update: getLastUpdatedDt=" + user.getLastUpdatedDt());
        Assert.assertNotNull(userMapper.selectById(user.getId()).getTestType());
        user.setName("999");
        userMapper.updateById(user);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        Long id = user.getId();
        H2UserMetaObj userDB = userMapper.selectById(id);
        Date lastUpdatedDt = userDB.getLastUpdatedDt();
        System.out.println("after update: testDate=" + lastUpdatedDt);
        String versionDateStr = sdf.format(lastUpdatedDt);
        //MyMetaObjectHandler.updateFill() : set lastUpdatedDt=currentTimestamp
        Assert.assertEquals(sdf.format(new Date()), versionDateStr);//before update: lastUpdatedDt=currentTimestamp-1day
    }


    @Test
    public void testInsertMy() {
        String name = "testInsertMy";
        int version = 1;
        int row = userMapper.myInsertWithNameVersion(name, version);
        Assert.assertEquals(1, row);
    }

    @Test
    public void testUpdateMy() {
        H2UserMetaObj user = new H2UserMetaObj();
        user.setName("myUpdate");
        user.setVersion(1);
        userMapper.insert(user);
        Long id = user.getId();
        H2UserMetaObj dbUser = userMapper.selectById(id);
        Assert.assertNotNull(dbUser);
        Assert.assertEquals("myUpdate", dbUser.getName());

        Assert.assertEquals(1, userMapper.myUpdateWithNameId(id, "updateMy"));

        dbUser = userMapper.selectById(id);
        Assert.assertNotNull(dbUser);
        Assert.assertEquals("updateMy", dbUser.getName());
        Assert.assertEquals(1, user.getVersion().intValue());
    }

}

@@ -2508,110 +2508,8 @@
 r);%0A
-        SimpleDateFormat sdf = new SimpleDateFormat(%22yyyy-MM-dd HH%22);%0A        Long id = user.getId();%0A
     
@@ -2561,14 +2561,23 @@
 yId(
-id
+user.getId()
 );%0A
-%0A
     
@@ -2765,25 +2765,24 @@
 ame());%0A
-%0A
         
 Date las
@@ -2777,53 +2777,183 @@
     
-Date lastUpdatedDt = userDB.getLastUpdatedDt(
+assertUpdateFill(userDB.getLastUpdatedDt());%0A    %7D%0A%0A    private void assertUpdateFill(Date lastUpdatedDt) %7B%0A        SimpleDateFormat sdf = new SimpleDateFormat(%22yyyy-MM-dd HH%22
 );%0A 
@@ -3801,110 +3801,8 @@
 r);%0A
-        SimpleDateFormat sdf = new SimpleDateFormat(%22yyyy-MM-dd HH%22);%0A        Long id = user.getId();%0A
     
@@ -3854,399 +3854,75 @@
 yId(
-id);%0A        Date lastUpdatedDt = 
 user
-DB
 .get
-LastUpdatedDt();%0A        System.out.println(%22after update: testDate=%22 + lastUpdatedDt);%0A        String versionDateStr = sdf.format(lastUpdatedDt);%0A        //MyMetaObjectHandler.updateFill() : set lastUpdatedDt=currentTimestamp%0A        Assert.assertEquals(sdf.format(new Date()), versionDateStr);//before update: lastUpdatedDt=currentTimestamp-1day
+Id());%0A        assertUpdateFill(userDB.getLastUpdatedDt());
 %0A   

enhance test case