public class BidirectionalOneToManyEnhancementsTest {
final Book book = new Book( 5L, "Lost Connections: Why You’re Depressed and How to Find Hope", " 978-1408878729" );
final Person author = new Person( 5L, "Johann Hari" );
@Test
public void normalBehaviourWithoutEnhancements() throws Exception {
book.setAuthor( author );
author.getBooks().add( book );
assertEquals( 1, author.getBooks().size() );
assertEquals( book, author.getBooks().get( 0 ) );
assertEquals( author, book.getAuthor() );
}
@Test
public void addToField() throws Exception {
book.setAuthor( author );
assertEquals( 1, author.getBooks().size() );
assertEquals( book, author.getBooks().get( 0 ) );
}
@Test
public void addToCollection() throws Exception {
author.getBooks().add( book );
assertEquals( author, book.getAuthor() );
}
@Test
public void setFieldToNull() throws Exception {
book.setAuthor( author );
author.getBooks().add( book );
book.setAuthor( null );
assertTrue( author.getBooks().isEmpty() );
}
}