KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.odmg.ODMGSessionBean}.
34  *
35  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
36  * @version $Id: ODMGClient.java,v 1.4.2.2 2005/12/21 22:21:39 tomdz Exp $
37  */

38 public class ODMGClient extends TestCase
39 {
40     ODMGSessionRemote sampleBean;
41     static int loops = 500;
42
43     public ODMGClient(String JavaDoc s)
44     {
45         super(s);
46     }
47
48     public ODMGClient()
49     {
50         super(ODMGClient.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[] {ODMGClient.class.getName()});
57     }
58
59     protected void setUp() throws Exception JavaDoc
60     {
61         super.setUp();
62         init();
63     }
64
65     protected void tearDown() throws Exception JavaDoc
66     {
67         super.tearDown();
68     }
69
70     protected void init()
71     {
72         Context JavaDoc ctx = ContextHelper.getContext();
73         try
74         {
75             Object JavaDoc object = PortableRemoteObject.narrow(ctx.lookup(ODMGSessionHome.JNDI_NAME), EJBHome JavaDoc.class);
76             sampleBean = ((ODMGSessionHome) object).create();
77         }
78         catch (Exception JavaDoc e)
79         {
80             e.printStackTrace();
81         }
82     }
83
84     public void testInsertDelete() throws RemoteException JavaDoc
85     {
86         System.out.println("# test: testInsertDelete()");
87         int articlesBefore = sampleBean.getArticleCount();
88         int personsBefore = sampleBean.getPersonCount();
89
90         List JavaDoc articles = VOHelper.createNewArticleList(10);
91         List JavaDoc persons = VOHelper.createNewPersonList(5);
92         articles = sampleBean.storeObjects(articles);
93         persons = sampleBean.storeObjects(persons);
94
95         int articlesAfterStore = sampleBean.getArticleCount();
96         int personsAfterStore = sampleBean.getPersonCount();
97         assertEquals("Storing of articles failed", articlesBefore + 10, articlesAfterStore);
98         assertEquals("Storing of persons faile", personsBefore + 5, personsAfterStore);
99
100
101         sampleBean.deleteObjects(articles);
102         sampleBean.deleteObjects(persons);
103
104         int articlesAfterDelete = sampleBean.getArticleCount();
105         int personsAfterDelete = sampleBean.getPersonCount();
106         assertEquals("Deleting of articles failed", articlesAfterStore - 10, articlesAfterDelete);
107         assertEquals("Deleting of persons failed", personsAfterStore - 5, personsAfterDelete);
108     }
109
110     public void testStress() throws Exception JavaDoc
111     {
112         System.out.println("## ODMG-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             sampleBean.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 = sampleBean.getAllObjects(ArticleVO.class);
122         System.out.println("\n# Delete #");
123         int i =0;
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             sampleBean.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("## ODMG-api testStress END ##");
135     }
136
137     public void testServerSideMethods() throws RemoteException JavaDoc
138     {
139         System.out.println("## testServerSideMethods");
140         boolean result = sampleBean.allInOne(VOHelper.createNewArticleList(10), VOHelper.createNewPersonList(5));
141         assertTrue("Something happened on sever side test method - 'allInOne(...)'", result);
142     }
143 }
144
Popular Tags