1 |
| package org.jboss.cache.loader; |
2 |
| |
3 |
| import junit.framework.TestCase; |
4 |
| import org.jboss.cache.CacheImpl; |
5 |
| import org.jboss.cache.DefaultCacheFactory; |
6 |
| import org.jboss.cache.Fqn; |
7 |
| import org.jboss.cache.config.CacheLoaderConfig; |
8 |
| import org.jboss.cache.factories.XmlConfigurationParser; |
9 |
| import org.jboss.cache.marshall.NodeData; |
10 |
| import org.jboss.cache.statetransfer.StateTransferManager; |
11 |
| import org.jboss.cache.xml.XmlHelper; |
12 |
| import org.jboss.util.stream.MarshalledValueInputStream; |
13 |
| import org.jboss.util.stream.MarshalledValueOutputStream; |
14 |
| import org.w3c.dom.Element; |
15 |
| |
16 |
| import java.io.ByteArrayInputStream; |
17 |
| import java.io.ByteArrayOutputStream; |
18 |
| import java.util.ArrayList; |
19 |
| import java.util.HashSet; |
20 |
| import java.util.Properties; |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| public class AdjListJDBCCacheLoaderCompatibilityTest extends TestCase |
31 |
| { |
32 |
| |
33 |
| private JDBCCacheLoaderOld oldImpl; |
34 |
| |
35 |
| private JDBCCacheLoader newImpl; |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
6
| protected void setUp() throws Exception
|
45 |
| { |
46 |
6
| super.setUp();
|
47 |
6
| newImpl = getNewCacheLoader();
|
48 |
6
| oldImpl = getOldLoader();
|
49 |
6
| CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
50 |
6
| CacheImpl cache2 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
51 |
6
| newImpl.setCache(cache);
|
52 |
6
| oldImpl.setCache(cache2);
|
53 |
6
| oldImpl.start();
|
54 |
6
| oldImpl.remove(Fqn.ROOT);
|
55 |
| } |
56 |
| |
57 |
6
| protected void tearDown() throws Exception
|
58 |
| { |
59 |
6
| super.tearDown();
|
60 |
6
| oldImpl.remove(Fqn.ROOT);
|
61 |
6
| oldImpl.stop();
|
62 |
6
| newImpl.stop();
|
63 |
| } |
64 |
| |
65 |
1
| public void testCommonOperations() throws Exception
|
66 |
| { |
67 |
1
| newImpl.start();
|
68 |
1
| oldImpl.put(Fqn.fromString("/a/b/c"), "key1", "value1");
|
69 |
1
| oldImpl.put(Fqn.fromString("/a/b/d"), "key2", "value2");
|
70 |
1
| oldImpl.put(Fqn.fromString("/a/b/e"), "key3", "value3");
|
71 |
1
| assertTrue(newImpl.exists(Fqn.fromString("/a/b/c")));
|
72 |
1
| assertTrue(newImpl.exists(Fqn.fromString("/a/b/d")));
|
73 |
1
| assertTrue(newImpl.exists(Fqn.fromString("/a/b/e")));
|
74 |
1
| assertEquals("value1", newImpl.get(Fqn.fromString("/a/b/c")).get("key1"));
|
75 |
1
| assertEquals("value2", newImpl.get(Fqn.fromString("/a/b/d")).get("key2"));
|
76 |
1
| assertEquals("value3", newImpl.get(Fqn.fromString("/a/b/e")).get("key3"));
|
77 |
| } |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
1
| public void testRemove() throws Exception
|
83 |
| { |
84 |
1
| oldImpl.put(Fqn.fromString("/a/b/c"), "key1", "value1");
|
85 |
1
| oldImpl.put(Fqn.fromString("/a/b/d"), "key2", "value2");
|
86 |
1
| oldImpl.put(Fqn.fromString("/a/b/e"), "key3", "value3");
|
87 |
1
| oldImpl.put(Fqn.fromString("/a/f/e"), "key4", "value4");
|
88 |
1
| assertTrue(newImpl.exists(Fqn.fromString("/a/b/c")));
|
89 |
1
| assertTrue(newImpl.exists(Fqn.fromString("/a/b/d")));
|
90 |
1
| assertTrue(newImpl.exists(Fqn.fromString("/a/b/e")));
|
91 |
1
| newImpl.start();
|
92 |
1
| newImpl.remove(Fqn.fromString("/a/b"));
|
93 |
1
| assertFalse(newImpl.exists(Fqn.fromString("/a/b/c")));
|
94 |
1
| assertFalse(newImpl.exists(Fqn.fromString("/a/b/d")));
|
95 |
1
| assertFalse(newImpl.exists(Fqn.fromString("/a/b/e")));
|
96 |
1
| assertTrue(newImpl.exists(Fqn.fromString("/a/f")));
|
97 |
1
| assertTrue(newImpl.exists(Fqn.fromString("/a/f/e")));
|
98 |
| } |
99 |
| |
100 |
1
| public void testLoadEntireState() throws Exception
|
101 |
| { |
102 |
1
| oldImpl.put(Fqn.fromString("/a/b/c"), "key1", "value1");
|
103 |
1
| oldImpl.put(Fqn.fromString("/a/b/d"), "key2", "value2");
|
104 |
1
| oldImpl.put(Fqn.fromString("/a/b/e"), "key3", "value3");
|
105 |
1
| oldImpl.put(Fqn.fromString("/a/f/e"), "key4", "value4");
|
106 |
1
| oldImpl.put(Fqn.ROOT, "root_key", "root_value");
|
107 |
| |
108 |
1
| ByteArrayOutputStream newBaos = new ByteArrayOutputStream(1024);
|
109 |
1
| MarshalledValueOutputStream newOs = new MarshalledValueOutputStream(newBaos);
|
110 |
1
| newImpl.start();
|
111 |
1
| newImpl.loadEntireState(newOs);
|
112 |
1
| newImpl.getMarshaller().objectToObjectStream(StateTransferManager.STREAMING_DELIMITER_NODE, newOs);
|
113 |
1
| newOs.close();
|
114 |
1
| newImpl.remove(Fqn.ROOT);
|
115 |
1
| assertNull(newImpl.get(Fqn.fromString("/a/b/c")));
|
116 |
1
| assertNull(newImpl.get(Fqn.fromString("/a/b/d")));
|
117 |
1
| assertNull(newImpl.get(Fqn.fromString("/a/b/e")));
|
118 |
1
| assertNull(newImpl.get(Fqn.fromString("/a/f/e")));
|
119 |
1
| assertNull(newImpl.get(Fqn.ROOT));
|
120 |
1
| ByteArrayInputStream bais = new ByteArrayInputStream(newBaos.toByteArray());
|
121 |
1
| MarshalledValueInputStream is = new MarshalledValueInputStream(bais);
|
122 |
1
| newImpl.storeEntireState(is);
|
123 |
1
| assertEquals(newImpl.get(Fqn.fromString("/a/b/c")).get("key1"), "value1");
|
124 |
1
| assertEquals(newImpl.get(Fqn.fromString("/a/b/d")).get("key2"), "value2");
|
125 |
1
| assertEquals(newImpl.get(Fqn.fromString("/a/b/e")).get("key3"), "value3");
|
126 |
1
| assertEquals(newImpl.get(Fqn.fromString("/a/f/e")).get("key4"), "value4");
|
127 |
1
| assertEquals(newImpl.get(Fqn.ROOT).get("root_key"), "root_value");
|
128 |
1
| assertEquals(newImpl.getNodeCount(), 8);
|
129 |
| } |
130 |
| |
131 |
1
| public void testLoadNodeState() throws Exception
|
132 |
| { |
133 |
1
| oldImpl.put(Fqn.fromString("/a/b/c"), "key1", "value1");
|
134 |
1
| oldImpl.put(Fqn.fromString("/a/b/d"), "key2", "value2");
|
135 |
1
| oldImpl.put(Fqn.fromString("/a/b/e"), "key3", "value3");
|
136 |
1
| oldImpl.put(Fqn.fromString("/a/f/e"), "key4", "value4");
|
137 |
1
| oldImpl.put(Fqn.ROOT, "root_key", "root_value");
|
138 |
| |
139 |
1
| ByteArrayOutputStream newBaos = new ByteArrayOutputStream(1024);
|
140 |
1
| MarshalledValueOutputStream newOs = new MarshalledValueOutputStream(newBaos);
|
141 |
1
| newImpl.start();
|
142 |
1
| newImpl.loadState(Fqn.fromString("/a/b"), newOs);
|
143 |
1
| newImpl.getMarshaller().objectToObjectStream(StateTransferManager.STREAMING_DELIMITER_NODE, newOs);
|
144 |
1
| newOs.close();
|
145 |
| |
146 |
1
| newImpl.remove(Fqn.fromString("/a/b"));
|
147 |
1
| assertNull(newImpl.get(Fqn.fromString("/a/b/c")));
|
148 |
1
| assertNull(newImpl.get(Fqn.fromString("/a/b/d")));
|
149 |
1
| assertNull(newImpl.get(Fqn.fromString("/a/b/e")));
|
150 |
1
| assertNull(newImpl.get(Fqn.fromString("/a/b")));
|
151 |
| |
152 |
1
| ByteArrayInputStream bais = new ByteArrayInputStream(newBaos.toByteArray());
|
153 |
1
| MarshalledValueInputStream is = new MarshalledValueInputStream(bais);
|
154 |
1
| newImpl.storeState(Fqn.fromString("/a/b"), is);
|
155 |
| |
156 |
1
| assertEquals(newImpl.get(Fqn.fromString("/a/b/c")).get("key1"), "value1");
|
157 |
1
| assertEquals(newImpl.get(Fqn.fromString("/a/b/d")).get("key2"), "value2");
|
158 |
1
| assertEquals(newImpl.get(Fqn.fromString("/a/b/e")).get("key3"), "value3");
|
159 |
1
| assertEquals(newImpl.get(Fqn.fromString("/a/f/e")).get("key4"), "value4");
|
160 |
1
| assertEquals(newImpl.get(Fqn.ROOT).get("root_key"), "root_value");
|
161 |
1
| assertEquals(newImpl.getNodeCount(), 8);
|
162 |
| } |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
1
| public void testGetNodeData() throws Exception
|
169 |
| { |
170 |
1
| oldImpl.put(Fqn.fromString("/a/b/c"), "key1", "value1");
|
171 |
1
| oldImpl.put(Fqn.fromString("/a/b/d"), "key2", "value2");
|
172 |
1
| oldImpl.put(Fqn.fromString("/a/b/e"), "key3", "value3");
|
173 |
1
| oldImpl.put(Fqn.fromString("/a/f/e"), "key4", "value4");
|
174 |
1
| oldImpl.put(Fqn.ROOT, "root_key", "root_value");
|
175 |
1
| newImpl.start();
|
176 |
1
| ArrayList<NodeData> oldList = new ArrayList<NodeData>();
|
177 |
1
| oldImpl.getNodeDataList(Fqn.ROOT, oldList);
|
178 |
1
| ArrayList<NodeData> newList = new ArrayList<NodeData>();
|
179 |
1
| newImpl.getNodeDataList(Fqn.ROOT, newList);
|
180 |
1
| assertEquals(new HashSet<NodeData>(oldList), new HashSet<NodeData>(newList));
|
181 |
| } |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
1
| public void testStartWork() throws Exception
|
187 |
| { |
188 |
1
| oldImpl.put(Fqn.fromString("/a/b/c"), "key1", "value1");
|
189 |
1
| oldImpl.put(Fqn.fromString("/a/b/d"), "key2", "value2");
|
190 |
1
| assertNull(oldImpl.get(Fqn.ROOT));
|
191 |
1
| newImpl.start();
|
192 |
1
| assertNotNull(newImpl.get(Fqn.ROOT));
|
193 |
| } |
194 |
| |
195 |
| |
196 |
12
| protected CacheLoaderConfig getSingleCacheLoaderConfig(String preload, String cacheloaderClass, String properties) throws Exception
|
197 |
| { |
198 |
12
| String xml = "<config>\n" +
|
199 |
| "<passivation>false</passivation>\n" + |
200 |
| "<preload>" + preload + "</preload>\n" + |
201 |
| "<cacheloader>\n" + |
202 |
| "<class>" + cacheloaderClass + "</class>\n" + |
203 |
| "<properties>" + properties + "</properties>\n" + |
204 |
| "<async>false</async>\n" + |
205 |
| "<shared>false</shared>\n" + |
206 |
| "<fetchPersistentState>true</fetchPersistentState>\n" + |
207 |
| "<purgeOnStartup>false</purgeOnStartup>\n" + |
208 |
| "</cacheloader>\n" + |
209 |
| "</config>"; |
210 |
12
| Element element = XmlHelper.stringToElement(xml);
|
211 |
12
| return XmlConfigurationParser.parseCacheLoaderConfig(element);
|
212 |
| } |
213 |
| |
214 |
12
| protected Properties getProperties() throws Exception
|
215 |
| { |
216 |
12
| Properties prop = new Properties();
|
217 |
12
| try
|
218 |
| { |
219 |
12
| prop.load(this.getClass().getClassLoader().getResourceAsStream("cache-jdbc.properties"));
|
220 |
12
| return prop;
|
221 |
| } |
222 |
| catch (Exception e) |
223 |
| { |
224 |
0
| throw new Exception("Error loading jdbc properties ", e);
|
225 |
| } |
226 |
| } |
227 |
| |
228 |
6
| private JDBCCacheLoader getNewCacheLoader() throws Exception
|
229 |
| { |
230 |
6
| Properties prop = getProperties();
|
231 |
| |
232 |
6
| String props = "cache.jdbc.driver =" + prop.getProperty("cache.jdbc.driver") + "\n" +
|
233 |
| "cache.jdbc.url=" + prop.getProperty("cache.jdbc.url") + "\n" + |
234 |
| "cache.jdbc.user=" + prop.getProperty("cache.jdbc.user") + "\n" + |
235 |
| "cache.jdbc.password=" + prop.getProperty("cache.jdbc.password") + "\n" + |
236 |
| "cache.jdbc.node.type=" + prop.getProperty("cache.jdbc.node.type") + "\n" + |
237 |
| "cache.jdbc.sql-concat=" + prop.getProperty("cache.jdbc.sql-concat") + "\n"; |
238 |
| |
239 |
| |
240 |
6
| CacheLoaderConfig.IndividualCacheLoaderConfig base = getSingleCacheLoaderConfig("", "org.jboss.cache.loader.JDBCCacheLoader", props).getFirstCacheLoaderConfig();
|
241 |
| |
242 |
6
| JDBCCacheLoader jdbcCacheLoader = new JDBCCacheLoader();
|
243 |
6
| jdbcCacheLoader.setConfig(base);
|
244 |
6
| return jdbcCacheLoader;
|
245 |
| } |
246 |
| |
247 |
| |
248 |
6
| private JDBCCacheLoaderOld getOldLoader() throws Exception
|
249 |
| { |
250 |
6
| Properties prop = getProperties();
|
251 |
| |
252 |
6
| String props = "cache.jdbc.driver =" + prop.getProperty("cache.jdbc.driver") + "\n" +
|
253 |
| "cache.jdbc.url=" + prop.getProperty("cache.jdbc.url") + "\n" + |
254 |
| "cache.jdbc.user=" + prop.getProperty("cache.jdbc.user") + "\n" + |
255 |
| "cache.jdbc.password=" + prop.getProperty("cache.jdbc.password") + "\n" + |
256 |
| "cache.jdbc.node.type=" + prop.getProperty("cache.jdbc.node.type") + "\n" + |
257 |
| "cache.jdbc.sql-concat=" + prop.getProperty("cache.jdbc.sql-concat"); |
258 |
| |
259 |
| |
260 |
| |
261 |
6
| CacheLoaderConfig.IndividualCacheLoaderConfig base = getSingleCacheLoaderConfig("", "org.jboss.cache.loader.JDBCCacheLoader", props).getFirstCacheLoaderConfig();
|
262 |
6
| JDBCCacheLoaderOld jdbcCacheLoader = new JDBCCacheLoaderOld();
|
263 |
6
| jdbcCacheLoader.setConfig(base);
|
264 |
6
| return jdbcCacheLoader;
|
265 |
| } |
266 |
| |
267 |
| } |
268 |
| |