KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > ejb > pb > ArticleTestClient


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

17
18 import javax.ejb.EJBHome JavaDoc;
19 import javax.rmi.PortableRemoteObject JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22
23 import junit.framework.TestCase;
24 import org.apache.ojb.ejb.ArticleVO;
25 import org.apache.ojb.ejb.CategoryVO;
26 import org.apache.ojb.ejb.ContextHelper;
27 import org.apache.ojb.ejb.VOHelper;
28
29 /**
30  * Common test client class.
31  *
32  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
33  * @version $Id: ArticleTestClient.java,v 1.4.2.1 2005/12/21 22:21:38 tomdz Exp $
34  */

35 public class ArticleTestClient extends TestCase
36 {
37     private ArticleManagerPBRemote pbArticleBean;
38
39     public ArticleTestClient(String JavaDoc s)
40     {
41         super(s);
42     }
43
44     public ArticleTestClient()
45     {
46         super(ArticleTestClient.class.getName());
47     }
48
49     public static void main(String JavaDoc[] args)
50     {
51         String JavaDoc[] arr = {ArticleTestClient.class.getName()};
52         junit.textui.TestRunner.main(arr);
53     }
54
55     public void testPBCollectionRetrieve() throws Exception JavaDoc
56     {
57         long timestamp = System.currentTimeMillis();
58         String JavaDoc articleame = "collection_test_article" + timestamp;
59         String JavaDoc categoryName = "collection_test_category" + timestamp;
60         CategoryVO cat = pbCreatePersistentCategoryWithArticles(categoryName, articleame, 5);
61
62         assertNotNull(cat.getObjId());
63         assertNotNull(cat.getAssignedArticles());
64         assertEquals("Wrong number of referenced articles found", 5, cat.getAssignedArticles().size());
65
66         Collection JavaDoc result = pbArticleBean.getCategoryByName(categoryName);
67         assertNotNull(result);
68         assertEquals(1, result.size());
69         cat = (CategoryVO) result.iterator().next();
70         Collection JavaDoc articlesCol = cat.getAssignedArticles();
71         assertNotNull(articlesCol);
72         assertEquals("Wrong number of referenced articles found", 5, articlesCol.size());
73     }
74
75     public void testPBQueryObjects() throws Exception JavaDoc
76     {
77         long timestamp = System.currentTimeMillis();
78         String JavaDoc articleName = "query_test_article_" + timestamp;
79         String JavaDoc categoryName = "query_test_category_" + timestamp;
80         CategoryVO cat1 = pbCreatePersistentCategoryWithArticles(categoryName, articleName, 6);
81         CategoryVO cat2 = pbCreatePersistentCategoryWithArticles(categoryName, articleName, 6);
82         CategoryVO cat3 = pbCreatePersistentCategoryWithArticles(categoryName, articleName, 6);
83
84         Collection JavaDoc result = pbArticleBean.getArticles(articleName);
85         assertNotNull(result);
86         assertEquals("Wrong number of articles", 18, result.size());
87
88         result = pbArticleBean.getCategoryByName(categoryName);
89         assertNotNull(result);
90         assertEquals("Wrong number of returned category objects", 3, result.size());
91         CategoryVO cat = (CategoryVO) result.iterator().next();
92         assertNotNull(cat);
93         Collection JavaDoc articles = cat.getAssignedArticles();
94         assertNotNull(articles);
95         assertEquals("Wrong number of referenced articles", 6, articles.size());
96     }
97
98     private CategoryVO pbCreatePersistentCategoryWithArticles(
99             String JavaDoc categoryName, String JavaDoc articleName, int articleCount) throws Exception JavaDoc
100     {
101         CategoryVO cat = VOHelper.createNewCategory(categoryName);
102         // store new category
103
cat = pbArticleBean.storeCategory(cat);
104         ArrayList JavaDoc articles = new ArrayList JavaDoc();
105         for (int i = 0; i < articleCount; i++)
106         {
107             ArticleVO art = VOHelper.createNewArticle(articleName, 1);
108             // set category
109
art.setCategory(cat);
110             // store article
111
art = pbArticleBean.storeArticle(art);
112             articles.add(art);
113         }
114         // set article collection
115
if(articles.size() > 0) cat.setAssignedArticles(articles);
116         // persist updated category
117
cat = pbArticleBean.storeCategory(cat);
118
119         return cat;
120     }
121
122     protected void setUp() throws Exception JavaDoc
123     {
124         super.setUp();
125         init();
126     }
127
128     protected void init() throws Exception JavaDoc
129     {
130         try
131         {
132             Object JavaDoc object = PortableRemoteObject.narrow(
133                     ContextHelper.getContext().lookup(ArticleManagerPBHome.JNDI_NAME), EJBHome JavaDoc.class);
134             pbArticleBean = ((ArticleManagerPBHome) object).create();
135         }
136         catch (Exception JavaDoc e)
137         {
138             e.printStackTrace();
139             throw e;
140         }
141     }
142
143 }
144
Popular Tags