KickJava   Java API By Example, From Geeks To Geeks.

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


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.naming.Context JavaDoc;
20 import javax.rmi.PortableRemoteObject JavaDoc;
21 import java.rmi.RemoteException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import junit.framework.TestCase;
28 import org.apache.ojb.ejb.ArticleVO;
29 import org.apache.ojb.ejb.ContextHelper;
30 import org.apache.ojb.ejb.VOHelper;
31
32 /**
33  * Test client using the {@link org.apache.ojb.ejb.pb.PBSessionBean}.
34  *
35  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
36  * @version $Id: PBClient.java,v 1.5.2.2 2005/12/21 22:21:38 tomdz Exp $
37  */

38 public class PBClient extends TestCase
39 {
40     PBSessionRemote bean;
41     static int loops = 500;
42
43     public PBClient(String JavaDoc s)
44     {
45         super(s);
46     }
47
48     public PBClient()
49     {
50         super(PBClient.class.getName());
51     }
52
53     public static void main(String JavaDoc[] args)
54     {
55         loops = args.length > 0 ? new Integer JavaDoc(args[0]).intValue(): 500;
56         junit.textui.TestRunner.main(new String JavaDoc[]{PBClient.class.getName()});
57     }
58
59     protected void setUp() throws Exception JavaDoc
60     {
61         super.setUp();
62         init();
63     }
64
65     protected void init()
66     {
67         Context JavaDoc ctx = ContextHelper.getContext();
68         try
69         {
70             Object JavaDoc object = PortableRemoteObject.narrow(ctx.lookup(PBSessionHome.JNDI_NAME), EJBHome JavaDoc.class);
71             bean = ((PBSessionHome) object).create();
72         }
73         catch (Exception JavaDoc e)
74         {
75             e.printStackTrace();
76         }
77     }
78
79     public void testInsertDelete() throws RemoteException JavaDoc
80     {
81         int articlesBefore = bean.getArticleCount();
82         int personsBefore = bean.getPersonCount();
83
84         List JavaDoc articles = VOHelper.createNewArticleList(10);
85         List JavaDoc persons = VOHelper.createNewPersonList(5);
86         articles = bean.storeObjects(articles);
87         persons = bean.storeObjects(persons);
88
89         int articlesAfterStore = bean.getArticleCount();
90         int personsAfterStore = bean.getPersonCount();
91         assertEquals("Storing of articles failed", articlesBefore + 10, articlesAfterStore);
92         assertEquals("Storing of persons faile", personsBefore + 5, personsAfterStore);
93
94
95         bean.deleteObjects(articles);
96         bean.deleteObjects(persons);
97
98         int articlesAfterDelete = bean.getArticleCount();
99         int personsAfterDelete = bean.getPersonCount();
100         assertEquals("Deleting of articles failed", articlesAfterStore - 10, articlesAfterDelete);
101         assertEquals("Deleting of persons failed", personsAfterStore - 5, personsAfterDelete);
102     }
103
104     public void testServerSideMethods() throws RemoteException JavaDoc
105     {
106         boolean result = bean.allInOne(VOHelper.createNewArticleList(10), VOHelper.createNewPersonList(5));
107         assertTrue("Something happened on sever side test method - 'allInOne(...)'", result);
108     }
109
110     public void testStress() throws Exception JavaDoc
111     {
112         System.out.println("## PB-api testStress");
113         System.out.println("Stress test will be done with " + loops + " loops");
114         System.out.println("# Store #");
115         for (int i = 0; i < loops; i++)
116         {
117             bean.storeObjects(VOHelper.createNewArticleList(1));
118             if(i%10==0)System.out.print(".");
119             if(i%400==0)System.out.println();
120         }
121         Collection JavaDoc col = bean.getAllObjects(ArticleVO.class);
122         int i =0;
123         System.out.println("\n# Delete #");
124         for (Iterator JavaDoc iterator = col.iterator(); iterator.hasNext();)
125         {
126             ArticleVO article = (ArticleVO) iterator.next();
127             List JavaDoc del = new ArrayList JavaDoc();
128             del.add(article);
129             bean.deleteObjects(del);
130             if(++i%10==0)System.out.print(".");
131             if(i%400==0)System.out.println();
132         }
133         System.out.println("");
134         System.out.println("## PB-api testStress END ##");
135     }
136
137     public void testStress2() throws Exception JavaDoc
138     {
139         System.out.println("## PB-api testStress2");
140         System.out.println("# Store " + loops + " objects");
141         List JavaDoc newObjects = null;
142         for (int i = 0; i < loops; i++)
143         {
144             newObjects = VOHelper.createNewArticleList(loops);
145         }
146         bean.storeObjects(newObjects);
147         List JavaDoc lookupObjects = new ArrayList JavaDoc(bean.getAllObjects(ArticleVO.class));
148         System.out.println("# Delete " + loops + " objects");
149         bean.deleteObjects(lookupObjects);
150         System.out.println("## PB-api testStress2 END ##");
151     }
152 }
153
Popular Tags