KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.BasePeer;
26 import org.apache.torque.util.Criteria;
27
28 /**
29  * Runtime tests to make sure that the code which is supplied
30  * in the documentation actually works ;-)
31  *
32  * @author <a HREF="mailto:fischer@seitenbau.de">Thomas Fischer</a>
33  * @version $Id: DocsTest.java,v 1.4 2005/03/19 06:25:27 tfischer Exp $
34  */

35 public class DocsTest extends BaseRuntimeTestCase
36 {
37     public static final String JavaDoc AUTHOR_1_NAME = "Joshua Bloch";
38     
39     public static final String JavaDoc AUTHOR_2_NAME = "W. Stevens";
40
41     public static final String JavaDoc AUTHOR_3_NAME = "Author without book";
42     
43     public static final String JavaDoc BOOK_1_TITLE = "Effective Java";
44     
45     public static final String JavaDoc BOOK_1_ISBN = "0-618-12902-2";
46     
47     public static final String JavaDoc BOOK_2_TITLE = "TCP/IP Illustrated";
48     
49     public static final String JavaDoc BOOK_2_ISBN = "0-201-63346-9";
50   
51     public static final String JavaDoc BOOK_3_TITLE = "TCP/IP Illustrated";
52     
53     public static final String JavaDoc BOOK_3_ISBN = "0-201-63354-X";
54         
55     /**
56      * Creates a new instance.
57      */

58     public DocsTest(String JavaDoc name)
59     {
60         super(name);
61     }
62
63     public void setUp()
64     {
65         super.setUp();
66
67         // clean the books database
68
Criteria criteria = new Criteria();
69         criteria.add(BookPeer.BOOK_ID, (Long JavaDoc) null, Criteria.NOT_EQUAL);
70         try
71         {
72             BookPeer.doDelete(criteria);
73         }
74         catch(Exception JavaDoc e)
75         {
76             e.printStackTrace();
77             fail("cleaning books : Exception caught : "
78                      + e.getClass().getName()
79                      + " : " + e.getMessage());
80         }
81         criteria.clear();
82         criteria.add(
83                 AuthorPeer.AUTHOR_ID,
84                 (Long JavaDoc) null, Criteria.NOT_EQUAL);
85         try
86         {
87             AuthorPeer.doDelete(criteria);
88         }
89         catch(Exception JavaDoc e)
90         {
91             e.printStackTrace();
92             fail("cleaning authors : Exception caught : "
93                      + e.getClass().getName()
94                      + " : " + e.getMessage());
95         }
96         
97         
98         // insert some data into the database
99
// taken from tutorial step 4 with some changes
100
try
101         {
102             Author bloch = new Author();
103             bloch.setName(AUTHOR_1_NAME);
104             bloch.save();
105
106             Author stevens = new Author();
107             stevens.setName(AUTHOR_2_NAME);
108             AuthorPeer.doInsert(stevens);
109
110             Author withoutBook = new Author();
111             withoutBook.setName(AUTHOR_3_NAME);
112             AuthorPeer.doInsert(withoutBook);
113
114             Book effective = new Book();
115             effective.setTitle(BOOK_1_TITLE);
116             effective.setIsbn(BOOK_1_ISBN);
117             effective.setAuthor(bloch);
118             effective.save();
119
120             Book tcpip = new Book();
121             tcpip.setTitle(BOOK_2_TITLE);
122             tcpip.setIsbn(BOOK_2_ISBN);
123             tcpip.setAuthorId(stevens.getAuthorId());
124             tcpip.save();
125
126             Book tcpipVolTwo = new Book();
127             tcpipVolTwo.setTitle(BOOK_3_TITLE);
128             tcpipVolTwo.setIsbn(BOOK_3_ISBN);
129             tcpipVolTwo.setAuthorId(stevens.getAuthorId());
130             tcpipVolTwo.save();
131
132         }
133         catch (Exception JavaDoc e) {
134             e.printStackTrace();
135             fail("Exception caught : "
136                      + e.getClass().getName()
137                      + " : " + e.getMessage());
138         }
139     }
140
141     /**
142      * Criteria howto, section "Order by".
143      */

144     public void testCriteriaOrderBy()
145     {
146         List JavaDoc books = null;
147         try
148         {
149             Criteria criteria = new Criteria();
150             criteria.addAscendingOrderByColumn(BookPeer.TITLE);
151             criteria.addAscendingOrderByColumn(BookPeer.ISBN);
152     
153             books = BookPeer.doSelect(criteria);
154         }
155         catch (Exception JavaDoc e)
156         {
157             e.printStackTrace();
158             fail("Exception caught : "
159                      + e.getClass().getName()
160                      + " : " + e.getMessage());
161         }
162         
163         Book book = (Book) books.get(0);
164         assertTrue(
165                 "title of first book is not"
166                 + BOOK_1_TITLE
167                 + " but "
168                 + book.getTitle(),
169                 BOOK_1_TITLE.equals(book.getTitle())
170                 );
171
172         book = (Book) books.get(2);
173         assertTrue(
174                 "ISBN of third book is not"
175                 + BOOK_3_ISBN
176                 + " but "
177                 + book.getIsbn(),
178                 BOOK_3_ISBN.equals(book.getIsbn()));
179     }
180     
181     /**
182      * Criteria howto, section "Order by".
183      */

184     public void testCriteriaJoins()
185     {
186         // inner joins
187
List JavaDoc bookAuthors = null;
188         try
189         {
190             Criteria criteria = new Criteria();
191             criteria.addJoin(
192                     AuthorPeer.AUTHOR_ID,
193                     BookPeer.AUTHOR_ID,
194                     Criteria.INNER_JOIN);
195   
196             bookAuthors = AuthorPeer.doSelect(criteria);
197             
198             // from Details section
199
Author author = (Author) bookAuthors.get(0);
200             List JavaDoc books = author.getBooks();
201         }
202         catch (Exception JavaDoc e)
203         {
204             e.printStackTrace();
205             fail("inner join : Exception caught : "
206                      + e.getClass().getName()
207                      + " : " + e.getMessage());
208         }
209         
210         assertTrue(
211                 "inner join : size of bookAuthors is not 3, but"
212                 + bookAuthors.size(),
213                 bookAuthors.size() == 3);
214         
215         // test explicit sql statements from details section
216
List JavaDoc result = null;
217         try
218         {
219             result = BasePeer.executeQuery(
220                     "SELECT book.* FROM book "
221                     + "INNER JOIN author "
222                     + "ON book.AUTHOR_ID=author.AUTHOR_ID");
223         }
224         catch (Exception JavaDoc e)
225         {
226             e.printStackTrace();
227             fail("Explicit SQL query 1 : Exception caught : "
228                      + e.getClass().getName()
229                      + " : " + e.getMessage());
230         }
231         
232         assertTrue(
233             "Explicit SQL query 1 : size of result is not 3, but"
234             + result.size(),
235             result.size() == 3);
236
237         result = null;
238         try
239         {
240             result = BasePeer.executeQuery(
241                     "SELECT book.* FROM book,author "
242                     + "WHERE book.AUTHOR_ID=author.AUTHOR_ID");
243         }
244         catch (Exception JavaDoc e)
245         {
246             e.printStackTrace();
247             fail("Explicit SQL query 2 : Exception caught : "
248                      + e.getClass().getName()
249                      + " : " + e.getMessage());
250         }
251         
252         assertTrue(
253             "Explicit SQL query 2 : size of result is not 3, but"
254             + result.size(),
255             result.size() == 3);
256         
257         // test left outer join
258
bookAuthors = null;
259         try
260         {
261               Criteria criteria = new Criteria();
262               criteria.addJoin(
263                       AuthorPeer.AUTHOR_ID,
264                       BookPeer.AUTHOR_ID,
265                       Criteria.LEFT_JOIN);
266               bookAuthors = AuthorPeer.doSelect(criteria);
267         }
268         catch (Exception JavaDoc e)
269         {
270             e.printStackTrace();
271             fail("left join : Exception caught : "
272                      + e.getClass().getName()
273                      + " : " + e.getMessage());
274         }
275
276         assertTrue(
277             "left join : size of bookAuthors is not 4, but"
278             + bookAuthors.size(),
279             bookAuthors.size() == 4);
280
281     }
282     
283     /**
284      * Criteria Howto, section "Distinct".
285      */

286     public void testDistinct()
287     {
288         List JavaDoc bookAuthors = null;
289         try
290         {
291             Criteria criteria = new Criteria();
292             criteria.addJoin(
293                     AuthorPeer.AUTHOR_ID,
294                     BookPeer.AUTHOR_ID,
295                     Criteria.INNER_JOIN);
296             criteria.setDistinct();
297         
298             bookAuthors = AuthorPeer.doSelect(criteria);
299         }
300         catch (Exception JavaDoc e)
301         {
302             e.printStackTrace();
303             fail("Exception caught : "
304                      + e.getClass().getName()
305                      + " : " + e.getMessage());
306         }
307         assertTrue(
308             "size of bookAuthors is not 2, but"
309             + bookAuthors.size(),
310             bookAuthors.size() == 2);
311     }
312     
313     /**
314      * Criteria Howto, section "Join & Order & Distinct".
315      */

316     public void testJoinOrderDistinct()
317     {
318         List JavaDoc bookAuthors = null;
319         try
320         {
321             Criteria criteria = new Criteria();
322             criteria.addJoin(
323                     AuthorPeer.AUTHOR_ID,
324                     BookPeer.AUTHOR_ID,
325                     Criteria.INNER_JOIN);
326             criteria.setDistinct();
327             criteria.addAscendingOrderByColumn(AuthorPeer.NAME);
328                   
329             bookAuthors = AuthorPeer.doSelect(criteria);
330         }
331         catch (Exception JavaDoc e)
332         {
333             e.printStackTrace();
334             fail("Exception caught : "
335                      + e.getClass().getName()
336                      + " : " + e.getMessage());
337         }
338         assertTrue(
339             "size of bookAuthors is not 2, but"
340             + bookAuthors.size(),
341             bookAuthors.size() == 2);
342         
343         Author author = (Author) bookAuthors.get(0);
344         assertTrue(
345             "Author of first book is not"
346             + AUTHOR_1_NAME
347             + " but "
348             + author.getName(),
349             AUTHOR_1_NAME.equals(author.getName()));
350   
351         author = (Author) bookAuthors.get(1);
352         assertTrue(
353             "Author of second book is not"
354             + AUTHOR_2_NAME
355             + " but "
356             + author.getName(),
357             AUTHOR_2_NAME.equals(author.getName()));
358     }
359 }
360
Popular Tags