KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > BeanTest


1 package org.apache.torque;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.List JavaDoc;
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 /**
30  * Runtime tests to make sure that the code which is supplied
31  * in the documentation actually works ;-)
32  *
33  * @author <a HREF="mailto:fischer@seitenbau.de">Thomas Fischer</a>
34  * @version $Id: BeanTest.java,v 1.1 2005/03/28 18:25:28 tfischer Exp $
35  */

36 public class BeanTest extends BaseRuntimeTestCase
37 {
38     public static final String JavaDoc AUTHOR_1_NAME = "Joshua Bloch";
39     
40     public static final int AUTHOR_1_ID = 123;
41        
42     public static final String JavaDoc BOOK_1_TITLE = "Effective Java";
43     
44     public static final String JavaDoc BOOK_1_ISBN = "0-618-12902-2";
45     
46     public static final int BOOK_1_ID = 456;
47
48     public static final String JavaDoc AUTHOR_2_NAME = "W. Stevens";
49            
50     /**
51      * Creates a new instance.
52      */

53     public BeanTest(String JavaDoc name)
54     {
55         super(name);
56     }
57
58     public void setUp()
59     {
60         super.setUp();
61     }
62
63     /**
64      * tests the creation of beans from objects and vice versa
65      */

66     public void testCreateBeans() throws Exception JavaDoc
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     /**
90      * tests whether object relations are transferred correctly,
91      * if two objects refer to each other
92      */

93     public void testSameObjectRelations() throws Exception JavaDoc
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         // check one roundtrip from author
105
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         // check one roundtrip from book
122
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     /**
140      * tests whether object relations are transferred correctly,
141      * if there is no mutual reference between objects
142      * @throws Exception
143      */

144     public void testDifferentObjectRelations() throws Exception JavaDoc
145     {
146         // create a relation chain:
147
//
148
// getBooks() getAuthor() getBooks()
149
// | | |
150
// author ----> book -----> differentAuthor ---> differentBook
151
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         // check one roundtrip from author
169
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         // check one roundtrip from book
186
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 JavaDoc
206     {
207         Criteria criteria = new Criteria();
208         criteria.add(BookPeer.BOOK_ID, (Long JavaDoc) null, Criteria.NOT_EQUAL);
209         BookPeer.doDelete(criteria);
210         
211         criteria = new Criteria();
212         criteria.add(AuthorPeer.AUTHOR_ID, (Long JavaDoc) 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 JavaDoc 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 JavaDoc 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