001: package org.apache.ojb.ejb.pb;
002:
003: /* Copyright 2004-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import javax.ejb.EJBHome;
019: import javax.rmi.PortableRemoteObject;
020: import java.util.ArrayList;
021: import java.util.Collection;
022:
023: import junit.framework.TestCase;
024: import org.apache.ojb.ejb.ArticleVO;
025: import org.apache.ojb.ejb.CategoryVO;
026: import org.apache.ojb.ejb.ContextHelper;
027: import org.apache.ojb.ejb.VOHelper;
028:
029: /**
030: * Common test client class.
031: *
032: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
033: * @version $Id: ArticleTestClient.java,v 1.4.2.1 2005/12/21 22:21:38 tomdz Exp $
034: */
035: public class ArticleTestClient extends TestCase {
036: private ArticleManagerPBRemote pbArticleBean;
037:
038: public ArticleTestClient(String s) {
039: super (s);
040: }
041:
042: public ArticleTestClient() {
043: super (ArticleTestClient.class.getName());
044: }
045:
046: public static void main(String[] args) {
047: String[] arr = { ArticleTestClient.class.getName() };
048: junit.textui.TestRunner.main(arr);
049: }
050:
051: public void testPBCollectionRetrieve() throws Exception {
052: long timestamp = System.currentTimeMillis();
053: String articleame = "collection_test_article" + timestamp;
054: String categoryName = "collection_test_category" + timestamp;
055: CategoryVO cat = pbCreatePersistentCategoryWithArticles(
056: categoryName, articleame, 5);
057:
058: assertNotNull(cat.getObjId());
059: assertNotNull(cat.getAssignedArticles());
060: assertEquals("Wrong number of referenced articles found", 5,
061: cat.getAssignedArticles().size());
062:
063: Collection result = pbArticleBean
064: .getCategoryByName(categoryName);
065: assertNotNull(result);
066: assertEquals(1, result.size());
067: cat = (CategoryVO) result.iterator().next();
068: Collection articlesCol = cat.getAssignedArticles();
069: assertNotNull(articlesCol);
070: assertEquals("Wrong number of referenced articles found", 5,
071: articlesCol.size());
072: }
073:
074: public void testPBQueryObjects() throws Exception {
075: long timestamp = System.currentTimeMillis();
076: String articleName = "query_test_article_" + timestamp;
077: String categoryName = "query_test_category_" + timestamp;
078: CategoryVO cat1 = pbCreatePersistentCategoryWithArticles(
079: categoryName, articleName, 6);
080: CategoryVO cat2 = pbCreatePersistentCategoryWithArticles(
081: categoryName, articleName, 6);
082: CategoryVO cat3 = pbCreatePersistentCategoryWithArticles(
083: categoryName, articleName, 6);
084:
085: Collection result = pbArticleBean.getArticles(articleName);
086: assertNotNull(result);
087: assertEquals("Wrong number of articles", 18, result.size());
088:
089: result = pbArticleBean.getCategoryByName(categoryName);
090: assertNotNull(result);
091: assertEquals("Wrong number of returned category objects", 3,
092: result.size());
093: CategoryVO cat = (CategoryVO) result.iterator().next();
094: assertNotNull(cat);
095: Collection articles = cat.getAssignedArticles();
096: assertNotNull(articles);
097: assertEquals("Wrong number of referenced articles", 6, articles
098: .size());
099: }
100:
101: private CategoryVO pbCreatePersistentCategoryWithArticles(
102: String categoryName, String articleName, int articleCount)
103: throws Exception {
104: CategoryVO cat = VOHelper.createNewCategory(categoryName);
105: // store new category
106: cat = pbArticleBean.storeCategory(cat);
107: ArrayList articles = new ArrayList();
108: for (int i = 0; i < articleCount; i++) {
109: ArticleVO art = VOHelper.createNewArticle(articleName, 1);
110: // set category
111: art.setCategory(cat);
112: // store article
113: art = pbArticleBean.storeArticle(art);
114: articles.add(art);
115: }
116: // set article collection
117: if (articles.size() > 0)
118: cat.setAssignedArticles(articles);
119: // persist updated category
120: cat = pbArticleBean.storeCategory(cat);
121:
122: return cat;
123: }
124:
125: protected void setUp() throws Exception {
126: super .setUp();
127: init();
128: }
129:
130: protected void init() throws Exception {
131: try {
132: Object object = PortableRemoteObject.narrow(ContextHelper
133: .getContext()
134: .lookup(ArticleManagerPBHome.JNDI_NAME),
135: EJBHome.class);
136: pbArticleBean = ((ArticleManagerPBHome) object).create();
137: } catch (Exception e) {
138: e.printStackTrace();
139: throw e;
140: }
141: }
142:
143: }
|