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