package org.sfm.jdbc.spring;

import org.junit.Before;
import org.junit.Test;
import org.sfm.beans.DbObject;
import org.sfm.test.jdbc.DbHelper;
import org.sfm.utils.RowHandler;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;

import java.sql.SQLException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class JdbcTemplateCrudTest {
	
	JdbcTemplate template;
	
	@Before
	public void setUp() throws SQLException {
		template = new JdbcTemplate(new SingleConnectionDataSource(DbHelper.objectDb(), true));
	}

	@Test
	public void testCrud() throws SQLException {
		JdbcTemplateCrud<DbObject, Long> objectCrud =
				JdbcTemplateMapperFactory.newInstance()
						.<DbObject, Long>crud(DbObject.class, Long.class).to(template, "TEST_DB_OBJECT");
		DbObject object = DbObject.newInstance();


		assertNull(objectCrud.read(object.getId()));

		// create
		Long key =
				objectCrud.create(object, new RowHandler<Long>() {
					Long key;
					@Override
					public void handle(Long aLong) throws Exception {
						key = aLong;
					}
				}).key;

		assertNull(key);


		key = object.getId();
		// read
		assertEquals(object, objectCrud.read(key));

		object.setName("Udpdated");

		// update
		objectCrud.update(object);
		assertEquals(object, objectCrud.read(key));

		// delete
		objectCrud.delete(key);
		assertNull(objectCrud.read(key));

		objectCrud.create(DbObject.newInstance());
	}

}

@@ -133,24 +133,67 @@
 c.DbHelper;%0A
+import org.sfm.utils.ListCollectorHandler;%0A
 import org.s
@@ -205,32 +205,32 @@
 ils.RowHandler;%0A
-
 import org.sprin
@@ -365,16 +365,64 @@
 ception;
+%0Aimport java.util.Arrays;%0Aimport java.util.List;
 %0A%0Aimport
@@ -1533,32 +1533,32 @@
 ud.read(key));%0A%0A
-
 %09%09objectCrud.cre
@@ -1590,10 +1590,994 @@
 ));%0A%09%7D%0A%0A
+%0A%09@Test%0A%09public void testCrudBatch() throws SQLException %7B%0A%09%09JdbcTemplateCrud%3CDbObject, Long%3E objectCrud =%0A%09%09%09%09JdbcTemplateMapperFactory.newInstance()%0A%09%09%09%09%09%09.%3CDbObject, Long%3Ecrud(DbObject.class, Long.class).to(template, %22TEST_DB_OBJECT%22);%0A%09%09DbObject object = DbObject.newInstance();%0A%0A%0A%09%09assertNull(objectCrud.read(object.getId()));%0A%0A%09%09// create%0A%09%09final List%3CDbObject%3E objects = Arrays.asList(object);%0A%09%09Long key =%0A%09%09%09%09objectCrud.create(objects, new RowHandler%3CLong%3E() %7B%0A%09%09%09%09%09Long key;%0A%09%09%09%09%09@Override%0A%09%09%09%09%09public void handle(Long aLong) throws Exception %7B%0A%09%09%09%09%09%09key = aLong;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D).key;%0A%0A%09%09assertNull(key);%0A%0A%0A%09%09List%3CLong%3E keys = Arrays.asList(object.getId());%0A%09%09// read%0A%09%09assertEquals(objects, objectCrud.read(keys, new ListCollectorHandler%3CDbObject%3E()).getList());%0A%0A%09%09object.setName(%22Udpdated%22);%0A%0A%09%09// update%0A%09%09objectCrud.update(objects);%0A%09%09assertEquals(object, objectCrud.read(object.getId()));%0A%0A%09%09// delete%0A%09%09objectCrud.delete(keys);%0A%09%09assertNull(objectCrud.read(key));%0A%0A%09%7D%0A%0A
 %7D%0A

increase jdbc template coverage