SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-002-本章的源代码
0.结构
一、JDBC层
1.
1 package spittr.db; 2 3 import java.util.List; 4 5 import spittr.domain.Spitter; 6 7 /** 8 * Repository interface with operations for {@link Spitter} persistence. 9 * @author habuma 10 */ 11 public interface SpitterRepository { 12 13 long count(); 14 15 Spitter save(Spitter spitter); 16 17 Spitter findOne(long id); 18 19 Spitter findByUsername(String username); 20 21 List<Spitter> findAll(); 22 23 }
2.
1 package spittr.db; 2 3 import java.util.List; 4 5 import spittr.domain.Spittle; 6 7 /** 8 * Repository interface with operations for {@link Spittle} persistence. 9 * @author habuma 10 */ 11 public interface SpittleRepository { 12 13 long count(); 14 15 List<Spittle> findRecent(); 16 17 List<Spittle> findRecent(int count); 18 19 Spittle findOne(long id); 20 21 Spittle save(Spittle spittle); 22 23 List<Spittle> findBySpitterId(long spitterId); 24 25 void delete(long id); 26 27 }
3.
1 package spittr.db.jdbc; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 import org.springframework.jdbc.core.JdbcTemplate; 10 import org.springframework.jdbc.core.RowMapper; 11 import org.springframework.jdbc.core.simple.SimpleJdbcInsert; 12 13 import spittr.db.SpitterRepository; 14 import spittr.domain.Spitter; 15 16 public class JdbcSpitterRepository implements SpitterRepository { 17 18 private JdbcTemplate jdbcTemplate; 19 20 public JdbcSpitterRepository(JdbcTemplate jdbcTemplate) { 21 this.jdbcTemplate = jdbcTemplate; 22 } 23 24 public long count() { 25 //return jdbcTemplate.queryForLong("select count(id) from Spitter"); 26 return jdbcTemplate.queryForObject("select count(id) from Spitter", Long.class); 27 } 28 29 public Spitter save(Spitter spitter) { 30 Long id = spitter.getId(); 31 if (id == null) { 32 long spitterId = insertSpitterAndReturnId(spitter); 33 return new Spitter(spitterId, spitter.getUsername(), spitter.getPassword(), spitter.getFullName(), spitter.getEmail(), spitter.isUpdateByEmail()); 34 } else { 35 jdbcTemplate.update("update Spitter set username=?, password=?, fullname=?, email=?, updateByEmail=? where id=?", 36 spitter.getUsername(), 37 spitter.getPassword(), 38 spitter.getFullName(), 39 spitter.getEmail(), 40 spitter.isUpdateByEmail(), 41 id); 42 } 43 return spitter; 44 } 45 46 /** 47 * Inserts a spitter using SimpleJdbcInsert. 48 * Involves no direct SQL and is able to return the ID of the newly created Spitter. 49 * @param spitter a Spitter to insert into the databse 50 * @return the ID of the newly inserted Spitter 51 */ 52 private long insertSpitterAndReturnId(Spitter spitter) { 53 SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName("Spitter"); 54 jdbcInsert.setGeneratedKeyName("id"); 55 Map<String, Object> args = new HashMap<String, Object>(); 56 args.put("username", spitter.getUsername()); 57 args.put("password", spitter.getPassword()); 58 args.put("fullname", spitter.getFullName()); 59 args.put("email", spitter.getEmail()); 60 args.put("updateByEmail", spitter.isUpdateByEmail()); 61 long spitterId = jdbcInsert.executeAndReturnKey(args).longValue(); 62 return spitterId; 63 } 64 65 /** 66 * Inserts a spitter using a simple JdbcTemplate update() call. 67 * Does not return the ID of the newly created Spitter. 68 * @param spitter a Spitter to insert into the database 69 */ 70 @SuppressWarnings("unused") 71 private void insertSpitter(Spitter spitter) { 72 jdbcTemplate.update(INSERT_SPITTER, 73 spitter.getUsername(), 74 spitter.getPassword(), 75 spitter.getFullName(), 76 spitter.getEmail(), 77 spitter.isUpdateByEmail()); 78 } 79 80 public Spitter findOne(long id) { 81 return jdbcTemplate.queryForObject( 82 SELECT_SPITTER + " where id=?", new SpitterRowMapper(), id); 83 } 84 85 public Spitter findByUsername(String username) { 86 return jdbcTemplate.queryForObject("select id, username, password, fullname, email, updateByEmail from Spitter where username=?", new SpitterRowMapper(), username); 87 } 88 89 public List<Spitter> findAll() { 90 return jdbcTemplate.query("select id, username, password, fullname, email, updateByEmail from Spitter order by id", new SpitterRowMapper()); 91 } 92 93 private static final class SpitterRowMapper implements RowMapper<Spitter> { 94 public Spitter mapRow(ResultSet rs, int rowNum) throws SQLException { 95 long id = rs.getLong("id"); 96 String username = rs.getString("username"); 97 String password = rs.getString("password"); 98 String fullName = rs.getString("fullname"); 99 String email = rs.getString("email"); 100 boolean updateByEmail = rs.getBoolean("updateByEmail"); 101 return new Spitter(id, username, password, fullName, email, updateByEmail); 102 } 103 } 104 105 private static final String INSERT_SPITTER = "insert into Spitter (username, password, fullname, email, updateByEmail) values (?, ?, ?, ?, ?)"; 106 107 private static final String SELECT_SPITTER = "select id, username, password, fullname, email, updateByEmail from Spitter"; 108 109 }
4.
1 package spittr.db.jdbc; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.Date; 6 import java.util.HashMap; 7 import java.util.List; 8 import java.util.Map; 9 10 import org.springframework.dao.EmptyResultDataAccessException; 11 import org.springframework.jdbc.core.JdbcTemplate; 12 import org.springframework.jdbc.core.RowMapper; 13 import org.springframework.jdbc.core.simple.SimpleJdbcInsert; 14 15 import spittr.db.SpittleRepository; 16 import spittr.domain.Spitter; 17 import spittr.domain.Spittle; 18 19 public class JdbcSpittleRepository implements SpittleRepository { 20 21 private static final String SELECT_SPITTLE = "select sp.id, s.id as spitterId, s.username, s.password, s.fullname, s.email, s.updateByEmail, sp.message, sp.postedTime from Spittle sp, Spitter s where sp.spitter = s.id"; 22 private static final String SELECT_SPITTLE_BY_ID = SELECT_SPITTLE + " and sp.id=?"; 23 private static final String SELECT_SPITTLES_BY_SPITTER_ID = SELECT_SPITTLE + " and s.id=? order by sp.postedTime desc"; 24 private static final String SELECT_RECENT_SPITTLES = SELECT_SPITTLE + " order by sp.postedTime desc limit ?"; 25 26 private JdbcTemplate jdbcTemplate; 27 28 public JdbcSpittleRepository(JdbcTemplate jdbcTemplate) { 29 this.jdbcTemplate = jdbcTemplate; 30 } 31 32 public long count() { 33 //return jdbcTemplate.queryForLong("select count(id) from Spittle"); 34 return jdbcTemplate.queryForObject("select count(id) from Spittle", Long.class); 35 } 36 37 public List<Spittle> findRecent() { 38 return findRecent(10); 39 } 40 41 public List<Spittle> findRecent(int count) { 42 return jdbcTemplate.query(SELECT_RECENT_SPITTLES, new SpittleRowMapper(), count); 43 } 44 45 public Spittle findOne(long id) { 46 try { 47 return jdbcTemplate.queryForObject(SELECT_SPITTLE_BY_ID, new SpittleRowMapper(), id); 48 } catch (EmptyResultDataAccessException e) { 49 return null; 50 } 51 } 52 53 public List<Spittle> findBySpitterId(long spitterId) { 54 return jdbcTemplate.query(SELECT_SPITTLES_BY_SPITTER_ID, new SpittleRowMapper(), spitterId); 55 } 56 57 public Spittle save(Spittle spittle) { 58 long spittleId = insertSpittleAndReturnId(spittle); 59 return new Spittle(spittleId, spittle.getSpitter(), spittle.getMessage(), spittle.getPostedTime()); 60 } 61 62 private long insertSpittleAndReturnId(Spittle spittle) { 63 SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName("Spittle"); 64 jdbcInsert.setGeneratedKeyName("id"); 65 Map<String, Object> args = new HashMap<String, Object>(); 66 args.put("spitter", spittle.getSpitter().getId()); 67 args.put("message", spittle.getMessage()); 68 args.put("postedTime", spittle.getPostedTime()); 69 long spittleId = jdbcInsert.executeAndReturnKey(args).longValue(); 70 return spittleId; 71 } 72 73 public void delete(long id) { 74 jdbcTemplate.update("delete from Spittle where id=?", id); 75 } 76 77 private static final class SpittleRowMapper implements RowMapper<Spittle> { 78 public Spittle mapRow(ResultSet rs, int rowNum) throws SQLException { 79 long id = rs.getLong("id"); 80 String message = rs.getString("message"); 81 Date postedTime = rs.getTimestamp("postedTime"); 82 long spitterId = rs.getLong("spitterId"); 83 String username = rs.getString("username"); 84 String password = rs.getString("password"); 85 String fullName = rs.getString("fullname"); 86 String email = rs.getString("email"); 87 boolean updateByEmail = rs.getBoolean("updateByEmail"); 88 Spitter spitter = new Spitter(spitterId, username, password, fullName, email, updateByEmail); 89 return new Spittle(id, spitter, message, postedTime); 90 } 91 } 92 93 }
二、Domain层
1.
1 package spittr.domain; 2 3 public class Spitter { 4 5 private Long id; 6 private String username; 7 private String password; 8 private String fullName; 9 private String email; 10 private boolean updateByEmail; 11 12 public Spitter(Long id, String username, String password, String fullName, String email, boolean updateByEmail) { 13 this.id = id; 14 this.username = username; 15 this.password = password; 16 this.fullName = fullName; 17 this.email = email; 18 this.updateByEmail = updateByEmail; 19 } 20 21 public Long getId() { 22 return id; 23 } 24 25 public String getUsername() { 26 return username; 27 } 28 29 public String getPassword() { 30 return password; 31 } 32 33 public String getFullName() { 34 return fullName; 35 } 36 37 public String getEmail() { 38 return email; 39 } 40 41 public boolean isUpdateByEmail() { 42 return updateByEmail; 43 } 44 45 }
2.
1 package spittr.domain; 2 3 import java.util.Date; 4 5 public class Spittle { 6 private final Long id; 7 private final Spitter spitter; 8 private final String message; 9 private final Date postedTime; 10 11 public Spittle(Long id, Spitter spitter, String message, Date postedTime) { 12 this.id = id; 13 this.spitter = spitter; 14 this.message = message; 15 this.postedTime = postedTime; 16 } 17 18 public Long getId() { 19 return this.id; 20 } 21 22 public String getMessage() { 23 return this.message; 24 } 25 26 public Date getPostedTime() { 27 return this.postedTime; 28 } 29 30 public Spitter getSpitter() { 31 return this.spitter; 32 } 33 34 }
三、配置文件及数据库文件
1.JdbcConfig.java
1 package spittr.jdbc; 2 3 import javax.sql.DataSource; 4 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.jdbc.core.JdbcTemplate; 8 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 9 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; 10 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 11 import org.springframework.transaction.PlatformTransactionManager; 12 13 import spittr.db.SpitterRepository; 14 import spittr.db.SpittleRepository; 15 import spittr.db.jdbc.JdbcSpitterRepository; 16 import spittr.db.jdbc.JdbcSpittleRepository; 17 18 @Configuration 19 public class JdbcConfig { 20 21 @Bean 22 public DataSource dataSource() { 23 return new EmbeddedDatabaseBuilder() 24 .setType(EmbeddedDatabaseType.H2) 25 .addScripts("classpath:spittr/db/jdbc/schema.sql", "classpath:spittr/db/jdbc/test-data.sql") 26 .build(); 27 } 28 29 @Bean 30 public JdbcTemplate jdbcTemplate(DataSource dataSource) { 31 return new JdbcTemplate(dataSource); 32 } 33 34 @Bean 35 public SpitterRepository spitterRepository(JdbcTemplate jdbcTemplate) { 36 return new JdbcSpitterRepository(jdbcTemplate); 37 } 38 39 @Bean 40 public SpittleRepository spittleRepository(JdbcTemplate jdbcTemplate) { 41 return new JdbcSpittleRepository(jdbcTemplate); 42 } 43 44 @Bean 45 public PlatformTransactionManager transactionManager(DataSource dataSource) { 46 return new DataSourceTransactionManager(dataSource); 47 } 48 49 }
2.或用xml配置(JdbcRepositoryTests-context.xml)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:c="http://www.springframework.org/schema/c" 5 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 6 xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 8 9 <jdbc:embedded-database id="dataSource" type="H2"> 10 <jdbc:script location="classpath:spittr/db/jdbc/schema.sql" /> 11 <jdbc:script location="classpath:spittr/db/jdbc/test-data.sql" /> 12 </jdbc:embedded-database> 13 14 <bean id="jdbcTemplate" 15 class="org.springframework.jdbc.core.JdbcTemplate" 16 c:_-ref="dataSource" /> 17 18 <bean class="spittr.db.jdbc.JdbcSpitterRepository" 19 c:_-ref="jdbcTemplate" /> 20 21 <bean class="spittr.db.jdbc.JdbcSpittleRepository" 22 c:_-ref="jdbcTemplate" /> 23 24 <bean id="transactionManager" 25 class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 26 c:_-ref="dataSource" /> 27 28 </beans>
3.schema.sql
1 drop table if exists spittle; 2 drop table if exists spitter; 3 4 create table spitter ( 5 id identity, 6 username varchar(25) not null, 7 password varchar(25) not null, 8 fullName varchar(100) not null, 9 email varchar(50) not null, 10 updateByEmail boolean not null 11 ); 12 13 create table spittle ( 14 id integer identity primary key, 15 spitter integer not null, 16 message varchar(2000) not null, 17 postedTime datetime not null, 18 foreign key (spitter) references spitter(id) 19 );
4.test-data.sql
1 insert into Spitter (username, password, fullname, email, updateByEmail) values ('habuma', 'password', 'Craig Walls', 'craig@habuma.com', false); 2 insert into Spitter (username, password, fullname, email, updateByEmail) values ('mwalls', 'password', 'Michael Walls', 'mwalls@habuma.com', true); 3 insert into Spitter (username, password, fullname, email, updateByEmail) values ('chuck', 'password', 'Chuck Wagon', 'chuck@habuma.com', false); 4 insert into Spitter (username, password, fullname, email, updateByEmail) values ('artnames', 'password', 'Art Names', 'art@habuma.com', true); 5 6 insert into Spittle (spitter, message, postedTime) values (1, 'This is a test spittle message', '2012-06-09 22:00:00Z'); 7 insert into Spittle (spitter, message, postedTime) values (1, 'This is another test spittle message', '2012-06-09 22:10:00Z'); 8 insert into Spittle (spitter, message, postedTime) values (1, 'This is a third test spittle message', '2012-07-04 23:30:00Z'); 9 insert into Spittle (spitter, message, postedTime) values (2, 'Hello from Chuck!', '2012-03-25 12:15:00Z'); 10 insert into Spittle (spitter, message, postedTime) values (4, 'Hello from Art!', '2012-03-25 12:15:00Z'); 11 insert into Spittle (spitter, message, postedTime) values (4, 'Hello again from Art!', '2012-03-25 12:25:00Z'); 12 insert into Spittle (spitter, message, postedTime) values (4, 'Hola from Arthur!', '2012-03-25 12:35:00Z'); 13 insert into Spittle (spitter, message, postedTime) values (4, 'Buenos Dias from Art!', '2012-03-25 12:45:00Z'); 14 insert into Spittle (spitter, message, postedTime) values (4, 'Ni Hao from Art!', '2012-03-25 12:55:00Z'); 15 insert into Spittle (spitter, message, postedTime) values (4, 'Guten Tag from Art!', '2012-03-25 13:05:00Z'); 16 insert into Spittle (spitter, message, postedTime) values (4, 'Konnichi wa from Art!', '2012-03-25 13:15:00Z'); 17 insert into Spittle (spitter, message, postedTime) values (4, 'Buon giorno from Art!', '2012-03-25 13:25:00Z'); 18 insert into Spittle (spitter, message, postedTime) values (4, 'Bonjour from Art!', '2012-03-25 13:35:00Z'); 19 insert into Spittle (spitter, message, postedTime) values (4, 'Aloha from Art!', '2012-03-25 13:45:00Z'); 20 insert into Spittle (spitter, message, postedTime) values (4, 'God dag from Art!', '2012-03-25 13:55:00Z');
四、测试文件
1.
1 package spittr.jdbc; 2 3 import static org.junit.Assert.*; 4 5 import java.util.List; 6 7 import org.junit.BeforeClass; 8 import org.junit.Test; 9 import org.junit.runner.RunWith; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.test.context.ContextConfiguration; 12 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 import org.springframework.transaction.annotation.Transactional; 14 15 import spittr.db.jdbc.JdbcSpitterRepository; 16 import spittr.domain.Spitter; 17 18 @RunWith(SpringJUnit4ClassRunner.class) 19 @ContextConfiguration(classes=JdbcConfig.class) 20 public class JdbcSpitterRepositoryTest { 21 22 @Autowired 23 JdbcSpitterRepository spitterRepository; 24 25 @Test 26 public void count() { 27 assertEquals(4, spitterRepository.count()); 28 } 29 30 @Test 31 @Transactional 32 public void findAll() { 33 List<Spitter> spitters = spitterRepository.findAll(); 34 assertEquals(4, spitters.size()); 35 assertSpitter(0, spitters.get(0)); 36 assertSpitter(1, spitters.get(1)); 37 assertSpitter(2, spitters.get(2)); 38 assertSpitter(3, spitters.get(3)); 39 } 40 41 @Test 42 @Transactional 43 public void findByUsername() { 44 assertSpitter(0, spitterRepository.findByUsername("habuma")); 45 assertSpitter(1, spitterRepository.findByUsername("mwalls")); 46 assertSpitter(2, spitterRepository.findByUsername("chuck")); 47 assertSpitter(3, spitterRepository.findByUsername("artnames")); 48 } 49 50 @Test 51 @Transactional 52 public void findOne() { 53 assertSpitter(0, spitterRepository.findOne(1L)); 54 assertSpitter(1, spitterRepository.findOne(2L)); 55 assertSpitter(2, spitterRepository.findOne(3L)); 56 assertSpitter(3, spitterRepository.findOne(4L)); 57 } 58 59 @Test 60 @Transactional 61 public void save_newSpitter() { 62 assertEquals(4, spitterRepository.count()); 63 Spitter spitter = new Spitter(null, "newbee", "letmein", "New Bee", 64 "newbee@habuma.com", true); 65 Spitter saved = spitterRepository.save(spitter); 66 assertEquals(5, spitterRepository.count()); 67 assertSpitter(4, saved); 68 assertSpitter(4, spitterRepository.findOne(5L)); 69 } 70 71 @Test 72 @Transactional 73 public void save_existingSpitter() { 74 assertEquals(4, spitterRepository.count()); 75 Spitter spitter = new Spitter(4L, "arthur", "letmein", "Arthur Names", 76 "arthur@habuma.com", false); 77 Spitter saved = spitterRepository.save(spitter); 78 assertSpitter(5, saved); 79 assertEquals(4, spitterRepository.count()); 80 Spitter updated = spitterRepository.findOne(4L); 81 assertSpitter(5, updated); 82 } 83 84 private static void assertSpitter(int expectedSpitterIndex, Spitter actual) { 85 assertSpitter(expectedSpitterIndex, actual, "Newbie"); 86 } 87 88 private static void assertSpitter(int expectedSpitterIndex, Spitter actual, 89 String expectedStatus) { 90 Spitter expected = SPITTERS[expectedSpitterIndex]; 91 assertEquals(expected.getId(), actual.getId()); 92 assertEquals(expected.getUsername(), actual.getUsername()); 93 assertEquals(expected.getPassword(), actual.getPassword()); 94 assertEquals(expected.getFullName(), actual.getFullName()); 95 assertEquals(expected.getEmail(), actual.getEmail()); 96 assertEquals(expected.isUpdateByEmail(), actual.isUpdateByEmail()); 97 } 98 99 private static Spitter[] SPITTERS = new Spitter[6]; 100 101 @BeforeClass 102 public static void before() { 103 SPITTERS[0] = new Spitter(1L, "habuma", "password", "Craig Walls", 104 "craig@habuma.com", false); 105 SPITTERS[1] = new Spitter(2L, "mwalls", "password", "Michael Walls", 106 "mwalls@habuma.com", true); 107 SPITTERS[2] = new Spitter(3L, "chuck", "password", "Chuck Wagon", 108 "chuck@habuma.com", false); 109 SPITTERS[3] = new Spitter(4L, "artnames", "password", "Art Names", 110 "art@habuma.com", true); 111 SPITTERS[4] = new Spitter(5L, "newbee", "letmein", "New Bee", 112 "newbee@habuma.com", true); 113 SPITTERS[5] = new Spitter(4L, "arthur", "letmein", "Arthur Names", 114 "arthur@habuma.com", false); 115 } 116 117 }
2.
1 package spittr.jdbc; 2 3 import static org.junit.Assert.*; 4 5 import java.util.Date; 6 import java.util.List; 7 8 import org.junit.Test; 9 import org.junit.runner.RunWith; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.test.context.ContextConfiguration; 12 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 import org.springframework.transaction.annotation.Transactional; 14 15 import spittr.db.jdbc.JdbcSpittleRepository; 16 import spittr.domain.Spitter; 17 import spittr.domain.Spittle; 18 19 @RunWith(SpringJUnit4ClassRunner.class) 20 @ContextConfiguration(classes=JdbcConfig.class) 21 public class JdbcSpittleRepositoryTest { 22 23 @Autowired 24 JdbcSpittleRepository spittleRepository; 25 26 @Test 27 public void count() { 28 assertEquals(15, spittleRepository.count()); 29 } 30 31 @Test 32 public void findRecent() { 33 // default case 34 { 35 List<Spittle> recent = spittleRepository.findRecent(); 36 assertRecent(recent, 10); 37 } 38 39 // specific count case 40 { 41 List<Spittle> recent = spittleRepository.findRecent(5); 42 assertRecent(recent, 5); 43 } 44 } 45 46 @Test 47 public void findOne() { 48 Spittle thirteen = spittleRepository.findOne(13); 49 assertEquals(13, thirteen.getId().longValue()); 50 assertEquals("Bonjour from Art!", thirteen.getMessage()); 51 assertEquals(1332682500000L, thirteen.getPostedTime().getTime()); 52 assertEquals(4, thirteen.getSpitter().getId().longValue()); 53 assertEquals("artnames", thirteen.getSpitter().getUsername()); 54 assertEquals("password", thirteen.getSpitter().getPassword()); 55 assertEquals("Art Names", thirteen.getSpitter().getFullName()); 56 assertEquals("art@habuma.com", thirteen.getSpitter().getEmail()); 57 assertTrue(thirteen.getSpitter().isUpdateByEmail()); 58 } 59 60 @Test 61 public void findBySpitter() { 62 List<Spittle> spittles = spittleRepository.findBySpitterId(4L); 63 assertEquals(11, spittles.size()); 64 for (int i = 0; i < 11; i++) { 65 assertEquals(15 - i, spittles.get(i).getId().longValue()); 66 } 67 } 68 69 @Test 70 @Transactional 71 public void save() { 72 assertEquals(15, spittleRepository.count()); 73 Spitter spitter = spittleRepository.findOne(13).getSpitter(); 74 Spittle spittle = new Spittle(null, spitter, "Un Nuevo Spittle from Art", 75 new Date()); 76 Spittle saved = spittleRepository.save(spittle); 77 assertEquals(16, spittleRepository.count()); 78 assertNewSpittle(saved); 79 assertNewSpittle(spittleRepository.findOne(16L)); 80 } 81 82 @Test 83 @Transactional 84 public void delete() { 85 assertEquals(15, spittleRepository.count()); 86 assertNotNull(spittleRepository.findOne(13)); 87 spittleRepository.delete(13L); 88 assertEquals(14, spittleRepository.count()); 89 assertNull(spittleRepository.findOne(13)); 90 } 91 92 private void assertRecent(List<Spittle> recent, int count) { 93 long[] recentIds = new long[] { 3, 2, 1, 15, 14, 13, 12, 11, 10, 9 }; 94 assertEquals(count, recent.size()); 95 for (int i = 0; i < count; i++) { 96 assertEquals(recentIds[i], recent.get(i).getId().longValue()); 97 } 98 } 99 100 private void assertNewSpittle(Spittle spittle) { 101 assertEquals(16, spittle.getId().longValue()); 102 } 103 104 }
You can do anything you set your mind to, man!