1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.loader; |
8 |
| |
9 |
| import org.jboss.cache.CacheImpl; |
10 |
| import org.jboss.cache.DefaultCacheFactory; |
11 |
| import org.jboss.cache.config.CacheLoaderConfig; |
12 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
13 |
| |
14 |
| import javax.naming.Context; |
15 |
| import javax.naming.InitialContext; |
16 |
| import javax.naming.NameNotFoundException; |
17 |
| import javax.sql.DataSource; |
18 |
| import java.io.PrintWriter; |
19 |
| import java.sql.Connection; |
20 |
| import java.sql.SQLException; |
21 |
| |
22 |
| public class DataSourceIntegrationTest extends AbstractCacheLoaderTestBase |
23 |
| { |
24 |
| private String old_factory = null; |
25 |
| private final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; |
26 |
| private final String JNDI_NAME = "java:/MockDS"; |
27 |
| private CacheImpl cache; |
28 |
| |
29 |
1
| protected void setUp() throws Exception
|
30 |
| { |
31 |
1
| super.setUp();
|
32 |
1
| old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
|
33 |
1
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
|
34 |
1
| DummyTransactionManager.getInstance();
|
35 |
| |
36 |
| } |
37 |
| |
38 |
| |
39 |
1
| protected CacheLoaderConfig getCacheLoaderConfig(String jndi) throws Exception
|
40 |
| { |
41 |
1
| String props = "cache.jdbc.datasource=" + jndi + "\ncache.jdbc.table.create=false\ncache.jdbc.table.drop=false";
|
42 |
1
| return getSingleCacheLoaderConfig("", "org.jboss.cache.loader.JDBCCacheLoader", props, false, false, false);
|
43 |
| } |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
1
| public void testDataSourceIntegration() throws Exception
|
52 |
| { |
53 |
1
| Context context = new InitialContext();
|
54 |
1
| try
|
55 |
| { |
56 |
1
| Object obj = context.lookup(JNDI_NAME);
|
57 |
1
| assertNull(JNDI_NAME + " not bound", obj);
|
58 |
| } |
59 |
| catch (NameNotFoundException n) |
60 |
| { |
61 |
| |
62 |
| } |
63 |
1
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
64 |
1
| cache.getConfiguration().setCacheMode("local");
|
65 |
1
| cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
66 |
1
| cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig(JNDI_NAME));
|
67 |
1
| cache.create();
|
68 |
| |
69 |
| |
70 |
1
| context.bind(JNDI_NAME, new MockDataSource());
|
71 |
1
| assertNotNull(JNDI_NAME + " bound", context.lookup(JNDI_NAME));
|
72 |
1
| cache.start();
|
73 |
| |
74 |
1
| assertNotNull("Cache has a cache loader", cache.getCacheLoaderManager().getCacheLoader());
|
75 |
| } |
76 |
| |
77 |
1
| protected void tearDown() throws Exception
|
78 |
| { |
79 |
1
| super.tearDown();
|
80 |
| |
81 |
1
| Context ctx = new InitialContext();
|
82 |
1
| ctx.unbind(JNDI_NAME);
|
83 |
1
| if (old_factory != null)
|
84 |
| { |
85 |
0
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
|
86 |
| } |
87 |
| else |
88 |
| { |
89 |
1
| System.getProperties().remove(Context.INITIAL_CONTEXT_FACTORY);
|
90 |
| } |
91 |
| |
92 |
| |
93 |
1
| if (cache != null)
|
94 |
| { |
95 |
1
| cache.stop();
|
96 |
1
| cache.destroy();
|
97 |
| } |
98 |
| } |
99 |
| |
100 |
| private static class MockDataSource implements DataSource |
101 |
| { |
102 |
| |
103 |
1
| public Connection getConnection() throws SQLException
|
104 |
| { |
105 |
1
| return null;
|
106 |
| } |
107 |
| |
108 |
0
| public Connection getConnection(String user, String password) throws SQLException
|
109 |
| { |
110 |
0
| return null;
|
111 |
| } |
112 |
| |
113 |
0
| public int getLoginTimeout() throws SQLException
|
114 |
| { |
115 |
0
| return 0;
|
116 |
| } |
117 |
| |
118 |
0
| public PrintWriter getLogWriter() throws SQLException
|
119 |
| { |
120 |
0
| return null;
|
121 |
| } |
122 |
| |
123 |
0
| public void setLoginTimeout(int seconds) throws SQLException
|
124 |
| { |
125 |
| } |
126 |
| |
127 |
0
| public void setLogWriter(PrintWriter printWriter) throws SQLException
|
128 |
| { |
129 |
| } |
130 |
| |
131 |
| } |
132 |
| } |