Skip to content

Commit

Permalink
Added test that fails when multiple remove(i)s are applied.
Browse files Browse the repository at this point in the history
  • Loading branch information
blafond committed Feb 22, 2021
1 parent fe9dfba commit 29d375c
Showing 1 changed file with 52 additions and 0 deletions.
Expand Up @@ -147,6 +147,58 @@ public void test(TestContext context) {
);
}

@Test
public void testFailOnMultipleRemoves(TestContext context) {
Book book1 = new Book("Feersum Endjinn");
Book book2 = new Book("Use of Weapons");
Book book3 = new Book( "Third Book");
Book book4 = new Book( "Fourth Book");
Book book5 = new Book( "Fifth Book");
Author author = new Author("Iain M Banks");
author.books.add(book1);
author.books.add(book2); // << remove(1)
author.books.add(book3); // << remove(2)
author.books.add(book4);
author.books.add(book5);

test(context,
getMutinySessionFactory()
.withTransaction( (session, transaction) -> session.persistAll(author) )
.chain( () -> getMutinySessionFactory()
.withTransaction( (session, transaction) -> session.find(Author.class, author.id)
.invoke( a -> context.assertFalse( Hibernate.isInitialized(a.books) ) )
.chain( a -> session.fetch(a.books) )
.invoke( books -> context.assertEquals( 5, books.size() ) )
)
)
.chain( () -> getMutinySessionFactory()
.withTransaction( (session, transaction) -> session.createQuery("select distinct a from Author a left join fetch a.books", Author.class )

This comment has been minimized.

Copy link
@DavideD

DavideD Feb 23, 2021

Why distinct?

This comment has been minimized.

Copy link
@DavideD

DavideD Feb 23, 2021

Sorry...never mind... I get it now.

.getSingleResult()
.invoke( a -> context.assertTrue( Hibernate.isInitialized(a.books) ) )
.invoke( a -> context.assertEquals( 5, a.books.size() ) )

This comment has been minimized.

Copy link
@DavideD

DavideD Feb 23, 2021

Because we are testing @OrderColumn, I think it is essential to test that the order hasn't changed.

)
)
.chain( () -> getMutinySessionFactory()
.withTransaction( (session, transaction) -> session.find(Author.class, author.id)
.chain( a -> session.fetch(a.books) )
.invoke( books -> {
books.remove( 1 );
books.remove( 1 );

This comment has been minimized.

Copy link
@DavideD

DavideD Feb 23, 2021

Shouldn't this be remove( 2 )?

This comment has been minimized.

Copy link
@blafond

blafond Feb 23, 2021

Author Owner

the remove(int index) method creates a temp copy of the array with the reduced size. Calling remove(2) would remove the 3rd element of the array that's been truncated. That would change the expected book at the 3rd index.

This comment has been minimized.

Copy link
@DavideD

DavideD Feb 23, 2021

Right.
But your previous comment confuses me a bit:

author.books.add(book2);  // << remove(1)
author.books.add(book3);  // << remove(2)

I think replacing this with

 books.remove( 1 ); // Remove book2
 books.remove( 1 ); // Now, remove book3

or, this could also work

 books.remove( book2 ); 
 books.remove( book3 ); 

You might need to add an equal and hashcode on the title

} )
)
)
.chain( () -> getMutinySessionFactory()
.withTransaction( (session, transaction) -> session.find(Author.class, author.id)
.chain( a -> session.fetch(a.books) )
.invoke( books -> {
context.assertEquals( 3, books.size() );
context.assertEquals( book3.title, books.get(1).title );
} )
)
)
);
}

@Embeddable
static class Book {
Book(String title) {
Expand Down

0 comments on commit 29d375c

Please sign in to comment.