R: [OT] JNDI datasource lookup to test classes

2003-08-23 Thread Leonardo Francalanci
 The most common solution to this problem is using a Factory Method to
 return the correct implementation of the interface.  Your classes should
 never know which implementation is in use because they will all ask the
 Factory Method for the object.  You just need to change the one line in
 the method to return a different object for testing.

Ok, you mean something like that:

Class ConnectionHandlerFactory
...

public IConnectonHandler getConnectonHandler()
{
if (test)
return new ConnectionHandlerTest();

else
return new ConnectionHandlerForTomcat();


}

but I think this has 2 problems:

1) in my production code I'll have code that is written just for testing
purpose
2) Before executing any test I have to set the test variable to true.


Or I did not understand?


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: R: [OT] JNDI datasource lookup to test classes

2003-08-23 Thread James CE Johnson
 
  The most common solution to this problem is using a Factory Method to
  return the correct implementation of the interface.  Your classes should
  never know which implementation is in use because they will all ask the
  Factory Method for the object.  You just need to change the one line in
  the method to return a different object for testing.
 
 Ok, you mean something like that:
 
 Class ConnectionHandlerFactory
   ...
 
   public IConnectonHandler getConnectonHandler()
   {
   if (test)
   return new ConnectionHandlerTest();
 
   else
   return new ConnectionHandlerForTomcat();
 
 
   }
 
 but I think this has 2 problems:
 
 1) in my production code I'll have code that is written just for testing
 purpose
 2) Before executing any test I have to set the test variable to true.
 
 
 Or I did not understand?
 

That's the gist of it. We've solved it thusly:

if (Boolean.getBoolean(use.test.objects)) {
try {
return = (IConnectonHandler)
 Class.forName(
 path.to.ConnectionHandlerTest).newInstance();
} catch (Exception e) {
  log.fatal(Unable to create Test object., e);
  throw new RuntimeException(e);
}
} else {
return = new ConnectionHandlerForTomcat();
}

So when we're executing our tests we set the property
use.test.objects=true. It uses Class.forName() to create the object 
instance so that it doesn't have to know about the test object at
compile time or production runtime.

We also keep our testcase source in a separate, though parallel, tree in
the filesystem. That lets us easily build a production jarfile that
doesn't include any of the testcase classes.

(If you need to reach me please email directly as I don't always manage
to keep up with the mailing list.)

Later,
J


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: R: [OT] JNDI datasource lookup to test classes

2003-08-23 Thread James Harman
Alternatively,

If you are worried about test code in production, you could extend from 
ConnectionHandlerFactory with a TestConnectionHandlerFactor and override 
the getConnectionHandler() method to return the connectionHandler for 
testing. 

James

James CE Johnson wrote:

The most common solution to this problem is using a Factory Method to
return the correct implementation of the interface.  Your classes should
never know which implementation is in use because they will all ask the
Factory Method for the object.  You just need to change the one line in
the method to return a different object for testing.
 

Ok, you mean something like that:

Class ConnectionHandlerFactory
...
public IConnectonHandler getConnectonHandler()
{
if (test)
return new ConnectionHandlerTest();
else
return new ConnectionHandlerForTomcat();
	}

but I think this has 2 problems:

1) in my production code I'll have code that is written just for testing
purpose
2) Before executing any test I have to set the test variable to true.
Or I did not understand?

   

That's the gist of it. We've solved it thusly:

   if (Boolean.getBoolean(use.test.objects)) {
   try {
   return = (IConnectonHandler)
Class.forName(
path.to.ConnectionHandlerTest).newInstance();
   } catch (Exception e) {
 log.fatal(Unable to create Test object., e);
 throw new RuntimeException(e);
   }
   } else {
   return = new ConnectionHandlerForTomcat();
   }
So when we're executing our tests we set the property
use.test.objects=true. It uses Class.forName() to create the object 
instance so that it doesn't have to know about the test object at
compile time or production runtime.

We also keep our testcase source in a separate, though parallel, tree in
the filesystem. That lets us easily build a production jarfile that
doesn't include any of the testcase classes.
(If you need to reach me please email directly as I don't always manage
to keep up with the mailing list.)
Later,
J
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


[OT] JNDI datasource lookup to test classes

2003-08-22 Thread Leonardo Francalanci
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]



Re: [OT] JNDI datasource lookup to test classes

2003-08-22 Thread Manolo Ramirez T.
Maybe this files can help you.

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);

Re: [OT] JNDI datasource lookup to test classes

2003-08-22 Thread David Graham
--- Leonardo Francalanci [EMAIL PROTECTED] 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.

The most common solution to this problem is using a Factory Method to
return the correct implementation of the interface.  Your classes should
never know which implementation is in use because they will all ask the
Factory Method for the object.  You just need to change the one line in
the method to return a different object for testing.

David


 
 
 
 -
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 


__
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: [OT] JNDI datasource lookup to test classes

2003-08-22 Thread Adam Hardy
On 08/22/2003 04:43 PM Leonardo Francalanci wrote:
I read the JNDI tutorial, but is the worse thing I've ever read.
You've obviously never read any of my poetry.

--
struts 1.1 + tomcat 4.1.27 + java 1.4.2
Linux 2.4.20 RH9
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]