r2260 - trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library
Author: fdesbois Date: 2011-04-16 18:25:22 +0200 (Sat, 16 Apr 2011) New Revision: 2260 Url: http://nuiton.org/repositories/revision/topia/2260 Log: Improve test Modified: trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/MainTest.java Modified: trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/MainTest.java =================================================================== --- trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/MainTest.java 2011-04-16 16:11:31 UTC (rev 2259) +++ trunk/topia-tutorial/src/test/java/org/nuiton/topia/tutorial/library/MainTest.java 2011-04-16 16:25:22 UTC (rev 2260) @@ -19,36 +19,62 @@ */ public class MainTest { + protected TopiaContext rootContext; + protected TopiaContext transaction; @Before public void setUp() throws Exception { + + // Retrieve config and initialize TopiaContext root with Properties LibraryConfig config = new LibraryConfig(); - TopiaContext rootContext = - TopiaContextFactory.getContext(config.getProperties()); + rootContext = TopiaContextFactory.getContext(config.getProperties()); + + // Start the transaction transaction = rootContext.beginTransaction(); } @After public void tearDown() throws Exception { + + // Close the transaction transaction.closeContext(); + + // Drop the database (Work only with H2 database) + rootContext.clear(true); } @Test public void testCreateBook() throws Exception { - BookDAO dao = LibraryDAOHelper.getBookDAO(transaction); + // ---- PREPARE DATA ---- // Book book = new BookImpl(); book.setName("Topia for dummies"); book.setIsbn("T12"); + // ---- EXECUTE ---- // + + BookDAO dao = LibraryDAOHelper.getBookDAO(transaction); + dao.create(book); + // Commit current transaction to save the entity in the database transaction.commitTransaction(); + // ---- CHECK DATA ---- // + + // Close current transaction to ensure database result + transaction.closeContext(); + + // Reopen a new transaction to retrieve the previously saved book + transaction = rootContext.beginTransaction(); + dao = LibraryDAOHelper.getBookDAO(transaction); + + // Retrieve the book from its topiaId (primary key) Book bookLoaded = dao.findByTopiaId(book.getTopiaId()); + // It is the same value ! Assert.assertEquals(bookLoaded, book); }
participants (1)
-
fdesbois@users.nuiton.org