KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.derby.jdbc.EmbeddedXADataSource;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.transaction.DummyTransactionManager;
14
15 import javax.naming.Context JavaDoc;
16 import javax.naming.InitialContext JavaDoc;
17 import javax.naming.NameNotFoundException JavaDoc;
18 import java.util.Properties JavaDoc;
19
20 /**
21  * This test runs cache loader tests using Database as the cache loader store.
22  * The default test is configured using MySQL.
23  * The server and database configuration is read from a properties file located at
24  * /etc/cache-jdbc.properties.
25  * <p/>
26  * The appropriate JDBC driver (i.e mysql-connector-java-3.0.10-stable-bin.jar)
27  * must be in the lib directory for this test to run successfuly
28  *
29  * @author <a HREF="hmesha@novell.com">Hany Mesha</a>
30  * @version <tt>$Revision: 1.4 $</tt>
31  */

32 public class JDBCCacheLoaderDerbyDSTest
33         extends CacheLoaderTestsBase
34 {
35    private String JavaDoc old_factory = null;
36    private final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
37    private final String JavaDoc JNDI_NAME = "java:/DerbyDS";
38
39    protected void configureCache() throws Exception JavaDoc
40    {
41       old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
42       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
43       DummyTransactionManager.getInstance();
44
45       Context JavaDoc context = new InitialContext JavaDoc();
46       try
47       {
48          Object JavaDoc obj = context.lookup(JNDI_NAME);
49          assertNull(JNDI_NAME + " not bound", obj);
50       }
51       catch (NameNotFoundException JavaDoc n)
52       {
53          // expected
54
}
55
56       Properties JavaDoc prop = new Properties JavaDoc();
57       try
58       {
59          prop.load(this.getClass().getClassLoader().getResourceAsStream("cache-jdbc.properties"));
60       }
61       catch (Exception JavaDoc e)
62       {
63          System.out.println("Error loading jdbc properties ");
64       }
65
66       //MysqlDataSource ds = new MysqlDataSource();
67
EmbeddedXADataSource ds = new EmbeddedXADataSource();
68       ds.setDatabaseName("jbossdb");
69       ds.setCreateDatabase("create");
70       ds.setUser(prop.getProperty("cache.jdbc.user"));
71       ;
72       ds.setPassword(prop.getProperty("cache.jdbc.password"));
73
74
75       String JavaDoc props = "cache.jdbc.datasource =" + JNDI_NAME + "\n" +
76               "cache.jdbc.node.type=" + prop.getProperty("cache.jdbc.node.type");
77
78
79       cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.JDBCCacheLoader", props, false, true, false));
80       cache.create();
81
82
83       context.bind(JNDI_NAME, ds);
84       assertNotNull(JNDI_NAME + " bound", context.lookup(JNDI_NAME));
85    }
86
87    public void testLargeObject()
88    {
89       try
90       {
91          String JavaDoc key = "LargeObj";
92          // create an object with size bigger than 4k (k=1024 bytes)
93
StringBuffer JavaDoc text = new StringBuffer JavaDoc("LargeObject");
94          while (text.toString().getBytes().length < (1024 * 100))
95          {
96             text.append(text);
97          }
98          String JavaDoc initialValue = text.toString();
99          // insert it into the cache loader
100
loader.remove(Fqn.fromString("/"));
101
102          Object JavaDoc retVal = loader.put(FQN, key, initialValue);
103          assertNull(retVal);
104          addDelay();
105          // load the object from the cache loader and validate it
106
assertEquals(initialValue, (String JavaDoc) loader.get(FQN).get(key));
107          // update the object and validate it
108
String JavaDoc updatedValue = initialValue.concat(("UpdatedValue"));
109          retVal = loader.put(FQN, key, updatedValue);
110          assertEquals(initialValue, (String JavaDoc) retVal);
111          assertEquals(updatedValue, (String JavaDoc) loader.get(FQN).get(key));
112       }
113       catch (Exception JavaDoc e)
114       {
115          fail(e.toString());
116       }
117    }
118
119    public void testTransactionRollback() throws Exception JavaDoc
120    {
121       // no-op
122
}
123
124    public void testIntegratedTransactionRollback() throws Exception JavaDoc
125    {
126       // no-op
127
}
128
129    public static Test suite()
130    {
131       return new TestSuite(JDBCCacheLoaderDerbyDSTest.class);
132    }
133
134    public static void main(String JavaDoc[] args)
135    {
136       junit.textui.TestRunner.run(suite());
137    }
138
139    protected void tearDown() throws Exception JavaDoc
140    {
141       super.tearDown();
142
143       Context JavaDoc ctx = new InitialContext JavaDoc();
144       ctx.unbind(JNDI_NAME);
145       if (old_factory != null)
146       {
147          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
148       }
149       else
150       {
151          System.getProperties().remove(Context.INITIAL_CONTEXT_FACTORY);
152       }
153    }
154 }
155
Popular Tags