1 package org.apache.ojb.ejb.pb; 2 3 17 18 import javax.ejb.EJBHome ; 19 import javax.naming.Context ; 20 import javax.rmi.PortableRemoteObject ; 21 import java.rmi.RemoteException ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.List ; 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 38 public class PBClient extends TestCase 39 { 40 PBSessionRemote bean; 41 static int loops = 500; 42 43 public PBClient(String s) 44 { 45 super(s); 46 } 47 48 public PBClient() 49 { 50 super(PBClient.class.getName()); 51 } 52 53 public static void main(String [] args) 54 { 55 loops = args.length > 0 ? new Integer (args[0]).intValue(): 500; 56 junit.textui.TestRunner.main(new String []{PBClient.class.getName()}); 57 } 58 59 protected void setUp() throws Exception 60 { 61 super.setUp(); 62 init(); 63 } 64 65 protected void init() 66 { 67 Context ctx = ContextHelper.getContext(); 68 try 69 { 70 Object object = PortableRemoteObject.narrow(ctx.lookup(PBSessionHome.JNDI_NAME), EJBHome .class); 71 bean = ((PBSessionHome) object).create(); 72 } 73 catch (Exception e) 74 { 75 e.printStackTrace(); 76 } 77 } 78 79 public void testInsertDelete() throws RemoteException 80 { 81 int articlesBefore = bean.getArticleCount(); 82 int personsBefore = bean.getPersonCount(); 83 84 List articles = VOHelper.createNewArticleList(10); 85 List 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 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 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 col = bean.getAllObjects(ArticleVO.class); 122 int i =0; 123 System.out.println("\n# Delete #"); 124 for (Iterator iterator = col.iterator(); iterator.hasNext();) 125 { 126 ArticleVO article = (ArticleVO) iterator.next(); 127 List del = new ArrayList (); 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 138 { 139 System.out.println("## PB-api testStress2"); 140 System.out.println("# Store " + loops + " objects"); 141 List newObjects = null; 142 for (int i = 0; i < loops; i++) 143 { 144 newObjects = VOHelper.createNewArticleList(loops); 145 } 146 bean.storeObjects(newObjects); 147 List lookupObjects = new ArrayList (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 |