KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.Test;
10 import junit.framework.TestSuite;
11 import org.jboss.cache.Fqn;
12
13 import java.util.Properties JavaDoc;
14
15 /**
16  * This test runs cache loader tests using Database as the cache loader store.
17  * The default test is configured using Derby embedded framework.
18  * The server and database configuration is read from a properties file located at
19  * /etc/cache-jdbc.properties.
20  *
21  * To run this test with any other RDBMS, The appropriate JDBC driver
22  * (i.e mysql-connector-java-3.0.10-stable-bin.jar)
23  * must be in the lib directory.
24  *
25  * @author <a HREF="hmesha@novell.com">Hany Mesha</a>
26  * @version <tt>$Revision: 1.7 $</tt>
27  */

28 public class JDBCCacheLoaderTest
29    extends CacheLoaderTestsBase
30 {
31    protected void configureCache() throws Exception JavaDoc
32    {
33       Properties JavaDoc prop = new Properties JavaDoc();
34       try{
35          prop.load(this.getClass().getClassLoader().getResourceAsStream("cache-jdbc.properties"));
36       }catch(Exception JavaDoc e){
37          System.out.println("Error loading jdbc properties ");
38       }
39       String JavaDoc props = "cache.jdbc.driver =" + prop.getProperty("cache.jdbc.driver") + "\n" +
40                      "cache.jdbc.url=" + prop.getProperty("cache.jdbc.url") +"\n" +
41                      "cache.jdbc.user=" + prop.getProperty("cache.jdbc.user") + "\n" +
42                      "cache.jdbc.password=" + prop.getProperty("cache.jdbc.password") + "\n" +
43                      "cache.jdbc.node.type=" + prop.getProperty("cache.jdbc.node.type");
44       cache.getConfiguration().setCacheLoaderConfig( getSingleCacheLoaderConfig("", "org.jboss.cache.loader.JDBCCacheLoader", props, false, true, false) );
45    }
46    
47    public void testLargeObject()
48    {
49       try {
50          String JavaDoc key = "LargeObj";
51          // create an object with size bigger than 4k (k=1024 bytes)
52
StringBuffer JavaDoc text = new StringBuffer JavaDoc("LargeObject");
53          while (text.toString().getBytes().length < (1024*100)) {
54             text.append(text);
55          }
56          String JavaDoc initialValue = text.toString();
57          // insert it into the cache loader
58
loader.remove(Fqn.fromString("/"));
59          
60          Object JavaDoc retVal = loader.put(FQN, key, initialValue);
61          assertNull(retVal);
62          addDelay();
63          // load the object from the cache loader and validate it
64
assertEquals(initialValue, (String JavaDoc) loader.get(FQN).get(key));
65          // update the object and validate it
66
String JavaDoc updatedValue = initialValue.concat(("UpdatedValue"));
67          retVal = loader.put(FQN, key, updatedValue);
68          assertEquals(initialValue, (String JavaDoc) retVal);
69          assertEquals(updatedValue, (String JavaDoc) loader.get(FQN).get(key));
70       } catch(Exception JavaDoc e) {
71          fail(e.toString());
72       }
73    }
74        
75
76    public static Test suite()
77    {
78       return new TestSuite(JDBCCacheLoaderTest.class);
79    }
80    
81    public static void main(String JavaDoc[] args)
82    {
83        junit.textui.TestRunner.run(suite());
84    }
85 }
86
Popular Tags