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.naming.Context;
020: import javax.rmi.PortableRemoteObject;
021: import java.rmi.RemoteException;
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import junit.framework.TestCase;
028: import org.apache.ojb.ejb.ArticleVO;
029: import org.apache.ojb.ejb.ContextHelper;
030: import org.apache.ojb.ejb.VOHelper;
031:
032: /**
033: * Test client using the {@link org.apache.ojb.ejb.pb.PBSessionBean}.
034: *
035: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
036: * @version $Id: PBClient.java,v 1.5.2.2 2005/12/21 22:21:38 tomdz Exp $
037: */
038: public class PBClient extends TestCase {
039: PBSessionRemote bean;
040: static int loops = 500;
041:
042: public PBClient(String s) {
043: super (s);
044: }
045:
046: public PBClient() {
047: super (PBClient.class.getName());
048: }
049:
050: public static void main(String[] args) {
051: loops = args.length > 0 ? new Integer(args[0]).intValue() : 500;
052: junit.textui.TestRunner.main(new String[] { PBClient.class
053: .getName() });
054: }
055:
056: protected void setUp() throws Exception {
057: super .setUp();
058: init();
059: }
060:
061: protected void init() {
062: Context ctx = ContextHelper.getContext();
063: try {
064: Object object = PortableRemoteObject.narrow(ctx
065: .lookup(PBSessionHome.JNDI_NAME), EJBHome.class);
066: bean = ((PBSessionHome) object).create();
067: } catch (Exception e) {
068: e.printStackTrace();
069: }
070: }
071:
072: public void testInsertDelete() throws RemoteException {
073: int articlesBefore = bean.getArticleCount();
074: int personsBefore = bean.getPersonCount();
075:
076: List articles = VOHelper.createNewArticleList(10);
077: List persons = VOHelper.createNewPersonList(5);
078: articles = bean.storeObjects(articles);
079: persons = bean.storeObjects(persons);
080:
081: int articlesAfterStore = bean.getArticleCount();
082: int personsAfterStore = bean.getPersonCount();
083: assertEquals("Storing of articles failed", articlesBefore + 10,
084: articlesAfterStore);
085: assertEquals("Storing of persons faile", personsBefore + 5,
086: personsAfterStore);
087:
088: bean.deleteObjects(articles);
089: bean.deleteObjects(persons);
090:
091: int articlesAfterDelete = bean.getArticleCount();
092: int personsAfterDelete = bean.getPersonCount();
093: assertEquals("Deleting of articles failed",
094: articlesAfterStore - 10, articlesAfterDelete);
095: assertEquals("Deleting of persons failed",
096: personsAfterStore - 5, personsAfterDelete);
097: }
098:
099: public void testServerSideMethods() throws RemoteException {
100: boolean result = bean.allInOne(VOHelper
101: .createNewArticleList(10), VOHelper
102: .createNewPersonList(5));
103: assertTrue(
104: "Something happened on sever side test method - 'allInOne(...)'",
105: result);
106: }
107:
108: public void testStress() throws Exception {
109: System.out.println("## PB-api testStress");
110: System.out.println("Stress test will be done with " + loops
111: + " loops");
112: System.out.println("# Store #");
113: for (int i = 0; i < loops; i++) {
114: bean.storeObjects(VOHelper.createNewArticleList(1));
115: if (i % 10 == 0)
116: System.out.print(".");
117: if (i % 400 == 0)
118: System.out.println();
119: }
120: Collection col = bean.getAllObjects(ArticleVO.class);
121: int i = 0;
122: System.out.println("\n# Delete #");
123: for (Iterator iterator = col.iterator(); iterator.hasNext();) {
124: ArticleVO article = (ArticleVO) iterator.next();
125: List del = new ArrayList();
126: del.add(article);
127: bean.deleteObjects(del);
128: if (++i % 10 == 0)
129: System.out.print(".");
130: if (i % 400 == 0)
131: System.out.println();
132: }
133: System.out.println("");
134: System.out.println("## PB-api testStress END ##");
135: }
136:
137: public void testStress2() throws Exception {
138: System.out.println("## PB-api testStress2");
139: System.out.println("# Store " + loops + " objects");
140: List newObjects = null;
141: for (int i = 0; i < loops; i++) {
142: newObjects = VOHelper.createNewArticleList(loops);
143: }
144: bean.storeObjects(newObjects);
145: List lookupObjects = new ArrayList(bean
146: .getAllObjects(ArticleVO.class));
147: System.out.println("# Delete " + loops + " objects");
148: bean.deleteObjects(lookupObjects);
149: System.out.println("## PB-api testStress2 END ##");
150: }
151: }
|