User: schulze
Date: 00/10/25 16:01:23
Added: src/main/org/jboss/test/bmp/beans BMPHelperSessionBean.java
SimpleBMPBean.java
Log:
Test for BMP added tests create/remove store/load activation/passivation transaction
rollback
Should work with almost all databases (used types: INTEGER, VARCHAR(200))
uses Hypersonic by default
Revision Changes Path
1.1
jbosstest/src/main/org/jboss/test/bmp/beans/BMPHelperSessionBean.java
Index: BMPHelperSessionBean.java
===================================================================
package org.jboss.test.bmp.beans;
import java.rmi.RemoteException;
import java.sql.*;
import javax.naming.*;
import javax.ejb.*;
import javax.sql.DataSource;
import org.jboss.test.bmp.interfaces.*;
public class BMPHelperSessionBean
implements SessionBean
{
SessionContext ctx = null;
private DataSource ds = null;
public void ejbCreate () throws CreateException, EJBException, RemoteException
{
try
{
ds = (DataSource)new InitialContext ().lookup ("java:comp/env/datasource");
}
catch (NamingException _ne)
{
throw new CreateException ("Datasource not found: "+_ne.getMessage ());
}
}
public boolean existsSimpleBeanTable () throws EJBException, RemoteException
{
return tableExists ("simpleBean");
}
public void createSimpleBeanTable () throws EJBException, RemoteException
{
createTable ("CREATE TABLE simpleBean (id INTEGER, name VARCHAR(200))");
}
public void dropSimpleBeanTable () throws EJBException, RemoteException
{
dropTable ("simpleBean");
}
public String doTest () throws EJBException, RemoteException
{
StringBuffer sb = new StringBuffer ();
SimpleBMP b;
try
{
SimpleBMPHome home = (SimpleBMPHome) new InitialContext ().lookup
("java:comp/env/bean");
b = home.findByPrimaryKey(new Integer (1));
}
catch (Exception _ne)
{
throw new EJBException ("couldnt find entity: "+_ne.getMessage ());
}
sb.append ("found: "+b.getName ()+"\n");
sb.append ("set name to \"Name for rollback\"\n");
b.setName ("Name for rollback");
sb.append ("current name is: "+b.getName ()+"\n");
try
{
sb.append ("now rolling back...\n");
ctx.getUserTransaction().rollback ();
sb.append ("current name is: "+b.getName ()+"\n");
}
catch (Exception _e)
{
sb.append ("Error on rolling back: "+_e.getMessage ()+"\n");
}
sb.append ("done.");
return sb.toString ();
}
private boolean tableExists (String _tableName) throws EJBException,
RemoteException
{
boolean result = false;
Connection con = null;
try
{
con = ds.getConnection ();
DatabaseMetaData dmd = con.getMetaData ();
ResultSet rs = dmd.getTables (con.getCatalog (), null, _tableName, null);
if (rs.next ())
result = true;
rs.close ();
}
catch (Exception _e)
{
throw new EJBException ("Error while looking up table: "+_e.getMessage ());
}
finally
{
try
{
if (con != null)
con.close ();
}
catch (Exception _sqle)
{
}
}
return result;
}
private void createTable (String _sql) throws EJBException, RemoteException
{
Connection con = null;
try
{
con = ds.getConnection ();
Statement s = con.createStatement ();
s.executeUpdate (_sql);
s.close ();
}
catch (Exception _e)
{
throw new EJBException ("Error while creating table: "+_e.getMessage ());
}
finally
{
try
{
if (con != null)
con.close ();
}
catch (Exception _sqle)
{
}
}
}
private void dropTable (String _tableName) throws EJBException, RemoteException
{
Connection con = null;
try
{
con = ds.getConnection ();
Statement s = con.createStatement ();
s.executeUpdate ("DROP TABLE "+_tableName);
s.close ();
}
catch (Exception _e)
{
throw new EJBException ("Error while dropping table: "+_e.getMessage ());
}
finally
{
try
{
if (con != null)
con.close ();
}
catch (Exception _sqle)
{
}
}
}
public void ejbActivate () throws EJBException, RemoteException {}
public void ejbPassivate () throws EJBException, RemoteException {}
public void ejbRemove () throws EJBException, RemoteException {}
public void setSessionContext (SessionContext _ctx) throws EJBException,
RemoteException {ctx = _ctx;}
}
1.1 jbosstest/src/main/org/jboss/test/bmp/beans/SimpleBMPBean.java
Index: SimpleBMPBean.java
===================================================================
package org.jboss.test.bmp.beans;
import java.rmi.RemoteException;
import java.util.Vector;
import java.util.Collection;
import java.sql.*;
import javax.naming.*;
import javax.ejb.*;
import javax.sql.DataSource;
public class SimpleBMPBean
implements EntityBean
{
EntityContext ctx = null;
DataSource ds = null;
// bmp fields
Integer id;
String name;
public Integer ejbCreate (int _id, String _name) throws CreateException,
DuplicateKeyException, EJBException, RemoteException
{
System.out.println ("ejbCreate (int, String) called");
id = new Integer (_id);
boolean dublicate = false;
Connection con = null;
try
{
con = ds.getConnection ();
Statement s = con.createStatement ();
ResultSet rs = s.executeQuery ("SELECT id FROM simplebean WHERE id=" +
id.toString ());
dublicate = rs.next ();
rs.close ();
s.close ();
if (!dublicate)
{
PreparedStatement ps = con.prepareStatement ("INSERT INTO simplebean
VALUES (?,?)");
ps.setInt (1, _id);
ps.setString (2, _name);
ps.execute ();
ps.close ();
name = _name;
}
}
catch (Exception _e)
{
throw new EJBException ("couldnt create: "+_e.getMessage ());
}
finally
{
try
{
if (con != null)
con.close ();
}
catch (Exception _sqle)
{
}
}
if (dublicate)
throw new DuplicateKeyException ("Bean with id="+_id+" already exists.");
return id;
}
public void ejbPostCreate (int _id, String _name) throws CreateException,
DuplicateKeyException, EJBException, RemoteException
{
System.out.println ("ejbPostCreate (int, String) called");
}
public void ejbLoad () throws EJBException, RemoteException
{
System.out.println ("ejbLoad () called");
Connection con = null;
try
{
con = ds.getConnection ();
PreparedStatement ps = con.prepareStatement ("SELECT id,name FROM
simplebean WHERE id=?");
ps.setInt (1, ((Integer)ctx.getPrimaryKey ()).intValue ());
ResultSet rs = ps.executeQuery ();
if (rs.next ())
{
id = new Integer (rs.getInt ("id"));
name = rs.getString ("name");
}
rs.close ();
ps.close ();
}
catch (Exception _e)
{
throw new EJBException ("couldnt load: "+_e.getMessage ());
}
finally
{
try
{
if (con != null)
con.close ();
}
catch (Exception _sqle)
{
}
}
}
public void ejbStore () throws EJBException, RemoteException
{
System.out.println ("ejbStore () called");
Connection con = null;
try
{
con = ds.getConnection ();
PreparedStatement ps = con.prepareStatement ("UPDATE simplebean SET name=?
WHERE id=?");
ps.setString (1, name);
ps.setInt (2, id.intValue ());
ps.execute ();
ps.close ();
}
catch (Exception _e)
{
throw new EJBException ("couldnt store: "+_e.getMessage ());
}
finally
{
try
{
if (con != null)
con.close ();
}
catch (Exception _sqle)
{
}
}
}
public void ejbRemove () throws EJBException, RemoteException
{
System.out.println ("ejbRemove () called");
Connection con = null;
try
{
con = ds.getConnection ();
PreparedStatement ps = con.prepareStatement ("DELETE FROM simplebean WHERE
id=?");
ps.setInt (1, id.intValue ());
ps.execute ();
ps.close ();
}
catch (Exception _e)
{
throw new EJBException ("couldnt remove: "+_e.getMessage ());
}
finally
{
try
{
if (con != null)
con.close ();
}
catch (Exception _sqle)
{
}
}
}
public Integer ejbFindByPrimaryKey (Integer _key) throws FinderException,
EJBException, RemoteException
{
System.out.println ("ejbFindByPrimaryKey (Integer) called");
Connection con = null;
boolean found = false;
try
{
con = ds.getConnection ();
PreparedStatement ps = con.prepareStatement ("SELECT id FROM simplebean
WHERE id=?");
ps.setInt (1, _key.intValue ());
ResultSet rs = ps.executeQuery ();
found = rs.next ();
rs.close ();
ps.close ();
}
catch (Exception _e)
{
throw new EJBException ("couldnt seek: "+_e.getMessage ());
}
finally
{
try
{
if (con != null)
con.close ();
}
catch (Exception _sqle)
{
}
}
if (!found)
throw new FinderException ("No bean with id="+_key+" found.");
return _key;
}
public Collection ejbFindAll () throws EJBException, RemoteException
{
System.out.println ("ejbFindAll () called");
Connection con = null;
Vector result = new Vector ();
try
{
con = ds.getConnection ();
Statement s = con.createStatement ();
ResultSet rs = s.executeQuery ("SELECT id FROM simplebean");
while (rs.next ())
{
result.add (new Integer (rs.getInt ("id")));
}
rs.close ();
s.close ();
}
catch (Exception _e)
{
throw new EJBException ("couldnt seek: "+_e.getMessage ());
}
finally
{
try
{
if (con != null)
con.close ();
}
catch (Exception _sqle)
{
}
}
return result;
}
public void ejbActivate () throws EJBException, RemoteException
{
System.out.println ("ejbActivate () called");
}
public void ejbPassivate () throws EJBException, RemoteException
{
System.out.println ("ejbPassivate () called");
}
public void setEntityContext (EntityContext _ctx) throws EJBException,
RemoteException
{
System.out.println ("setEntityContext (\""+_ctx.getPrimaryKey ()+"\") called");
ctx = _ctx;
// lookup the datasource
try
{
ds = (DataSource)new InitialContext ().lookup ("java:comp/env/datasource");
}
catch (NamingException _ne)
{
throw new EJBException ("Datasource not found: "+_ne.getMessage ());
}
}
public void unsetEntityContext () throws EJBException, RemoteException
{
System.out.println ("unsetEntityContext () called");
ctx = null;
}
// business methods
---------------------------------------------------------------
public void setName (String _name) throws RemoteException
{
name = _name;
}
public String getName () throws RemoteException
{
return name;
}
}