package org.crazycake.jdbcTemplateTool;

import static org.crazycake.ScaffoldUnit.ScaffoldUnit.build;
import static org.crazycake.ScaffoldUnit.ScaffoldUnit.dbAssertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.crazycake.ScaffoldUnit.ScaffoldUnit;
import org.crazycake.jdbcTemplateTool.exception.NoColumnAnnotationFoundException;
import org.crazycake.jdbcTemplateTool.exception.NoIdAnnotationFoundException;
import org.crazycake.jdbcTemplateTool.model.Employee;
import org.crazycake.jdbcTemplateTool.model.Person;
import org.crazycake.jdbcTemplateTool.model.Student;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration(locations={"classpath:spring.xml"})
public class JdbcTemplateToolTest extends AbstractJUnit4SpringContextTests{

	@Test
	public void testList() throws IOException, SQLException {
		build();
		JdbcTemplateTool jtt = super.applicationContext.getBean("jdbcTemplateTool",JdbcTemplateTool.class);
		List<Employee> es = jtt.list("select * from employee where age < ? order by id desc", new Object[]{30}, Employee.class);
		
		assertThat(new Integer(es.size()),is(2));
		
		assertThat(es.get(1).getName(),is("tim"));
	}

	@Test
	public void testCount() throws IOException, SQLException {
		build();
		
		JdbcTemplateTool jtt = super.applicationContext.getBean("jdbcTemplateTool",JdbcTemplateTool.class);
		
		int total = jtt.count("select count(1) from employee", null);
		assertThat(total,is(3));
	}

	@Test
	public void testGet() throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException, IOException, SQLException {
		
		build();
		
		
		JdbcTemplateTool jtt = super.applicationContext.getBean("jdbcTemplateTool",JdbcTemplateTool.class);
		
		Employee e = jtt.get(Employee.class, 3);
		assertThat(e.getName(),is("jacob"));
	}
	
	@Test
    public void testGet2() throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException, IOException, SQLException {
        
        build();
        
        JdbcTemplateTool jtt = super.applicationContext.getBean("jdbcTemplateTool",JdbcTemplateTool.class);
        List<Person> personList = jtt.list("select * from person2", null, Person.class);
        assertThat(personList.size(),is(1));
        
        Person e = jtt.get(Person.class, 1);
        assertThat(e.getName(),is("jackie"));
    }

	@Test
	public void testUpdate() throws Exception {
		build();
		
		JdbcTemplateTool jtt = super.applicationContext.getBean("jdbcTemplateTool",JdbcTemplateTool.class);
		
		Employee e = jtt.get(Employee.class, 1);
		e.setAge(23);
		jtt.update(e);
		ScaffoldUnit.dbAssertThat("select age from employee where name='jack'", is(23));
	}

	@Test
	public void testBatchUpdate() throws SQLException, IOException {
		build();
		
		JdbcTemplateTool jtt = super.applicationContext.getBean("jdbcTemplateTool",JdbcTemplateTool.class);
		
		List<Object[]> params = new ArrayList<Object[]>();
		Object[] p1 = new Object[]{23,"jack"};
		params.add(p1);
		Object[] p2 = new Object[]{29,"tim"};
		params.add(p2);
		
		jtt.batchUpdate("update employee set age = ? where name = ?", params);
		
		ScaffoldUnit.dbAssertThat("select age from employee where name='jack'", is(23));
		ScaffoldUnit.dbAssertThat("select age from employee where name='tim'", is(29));
	}

	@Test
	public void testSave() throws Exception {
		build();
		
		JdbcTemplateTool jtt = super.applicationContext.getBean("jdbcTemplateTool",JdbcTemplateTool.class);
		
		Employee e = new Employee();
		e.setId(4);
		e.setName("billy");
		Date now = new Date();
		e.setJoinDate(new Timestamp(now.getTime()));
		e.setAge(33);
		
		jtt.save(e);
		
		dbAssertThat("select name from employee where id=4", is("billy"));
	}
	
	@Test
	public void testSaveWithoutSomeField() throws Exception {
		build();
		
		JdbcTemplateTool jtt = super.applicationContext.getBean("jdbcTemplateTool",JdbcTemplateTool.class);
		
		Student s = new Student();
		s.setName("michael");
		s.setNothing("nothing");
		jtt.save(s);
		
		dbAssertThat("select name from student where name = 'michael'", is("michael"));
	}

	@Test
	public void testDelete() throws Exception {
		build();
		
		JdbcTemplateTool jtt = super.applicationContext.getBean("jdbcTemplateTool",JdbcTemplateTool.class);
		Employee e = new Employee();
		e.setId(1);
		jtt.delete(e);
		dbAssertThat("select name from employee where id=1", nullValue());
	}

}

@@ -848,24 +848,81 @@
 junit.Test;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.s
@@ -1188,16 +1188,109 @@
 Tests%7B%0A%0A
+    private static Logger logger = LoggerFactory.getLogger(JdbcTemplateToolTest.class);%0A    %0A
 %09@Test%0A%09
@@ -2710,24 +2710,74 @@
 e(),is(1));%0A
+        logger.info(personList.get(0).getName());%0A
         %0A   

add some debug code