1 22 package org.jboss.test.bmp.test; 23 24 import java.util.Iterator ; 25 26 import javax.naming.InitialContext ; 27 28 import junit.framework.Test; 29 30 import org.jboss.test.JBossTestCase; 31 import org.jboss.test.bmp.interfaces.BMPHelperSession; 32 import org.jboss.test.bmp.interfaces.BMPHelperSessionHome; 33 import org.jboss.test.bmp.interfaces.SimpleBMP; 34 import org.jboss.test.bmp.interfaces.SimpleBMPHome; 35 36 public class BmpUnitTestCase 37 extends JBossTestCase 38 { 39 public BmpUnitTestCase(String name) 40 { 41 super(name); 42 } 43 44 public void testBMP() throws Exception 45 { 46 BMPHelperSessionHome sessionHome = (BMPHelperSessionHome)new InitialContext ().lookup ("bmp.BMPHelperSession"); 47 BMPHelperSession session = sessionHome.create (); 48 49 getLog().debug ("looking up table:"); 50 boolean exists = session.existsSimpleBeanTable (); 51 if (exists) 52 { 53 getLog().debug ("table exists."); 54 getLog().debug ("delete it..."); 55 session.dropSimpleBeanTable(); 56 getLog().debug ("done."); 57 } 58 59 getLog().debug ("table does not exist."); 60 getLog().debug ("create it..."); 61 session.createSimpleBeanTable(); 62 try 63 { 64 getLog().debug ("done."); 65 66 getLog().debug ("start playing with bmp beans."); 67 SimpleBMPHome home = (SimpleBMPHome)new InitialContext ().lookup ("bmp.SimpleBMP"); 68 getLog().debug ("create bean1: 1, Daniel"); 69 SimpleBMP b1 = home.create (1, "Daniel"); 70 getLog().debug ("getName (): "+b1.getName ()); 71 72 getLog().debug ("create bean2: 2, Robert"); 73 b1 = home.createMETHOD (2, "Robert"); 74 getLog().debug ("getName (): "+b1.getName ()); 75 76 try 77 { 78 getLog().debug ("trying to create one with same primkey: 1, Patrick"); 79 b1 = home.create (1, "Patrick"); 80 } 81 catch (Exception _e) 82 { 83 getLog().debug (_e.toString ()); 84 } 85 86 getLog().debug ("create some more dummys:"); 87 for (int i = 0; i < 50; ++i) 88 home.create (i + 3, ("Dummy "+i)); 89 90 try 91 { 92 getLog().debug ("trying to find Robert again"); 93 b1 = home.findByPrimaryKey (new Integer (2)); 94 getLog().debug ("getName (): "+b1.getName ()); 95 } 96 catch (Exception _e) 97 { 98 getLog().debug (_e.toString ()); 99 } 100 101 try 102 { 103 getLog().debug ("trying to find an not existing bean"); 104 b1 = home.findByPrimaryKey (new Integer (0)); 105 getLog().debug ("getName (): "+b1.getName ()); 106 } 107 catch (Exception _e) 108 { 109 getLog().debug (_e.toString ()); 110 } 111 112 113 getLog().debug ("rename Daniel to Maria: 1, Daniel"); 114 b1 = home.findByPrimaryKey (new Integer (1)); 115 getLog().debug ("name old: " + b1.getName ()); 116 b1.setName ("Maria"); 117 getLog().debug ("name new: " + b1.getName ()); 118 119 120 getLog().debug ("find all beans:"); 121 Iterator it = home.findAll ().iterator (); 122 while (it.hasNext ()) 123 { 124 getLog().debug ("found:"+((SimpleBMP)it.next ()).getName ()); 125 } 126 127 128 getLog().debug ("*******Now trying from within the Session bean (to be able to rollback):"); 129 getLog().debug (session.doTest ()); 130 getLog().debug ("Getting the name after a rollback:"); 131 getLog().debug (session.doTestAfterRollback ()); 132 133 getLog().debug ("removing all beans:"); 134 it = home.findAll ().iterator (); 135 while (it.hasNext ()) 136 ((SimpleBMP)it.next ()).remove (); 137 } 138 finally 139 { 140 getLog().debug ("table exists."); 141 getLog().debug ("delete it..."); 142 session.dropSimpleBeanTable(); 143 getLog().debug ("done."); 144 } 145 } 146 147 public static Test suite() throws Exception 148 { 149 return getDeploySetup(BmpUnitTestCase.class, "bmp.jar"); 150 } 151 } 152 | Popular Tags |