1 package org.apache.ojb.ejb.odmg; 2 3 17 18 import javax.ejb.EJBHome ; 19 import javax.rmi.PortableRemoteObject ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 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 35 public class ArticleTestClient extends TestCase 36 { 37 private ArticleManagerODMGRemote odmgArticleBean; 38 39 public ArticleTestClient(String s) 40 { 41 super(s); 42 } 43 44 public ArticleTestClient() 45 { 46 super(ArticleTestClient.class.getName()); 47 } 48 49 public static void main(String [] args) 50 { 51 String [] arr = {ArticleTestClient.class.getName()}; 52 junit.textui.TestRunner.main(arr); 53 } 54 55 public void testODMGCollectionRetrieve() throws Exception 56 { 57 long timestamp = System.currentTimeMillis(); 58 String articleame = "collection_test_article" + timestamp; 59 String 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 result = odmgArticleBean.getCategoryByName(categoryName); 67 assertNotNull(result); 68 assertEquals(1, result.size()); 69 cat = (CategoryVO) result.iterator().next(); 70 Collection 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 76 { 77 long timestamp = System.currentTimeMillis(); 78 String articleName = "query_test_article_" + timestamp; 79 String 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 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 articles = cat.getAssignedArticles(); 94 assertNotNull(articles); 95 assertEquals("Wrong number of referenced articles", 6, articles.size()); 96 } 97 98 private CategoryVO odmgCreatePersistentCategoryWithArticles( 99 String categoryName, String articleName, int articleCount) throws Exception 100 { 101 CategoryVO cat = VOHelper.createNewCategory(categoryName); 102 cat = odmgArticleBean.storeCategory(cat); 104 ArrayList articles = new ArrayList (); 105 for (int i = 0; i < articleCount; i++) 106 { 107 ArticleVO art = VOHelper.createNewArticle(articleName, 1); 108 art.setCategory(cat); 110 art = odmgArticleBean.storeArticle(art); 112 articles.add(art); 113 } 114 if(articles.size() > 0) cat.setAssignedArticles(articles); 116 cat = odmgArticleBean.storeCategory(cat); 118 119 return cat; 120 } 121 122 123 protected void setUp() throws Exception 124 { 125 super.setUp(); 126 init(); 127 } 128 129 protected void init() throws Exception 130 { 131 try 132 { 133 Object object = PortableRemoteObject.narrow( 134 ContextHelper.getContext().lookup(ArticleManagerODMGHome.JNDI_NAME), EJBHome .class); 135 odmgArticleBean = ((ArticleManagerODMGHome) object).create(); 136 } 137 catch (Exception e) 138 { 139 e.printStackTrace(); 140 throw e; 141 } 142 } 143 144 } 145 | Popular Tags |