01: package net.sourceforge.jaxor.util.tests;
02:
03: import junit.framework.TestCase;
04: import net.sourceforge.jaxor.JaxorContextImpl;
05: import net.sourceforge.jaxor.JaxorSession;
06: import net.sourceforge.jaxor.api.ConnectionFactory;
07: import net.sourceforge.jaxor.api.JaxorContext;
08: import net.sourceforge.jaxor.api.JaxorContextFactory;
09: import net.sourceforge.jaxor.db.SingleConnectionTransaction;
10: import net.sourceforge.jaxor.example.db.HyperConnection;
11: import net.sourceforge.jaxor.util.JaxorFilter;
12:
13: import javax.servlet.FilterChain;
14: import javax.servlet.ServletException;
15: import javax.servlet.ServletRequest;
16: import javax.servlet.ServletResponse;
17: import java.io.IOException;
18: import java.sql.Connection;
19:
20: public class JaxorFilterTest extends TestCase {
21:
22: public void testCalling() throws ServletException, IOException {
23: JaxorFilter filter = new JaxorFilter();
24: JaxorContextFactory factory = creatTransactionFactory(HyperConnection.INSTANCE);
25: filter.setContextFactory(factory);
26: MockChain chain = new MockChain();
27: filter.doFilter(null, null, chain);
28: // this will work b/c there will be nothing to flush, so the datasource won't be accessed.
29: assertTrue(chain.calledFilter);
30: assertNull(JaxorSession.getJaxorContext());
31: }
32:
33: private JaxorContextFactory creatTransactionFactory(
34: final ConnectionFactory fact) {
35: JaxorContextFactory factory = new JaxorContextFactory() {
36: public JaxorContext create() {
37: return new JaxorContextImpl(
38: new SingleConnectionTransaction(fact));
39: }
40: };
41: return factory;
42: }
43:
44: public void testNeverGettingAConnection() throws IOException,
45: ServletException {
46: ConnectionFactory fact = new ConnectionFactory() {
47: public Connection getConnection() {
48: fail("Failed: should not get a connection ever");
49: return null;
50: }
51: };
52: JaxorFilter filter = new JaxorFilter();
53: filter.setContextFactory(creatTransactionFactory(fact));
54: MockChain chain = new MockChain();
55: filter.doFilter(null, null, chain);
56: // this will work b/c there will be nothing to flush, so the datasource won't be accessed.
57: assertTrue(chain.calledFilter);
58: }
59:
60: private class MockChain implements FilterChain {
61: public boolean calledFilter = false;
62:
63: public void doFilter(ServletRequest arg0, ServletResponse arg1)
64: throws IOException, ServletException {
65: calledFilter = true;
66: }
67: }
68:
69: }
|