1 package org.apache.torque; 2 3 18 19 import java.util.List ; 20 21 import org.apache.torque.test.Author; 22 import org.apache.torque.test.AuthorPeer; 23 import org.apache.torque.test.Book; 24 import org.apache.torque.test.BookPeer; 25 import org.apache.torque.test.bean.AuthorBean; 26 import org.apache.torque.test.bean.BookBean; 27 import org.apache.torque.util.Criteria; 28 29 36 public class BeanTest extends BaseRuntimeTestCase 37 { 38 public static final String AUTHOR_1_NAME = "Joshua Bloch"; 39 40 public static final int AUTHOR_1_ID = 123; 41 42 public static final String BOOK_1_TITLE = "Effective Java"; 43 44 public static final String BOOK_1_ISBN = "0-618-12902-2"; 45 46 public static final int BOOK_1_ID = 456; 47 48 public static final String AUTHOR_2_NAME = "W. Stevens"; 49 50 53 public BeanTest(String name) 54 { 55 super(name); 56 } 57 58 public void setUp() 59 { 60 super.setUp(); 61 } 62 63 66 public void testCreateBeans() throws Exception 67 { 68 Author author = new Author(); 69 author.setName(AUTHOR_1_NAME); 70 author.setAuthorId(AUTHOR_1_ID); 71 72 AuthorBean authorBean = author.getBean(); 73 assertTrue("bean.getName() is " + authorBean.getName() 74 + " should be " + author.getName(), 75 author.getName().equals(authorBean.getName())); 76 assertTrue("bean.getId() is " + authorBean.getAuthorId() 77 + " should be " + AUTHOR_1_ID, 78 author.getAuthorId() == authorBean.getAuthorId()); 79 80 Author authorFromBean = Author.createAuthor(authorBean); 81 assertTrue("author from bean has name " + authorFromBean.getName() 82 + " should be " + author.getName(), 83 author.getName().equals(authorFromBean.getName())); 84 assertTrue("author from bean has Id " + authorFromBean.getAuthorId() 85 + " should be " + author.getAuthorId(), 86 author.getAuthorId() == authorBean.getAuthorId()); 87 } 88 89 93 public void testSameObjectRelations() throws Exception 94 { 95 Author author = new Author(); 96 author.setAuthorId(AUTHOR_1_ID); 97 98 Book book = new Book(); 99 book.setBookId(BOOK_1_ID); 100 101 author.addBook(book); 102 book.setAuthor(author); 103 104 assertTrue("author from book should be the same object as author", 106 author == book.getAuthor()); 107 108 AuthorBean authorBean = author.getBean(); 109 BookBean bookBean = (BookBean) authorBean.getBookBeans().get(0); 110 assertTrue("authorBean from BookBean should be the same " 111 + "object as authorBean", 112 bookBean.getAuthorBean() == authorBean); 113 114 author = Author.createAuthor(authorBean); 115 book = (Book) author.getBooks().get(0); 116 117 assertTrue("author from book should be the same object as author " 118 + "after creating from bean", 119 author == book.getAuthor()); 120 121 assertTrue("book from author should be the same object as book", 123 book == author.getBooks().get(0)); 124 125 bookBean = book.getBean(); 126 authorBean = bookBean.getAuthorBean(); 127 assertTrue("bookBean from authorBean should be the same " 128 + "object as bookBean", 129 authorBean.getBookBeans().get(0) == bookBean); 130 131 book = Book.createBook(bookBean); 132 author = book.getAuthor(); 133 134 assertTrue("book from author should be the same object as book " 135 + "after creating from bean", 136 author.getBooks().get(0) == book); 137 } 138 139 144 public void testDifferentObjectRelations() throws Exception 145 { 146 Author author = new Author(); 152 author.setAuthorId(AUTHOR_1_ID); 153 154 Book book = new Book(); 155 book.setBookId(BOOK_1_ID); 156 157 Author differentAuthor = new Author(); 158 author.setAuthorId(AUTHOR_1_ID); 159 160 author.addBook(book); 161 book.setAuthor(differentAuthor); 162 163 Book differentBook = new Book(); 164 book.setBookId(BOOK_1_ID); 165 166 differentAuthor.addBook(differentBook); 167 168 assertTrue("author from book should not be the same object as author", 170 author != book.getAuthor()); 171 172 AuthorBean authorBean = author.getBean(); 173 BookBean bookBean = (BookBean) authorBean.getBookBeans().get(0); 174 assertTrue("authorBean from BookBean should not be the same " 175 + "object as authorBean", 176 bookBean.getAuthorBean() != authorBean); 177 178 author = Author.createAuthor(authorBean); 179 book = (Book) author.getBooks().get(0); 180 181 assertTrue("author from book should not be the same object as author " 182 + "after creating from bean", 183 author != book.getAuthor()); 184 185 assertTrue("book from differentAuthor should not be " 187 + "the same object as book", 188 book != differentAuthor.getBooks().get(0)); 189 190 bookBean = book.getBean(); 191 AuthorBean differentAuthorBean = bookBean.getAuthorBean(); 192 assertTrue("bookBean from differentAuthorBean should not be the same " 193 + "object as bookBean", 194 differentAuthorBean.getBookBeans().get(0) != bookBean); 195 196 book = Book.createBook(bookBean); 197 differentAuthor = book.getAuthor(); 198 199 assertTrue("book from differentAuthor should not be " 200 + "the same object as book " 201 + "after creating from bean", 202 differentAuthor.getBooks().get(0) != book); 203 } 204 205 public void testSaves() throws Exception 206 { 207 Criteria criteria = new Criteria(); 208 criteria.add(BookPeer.BOOK_ID, (Long ) null, Criteria.NOT_EQUAL); 209 BookPeer.doDelete(criteria); 210 211 criteria = new Criteria(); 212 criteria.add(AuthorPeer.AUTHOR_ID, (Long ) null, Criteria.NOT_EQUAL); 213 AuthorPeer.doDelete(criteria); 214 215 Author author = new Author(); 216 author.setName(AUTHOR_1_NAME); 217 author.save(); 218 219 assertFalse("isModified() should return false after save", 220 author.isModified()); 221 assertFalse("isNew() should return false after save", 222 author.isNew()); 223 224 AuthorBean authorBean = author.getBean(); 225 226 assertFalse("bean.isModified() should return false after save " 227 + "and bean creation", 228 authorBean.isModified()); 229 assertFalse("bean.isNew() should return false after save " 230 + "and bean creation", 231 authorBean.isNew()); 232 233 author = Author.createAuthor(authorBean); 234 235 assertFalse("isModified() should return false after save " 236 + "and bean roundtrip", 237 author.isModified()); 238 assertFalse("isNew() should return false after save " 239 + "and bean rounddtrip", 240 author.isNew()); 241 242 authorBean.setName(AUTHOR_2_NAME); 243 assertTrue("bean.isModified() should return true after it was modified", 244 authorBean.isModified()); 245 assertFalse("bean.isNew() should still return false " 246 + "after bean creation and modification", 247 authorBean.isNew()); 248 249 author = Author.createAuthor(authorBean); 250 assertTrue("isModified() should return true after creation of object " 251 + "from modified bean", 252 author.isModified()); 253 254 author.save(); 255 256 List authorList = AuthorPeer.doSelect(new Criteria()); 257 Author readAuthor = (Author) authorList.get(0); 258 assertEquals("name from read Author is " + readAuthor.getName() 259 +" but should be " + authorBean.getName(), 260 readAuthor.getName(), 261 authorBean.getName()); 262 263 BookBean bookBean = new BookBean(); 264 bookBean.setTitle(BOOK_1_TITLE); 265 bookBean.setIsbn(BOOK_1_ISBN); 266 267 Book book = Book.createBook(bookBean); 268 assertTrue("isModified() should return true after creation of object " 269 + " from new bean", 270 book.isModified()); 271 assertTrue("isNew() should return true after creation of object " 272 + " from new bean", 273 book.isNew()); 274 book.setAuthor(author); 275 book.save(); 276 277 List bookList = BookPeer.doSelect(new Criteria()); 278 assertTrue("Ther should be one book in DB but there are " 279 + bookList.size(), 280 bookList.size() == 1); 281 } 282 } 283 | Popular Tags |