Regards.
________________ Manolo Ramirez T.
Leonardo Francalanci wrote:
In my DB tier I use an interface to get the connection. When used under Tomcat, the class that implements the connection gets the connecton through JNDI. I want to test these classes outside of Tomcat. Is there a simple way to put objects in the Context, I mean a simple way to emulate Tomcat behavior? I read the JNDI tutorial, but is the worse thing I've ever read.
At this moment I created a class that implements my interface and that open a simple connection to the db, but in this way I need to set the right interface (JNDI/Simple) in each class a want to test.
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
package com.handsoftware.utils;
import javax.naming.InitialContext; import javax.naming.Context; import javax.naming.NamingException; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class DBjndi { protected static DataSource ds; public static void init(String name) throws NamingException{ Context ctx = new InitialContext(); init(name,ctx); } public static void init(String name,Context context) throws NamingException { ds = (DataSource)context.lookup(name); } public static Connection getConnection() throws SQLException { if(ds == null) throw new IllegalStateException("DBjndi was not initialized"); return ds.getConnection(); } }
package com.handsoftware.utils.test; import java.util.Hashtable; import javax.naming.InitialContext; import javax.naming.NamingException; public class MockContext extends InitialContext { Hashtable data; public MockContext() throws NamingException{ super(); data = new Hashtable(); } public Object add2Context(String name,Object obj) { return data.put(name,obj); } public Object lookup(String name) throws NamingException { Object obj=data.get(name); if(obj!=null) return obj; return super.lookup(name); } }
package com.handsoftware.utils.test; import javax.sql.DataSource; import java.sql.*; import java.io.PrintWriter; public class MockDataSource implements DataSource { protected Driver m_driver; protected String m_url; protected String m_user; protected String m_passwd; public MockDataSource(String driverClass, String url, String user, String passwd) { try { m_driver = (Driver)Class.forName(driverClass).newInstance(); DriverManager.registerDriver(m_driver); } catch (Exception e) { e.printStackTrace(); } m_url=url; m_user=user; m_passwd=passwd; } public Connection getConnection() throws SQLException { return DriverManager.getConnection(m_url,m_user,m_passwd); } public Connection getConnection(String username,String password) throws SQLException { return DriverManager.getConnection(m_url,username,password); } public PrintWriter getLogWriter() throws SQLException { return null; } public void setLogWriter(PrintWriter out) throws SQLException { } public void setLoginTimeout(int seconds) throws SQLException { } public int getLoginTimeout() throws SQLException { return 0; } }
package com.handsoftware.utils; import junit.framework.TestCase; import com.handsoftware.utils.test.MockContext; import com.handsoftware.utils.test.MockDataSource; import java.sql.*; import javax.naming.NamingException; public class TestDBjndi extends TestCase { public TestDBjndi () { super(); } public void setUp() throws NamingException { MockContext context = new MockContext(); String jndiName="java:comp/env/jdbc/PruebasDB"; context.add2Context(jndiName,new MockDataSource("org.postgresql.Driver" ,"jdbc:postgresql://localhost:5432/pruebas" ,"manolo","") ); DBjndi.init(jndiName,context); } public void testConnection() { try { Connection con = DBjndi.getConnection(); //insercion PreparedStatement st = con.prepareStatement("insert into pruebas(id,nombre,apellido) values(?,?,?)"); st.setInt(1,666); st.setString(2,"juan"); st.setString(3,"perez"); st.execute(); //select st = con.prepareStatement("select nombre,apellido from pruebas where id=?"); st.setInt(1,666); ResultSet rset = st.executeQuery(); rset.next(); assertEquals("no funciona select:nombre",rset.getString("nombre"),"juan"); assertEquals("no funciona select: apellido",rset.getString("apellido"),"perez"); rset.close(); //delete st = con.prepareStatement("delete from pruebas where id=?"); st.setInt(1,666); st.execute(); st = con.prepareStatement("select * from pruebas where id=?"); st.setInt(1,666); rset = st.executeQuery(); rset.next(); assertFalse("No funciona delete!",rset.next()); } catch(SQLException e) { fail(e.toString()); } } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]