zoukankan      html  css  js  c++  java
  • SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-005Spring-Data-JPA例子的代码

    一、结构

    二、Repository层

    1.

     1 package spittr.db;
     2 
     3 import java.util.List;
     4 
     5 import org.springframework.data.jpa.repository.JpaRepository;
     6 
     7 import spittr.domain.Spitter;
     8 
     9 /**
    10  * Repository interface with operations for {@link Spitter} persistence.
    11  * @author habuma
    12  */
    13 public interface SpitterRepository extends JpaRepository<Spitter, Long>, SpitterSweeper {
    14       
    15     Spitter findByUsername(String username);
    16     
    17     List<Spitter> findByUsernameOrFullNameLike(String username, String fullName);
    18 
    19 }

    2.

    1 package spittr.db;
    2 
    3 public interface SpitterSweeper {
    4 
    5     int eliteSweep();
    6 
    7 }

    3.

     1 package spittr.db;
     2 
     3 import javax.persistence.EntityManager;
     4 import javax.persistence.PersistenceContext;
     5 
     6 public class SpitterRepositoryImpl implements SpitterSweeper {
     7 
     8     @PersistenceContext
     9     private EntityManager em;
    10     
    11     public int eliteSweep() {
    12       String update = 
    13           "UPDATE Spitter spitter " +
    14                "SET spitter.status = 'Elite' " +
    15                "WHERE spitter.status = 'Newbie' " +
    16                "AND spitter.id IN (" +
    17                "SELECT s FROM Spitter s WHERE (" +
    18                "  SELECT COUNT(spittles) FROM s.spittles spittles) > 10000" +
    19                ")";
    20         return em.createQuery(update).executeUpdate();
    21     }
    22     
    23 }

    4.

     1 package spittr.db;
     2 
     3 import java.util.List;
     4 
     5 import org.springframework.data.jpa.repository.JpaRepository;
     6 
     7 import spittr.domain.Spittle;
     8 
     9 /**
    10  * Repository interface with operations for {@link Spittle} persistence.
    11  * @author habuma
    12  */
    13 public interface SpittleRepository extends JpaRepository<Spittle, Long>, SpittleRepositoryCustom {
    14   
    15   List<Spittle> findBySpitterId(long spitterId);
    16   
    17 }

    5.

     1 package spittr.db;
     2 
     3 import java.util.List;
     4 
     5 import spittr.domain.Spittle;
     6 
     7 public interface SpittleRepositoryCustom {
     8 
     9   List<Spittle> findRecent();
    10 
    11   List<Spittle> findRecent(int count);
    12 
    13 }

    6.

     1 package spittr.db;
     2 
     3 import java.util.List;
     4 
     5 import javax.persistence.EntityManager;
     6 import javax.persistence.PersistenceContext;
     7 
     8 import spittr.domain.Spittle;
     9 
    10 public class SpittleRepositoryImpl implements SpittleRepositoryCustom {
    11 
    12   @PersistenceContext
    13   private EntityManager entityManager;
    14 
    15   public List<Spittle> findRecent() {
    16     return findRecent(10);
    17   }
    18 
    19   public List<Spittle> findRecent(int count) {
    20     return (List<Spittle>) entityManager.createQuery("select s from Spittle s order by s.postedTime desc")
    21         .setMaxResults(count)
    22         .getResultList();
    23   }
    24   
    25 }

    三、domain层

    1.

     1 package spittr.domain;
     2 
     3 import java.util.List;
     4 
     5 import javax.persistence.Column;
     6 import javax.persistence.Entity;
     7 import javax.persistence.FetchType;
     8 import javax.persistence.GeneratedValue;
     9 import javax.persistence.GenerationType;
    10 import javax.persistence.Id;
    11 import javax.persistence.OneToMany;
    12 
    13 
    14 @Entity
    15 public class Spitter {
    16   
    17   private Spitter() {}
    18 
    19   @Id
    20   @GeneratedValue(strategy=GenerationType.IDENTITY)
    21   private Long id;
    22 
    23   @Column(name="username")
    24   private String username;
    25 
    26   @Column(name="password")
    27   private String password;
    28 
    29   @Column(name="fullname")
    30   private String fullName;
    31 
    32   @Column(name="email")
    33   private String email;
    34 
    35   @Column(name="updateByEmail")
    36   private boolean updateByEmail;
    37   
    38   @Column(name="status")
    39   private String status;
    40   
    41   @OneToMany(targetEntity=Spittle.class, fetch=FetchType.EAGER, mappedBy="spitter")
    42   private List<Spittle> spittles;
    43 
    44   public Spitter(Long id, String username, String password, String fullName,
    45       String email, boolean updateByEmail) {
    46     this.id = id;
    47     this.username = username;
    48     this.password = password;
    49     this.fullName = fullName;
    50     this.email = email;
    51     this.updateByEmail = updateByEmail;
    52     this.status = "Newbie";
    53   }
    54 
    55   public Long getId() {
    56     return id;
    57   }
    58 
    59   public String getUsername() {
    60     return username;
    61   }
    62 
    63   public String getPassword() {
    64     return password;
    65   }
    66 
    67   public String getFullName() {
    68     return fullName;
    69   }
    70 
    71   public String getEmail() {
    72     return email;
    73   }
    74 
    75   public boolean isUpdateByEmail() {
    76     return updateByEmail;
    77   }
    78 
    79   public String getStatus() {
    80     return status;
    81   }
    82   
    83   public List<Spittle> getSpittles() {
    84     return spittles;
    85   }
    86 }

    2.

     1 package spittr.domain;
     2 
     3 import java.util.Date;
     4 
     5 import javax.persistence.Column;
     6 import javax.persistence.Entity;
     7 import javax.persistence.GeneratedValue;
     8 import javax.persistence.GenerationType;
     9 import javax.persistence.Id;
    10 import javax.persistence.JoinColumn;
    11 import javax.persistence.ManyToOne;
    12 
    13 @Entity
    14 public class Spittle {
    15 
    16   @Id    
    17   @GeneratedValue(strategy=GenerationType.IDENTITY)
    18   private Long id;
    19   
    20   @ManyToOne
    21   @JoinColumn(name="spitter")
    22   private Spitter spitter;
    23   
    24   @Column
    25   private String message;
    26   
    27   @Column
    28   private Date postedTime;
    29 
    30   private Spittle() {}
    31   
    32   public Spittle(Long id, Spitter spitter, String message, Date postedTime) {
    33     this.id = id;
    34     this.spitter = spitter;
    35     this.message = message;
    36     this.postedTime = postedTime;
    37   }
    38   
    39   public Long getId() {
    40     return this.id;
    41   }
    42   
    43   public String getMessage() {
    44     return this.message;
    45   }
    46   
    47   public Date getPostedTime() {
    48     return this.postedTime;
    49   }
    50 
    51   public Spitter getSpitter() {
    52     return this.spitter;
    53   }
    54 
    55 }

    四、配置文件及数据库文件

    1.

     1 package spittr.db;
     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.data.jpa.repository.config.EnableJpaRepositories;
     8 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
     9 import org.springframework.orm.jpa.JpaTransactionManager;
    10 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    11 import org.springframework.orm.jpa.vendor.Database;
    12 import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
    13 
    14 @Configuration
    15 @EnableJpaRepositories(basePackages="spitter.db")
    16 public class SpringDataJpaConfig {
    17   
    18   @Bean
    19   public DataSource dataSource() {
    20     return new EmbeddedDatabaseBuilder()
    21         .addScript("classpath:spitter/db/jpa/schema.sql")
    22         .addScript("classpath:spitter/db/jpa/test-data.sql")
    23         .build();
    24   }
    25   
    26   @Bean
    27   public JpaTransactionManager transactionManager() {
    28     return new JpaTransactionManager(); // does this need an emf???
    29   }
    30   
    31   @Bean
    32   public HibernateJpaVendorAdapter jpaVendorAdapter() {
    33     HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
    34     adapter.setDatabase(Database.H2);
    35     adapter.setShowSql(false);
    36     adapter.setGenerateDdl(true);
    37     return adapter;
    38   }
    39   
    40   @Bean
    41   public Object emf() {
    42     LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    43     emf.setDataSource(dataSource());
    44     emf.setPersistenceUnitName("spitter");
    45     emf.setJpaVendorAdapter(jpaVendorAdapter());
    46     return emf;
    47   }
    48   
    49 }

    2.

     1 package spittr.db.jpa;
     2 
     3 import javax.inject.Inject;
     4 import javax.persistence.EntityManagerFactory;
     5 import javax.sql.DataSource;
     6 
     7 import org.springframework.context.annotation.Bean;
     8 import org.springframework.context.annotation.Configuration;
     9 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    10 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
    11 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
    12 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
    13 import org.springframework.orm.jpa.JpaTransactionManager;
    14 import org.springframework.orm.jpa.JpaVendorAdapter;
    15 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    16 import org.springframework.orm.jpa.vendor.Database;
    17 import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
    18 import org.springframework.transaction.PlatformTransactionManager;
    19 import org.springframework.transaction.annotation.EnableTransactionManagement;
    20 
    21 @Configuration
    22 @EnableJpaRepositories(basePackages="spittr.db")
    23 public class JpaConfig {
    24 
    25   @Bean
    26   public DataSource dataSource() {
    27     EmbeddedDatabaseBuilder edb = new EmbeddedDatabaseBuilder();
    28     edb.setType(EmbeddedDatabaseType.H2);
    29     edb.addScript("spittr/db/jpa/schema.sql");
    30     edb.addScript("spittr/db/jpa/test-data.sql");
    31     EmbeddedDatabase embeddedDatabase = edb.build();
    32     return embeddedDatabase;
    33   }
    34 
    35   @Bean
    36   public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
    37     LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    38     emf.setDataSource(dataSource);
    39     emf.setPersistenceUnitName("spittr");
    40     emf.setJpaVendorAdapter(jpaVendorAdapter);
    41     emf.setPackagesToScan("spittr.domain");
    42     return emf;
    43   }
    44   
    45   @Bean
    46   public JpaVendorAdapter jpaVendorAdapter() {
    47     HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
    48     adapter.setDatabase(Database.H2);
    49     adapter.setShowSql(true);
    50     adapter.setGenerateDdl(false);
    51     adapter.setDatabasePlatform("org.hibernate.dialect.H2Dialect");
    52     return adapter;
    53   }
    54   
    55 
    56   @Configuration
    57   @EnableTransactionManagement
    58   public static class TransactionConfig {
    59 
    60     @Inject
    61     private EntityManagerFactory emf;
    62 
    63     @Bean
    64     public PlatformTransactionManager transactionManager() {
    65       JpaTransactionManager transactionManager = new JpaTransactionManager();
    66       transactionManager.setEntityManagerFactory(emf);
    67       return transactionManager;
    68     }    
    69   }
    70   
    71 }

    3.RepositoryTest-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:jdbc="http://www.springframework.org/schema/jdbc"
     5     xmlns:c="http://www.springframework.org/schema/c"
     6     xmlns:context="http://www.springframework.org/schema/context"
     7     xmlns:p="http://www.springframework.org/schema/p"
     8     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
     9     xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
    10         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    11         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
    12         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    13 
    14 
    15     <jpa:repositories base-package="spittr.db" />
    16     
    17     <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    18         p:dataSource-ref="dataSource" 
    19         p:persistenceUnitName="spitter"
    20         p:jpaVendorAdapter-ref="jpaVendorAdapter" />
    21 
    22     <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    23         <property name="database" value="H2" />
    24         <property name="showSql" value="false" />
    25         <property name="generateDdl" value="false" />
    26     </bean>
    27 
    28     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    29         p:entityManagerFactory-ref="emf" />
    30 
    31     <jdbc:embedded-database id="dataSource" type="H2">
    32         <jdbc:script location="spittr/db/jpa/schema.sql" />
    33         <jdbc:script location="spittr/db/jpa/test-data.sql" />
    34     </jdbc:embedded-database>
    35 
    36 </beans>

    4.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   status varchar(10) not null
    12 );
    13 
    14 create table spittle (
    15   id integer identity primary key,
    16   spitter integer not null,
    17   message varchar(2000) not null,
    18   postedTime datetime not null,
    19   foreign key (spitter) references spitter(id)
    20 );

    5.test-data.sql

     1 insert into Spitter (username, password, fullname, email, updateByEmail, status) values ('habuma', 'password', 'Craig Walls', 'craig@habuma.com', false, 'Newbie');
     2 insert into Spitter (username, password, fullname, email, updateByEmail, status) values ('mwalls', 'password', 'Michael Walls', 'mwalls@habuma.com', true, 'Newbie');
     3 insert into Spitter (username, password, fullname, email, updateByEmail, status) values ('chuck', 'password', 'Chuck Wagon', 'chuck@habuma.com', false, 'Newbie');
     4 insert into Spitter (username, password, fullname, email, updateByEmail, status) values ('artnames', 'password', 'Art Names', 'art@habuma.com', true, 'Newbie');
     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');

    6.

    1 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    2 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    3 log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %40.40c:%4L - %m%n
    4 
    5 log4j.rootLogger=WARN,stdout
    6 
    7 log4j.category.org.hibernate=WARN

    五、测试文件

    1.

      1 package spittr.db.jpa;
      2 
      3 import static org.junit.Assert.*;
      4 
      5 import java.util.List;
      6 
      7 import org.junit.BeforeClass;
      8 import org.junit.Ignore;
      9 import org.junit.Test;
     10 import org.junit.runner.RunWith;
     11 import org.springframework.beans.factory.annotation.Autowired;
     12 import org.springframework.test.context.ContextConfiguration;
     13 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     14 import org.springframework.transaction.annotation.Transactional;
     15 
     16 import spittr.db.SpitterRepository;
     17 import spittr.db.SpringDataJpaConfig;
     18 import spittr.domain.Spitter;
     19 
     20 @RunWith(SpringJUnit4ClassRunner.class)
     21 @ContextConfiguration(classes=SpringDataJpaConfig.class)
     22 public class SpitterRepositoryTest {
     23 
     24     @Autowired
     25     SpitterRepository spitterRepository;
     26     
     27     @Test
     28     @Transactional
     29     public void count() {
     30         assertEquals(4, spitterRepository.count());
     31     }
     32     
     33     @Test
     34     @Transactional
     35     public void findAll() {
     36         List<Spitter> spitters = spitterRepository.findAll();
     37         assertEquals(4, spitters.size());
     38         assertSpitter(0, spitters.get(0));
     39         assertSpitter(1, spitters.get(1));
     40         assertSpitter(2, spitters.get(2));
     41         assertSpitter(3, spitters.get(3));
     42     }
     43     
     44     @Test
     45     @Transactional
     46     public void findByUsername() {
     47         assertSpitter(0, spitterRepository.findByUsername("habuma"));
     48         assertSpitter(1, spitterRepository.findByUsername("mwalls"));
     49         assertSpitter(2, spitterRepository.findByUsername("chuck"));
     50         assertSpitter(3, spitterRepository.findByUsername("artnames"));
     51     }
     52     
     53     @Test
     54     @Transactional
     55     public void findOne() {
     56         assertSpitter(0, spitterRepository.findOne(1L));
     57         assertSpitter(1, spitterRepository.findOne(2L));
     58         assertSpitter(2, spitterRepository.findOne(3L));
     59         assertSpitter(3, spitterRepository.findOne(4L));
     60     }
     61     
     62     @Test
     63     @Transactional
     64     public void save_newSpitter() {
     65         assertEquals(4, spitterRepository.count());
     66         Spitter spitter = new Spitter(null, "newbee", "letmein", "New Bee", "newbee@habuma.com", true);
     67         Spitter saved = spitterRepository.save(spitter);
     68         assertEquals(5, spitterRepository.count());
     69         assertSpitter(4, saved);
     70         assertSpitter(4, spitterRepository.findOne(5L));
     71     }
     72 
     73     @Test
     74     @Transactional
     75     @Ignore
     76     public void save_existingSpitter() {
     77         assertEquals(4, spitterRepository.count());
     78         Spitter spitter = new Spitter(4L, "arthur", "letmein", "Arthur Names", "arthur@habuma.com", false);
     79         Spitter saved = spitterRepository.save(spitter);
     80         assertSpitter(5, saved);
     81         assertEquals(4, spitterRepository.count());
     82         Spitter updated = spitterRepository.findOne(4L);
     83         assertSpitter(5, updated);
     84     }
     85 
     86     private static void assertSpitter(int expectedSpitterIndex, Spitter actual) {
     87         assertSpitter(expectedSpitterIndex, actual, "Newbie");
     88     }
     89     
     90     private static void assertSpitter(int expectedSpitterIndex, Spitter actual, String expectedStatus) {
     91         Spitter expected = SPITTERS[expectedSpitterIndex];
     92         assertEquals(expected.getId(), actual.getId());
     93         assertEquals(expected.getUsername(), actual.getUsername());
     94         assertEquals(expected.getPassword(), actual.getPassword());
     95         assertEquals(expected.getFullName(), actual.getFullName());
     96         assertEquals(expected.getEmail(), actual.getEmail());
     97         assertEquals(expected.isUpdateByEmail(), actual.isUpdateByEmail());
     98     }
     99     
    100     private static Spitter[] SPITTERS = new Spitter[6];
    101     
    102     @BeforeClass
    103     public static void before() {
    104         SPITTERS[0] = new Spitter(1L, "habuma", "password", "Craig Walls", "craig@habuma.com", false);
    105         SPITTERS[1] = new Spitter(2L, "mwalls", "password", "Michael Walls", "mwalls@habuma.com", true);
    106         SPITTERS[2] = new Spitter(3L, "chuck", "password", "Chuck Wagon", "chuck@habuma.com", false);
    107         SPITTERS[3] = new Spitter(4L, "artnames", "password", "Art Names", "art@habuma.com", true);
    108         SPITTERS[4] = new Spitter(5L, "newbee", "letmein", "New Bee", "newbee@habuma.com", true);        
    109         SPITTERS[5] = new Spitter(4L, "arthur", "letmein", "Arthur Names", "arthur@habuma.com", false);        
    110     }
    111     
    112 }

    2.

      1 package spittr.db.jpa;
      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.SpittleRepository;
     16 import spittr.domain.Spitter;
     17 import spittr.domain.Spittle;
     18 
     19 @RunWith(SpringJUnit4ClassRunner.class)
     20 @ContextConfiguration(classes=JpaConfig.class)
     21 public class SpittleRepositoryTest {
     22     
     23     @Autowired
     24     SpittleRepository spittleRepository;
     25 
     26     @Test
     27     @Transactional
     28     public void count() {
     29         assertEquals(15, spittleRepository.count());
     30     }
     31 
     32     @Test
     33     @Transactional
     34     public void findRecent() {
     35         // default case
     36         {
     37             List<Spittle> recent = spittleRepository.findRecent();
     38             assertRecent(recent, 10);
     39         }
     40         
     41         // specific count case
     42         {
     43             List<Spittle> recent = spittleRepository.findRecent(5);
     44             assertRecent(recent, 5);
     45         }
     46     }
     47 
     48     @Test
     49     @Transactional
     50     public void findOne() {
     51         Spittle thirteen = spittleRepository.findOne(13L);
     52         assertEquals(13, thirteen.getId().longValue());
     53         assertEquals("Bonjour from Art!", thirteen.getMessage());
     54         assertEquals(1332682500000L, thirteen.getPostedTime().getTime());
     55         assertEquals(4, thirteen.getSpitter().getId().longValue());
     56         assertEquals("artnames", thirteen.getSpitter().getUsername());
     57         assertEquals("password", thirteen.getSpitter().getPassword());
     58         assertEquals("Art Names", thirteen.getSpitter().getFullName());
     59         assertEquals("art@habuma.com", thirteen.getSpitter().getEmail());
     60         assertTrue(thirteen.getSpitter().isUpdateByEmail());
     61     }
     62 
     63     @Test
     64     @Transactional
     65     public void findBySpitter() {
     66         List<Spittle> spittles = spittleRepository.findBySpitterId(4L);
     67         assertEquals(11, spittles.size());
     68         for (int i = 0; i < 11; i++) {
     69             assertEquals(i+5, spittles.get(i).getId().longValue());
     70         }
     71     }
     72     
     73     @Test
     74     @Transactional
     75     public void save() {
     76         assertEquals(15, spittleRepository.count());
     77         Spitter spitter = spittleRepository.findOne(13L).getSpitter();
     78         Spittle spittle = new Spittle(null, spitter, "Un Nuevo Spittle from Art", new Date());
     79         Spittle saved = spittleRepository.save(spittle);
     80         assertEquals(16, spittleRepository.count());
     81         assertNewSpittle(saved);
     82         assertNewSpittle(spittleRepository.findOne(16L));
     83     }
     84 
     85     @Test
     86     @Transactional
     87     public void delete() {
     88         assertEquals(15, spittleRepository.count());
     89         assertNotNull(spittleRepository.findOne(13L));
     90         spittleRepository.delete(13L);
     91         assertEquals(14, spittleRepository.count());
     92         assertNull(spittleRepository.findOne(13L));
     93     }
     94     
     95     private void assertRecent(List<Spittle> recent, int count) {
     96         long[] recentIds = new long[] {3,2,1,15,14,13,12,11,10,9};
     97         assertEquals(count, recent.size());
     98         for (int i = 0; i < count; i++) {
     99             assertEquals(recentIds[i], recent.get(i).getId().longValue());
    100         }
    101     }
    102     
    103     private void assertNewSpittle(Spittle spittle) {
    104         assertEquals(16, spittle.getId().longValue());
    105     }
    106     
    107 }
  • 相关阅读:
    Struts1简单开发流程梳理
    更改数据库字符集编码引起的问题、textarea标签输出内容时不能顶格(左对齐)输出
    FineReport基本使用
    Navicat for MySQL笔记1
    Hibernate(十)
    Elasticsearch NEST 控制字段名称命名格式
    ckeditor 敏感词标记显示处理方法
    Elasticsearch .Net Client NEST 多条件查询示例
    Elasticsearch .Net Client NEST 索引DataSet数据
    一个很简单的SqlServer生成常用C#语句工具的诞生
  • 原文地址:https://www.cnblogs.com/shamgod/p/5345464.html
Copyright © 2011-2022 走看看