KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > loader > DataSourceIntegrationTest


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.loader;
8
9 import org.jboss.cache.CacheImpl;
10 import org.jboss.cache.config.CacheLoaderConfig;
11 import org.jboss.cache.transaction.DummyTransactionManager;
12
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.naming.NameNotFoundException JavaDoc;
16 import javax.sql.DataSource JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import java.sql.Connection JavaDoc;
19 import java.sql.SQLException JavaDoc;
20
21 public class DataSourceIntegrationTest extends AbstractCacheLoaderTestBase
22 {
23    private String JavaDoc old_factory = null;
24    private final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
25    private final String JavaDoc JNDI_NAME = "java:/MockDS";
26    private CacheImpl cache;
27
28    protected void setUp() throws Exception JavaDoc
29    {
30       super.setUp();
31       old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
32       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
33       DummyTransactionManager.getInstance();
34
35    }
36
37
38    protected CacheLoaderConfig getCacheLoaderConfig(String JavaDoc jndi) throws Exception JavaDoc
39    {
40       String JavaDoc props = "cache.jdbc.datasource=" + jndi + "\ncache.jdbc.table.create=false\ncache.jdbc.table.drop=false";
41       return getSingleCacheLoaderConfig("", "org.jboss.cache.loader.JDBCCacheLoader", props, false, false, false);
42    }
43
44    /**
45     * Tests fix for JBCACHE-303, ensuring that JDBCCacheLoader works if
46     * its DataSource is not in JNDI until start is called.
47     *
48     * @throws Exception
49     */

50    public void testDataSourceIntegration() throws Exception JavaDoc
51    {
52       Context JavaDoc context = new InitialContext JavaDoc();
53       try
54       {
55          Object JavaDoc obj = context.lookup(JNDI_NAME);
56          assertNull(JNDI_NAME + " not bound", obj);
57       }
58       catch (NameNotFoundException JavaDoc n)
59       {
60          // expected
61
}
62       cache = new CacheImpl();
63       cache.getConfiguration().setCacheMode("local");
64       cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
65       cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig(JNDI_NAME));
66       cache.create();
67
68
69       context.bind(JNDI_NAME, new MockDataSource());
70       assertNotNull(JNDI_NAME + " bound", context.lookup(JNDI_NAME));
71       cache.start();
72
73       assertNotNull("Cache has a cache loader", cache.getCacheLoaderManager().getCacheLoader());
74    }
75
76    protected void tearDown() throws Exception JavaDoc
77    {
78       super.tearDown();
79
80       Context JavaDoc ctx = new InitialContext JavaDoc();
81       ctx.unbind(JNDI_NAME);
82       if (old_factory != null)
83       {
84          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
85       }
86       else
87       {
88          System.getProperties().remove(Context.INITIAL_CONTEXT_FACTORY);
89       }
90
91
92       if (cache != null)
93       {
94          cache.stop();
95          cache.destroy();
96       }
97    }
98
99    private static class MockDataSource implements DataSource JavaDoc
100    {
101
102       public Connection JavaDoc getConnection() throws SQLException JavaDoc
103       {
104          return null;
105       }
106
107       public Connection JavaDoc getConnection(String JavaDoc user, String JavaDoc password) throws SQLException JavaDoc
108       {
109          return null;
110       }
111
112       public int getLoginTimeout() throws SQLException JavaDoc
113       {
114          return 0;
115       }
116
117       public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc
118       {
119          return null;
120       }
121
122       public void setLoginTimeout(int seconds) throws SQLException JavaDoc
123       {
124       }
125
126       public void setLogWriter(PrintWriter JavaDoc printWriter) throws SQLException JavaDoc
127       {
128       }
129
130    }
131 }
132
Popular Tags