KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.ejb.odmg;
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.5.2.1 2005/12/21 22:21:39 tomdz Exp $
34  */

35 public class ArticleTestClient extends TestCase
36 {
37     private ArticleManagerODMGRemote odmgArticleBean;
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 testODMGCollectionRetrieve() 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 = odmgCreatePersistentCategoryWithArticles(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 = odmgArticleBean.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 testODMGQueryObjects() 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 = odmgCreatePersistentCategoryWithArticles(categoryName, articleName, 6);
81         CategoryVO cat2 = odmgCreatePersistentCategoryWithArticles(categoryName, articleName, 6);
82         CategoryVO cat3 = odmgCreatePersistentCategoryWithArticles(categoryName, articleName, 6);
83
84         Collection JavaDoc result = odmgArticleBean.getArticles(articleName);
85         assertNotNull(result);
86         assertEquals("Wrong number of articles", 18, result.size());
87
88         result = odmgArticleBean.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 odmgCreatePersistentCategoryWithArticles(
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 = odmgArticleBean.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 = odmgArticleBean.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 = odmgArticleBean.storeCategory(cat);
118
119         return cat;
120     }
121
122
123     protected void setUp() throws Exception JavaDoc
124     {
125         super.setUp();
126         init();
127     }
128
129     protected void init() throws Exception JavaDoc
130     {
131         try
132         {
133             Object JavaDoc object = PortableRemoteObject.narrow(
134                     ContextHelper.getContext().lookup(ArticleManagerODMGHome.JNDI_NAME), EJBHome JavaDoc.class);
135             odmgArticleBean = ((ArticleManagerODMGHome) object).create();
136         }
137         catch (Exception JavaDoc e)
138         {
139             e.printStackTrace();
140             throw e;
141         }
142     }
143
144 }
145
Popular Tags