|
| 1 | +package com.baeldung.ormlite; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.AfterClass; |
| 11 | +import org.junit.BeforeClass; |
| 12 | +import org.junit.Test; |
| 13 | +import com.j256.ormlite.dao.CloseableWrappedIterable; |
| 14 | +import com.j256.ormlite.dao.Dao; |
| 15 | +import com.j256.ormlite.dao.DaoManager; |
| 16 | +import com.j256.ormlite.jdbc.JdbcPooledConnectionSource; |
| 17 | +import com.j256.ormlite.table.TableUtils; |
| 18 | + |
| 19 | +public class ORMLiteTest { |
| 20 | + private static JdbcPooledConnectionSource connectionSource; |
| 21 | + |
| 22 | + private static Dao<Library, Long> libraryDao; |
| 23 | + private static Dao<Book, Long> bookDao; |
| 24 | + |
| 25 | + @BeforeClass |
| 26 | + public static void setup() throws SQLException { |
| 27 | + connectionSource = new JdbcPooledConnectionSource("jdbc:h2:mem:myDb"); |
| 28 | + TableUtils.createTableIfNotExists(connectionSource, Library.class); |
| 29 | + TableUtils.createTableIfNotExists(connectionSource, Address.class); |
| 30 | + TableUtils.createTableIfNotExists(connectionSource, Book.class); |
| 31 | + |
| 32 | + libraryDao = DaoManager.createDao(connectionSource, Library.class); |
| 33 | + |
| 34 | + bookDao = DaoManager.createDao(connectionSource, Book.class); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void givenDAO_whenCRUD_thenOk() throws SQLException { |
| 39 | + Library library = new Library(); |
| 40 | + library.setName("My Library"); |
| 41 | + libraryDao.create(library); |
| 42 | + |
| 43 | + Library result = libraryDao.queryForId(library.getLibraryId()); |
| 44 | + assertEquals("My Library", result.getName()); |
| 45 | + |
| 46 | + library.setName("My Other Library"); |
| 47 | + libraryDao.update(library); |
| 48 | + |
| 49 | + libraryDao.delete(library); |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void whenLoopDao_thenOk() throws SQLException { |
| 55 | + Library library1 = new Library(); |
| 56 | + library1.setName("My Library"); |
| 57 | + libraryDao.create(library1); |
| 58 | + |
| 59 | + Library library2 = new Library(); |
| 60 | + library2.setName("My Other Library"); |
| 61 | + libraryDao.create(library2); |
| 62 | + |
| 63 | + libraryDao.forEach(lib -> { |
| 64 | + System.out.println(lib.getName()); |
| 65 | + }); |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenIterator_whenLoop_thenOk() throws SQLException, IOException { |
| 71 | + Library library1 = new Library(); |
| 72 | + library1.setName("My Library"); |
| 73 | + libraryDao.create(library1); |
| 74 | + |
| 75 | + Library library2 = new Library(); |
| 76 | + library2.setName("My Other Library"); |
| 77 | + libraryDao.create(library2); |
| 78 | + |
| 79 | + CloseableWrappedIterable<Library> wrappedIterable = libraryDao.getWrappedIterable(); |
| 80 | + try { |
| 81 | + wrappedIterable.forEach(lib -> { |
| 82 | + System.out.println(lib.getName()); |
| 83 | + }); |
| 84 | + } finally { |
| 85 | + wrappedIterable.close(); |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void givenCustomDao_whenSave_thenOk() throws SQLException, IOException { |
| 92 | + Library library = new Library(); |
| 93 | + library.setName("My Library"); |
| 94 | + |
| 95 | + LibraryDao customLibraryDao = DaoManager.createDao(connectionSource, Library.class); |
| 96 | + customLibraryDao.create(library); |
| 97 | + assertEquals(1, customLibraryDao.findByName("My Library") |
| 98 | + .size()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void whenSaveForeignField_thenOk() throws SQLException, IOException { |
| 103 | + Library library = new Library(); |
| 104 | + library.setName("My Library"); |
| 105 | + library.setAddress(new Address("Main Street nr 20")); |
| 106 | + libraryDao.create(library); |
| 107 | + |
| 108 | + Dao<Address, Long> addressDao = DaoManager.createDao(connectionSource, Address.class); |
| 109 | + assertEquals(1, addressDao.queryForEq("addressLine", "Main Street nr 20") |
| 110 | + .size()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void whenSaveForeignCollection_thenOk() throws SQLException, IOException { |
| 115 | + Library library = new Library(); |
| 116 | + library.setName("My Library"); |
| 117 | + libraryDao.create(library); |
| 118 | + libraryDao.refresh(library); |
| 119 | + library.getBooks() |
| 120 | + .add(new Book("1984")); |
| 121 | + |
| 122 | + Book book = new Book("It"); |
| 123 | + book.setLibrary(library); |
| 124 | + bookDao.create(book); |
| 125 | + |
| 126 | + assertEquals(2, bookDao.queryForEq("library_id", library) |
| 127 | + .size()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void whenGetLibrariesWithMoreThanOneBook_thenOk() throws SQLException, IOException { |
| 132 | + Library library = new Library(); |
| 133 | + library.setName("My Library"); |
| 134 | + libraryDao.create(library); |
| 135 | + Library library2 = new Library(); |
| 136 | + library2.setName("My Other Library"); |
| 137 | + libraryDao.create(library2); |
| 138 | + |
| 139 | + libraryDao.refresh(library); |
| 140 | + libraryDao.refresh(library2); |
| 141 | + |
| 142 | + library.getBooks() |
| 143 | + .add(new Book("Book1")); |
| 144 | + library2.getBooks() |
| 145 | + .add(new Book("Book2")); |
| 146 | + library2.getBooks() |
| 147 | + .add(new Book("Book3")); |
| 148 | + |
| 149 | + List<Library> libraries = libraryDao.queryBuilder() |
| 150 | + .where() |
| 151 | + .in("libraryId", bookDao.queryBuilder() |
| 152 | + .selectColumns("library_id") |
| 153 | + .groupBy("library_id") |
| 154 | + .having("count(*) > 1")) |
| 155 | + .query(); |
| 156 | + assertEquals(1, libraries.size()); |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + @After |
| 161 | + public void clear() throws SQLException { |
| 162 | + TableUtils.clearTable(connectionSource, Library.class); |
| 163 | + TableUtils.clearTable(connectionSource, Book.class); |
| 164 | + TableUtils.clearTable(connectionSource, Address.class); |
| 165 | + } |
| 166 | + |
| 167 | + @AfterClass |
| 168 | + public static void tearDown() throws SQLException, IOException { |
| 169 | + connectionSource.close(); |
| 170 | + } |
| 171 | +} |
0 commit comments