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.DriverManager; |
21 |
| import java.sql.SQLException; |
22 |
| import java.util.Properties; |
23 |
| |
24 |
| public class DataSourceIntegrationTest extends AbstractCacheLoaderTestBase |
25 |
| { |
26 |
| private String old_factory = null; |
27 |
| private final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; |
28 |
| private final String JNDI_NAME = "java:/MockDS"; |
29 |
| private CacheImpl cache; |
30 |
| |
31 |
1
| protected void setUp() throws Exception
|
32 |
| { |
33 |
1
| super.setUp();
|
34 |
1
| old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
|
35 |
1
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
|
36 |
1
| DummyTransactionManager.getInstance();
|
37 |
| |
38 |
| } |
39 |
| |
40 |
| |
41 |
1
| protected CacheLoaderConfig getCacheLoaderConfig(String jndi) throws Exception
|
42 |
| { |
43 |
1
| String props = "cache.jdbc.datasource=" + jndi + "\ncache.jdbc.table.create=true\ncache.jdbc.table.drop=true";
|
44 |
1
| CacheLoaderConfig cacheLoaderConfig = getSingleCacheLoaderConfig("", "org.jboss.cache.loader.JDBCCacheLoader", props, false, false, false);
|
45 |
1
| return cacheLoaderConfig;
|
46 |
| } |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
1
| public void testDataSourceIntegration() throws Exception
|
55 |
| { |
56 |
1
| Context context = new InitialContext();
|
57 |
1
| try
|
58 |
| { |
59 |
1
| Object obj = context.lookup(JNDI_NAME);
|
60 |
1
| assertNull(JNDI_NAME + " not bound", obj);
|
61 |
| } |
62 |
| catch (NameNotFoundException n) |
63 |
| { |
64 |
| |
65 |
| } |
66 |
1
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
67 |
1
| cache.getConfiguration().setCacheMode("local");
|
68 |
1
| cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
69 |
1
| cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig(JNDI_NAME));
|
70 |
1
| cache.create();
|
71 |
| |
72 |
| |
73 |
1
| context.bind(JNDI_NAME, new MockDataSource());
|
74 |
1
| assertNotNull(JNDI_NAME + " bound", context.lookup(JNDI_NAME));
|
75 |
1
| cache.start();
|
76 |
| |
77 |
1
| assertNotNull("Cache has a cache loader", cache.getCacheLoaderManager().getCacheLoader());
|
78 |
| } |
79 |
| |
80 |
1
| protected void tearDown() throws Exception
|
81 |
| { |
82 |
1
| super.tearDown();
|
83 |
| |
84 |
1
| Context ctx = new InitialContext();
|
85 |
1
| ctx.unbind(JNDI_NAME);
|
86 |
1
| if (old_factory != null)
|
87 |
| { |
88 |
0
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
|
89 |
| } |
90 |
| else |
91 |
| { |
92 |
1
| System.getProperties().remove(Context.INITIAL_CONTEXT_FACTORY);
|
93 |
| } |
94 |
| |
95 |
| |
96 |
1
| if (cache != null)
|
97 |
| { |
98 |
1
| cache.stop();
|
99 |
1
| cache.destroy();
|
100 |
| } |
101 |
| } |
102 |
| |
103 |
| private static class MockDataSource implements DataSource |
104 |
| { |
105 |
| private String userName; |
106 |
| private String jdbcUrl; |
107 |
| private String jdbcPassword; |
108 |
| |
109 |
1
| public MockDataSource()
|
110 |
| { |
111 |
1
| Properties properties = new Properties();
|
112 |
1
| try
|
113 |
| { |
114 |
1
| properties.load(this.getClass().getClassLoader().getResourceAsStream("cache-jdbc.properties"));
|
115 |
1
| Class.forName(properties.getProperty("cache.jdbc.driver"));
|
116 |
| } |
117 |
| catch (Exception e) |
118 |
| { |
119 |
0
| throw new IllegalStateException("Error loading jdbc properties ", e);
|
120 |
| } |
121 |
1
| userName = properties.getProperty("cache.jdbc.user");
|
122 |
1
| jdbcUrl = properties.getProperty("cache.jdbc.url");
|
123 |
1
| jdbcPassword = properties.getProperty("cache.jdbc.password");
|
124 |
| } |
125 |
| |
126 |
4
| public Connection getConnection() throws SQLException
|
127 |
| { |
128 |
4
| return DriverManager.getConnection(jdbcUrl, userName, jdbcPassword);
|
129 |
| } |
130 |
| |
131 |
0
| public Connection getConnection(String user, String password) throws SQLException
|
132 |
| { |
133 |
0
| return DriverManager.getConnection(jdbcUrl, userName, jdbcPassword);
|
134 |
| } |
135 |
| |
136 |
0
| public int getLoginTimeout() throws SQLException
|
137 |
| { |
138 |
0
| return 0;
|
139 |
| } |
140 |
| |
141 |
0
| public PrintWriter getLogWriter() throws SQLException
|
142 |
| { |
143 |
0
| return null;
|
144 |
| } |
145 |
| |
146 |
0
| public void setLoginTimeout(int seconds) throws SQLException
|
147 |
| { |
148 |
| } |
149 |
| |
150 |
0
| public void setLogWriter(PrintWriter printWriter) throws SQLException
|
151 |
| { |
152 |
| } |
153 |
| |
154 |
| |
155 |
0
| public boolean isWrapperFor(Class<?> ifc)
|
156 |
| { |
157 |
0
| return false;
|
158 |
| } |
159 |
| |
160 |
0
| public <T> T unwrap(Class<T> iface)
|
161 |
| { |
162 |
0
| return null;
|
163 |
| } |
164 |
| } |
165 |
| } |