JBoss Cache SVN: r7854 - in core/branches/flat/src: test/java/org/horizon/loader/bdbje and 1 other directory.
by jbosscache-commits@lists.jboss.org
Author: adriancole
Date: 2009-03-04 21:09:04 -0500 (Wed, 04 Mar 2009)
New Revision: 7854
Added:
core/branches/flat/src/main/java/org/horizon/loader/bdbje/BdbjeResourceFactory.java
core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeCacheStoreTest.java
core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeResourceFactoryTest.java
Modified:
core/branches/flat/src/main/java/org/horizon/loader/bdbje/BdbjeCacheStore.java
core/branches/flat/src/main/java/org/horizon/loader/bdbje/BdbjeCacheStoreConfig.java
core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeCacheStoreConfigTest.java
core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeCacheStoreIntegrationTest.java
core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeLearningTest.java
Log:
added test cases and cleaned code
Modified: core/branches/flat/src/main/java/org/horizon/loader/bdbje/BdbjeCacheStore.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/bdbje/BdbjeCacheStore.java 2009-03-05 00:23:55 UTC (rev 7853)
+++ core/branches/flat/src/main/java/org/horizon/loader/bdbje/BdbjeCacheStore.java 2009-03-05 02:09:04 UTC (rev 7854)
@@ -1,11 +1,17 @@
package org.horizon.loader.bdbje;
-import com.sleepycat.bind.EntryBinding;
-import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.collections.CurrentTransaction;
import com.sleepycat.collections.StoredMap;
-import com.sleepycat.je.*;
+import com.sleepycat.je.Cursor;
+import com.sleepycat.je.Database;
+import com.sleepycat.je.DatabaseEntry;
+import com.sleepycat.je.DatabaseException;
+import com.sleepycat.je.Environment;
+import com.sleepycat.je.EnvironmentConfig;
+import com.sleepycat.je.JEVersion;
+import com.sleepycat.je.OperationStatus;
+import com.sleepycat.je.Transaction;
import com.sleepycat.util.ExceptionUnwrapper;
import org.horizon.Cache;
import org.horizon.loader.AbstractCacheStore;
@@ -17,7 +23,6 @@
import org.horizon.logging.LogFactory;
import org.horizon.marshall.Marshaller;
import org.horizon.util.ReflectionUtil;
-import org.horizon.util.concurrent.WithinThreadExecutor;
import java.io.File;
import java.io.ObjectInput;
@@ -28,8 +33,6 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
/**
* An Oracle SleepyCat JE implementation of a {@link org.horizon.loader.CacheStore}. <p/>This implementation uses two
@@ -62,34 +65,37 @@
private BdbjeCacheStoreConfig cfg;
private Cache cache;
- private Marshaller m;
private Environment env;
- private String cacheDbName;
- private String catalogDbName;
private StoredClassCatalog catalog;
private Database cacheDb;
private StoredMap<Object, StoredEntry> cacheMap;
private PreparableTransactionRunner transactionRunner;
private Map<javax.transaction.Transaction, Transaction> txnMap;
- private ExecutorService purgerService;
private CurrentTransaction currentTransaction;
+ private BdbjeResourceFactory factory;
/**
- * {@inheritDoc} This implementation expects config to be an instance of {@link BdbjeCacheStoreConfig}
+ * {@inheritDoc} This implementation expects config to be an instance of {@link BdbjeCacheStoreConfig} <p /> note
+ * that the <code>m</code> is not currently used as SleepyCat has its own efficient solution.
*
* @see BdbjeCacheStoreConfig
*/
public void init(CacheLoaderConfig config, Cache cache, Marshaller m) {
+ BdbjeCacheStoreConfig cfg = (BdbjeCacheStoreConfig) config;
+ init(cfg, new BdbjeResourceFactory(cfg), cache);
+ }
+
+ public void init(BdbjeCacheStoreConfig cfg, BdbjeResourceFactory factory, Cache cache) {
if (trace) log.trace("initializing BdbjeCacheStore");
printLicense();
- this.cfg = (BdbjeCacheStoreConfig) config;
+ super.init(cfg, cache, null);
+ this.cfg = cfg;
+ this.factory = factory;
this.cache = cache;
- this.m = m;
}
-
/**
* {@inheritDoc}
*
@@ -101,39 +107,49 @@
/**
* {@inheritDoc} Validates configuration, configures and opens the {@link Environment}, then {@link
- * org.horizon.loader.bdbje.BdbjeCacheStore#openDatabases() opens the databases}. When this is finished,
- * transactional services are instantiated.
+ * org.horizon.loader.bdbje.BdbjeCacheStore#openSleepyCatResources() opens the databases}. When this is finished,
+ * transactional and purging services are instantiated.
*/
public void start() throws CacheLoaderException {
if (trace) log.trace("starting BdbjeCacheStore");
- checkNotOpen();
- if (cache == null) {
- throw new IllegalStateException(
- "A non-null Cache property (CacheSPI object) is required");
- }
+ openSleepyCatResources();
+ openTransactionServices();
+ super.start();
- String configStr = cfg.getLocation();
+ log.debug("started cache store {1}", this);
+ }
- if (cfg.isPurgeSynchronously()) {
- purgerService = new WithinThreadExecutor();
- } else {
- purgerService = Executors.newSingleThreadExecutor();
- }
+ private void openTransactionServices() {
+ txnMap = new ConcurrentHashMap<javax.transaction.Transaction, Transaction>();
+ currentTransaction = factory.createCurrentTransaction(env);
+ transactionRunner = factory.createPreparableTransactionRunner(env);
+ }
- // JBCACHE-1448 db name parsing fix courtesy of Ciro Cavani
- /* Parse config string. */
- int offset = configStr.indexOf('#');
- if (offset >= 0 && offset < configStr.length() - 1) {
- cacheDbName = configStr.substring(offset + 1);
- configStr = configStr.substring(0, offset);
- } else {
- cacheDbName = cache.getName();
- if (cacheDbName == null) cacheDbName = "CacheInstance-" + System.identityHashCode(cache);
+ /**
+ * Opens the SleepyCat environment and all databases. A {@link StoredMap} instance is provided which persists the
+ * CacheStore.
+ */
+ private void openSleepyCatResources() throws CacheLoaderException {
+ if (trace) log.trace("creating je environment with home dir {0}", cfg.getLocation());
+
+ if (cfg.getCacheDbName() == null) cfg.setCacheDbName(cache.getName());
+ if (cfg.getCatalogDbName() == null) cfg.setCatalogDbName(cfg.getCacheDbName() + "_class_catalog");
+
+ File location = verifyOrCreateEnvironmentDirectory(new File(cfg.getLocation()));
+ try {
+ env = factory.createEnvironment(location);
+ cacheDb = factory.createDatabase(env, cfg.getCacheDbName());
+ Database catalogDb = factory.createDatabase(env, cfg.getCatalogDbName());
+ catalog = factory.createStoredClassCatalog(catalogDb);
+ cacheMap = factory.createStoredMapViewOfDatabase(cacheDb, catalog);
+ } catch (DatabaseException e) {
+ throw convertToCacheLoaderException("could not open sleepycat je resource", e);
}
+ }
- // datafile location
- File location = new File(configStr);
+ // not private so that this can be unit tested
+ File verifyOrCreateEnvironmentDirectory(File location) throws CacheLoaderException {
if (!location.exists()) {
boolean created = location.mkdirs();
if (!created) throw new CacheLoaderException("Unable to create cache loader location " + location);
@@ -142,120 +158,61 @@
if (!location.isDirectory()) {
throw new CacheLoaderException("Cache loader location [" + location + "] is not a directory!");
}
-
- catalogDbName = cacheDbName + "_class_catalog";
-
- try {
- /* Open the environment, creating it if it doesn't exist. */
- EnvironmentConfig envConfig = new EnvironmentConfig();
- envConfig.setAllowCreate(true);
- envConfig.setTransactional(true);
- /* lock timeout is in microseconds */
- envConfig.setLockTimeout(cfg.getLockAcquistionTimeout() * 1000);
- if (log.isDebugEnabled()) {
- envConfig.setConfigParam("je.txn.deadlockStackTrace", "true");
- envConfig.setConfigParam("je.txn.dumpLocks", "true");
- }
- if (trace) log.trace("creating je environment with home dir {0}", location);
- env = new Environment(location, envConfig);
- log.debug("created je environment {0} for cache store {1}", env, this);
- /* Open cache and catalog databases. */
- openDatabases();
- }
- catch (DatabaseException e) {
- throw new CacheLoaderException("could not open the sleepycat database", e);
- }
- txnMap = new ConcurrentHashMap<javax.transaction.Transaction, Transaction>();
- currentTransaction = CurrentTransaction.getInstance(env);
- transactionRunner = new PreparableTransactionRunner(env);
- transactionRunner.setMaxRetries(cfg.getMaxTxRetries());
- log.debug("started cache store {1}", this);
+ return location;
}
-
/**
- * Opens all databases and initializes database related information. A {@link StoredMap} instance is {@link
- * BdbjeCacheStore#createStoredMapViewOfDatabase(com.sleepycat.je.Database, com.sleepycat.bind.serial.StoredClassCatalog)
- * associated} with the stored entry and class catalog databases.
+ * Stops transaction and purge processing and closes the SleepyCat environment. The environment and databases are
+ * not removed from the file system. Exceptions during close of databases are ignored as closing the environment will
+ * ensure the databases are also.
*/
- private void openDatabases() throws DatabaseException {
- if (trace) log.trace("opening databases");
- /* Use a generic database config, with no duplicates allowed. */
- DatabaseConfig dbConfig = new DatabaseConfig();
- dbConfig.setTransactional(true);
- dbConfig.setAllowCreate(true);
+ public void stop() throws CacheLoaderException {
+ if (trace) log.trace("stopping BdbjeCacheStore");
+ super.stop();
+ closeTransactionServices();
+ closeSleepyCatResources();
+ log.debug("started cache store {1}", this);
+ }
- if (trace) log.trace("opening or creating stored entry database {0}", cacheDbName);
- cacheDb = env.openDatabase(null, cacheDbName, dbConfig);
- log.debug("opened stored entry database {0}", cacheDbName);
-
- if (trace) log.trace("opening or creating class catalog database {0}", catalogDbName);
- Database catalogDb = env.openDatabase(null, catalogDbName, dbConfig);
- catalog = new StoredClassCatalog(catalogDb);
- log.debug("created stored class catalog from database {0}", catalogDbName);
-
- cacheMap = createStoredMapViewOfDatabase(cacheDb, catalog);
+ private void closeTransactionServices() {
+ transactionRunner = null;
+ currentTransaction = null;
+ txnMap = null;
}
- /**
- * create a {@link StoredMap} persisted by the <code>database</code>
- *
- * @param database where entries in the StoredMap are persisted
- * @param classCatalog location to store class descriptions
- * @return StoredMap backed by the database and classCatalog
- * @throws DatabaseException if the StoredMap cannot be opened.
- */
- private StoredMap createStoredMapViewOfDatabase(Database database, StoredClassCatalog classCatalog) throws DatabaseException {
- EntryBinding storedEntryKeyBinding =
- new SerialBinding(classCatalog, Object.class);
- EntryBinding storedEntryValueBinding =
- new SerialBinding(classCatalog, StoredEntry.class);
- try {
- return new StoredMap<Object, StoredEntry>(database,
- storedEntryKeyBinding, storedEntryValueBinding, true);
- } catch (Exception caught) {
- caught = ExceptionUnwrapper.unwrap(caught);
- throw new DatabaseException("error opening stored map", caught);
- }
+ private void closeSleepyCatResources() throws CacheLoaderException {
+ cacheMap = null;
+ closeDatabases();
+ closeEnvironment();
}
/**
- * Closes the JE databases and their associated {@link StoredMap}, and nulls references to them. The databases are
- * not removed from the file system. Exceptions during close are ignored.
+ * Exceptions are ignored so that {@link org.horizon.loader.bdbje.BdbjeCacheStore#closeEnvironment()} will execute.
*/
private void closeDatabases() {
if (trace) log.trace("closing databases");
try {
cacheDb.close();
+ } catch (Exception e) {
+ log.error("Error closing database", e);
+ }
+ try {
catalog.close();
- } catch (DatabaseException e) {
- log.error("Error closing databases", e);
+ } catch (Exception e) {
+ log.error("Error closing catalog", e);
}
- cacheDb = null;
- catalog = null;
cacheMap = null;
+ catalog = null;
+ cacheDb = null;
}
-
- /**
- * {@link org.horizon.loader.bdbje.BdbjeCacheStore#closeDatabases() Closes the JE databases} and the {@link
- * Environment}. The environment and databases are not removed from the file system. Exceptions during close are
- * ignored.
- */
- public void stop() throws CacheLoaderException {
- checkOpen();
- if (trace) log.trace("stopping BdbjeCacheStore");
- transactionRunner = null;
- currentTransaction = null;
- txnMap = null;
- closeDatabases();
+ private void closeEnvironment() throws CacheLoaderException {
if (env != null) {
try {
env.close();
+ } catch (DatabaseException e) {
+ throw new CacheLoaderException("Unexpected exception closing cacheStore", e);
}
- catch (Exception shouldNotOccur) {
- log.warn("Unexpected exception closing cacheStore", shouldNotOccur);
- }
}
env = null;
}
@@ -281,30 +238,27 @@
*/
@Override
protected void applyModifications(List<? extends Modification> mods) throws CacheLoaderException {
- checkOpen();
- checkNonNull(mods, "modifications");
- log.debug("performing one phase transaction");
+ if (trace) log.trace("performing one phase transaction");
try {
transactionRunner.run(new ModificationsTransactionWorker(this, mods));
- } catch (Exception e) {
- throw new CacheLoaderException("Problem committing modifications: " + mods, e);
+ } catch (Exception caught) {
+ throw convertToCacheLoaderException("Problem committing modifications: " + mods, caught);
}
}
/**
* Looks up the {@link Transaction SleepyCat transaction} associated with <code>tx</code>. Creates a {@link
* org.horizon.loader.bdbje.ModificationsTransactionWorker} instance from <code>mods</code>. Then prepares the
- * transaction via {@link PreparableTransactionRunner#prepare(com.sleepycat.collections.TransactionWorker)}.
+ * transaction via {@link PreparableTransactionRunner#prepare(com.sleepycat.collections.TransactionWorker)}. Finally,
+ * it invalidates {@link com.sleepycat.collections.CurrentTransaction#getTransaction()} so that no other thread can
+ * accidentally commit this.
*
* @param mods modifications to be applied
* @param tx transaction identifier
* @throws CacheLoaderException in the event of problems writing to the store
*/
protected void prepare(List<? extends Modification> mods, javax.transaction.Transaction tx) throws CacheLoaderException {
- checkOpen();
- checkNonNull(mods, "modifications");
- checkNonNull(tx, "tx");
- log.debug("preparing transaction {0}", tx);
+ if (trace) log.trace("preparing transaction {0}", tx);
try {
transactionRunner.prepare(new ModificationsTransactionWorker(this, mods));
Transaction txn = currentTransaction.getTransaction();
@@ -312,7 +266,7 @@
txnMap.put(tx, txn);
ReflectionUtil.setValue(currentTransaction, "localTrans", new ThreadLocal());
} catch (Exception e) {
- throw new CacheLoaderException("Problem preparing transaction", e);
+ throw convertToCacheLoaderException("Problem preparing transaction", e);
}
}
@@ -343,26 +297,23 @@
/**
* Looks up the SleepyCat transaction associated with the parameter <code>tx</code>. If there is no associated
- * sleepycat transaction, an error is logged. If this transaction is the {@link
- * com.sleepycat.collections.CurrentTransaction#getTransaction()} current transaction}, it calls {@link
- * BdbjeCacheStore#completeCurrentTransaction(boolean)} passing the argument <code>commit</code>. Otherwise, {@link
- * BdbjeCacheStore#completeTransaction(com.sleepycat.je.Transaction, boolean) completeTransaction} is called, passing
- * the SleepyCat transaction and <code>commit</code> as arguments.
+ * sleepycat transaction, an error is logged.
*
* @param tx java transaction used to lookup a SleepyCat transaction
* @param commit true to commit false to abort
* @throws CacheLoaderException if there are problems committing or aborting the transaction
*/
protected void completeTransaction(javax.transaction.Transaction tx, boolean commit) throws CacheLoaderException {
- checkOpen();
- checkNonNull(tx, "tx");
Transaction txn = txnMap.remove(tx);
if (txn != null) {
- log.debug("transaction {0} == sleepycat transaction {1}", tx, txn);
- if (currentTransaction.getTransaction() == txn) {
- completeCurrentTransaction(commit);
- } else {
- completeTransaction(txn, commit);
+ if (trace) log.trace("{0} sleepycat transaction {1}", commit ? "committing" : "aborting", txn);
+ try {
+ if (commit)
+ txn.commit();
+ else
+ txn.abort();
+ } catch (Exception caught) {
+ throw convertToCacheLoaderException("Problem completing transaction", caught);
}
} else {
if (trace) log.trace("no sleepycat transaction associated transaction {0}", tx);
@@ -370,24 +321,6 @@
}
/**
- * commits or aborts the {@link Transaction}
- *
- * @param commit true to commit, false to abort
- * @throws CacheLoaderException if there was a problem completing the transaction
- */
- private void completeTransaction(Transaction txn, boolean commit) throws CacheLoaderException {
- try {
- log.debug("{0} sleepycat transaction {1}", commit ? "committing" : "aborting", txn);
- if (commit)
- txn.commit();
- else
- txn.abort();
- } catch (DatabaseException e) {
- throw new CacheLoaderException("Problem completing transaction", e);
- }
- }
-
- /**
* commits or aborts the {@link com.sleepycat.collections.CurrentTransaction#getTransaction() current transaction}
*
* @param commit true to commit, false to abort
@@ -395,13 +328,14 @@
*/
private void completeCurrentTransaction(boolean commit) throws CacheLoaderException {
try {
- log.debug("{0} current sleepycat transaction {1}", commit ? "committing" : "aborting", currentTransaction.getTransaction());
+ if (trace)
+ log.trace("{0} current sleepycat transaction {1}", commit ? "committing" : "aborting", currentTransaction.getTransaction());
if (commit)
currentTransaction.commitTransaction();
else
currentTransaction.abortTransaction();
- } catch (DatabaseException e) {
- throw new CacheLoaderException("Problem completing transaction", e);
+ } catch (Exception caught) {
+ throw convertToCacheLoaderException("Problem completing transaction", caught);
}
}
@@ -409,19 +343,15 @@
* {@inheritDoc} This implementation delegates to {@link StoredMap#remove(Object)}
*/
public boolean remove(Object key) throws CacheLoaderException {
- checkOpen();
- checkNonNull(key, "key");
- if (trace) log.trace("Removing key {0}", key);
try {
if (cacheMap.containsKey(key)) {
cacheMap.remove(key);
return true;
}
- } catch (Exception caught) {
- caught = ExceptionUnwrapper.unwrap(caught);
- throw new CacheLoaderException("error removing key " + key, caught);
+ return false;
+ } catch (RuntimeException caught) {
+ throw convertToCacheLoaderException("error removing key " + key, caught);
}
- return false;
}
/**
@@ -429,36 +359,26 @@
* not be returned.
*/
public StoredEntry load(Object key) throws CacheLoaderException {
- checkOpen();
- checkNonNull(key, "key");
- if (trace) log.trace("Loading key {0}", key);
try {
StoredEntry s = cacheMap.get(key);
- if (s == null)
- return null;
- if (!s.isExpired())
- return s;
- else
+ if (s != null && s.isExpired()) {
cacheMap.remove(key);
- } catch (Exception caught) {
- caught = ExceptionUnwrapper.unwrap(caught);
- throw new CacheLoaderException("error loading key " + key, caught);
+ s = null;
+ }
+ return s;
+ } catch (RuntimeException caught) {
+ throw convertToCacheLoaderException("error loading key " + key, caught);
}
- return null;
}
/**
* {@inheritDoc} This implementation delegates to {@link StoredMap#put(Object, Object)}
*/
public void store(StoredEntry ed) throws CacheLoaderException {
- checkOpen();
- checkNonNull(ed, "entry");
- if (trace) log.trace("Storing entry {0}", ed);
try {
cacheMap.put(ed.getKey(), ed);
- } catch (Exception caught) {
- caught = ExceptionUnwrapper.unwrap(caught);
- throw new CacheLoaderException("error storing entry " + ed, caught);
+ } catch (RuntimeException caught) {
+ throw convertToCacheLoaderException("error storing entry " + ed, caught);
}
}
@@ -466,11 +386,10 @@
* {@inheritDoc} This implementation delegates to {@link StoredMap#clear()}
*/
public void clear() throws CacheLoaderException {
- checkOpen();
- if (trace) log.trace("Clearing store");
- try {cacheMap.clear(); } catch (Exception caught) {
- caught = ExceptionUnwrapper.unwrap(caught);
- throw new CacheLoaderException("error clearing store", caught);
+ try {
+ cacheMap.clear();
+ } catch (RuntimeException caught) {
+ throw convertToCacheLoaderException("error clearing store", caught);
}
}
@@ -478,13 +397,10 @@
* {@inheritDoc} This implementation returns a Set from {@link StoredMap#values()}
*/
public Set<StoredEntry> loadAll() throws CacheLoaderException {
- checkOpen();
- if (trace) log.trace("Loading all entries");
try {
return new HashSet(cacheMap.values());
- } catch (Exception caught) {
- caught = ExceptionUnwrapper.unwrap(caught);
- throw new CacheLoaderException("error loading all entries ", caught);
+ } catch (RuntimeException caught) {
+ throw convertToCacheLoaderException("error loading all entries", caught);
}
}
@@ -497,13 +413,10 @@
* @see BdbjeCacheStore#toStream(java.io.ObjectOutput)
*/
public void fromStream(ObjectInput ois) throws CacheLoaderException {
- checkOpen();
- log.info("Clearing all entries and loading from input");
try {
long recordCount = ois.readLong();
- log.info("reading {0} records from stream", recordCount);
- log.info("clearing all records");
currentTransaction.beginTransaction(null);
+ log.debug("clearing and reading {0} records from stream", recordCount);
cacheMap.clear();
Cursor cursor = null;
try {
@@ -520,13 +433,10 @@
if (cursor != null) cursor.close();
}
completeCurrentTransaction(true);
- }
- catch (Exception caught) {
+ } catch (Exception caught) {
completeCurrentTransaction(false);
- caught = ExceptionUnwrapper.unwrap(caught);
- CacheLoaderException cle = (caught instanceof CacheLoaderException) ? (CacheLoaderException) caught :
- new CacheLoaderException("Problems reading from stream", caught);
- throw cle;
+ clear();
+ throw convertToCacheLoaderException("Problems reading from stream", caught);
}
}
@@ -537,103 +447,69 @@
* This implementation holds a transaction open to ensure that we see no new records added while iterating.
*/
public void toStream(ObjectOutput oos) throws CacheLoaderException {
- checkOpen();
- if (trace) log.trace("dumping current database to outputstream");
- Cursor cursor = null;
try {
currentTransaction.beginTransaction(null);
long recordCount = cacheDb.count();
- log.debug("writing {0} records to stream", recordCount);
+ if (trace) log.trace("writing {0} records to stream", recordCount);
oos.writeLong(recordCount);
-
- cursor = cacheDb.openCursor(currentTransaction.getTransaction(), null);
- DatabaseEntry key = new DatabaseEntry();
- DatabaseEntry data = new DatabaseEntry();
- int recordsWritten = 0;
- while (cursor.getNext(key, data, null) ==
- OperationStatus.SUCCESS) {
- oos.writeObject(key.getData());
- oos.writeObject(data.getData());
- recordsWritten++;
- }
- log.debug("wrote {0} records to stream", recordsWritten);
- if (recordsWritten != recordCount)
- log.warn("expected to write {0} records, but wrote {1}", recordCount, recordsWritten);
- cursor.close();
- cursor = null;
- currentTransaction.commitTransaction();
- } catch (Exception caught) {
+ Cursor cursor = null;
try {
- currentTransaction.abortTransaction();
- } catch (DatabaseException e) {
- log.error("error aborting transaction", e);
+ cursor = cacheDb.openCursor(currentTransaction.getTransaction(), null);
+ DatabaseEntry key = new DatabaseEntry();
+ DatabaseEntry data = new DatabaseEntry();
+ int recordsWritten = 0;
+ while (cursor.getNext(key, data, null) ==
+ OperationStatus.SUCCESS) {
+ oos.writeObject(key.getData());
+ oos.writeObject(data.getData());
+ recordsWritten++;
+ }
+ if (trace) log.trace("wrote {0} records to stream", recordsWritten);
+ if (recordsWritten != recordCount)
+ log.warn("expected to write {0} records, but wrote {1}", recordCount, recordsWritten);
+ } finally {
+ if (cursor != null) cursor.close();
}
- caught = ExceptionUnwrapper.unwrap(caught);
- CacheLoaderException cle = (caught instanceof CacheLoaderException) ? (CacheLoaderException) caught :
- new CacheLoaderException("Problems writing to stream", caught);
- throw cle;
+ completeCurrentTransaction(true);
+ } catch (Exception caught) {
+ completeCurrentTransaction(false);
+ throw convertToCacheLoaderException("Problems writing to stream", caught);
}
- finally {
- if (cursor != null) try {
- cursor.close();
- } catch (DatabaseException e) {
- throw new CacheLoaderException("Error closing cursor", e);
- }
- }
}
/**
- * {@inheritDoc} If there is a {@link com.sleepycat.collections.CurrentTransaction#getTransaction() transaction in
- * progress}, this method will invoke {@link #doPurgeExpired()} Otherwise, it will purge expired entries,
- * autocommitting each.
+ * In order to adhere to APIs which do not throw checked exceptions, BDBJE wraps IO and DatabaseExceptions inside
+ * RuntimeExceptions. These special Exceptions implement {@link com.sleepycat.util.ExceptionWrapper}. This method
+ * will look for any of that type of Exception and encapsulate it into a CacheLoaderException. In doing so, the real
+ * root cause can be obtained.
*
- * @see org.horizon.loader.bdbje.BdbjeCacheStoreConfig#isPurgeSynchronously()
- * @see org.horizon.loader.bdbje.BdbjeCacheStoreConfig#setMaxTxRetries(int)
+ * @param message what to attach to the CacheLoaderException
+ * @param caught exception to parse
+ * @return CacheLoaderException with the correct cause
*/
- public void purgeExpired() throws CacheLoaderException {
- checkOpen();
- if (currentTransaction.getTransaction() != null) {
- doPurgeExpired();
- } else {
- purgerService.execute(new Runnable() {
- public void run() {
- try {
- doPurgeExpired();
- } catch (Exception e) {
- log.error("error purging expired entries", e);
- }
- }
- });
- }
+ CacheLoaderException convertToCacheLoaderException(String message, Exception caught) {
+ caught = ExceptionUnwrapper.unwrap(caught);
+ return (caught instanceof CacheLoaderException) ? (CacheLoaderException) caught :
+ new CacheLoaderException(message, caught);
}
/**
* Iterate through {@link com.sleepycat.collections.StoredMap#entrySet()} and remove, if expired.
*/
- private void doPurgeExpired() {
- log.info("purging expired from database");
- Iterator<Map.Entry<Object, StoredEntry>> i = cacheMap.entrySet().iterator();
- while (i.hasNext()) {
- if (i.next().getValue().isExpired())
- i.remove();
+ @Override
+ protected void purgeInternal() throws CacheLoaderException {
+ try {
+ Iterator<Map.Entry<Object, StoredEntry>> i = cacheMap.entrySet().iterator();
+ while (i.hasNext()) {
+ if (i.next().getValue().isExpired())
+ i.remove();
+ }
+ } catch (RuntimeException caught) {
+ throw convertToCacheLoaderException("error purging expired entries", caught);
}
}
/**
- * @return the name of the SleepyCat database persisting this store
- */
- public String getCacheDbName() {
- return cacheDbName;
- }
-
- /**
- * @return the name of the SleepyCat database persisting the class information for objects in this store
- */
- public String getCatalogDbName() {
- return catalogDbName;
- }
-
- /**
* prints terms of use for Berkeley DB JE
*/
public void printLicense() {
@@ -652,34 +528,4 @@
System.out.println(license);
}
- /**
- * Throws an exception if the environment is not open.
- */
- private void checkNotOpen() {
- if (env != null) {
- throw new IllegalStateException(
- "Operation not allowed after calling create()");
- }
- }
-
- /**
- * Throws an exception if the environment is not open.
- */
- private void checkOpen() {
- if (env == null) {
- throw new IllegalStateException(
- "Operation not allowed before calling create()");
- }
- }
-
- /**
- * Throws an exception if the parameter is null.
- */
- private void checkNonNull(Object param, String paramName) {
- if (param == null) {
- throw new NullPointerException(
- "Parameter must not be null: " + paramName);
- }
- }
-
}
Modified: core/branches/flat/src/main/java/org/horizon/loader/bdbje/BdbjeCacheStoreConfig.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/bdbje/BdbjeCacheStoreConfig.java 2009-03-05 00:23:55 UTC (rev 7853)
+++ core/branches/flat/src/main/java/org/horizon/loader/bdbje/BdbjeCacheStoreConfig.java 2009-03-05 02:09:04 UTC (rev 7854)
@@ -1,30 +1,37 @@
package org.horizon.loader.bdbje;
-import org.horizon.loader.AbstractCacheLoaderConfig;
+import org.horizon.loader.AbstractCacheStoreConfig;
/**
* Configures {@link org.horizon.loader.bdbje.BdbjeCacheStore}. This allows you to tune a number of characteristics of
* the {@link BdbjeCacheStore}.
* <p/>
* <ul> <li><tt>location</tt> - a location on disk where the store can write internal files. This defaults to
- * <tt>Horizon-BdbjeCacheStore</tt> in the current working directory.</li> <li><tt>purgeSynchronously</tt> - whether {@link
- * org.horizon.loader.CacheStore#purgeExpired()} calls happen synchronously or not. By default, this is set to
- * <tt>false</tt>.</li> <li><tt>lockAcquistionTimeout</tt> - the length of time, in milliseconds, to wait for locks
+ * <tt>Horizon-BdbjeCacheStore</tt> in the current working directory.</li> <li><tt>lockAcquistionTimeout</tt> - the length of time, in milliseconds, to wait for locks
* before timing out and throwing an exception. By default, this is set to <tt>60000</tt>.</li>
* <li><tt>maxTxRetries</tt> - the number of times transaction prepares will attempt to resolve a deadlock before
- * throwing an exception. By default, this is set to <tt>5</tt>.</li> </ul>
+ * throwing an exception. By default, this is set to <tt>5</tt>.</li>
+ * <p/>
+ * <li><tt>cacheDbName</tt> - the name of the SleepyCat database persisting this store. This defaults to <tt>{@link
+ * org.horizon.Cache#getName()} cache#name}</tt>.</li> <li><tt>catalogDbName</tt> - the name of the SleepyCat database
+ * persisting the class information for objects in this store. This defaults to <tt>{@link org.horizon.Cache#getName()}
+ * cache#name}_class_catalog</tt>.</li>
+ * <p/>
+ * </ul>
*
+ * Please see {@link AbstractCacheStoreConfig} for more configuration parameters.
+ *
* @author Adrian Cole
* @version $Id: $
* @since 1.0
*/
-public class BdbjeCacheStoreConfig extends AbstractCacheLoaderConfig {
+public class BdbjeCacheStoreConfig extends AbstractCacheStoreConfig {
private String location = "Horizon-BdbjeCacheStore";
- private boolean purgeSynchronously;
private long lockAcquistionTimeout = 60 * 1000;
private int maxTxRetries = 5;
+ private String cacheDbName;
+ private String catalogDbName;
-
public BdbjeCacheStoreConfig() {
setClassName(BdbjeCacheStore.class.getName());
}
@@ -55,12 +62,21 @@
this.location = location;
}
- public boolean isPurgeSynchronously() {
- return purgeSynchronously;
+
+ public String getCacheDbName() {
+ return cacheDbName;
}
- public void setPurgeSynchronously(boolean purgeSynchronously) {
- this.purgeSynchronously = purgeSynchronously;
+ public void setCacheDbName(String cacheDbName) {
+ this.cacheDbName = cacheDbName;
}
+ public String getCatalogDbName() {
+ return catalogDbName;
+ }
+
+ public void setCatalogDbName(String catalogDbName) {
+ this.catalogDbName = catalogDbName;
+ }
+
}
Added: core/branches/flat/src/main/java/org/horizon/loader/bdbje/BdbjeResourceFactory.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/bdbje/BdbjeResourceFactory.java (rev 0)
+++ core/branches/flat/src/main/java/org/horizon/loader/bdbje/BdbjeResourceFactory.java 2009-03-05 02:09:04 UTC (rev 7854)
@@ -0,0 +1,107 @@
+package org.horizon.loader.bdbje;
+
+import com.sleepycat.bind.EntryBinding;
+import com.sleepycat.bind.serial.SerialBinding;
+import com.sleepycat.bind.serial.StoredClassCatalog;
+import com.sleepycat.collections.CurrentTransaction;
+import com.sleepycat.collections.StoredMap;
+import com.sleepycat.je.Database;
+import com.sleepycat.je.DatabaseConfig;
+import com.sleepycat.je.DatabaseException;
+import com.sleepycat.je.Environment;
+import com.sleepycat.je.EnvironmentConfig;
+import com.sleepycat.util.ExceptionUnwrapper;
+import org.horizon.loader.StoredEntry;
+import org.horizon.logging.Log;
+import org.horizon.logging.LogFactory;
+
+import java.io.File;
+
+/**
+ * Factory that assembles objects specific to the SleepyCat JE API.
+ *
+ * @author Adrian Cole
+ * @version $Id: $
+ * @since 1.0
+ */
+public class BdbjeResourceFactory {
+ private static final Log log = LogFactory.getLog(BdbjeResourceFactory.class);
+ private static final boolean trace = log.isTraceEnabled();
+
+ private BdbjeCacheStoreConfig config;
+
+ public BdbjeResourceFactory(BdbjeCacheStoreConfig config) {
+ this.config = config;
+ }
+
+ /**
+ * @return PreparableTransactionRunner that will try to resolve deadlocks maximum of {@link BdbjeCacheStoreConfig#getMaxTxRetries()} times.
+ */
+ public PreparableTransactionRunner createPreparableTransactionRunner(Environment env) {
+ return new PreparableTransactionRunner(env, config.getMaxTxRetries(), null);
+ }
+
+ public CurrentTransaction createCurrentTransaction(Environment env) {
+ return CurrentTransaction.getInstance(env);
+ }
+
+ /**
+ * Open the environment, creating it if it doesn't exist.
+ * @param envLocation base directory where the Environment will write files
+ * @return open Environment with a lock timeout of {@link org.horizon.loader.bdbje.BdbjeCacheStoreConfig#getLockAcquistionTimeout()} milliseconds.
+ */
+ public Environment createEnvironment(File envLocation) throws DatabaseException {
+ EnvironmentConfig envConfig = new EnvironmentConfig();
+ envConfig.setAllowCreate(true);
+ envConfig.setTransactional(true);
+ /* lock timeout is in microseconds */
+ envConfig.setLockTimeout(config.getLockAcquistionTimeout() * 1000);
+ if (trace) log.trace("opening or creating je environment at {0}", envLocation);
+ Environment env = new Environment(envLocation, envConfig);
+ log.debug("opened je environment at {0}", envLocation);
+ return env;
+ }
+
+ public StoredClassCatalog createStoredClassCatalog(Database catalogDb) throws DatabaseException {
+ StoredClassCatalog catalog = new StoredClassCatalog(catalogDb);
+ log.debug("created stored class catalog from database {0}", config.getCatalogDbName());
+ return catalog;
+ }
+
+ /**
+ * Open the database, creating it if it doesn't exist.
+ * @return open transactional Database
+ */
+ public Database createDatabase(Environment env, String name) throws DatabaseException {
+ DatabaseConfig dbConfig = new DatabaseConfig();
+ dbConfig.setTransactional(true);
+ dbConfig.setAllowCreate(true);
+ if (trace) log.trace("opening or creating database {0}", name);
+ Database db = env.openDatabase(null, name, dbConfig);
+ log.debug("opened database {0}", name);
+ return db;
+ }
+
+ /**
+ * create a {@link com.sleepycat.collections.StoredMap} persisted by the <code>database</code>
+ *
+ * @param database where entries in the StoredMap are persisted
+ * @param classCatalog location to store class descriptions
+ * @return StoredMap backed by the database and classCatalog
+ * @throws com.sleepycat.je.DatabaseException
+ * if the StoredMap cannot be opened.
+ */
+ public StoredMap createStoredMapViewOfDatabase(Database database, StoredClassCatalog classCatalog) throws DatabaseException {
+ EntryBinding storedEntryKeyBinding =
+ new SerialBinding(classCatalog, Object.class);
+ EntryBinding storedEntryValueBinding =
+ new SerialBinding(classCatalog, StoredEntry.class);
+ try {
+ return new StoredMap<Object, StoredEntry>(database,
+ storedEntryKeyBinding, storedEntryValueBinding, true);
+ } catch (Exception caught) {
+ caught = ExceptionUnwrapper.unwrap(caught);
+ throw new DatabaseException("error opening stored map", caught);
+ }
+ }
+}
\ No newline at end of file
Modified: core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeCacheStoreConfigTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeCacheStoreConfigTest.java 2009-03-05 00:23:55 UTC (rev 7853)
+++ core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeCacheStoreConfigTest.java 2009-03-05 02:09:04 UTC (rev 7854)
@@ -67,13 +67,15 @@
}
@Test
- public void testIsPurgeSynchronously() {
- assert !config.isPurgeSynchronously();
+ public void testSetCacheDb() {
+ config.setCacheDbName("foo");
+ assert config.getCacheDbName().equals("foo");
}
@Test
- public void testSetPurgeSynchronously() {
- config.setPurgeSynchronously(true);
- assert config.isPurgeSynchronously();
+ public void testSetCatalogDb() {
+ config.setCatalogDbName("foo");
+ assert config.getCatalogDbName().equals("foo");
}
+
}
Modified: core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeCacheStoreIntegrationTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeCacheStoreIntegrationTest.java 2009-03-05 00:23:55 UTC (rev 7853)
+++ core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeCacheStoreIntegrationTest.java 2009-03-05 02:09:04 UTC (rev 7854)
@@ -23,6 +23,7 @@
*/
@Test(groups = "unit", enabled = true, testName = "loader.bdbje.BdbjeCacheStoreIntegrationTest")
public class BdbjeCacheStoreIntegrationTest extends BaseCacheStoreTest {
+
protected CacheStore createCacheStore() throws CacheLoaderException {
CacheStore cs = new BdbjeCacheStore();
String tmpDir = TestingUtil.TEST_FILES;
Added: core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeCacheStoreTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeCacheStoreTest.java (rev 0)
+++ core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeCacheStoreTest.java 2009-03-05 02:09:04 UTC (rev 7854)
@@ -0,0 +1,358 @@
+package org.horizon.loader.bdbje;
+
+import com.sleepycat.bind.serial.StoredClassCatalog;
+import com.sleepycat.collections.CurrentTransaction;
+import com.sleepycat.collections.StoredMap;
+import com.sleepycat.collections.TransactionWorker;
+import com.sleepycat.je.Cursor;
+import com.sleepycat.je.Database;
+import com.sleepycat.je.DatabaseException;
+import com.sleepycat.je.Environment;
+import com.sleepycat.util.RuntimeExceptionWrapper;
+import static org.easymock.classextension.EasyMock.*;
+import org.horizon.Cache;
+import org.horizon.loader.CacheLoaderException;
+import org.horizon.loader.StoredEntry;
+import org.horizon.loader.modifications.Store;
+import org.horizon.util.ReflectionUtil;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import javax.transaction.Transaction;
+import java.io.File;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.lang.ref.WeakReference;
+import java.util.Collections;
+
+/**
+ * @author Adrian Cole
+ * @version $Id: $
+ * @since 1.0
+ */
+@Test(groups = "unit", enabled = true, testName = "loader.bdbje.BdbjeCacheStoreTest")
+public class BdbjeCacheStoreTest {
+ private BdbjeCacheStore cs;
+ private BdbjeCacheStoreConfig cfg;
+ private BdbjeResourceFactory factory;
+ private Cache cache;
+ private Environment env;
+ private Database cacheDb;
+ private Database catalogDb;
+ private StoredClassCatalog catalog;
+ private StoredMap cacheMap;
+ private PreparableTransactionRunner runner;
+ private CurrentTransaction currentTransaction;
+
+ private class MockBdbjeResourceFactory extends BdbjeResourceFactory {
+
+ @Override
+ public PreparableTransactionRunner createPreparableTransactionRunner(Environment env) {
+ return runner;
+ }
+
+ @Override
+ public CurrentTransaction createCurrentTransaction(Environment env) {
+ return currentTransaction;
+ }
+
+ @Override
+ public Environment createEnvironment(File envLocation) throws DatabaseException {
+ return env;
+ }
+
+ @Override
+ public StoredClassCatalog createStoredClassCatalog(Database catalogDb) throws DatabaseException {
+ return catalog;
+ }
+
+ @Override
+ public Database createDatabase(Environment env, String name) throws DatabaseException {
+ if (name.equals(cfg.getCacheDbName()))
+ return cacheDb;
+ else
+ return catalogDb;
+ }
+
+ @Override
+ public StoredMap createStoredMapViewOfDatabase(Database database, StoredClassCatalog classCatalog) throws DatabaseException {
+ return cacheMap;
+ }
+
+ public MockBdbjeResourceFactory(BdbjeCacheStoreConfig config) {
+ super(config);
+ }
+ }
+
+ @BeforeMethod
+ public void setUp() throws Exception {
+ cfg = new BdbjeCacheStoreConfig();
+ factory = new MockBdbjeResourceFactory(cfg);
+ cache = createMock(Cache.class);
+ cs = new BdbjeCacheStore();
+ env = createMock(Environment.class);
+ cacheDb = createMock(Database.class);
+ catalogDb = createMock(Database.class);
+ catalog = createMock(StoredClassCatalog.class);
+ cacheMap = createMock(StoredMap.class);
+ currentTransaction = createMock(CurrentTransaction.class);
+ WeakReference<Environment> envRef = new WeakReference<Environment>(env);
+ ReflectionUtil.setValue(currentTransaction,"envRef",envRef);
+ ThreadLocal localTrans = new ThreadLocal();
+ ReflectionUtil.setValue(currentTransaction,"localTrans",localTrans);
+ runner = createMock(PreparableTransactionRunner.class);
+ }
+
+ @AfterMethod
+ public void tearDown() throws CacheLoaderException {
+ runner = null;
+ currentTransaction = null;
+ cacheMap = null;
+ catalogDb = null;
+ cacheDb = null;
+ env = null;
+ factory = null;
+ cache = null;
+ cfg = null;
+ cs = null;
+ }
+
+ void start() throws DatabaseException, CacheLoaderException {
+ cs.init(cfg, factory, cache);
+ expect(cache.getName()).andReturn("cache");
+ }
+
+ @Test
+ public void testGetConfigurationClass() throws Exception {
+ replayAll();
+ assert cs.getConfigurationClass().equals(BdbjeCacheStoreConfig.class);
+ verifyAll();
+ }
+
+ void replayAll() throws Exception {
+ replay(runner);
+ replay(currentTransaction);
+ replay(cacheMap);
+ replay(catalog);
+ replay(catalogDb);
+ replay(cacheDb);
+ replay(env);
+ replay(cache);
+ }
+
+ void verifyAll() throws Exception {
+ verify(runner);
+ verify(currentTransaction);
+ verify(cacheMap);
+ verify(catalog);
+ verify(catalogDb);
+ verify(env);
+ verify(cache);
+ }
+
+ @Test
+ public void testInitNoMock() throws Exception {
+ replayAll();
+ cs.init(cfg, cache, null);
+ assert cfg.equals(ReflectionUtil.getValue(cs, "cfg"));
+ assert cache.equals(ReflectionUtil.getValue(cs, "cache"));
+ assert ReflectionUtil.getValue(cs, "factory") instanceof BdbjeResourceFactory;
+ verifyAll();
+ }
+
+ @Test
+ void testExceptionClosingCacheDatabaseDoesntPreventEnvironmentFromClosing() throws Exception {
+ start();
+ cacheDb.close();
+ expectLastCall().andThrow(new DatabaseException());
+ catalog.close();
+ env.close();
+ replayAll();
+ cs.start();
+ cs.stop();
+
+ verifyAll();
+ }
+
+ @Test
+ void testExceptionClosingCatalogDoesntPreventEnvironmentFromClosing() throws Exception {
+ start();
+ cacheDb.close();
+ catalog.close();
+ expectLastCall().andThrow(new DatabaseException());
+ env.close();
+ replayAll();
+ cs.start();
+ cs.stop();
+ verifyAll();
+ }
+
+ @Test(expectedExceptions = CacheLoaderException.class)
+ void testExceptionClosingEnvironment() throws Exception {
+ start();
+ cacheDb.close();
+ catalog.close();
+ env.close();
+ expectLastCall().andThrow(new DatabaseException());
+ replayAll();
+ cs.start();
+ cs.stop();
+ verifyAll();
+ }
+
+
+ @Test(expectedExceptions = CacheLoaderException.class)
+ void testThrowsCorrectExceptionOnStartForDatabaseException() throws Exception {
+ factory = new MockBdbjeResourceFactory(cfg) {
+ @Override
+ public StoredClassCatalog createStoredClassCatalog(Database catalogDb) throws DatabaseException {
+ throw new DatabaseException();
+ }
+ };
+ start();
+ replayAll();
+ cs.start();
+
+ }
+
+ @Test(expectedExceptions = CacheLoaderException.class)
+ void testEnvironmentDirectoryExistsButNotAFile() throws Exception {
+ File file = createMock(File.class);
+ expect(file.exists()).andReturn(true);
+ expect(file.isDirectory()).andReturn(false);
+ replay(file);
+ cs.verifyOrCreateEnvironmentDirectory(file);
+ }
+
+ @Test(expectedExceptions = CacheLoaderException.class)
+ void testCantCreateEnvironmentDirectory() throws Exception {
+ File file = createMock(File.class);
+ expect(file.exists()).andReturn(false);
+ expect(file.mkdirs()).andReturn(false);
+ replay(file);
+ cs.verifyOrCreateEnvironmentDirectory(file);
+ }
+
+ @Test
+ void testCanCreateEnvironmentDirectory() throws Exception {
+ File file = createMock(File.class);
+ expect(file.exists()).andReturn(false);
+ expect(file.mkdirs()).andReturn(true);
+ expect(file.isDirectory()).andReturn(true);
+ replay(file);
+ assert file.equals(cs.verifyOrCreateEnvironmentDirectory(file));
+ }
+
+ @Test
+ public void testNoExceptionOnRollback() throws Exception {
+ start();
+ Transaction tx = createMock(Transaction.class);
+ replayAll();
+ cs.start();
+ cs.rollback(tx);
+ verifyAll();
+ }
+
+ @Test
+ protected void testApplyModificationsThrowsOriginalDatabaseException() throws Exception {
+ start();
+ DatabaseException ex = new DatabaseException();
+ runner.run(isA(TransactionWorker.class));
+ expectLastCall().andThrow(new RuntimeExceptionWrapper(ex));
+ replayAll();
+ cs.start();
+ try {
+ cs.applyModifications(Collections.singletonList(new Store(new StoredEntry("k", "v"))));
+ assert false : "should have gotten an exception";
+ } catch (CacheLoaderException e) {
+ assert ex.equals(e.getCause());
+ verifyAll();
+ return;
+ }
+ assert false : "should have returned";
+
+ }
+
+ @Test
+ protected void testCommitThrowsOriginalDatabaseException() throws Exception {
+ start();
+ DatabaseException ex = new DatabaseException();
+ com.sleepycat.je.Transaction txn = createMock(com.sleepycat.je.Transaction.class);
+ expect(currentTransaction.beginTransaction(null)).andReturn(txn);
+ runner.prepare(isA(TransactionWorker.class));
+ txn.commit();
+ expectLastCall().andThrow(new RuntimeExceptionWrapper(ex));
+ replayAll();
+ replay(txn);
+ cs.start();
+ try {
+ txn = currentTransaction.beginTransaction(null);
+ Transaction t = createMock(Transaction.class);
+ cs.prepare(Collections.singletonList(new Store(new StoredEntry("k", "v"))), t,false);
+ cs.commit(t);
+ assert false : "should have gotten an exception";
+ } catch (CacheLoaderException e) {
+ assert ex.equals(e.getCause());
+ verifyAll();
+ return;
+ }
+ assert false : "should have returned";
+
+ }
+
+ @Test
+ protected void testPrepareThrowsOriginalDatabaseException() throws Exception {
+ start();
+ DatabaseException ex = new DatabaseException();
+ runner.prepare(isA(TransactionWorker.class));
+ expectLastCall().andThrow(new RuntimeExceptionWrapper(ex));
+ replayAll();
+ cs.start();
+ try {
+ cs.prepare(Collections.singletonList(new Store(new StoredEntry("k", "v"))), createMock(Transaction.class),false);
+ assert false : "should have gotten an exception";
+ } catch (CacheLoaderException e) {
+ assert ex.equals(e.getCause());
+ verifyAll();
+ return;
+ }
+ assert false : "should have returned";
+
+ }
+
+ @Test
+ void testClearOnAbortFromStream() throws Exception {
+ start();
+ StoredEntry entry = new StoredEntry();
+ expect(cacheMap.put(entry.getKey(), entry)).andReturn(null);
+ ObjectInput ois = createMock(ObjectInput.class);
+ expect(ois.readLong()).andReturn(new Long(1));
+ com.sleepycat.je.Transaction txn = createMock( com.sleepycat.je.Transaction.class);
+ expect(currentTransaction.beginTransaction(null)).andReturn(txn);
+ cacheMap.clear();
+ Cursor cursor = createMock(Cursor.class);
+ expect(cacheDb.openCursor(txn, null)).andReturn(cursor);
+ IOException ex = new IOException();
+ expect(ois.readObject()).andReturn(new byte[0]);
+ expectLastCall().andThrow(ex);
+ txn.abort();
+ cacheMap.clear();
+ replay(ois);
+ replay(txn);
+ replayAll();
+ cs.start();
+ try {
+ cs.store(entry);
+ cs.fromStream(ois);
+ assert false : "should have gotten an exception";
+ } catch (CacheLoaderException e) {
+ assert ex.equals(e.getCause());
+ verifyAll();
+ verify(ois);
+ verify(txn);
+ return;
+ }
+ assert false : "should have returned";
+ }
+}
Modified: core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeLearningTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeLearningTest.java 2009-03-05 00:23:55 UTC (rev 7853)
+++ core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeLearningTest.java 2009-03-05 02:09:04 UTC (rev 7854)
@@ -662,6 +662,7 @@
assert expected.isEmpty();
}
+ @Test (enabled = false)
public void testConcurrency() throws Throwable {
int numThreads = 3;
final int loops = 500;
Added: core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeResourceFactoryTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeResourceFactoryTest.java (rev 0)
+++ core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeResourceFactoryTest.java 2009-03-05 02:09:04 UTC (rev 7854)
@@ -0,0 +1,48 @@
+package org.horizon.loader.bdbje;
+
+import com.sleepycat.bind.serial.StoredClassCatalog;
+import com.sleepycat.je.Database;
+import com.sleepycat.je.DatabaseException;
+import com.sleepycat.je.Environment;
+import static org.easymock.classextension.EasyMock.createMock;
+import org.horizon.loader.CacheLoaderException;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * @author Adrian Cole
+ * @version $Id: $
+ * @since 1.0
+ */
+@Test(groups = "unit", enabled = true, testName = "loader.bdbje.BdbjeResourceFactoryTest")
+public class BdbjeResourceFactoryTest {
+ private BdbjeCacheStoreConfig cfg;
+ private Environment env;
+ private BdbjeResourceFactory factory;
+ private Database cacheDb;
+ private StoredClassCatalog catalog;
+
+ @BeforeMethod
+ public void setUp() throws Exception {
+ cfg = new BdbjeCacheStoreConfig();
+ factory = new BdbjeResourceFactory(cfg);
+ env = createMock(Environment.class);
+ cacheDb = createMock(Database.class);
+ catalog = createMock(StoredClassCatalog.class);
+ }
+
+ @AfterMethod
+ public void tearDown() throws CacheLoaderException {
+ env = null;
+ factory = null;
+ cfg = null;
+ cacheDb = null;
+ catalog = null;
+ }
+
+ @Test(expectedExceptions = DatabaseException.class)
+ public void testCreateStoredMapViewOfDatabaseThrowsException() throws DatabaseException {
+ factory.createStoredMapViewOfDatabase(cacheDb, catalog);
+ }
+}
15 years, 10 months
JBoss Cache SVN: r7853 - core/branches/flat/src/test/java/org/horizon/marshall.
by jbosscache-commits@lists.jboss.org
Author: adriancole
Date: 2009-03-04 19:23:55 -0500 (Wed, 04 Mar 2009)
New Revision: 7853
Modified:
core/branches/flat/src/test/java/org/horizon/marshall/ObjectStreamMarshaller.java
Log:
needed to be Serializable for Bdbje to work
Modified: core/branches/flat/src/test/java/org/horizon/marshall/ObjectStreamMarshaller.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/marshall/ObjectStreamMarshaller.java 2009-03-05 00:22:03 UTC (rev 7852)
+++ core/branches/flat/src/test/java/org/horizon/marshall/ObjectStreamMarshaller.java 2009-03-05 00:23:55 UTC (rev 7853)
@@ -10,13 +10,14 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
+import java.io.Serializable;
/**
* A dummy marshaller impl that uses JDK object streams
*
* @author Manik Surtani
*/
-public class ObjectStreamMarshaller implements Marshaller {
+public class ObjectStreamMarshaller implements Marshaller, Serializable {
public void objectToObjectStream(Object obj, ObjectOutput out) throws IOException {
out.writeObject(obj);
}
15 years, 10 months
JBoss Cache SVN: r7852 - in core/branches/flat/src: test/java/org/horizon/loader and 1 other directory.
by jbosscache-commits@lists.jboss.org
Author: adriancole
Date: 2009-03-04 19:22:03 -0500 (Wed, 04 Mar 2009)
New Revision: 7852
Added:
core/branches/flat/src/test/java/org/horizon/loader/AbstractCacheStoreConfigTest.java
core/branches/flat/src/test/java/org/horizon/loader/AbstractCacheStoreTest.java
Modified:
core/branches/flat/src/main/java/org/horizon/loader/AbstractCacheStoreConfig.java
Log:
added javadoc and tests for base classes
Modified: core/branches/flat/src/main/java/org/horizon/loader/AbstractCacheStoreConfig.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/AbstractCacheStoreConfig.java 2009-03-04 18:04:58 UTC (rev 7851)
+++ core/branches/flat/src/main/java/org/horizon/loader/AbstractCacheStoreConfig.java 2009-03-05 00:22:03 UTC (rev 7852)
@@ -1,9 +1,17 @@
package org.horizon.loader;
/**
- * Configuration for {@link AbstractCacheStore}.
+ * Configures {@link AbstractCacheStore}. This allows you to tune a number of characteristics of the {@link
+ * AbstractCacheStore}.
+ * <p/>
+ * <ul> <li><tt>purgeSynchronously</tt> - whether {@link org.horizon.loader.CacheStore#purgeExpired()} calls happen
+ * synchronously or not. By default, this is set to <tt>false</tt>.</li>
+ * <p/>
+ * </ul>
*
* @author Mircea.Markus(a)jboss.com
+ * @version $Id$
+ * @since 1.0
*/
public class AbstractCacheStoreConfig extends AbstractCacheLoaderConfig {
Added: core/branches/flat/src/test/java/org/horizon/loader/AbstractCacheStoreConfigTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/AbstractCacheStoreConfigTest.java (rev 0)
+++ core/branches/flat/src/test/java/org/horizon/loader/AbstractCacheStoreConfigTest.java 2009-03-05 00:22:03 UTC (rev 7852)
@@ -0,0 +1,38 @@
+package org.horizon.loader;
+
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * Unit tests that cover {@link AbstractCacheStoreConfig }
+ *
+ * @author Adrian Cole
+ * @version $Id: $
+ * @since 1.0
+ */
+@Test(groups = "unit", enabled = true, testName = "loader.bdbje.AbstractCacheStoreConfig")
+public class AbstractCacheStoreConfigTest {
+ private AbstractCacheStoreConfig config;
+
+ @BeforeMethod
+ public void setUp() throws Exception {
+ config = new AbstractCacheStoreConfig();
+ }
+
+ @AfterMethod
+ public void tearDown() throws CacheLoaderException {
+ config = null;
+ }
+
+ @Test
+ public void testIsPurgeSynchronously() {
+ assert !config.isPurgeSynchronously();
+ }
+
+ @Test
+ public void testSetPurgeSynchronously() {
+ config.setPurgeSynchronously(true);
+ assert config.isPurgeSynchronously();
+ }
+}
\ No newline at end of file
Added: core/branches/flat/src/test/java/org/horizon/loader/AbstractCacheStoreTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/AbstractCacheStoreTest.java (rev 0)
+++ core/branches/flat/src/test/java/org/horizon/loader/AbstractCacheStoreTest.java 2009-03-05 00:22:03 UTC (rev 7852)
@@ -0,0 +1,56 @@
+package org.horizon.loader;
+
+import static org.easymock.classextension.EasyMock.*;
+import org.horizon.util.ReflectionUtil;
+import org.horizon.util.concurrent.WithinThreadExecutor;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+import java.lang.reflect.Method;
+import java.util.concurrent.ExecutorService;
+
+/**
+ * Unit tests that cover {@link AbstractCacheStoreTest }
+ *
+ * @author Adrian Cole
+ * @version $Id: $
+ * @since 1.0
+ */
+@Test(groups = "unit", enabled = true, testName = "loader.bdbje.AbstractCacheStoreTest")
+public class AbstractCacheStoreTest {
+ private AbstractCacheStore cs;
+ private AbstractCacheStoreConfig cfg;
+
+ @BeforeTest
+ public void setUp() throws NoSuchMethodException {
+ cs = createMock(AbstractCacheStore.class,new Method[] { AbstractCacheStore.class.getMethod("clear", null) });
+ cfg = new AbstractCacheStoreConfig();
+ cs.init(cfg,null,null);
+ }
+
+ @AfterTest
+ public void tearDown() throws CacheLoaderException {
+ cs.stop();
+ cs = null;
+ cfg = null;
+ }
+
+
+ @Test
+ void testSynchExecutorIsSetWhenCfgPurgeSynchIsTrueOnStart() throws Exception {
+ cfg.setPurgeSynchronously(true);
+ cs.start();
+ ExecutorService service = (ExecutorService) ReflectionUtil.getValue(cs, "purgerService");
+ assert service instanceof WithinThreadExecutor;
+ }
+
+
+ @Test
+ void testASynchExecutorIsDefaultOnStart() throws Exception {
+ cs.start();
+ ExecutorService service = (ExecutorService) ReflectionUtil.getValue(cs, "purgerService");
+ assert !(service instanceof WithinThreadExecutor);
+ }
+
+}
15 years, 10 months
JBoss Cache SVN: r7851 - in core/branches/flat/src: main/java/org/horizon/loader/jdbc/stringbased and 2 other directories.
by jbosscache-commits@lists.jboss.org
Author: mircea.markus
Date: 2009-03-04 13:04:58 -0500 (Wed, 04 Mar 2009)
New Revision: 7851
Added:
core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/
core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/DefaultKey2StringMapperTest.java
core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreTest.java
core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreTest2.java
core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/Person.java
core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/PersonKey2StringMapper.java
Removed:
core/branches/flat/src/test/java/org/horizon/loader/jdbc/DefaultKey2StringMapperTest.java
core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcStringBasedCacheStoreTest.java
Modified:
core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStore.java
core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/DefaultKey2StringMapper.java
core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStore.java
core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/Key2StringMapper.java
core/branches/flat/src/test/java/org/horizon/loader/jdbc/UnitTestDatabaseManager.java
Log:
ongoing jdbc cache store work
Modified: core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStore.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStore.java 2009-03-04 17:31:09 UTC (rev 7850)
+++ core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStore.java 2009-03-04 18:04:58 UTC (rev 7851)
@@ -101,6 +101,6 @@
}
private CacheStore getCacheStore(Object key) {
- return stringBasedCacheStore.supportsKey(key) ? stringBasedCacheStore : binaryCacheStore;
+ return stringBasedCacheStore.supportsKey(key.getClass()) ? stringBasedCacheStore : binaryCacheStore;
}
}
Modified: core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/DefaultKey2StringMapper.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/DefaultKey2StringMapper.java 2009-03-04 17:31:09 UTC (rev 7850)
+++ core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/DefaultKey2StringMapper.java 2009-03-04 18:04:58 UTC (rev 7851)
@@ -11,8 +11,7 @@
/**
* Returns true if this is an primitive wrapper, false otherwise.
*/
- public boolean isSupportedType(Object key) {
- Class clazz = key.getClass();
+ public boolean isSupportedType(Class key) {
return key == String.class ||
key == Short.class ||
key == Byte.class ||
Modified: core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStore.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStore.java 2009-03-04 17:31:09 UTC (rev 7850)
+++ core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStore.java 2009-03-04 18:04:58 UTC (rev 7851)
@@ -311,8 +311,8 @@
throw new CacheLoaderException(message, e);
}
- public boolean supportsKey(Object obj) {
- return key2StringMapper.isSupportedType(obj);
+ public boolean supportsKey(Class keyType) {
+ return key2StringMapper.isSupportedType(keyType);
}
/**
@@ -327,7 +327,7 @@
tableManipulation.start(connectionFactory);
}
- public Object getConnectionFactory() {
+ public ConnectionFactory getConnectionFactory() {
return connectionFactory;
}
}
Modified: core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/Key2StringMapper.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/Key2StringMapper.java 2009-03-04 17:31:09 UTC (rev 7850)
+++ core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/Key2StringMapper.java 2009-03-04 18:04:58 UTC (rev 7851)
@@ -13,7 +13,7 @@
/**
* Do we support this key type?
*/
- boolean isSupportedType(Object key);
+ boolean isSupportedType(Class keyType);
/**
* Must return an unique String for the supplied key.
Deleted: core/branches/flat/src/test/java/org/horizon/loader/jdbc/DefaultKey2StringMapperTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/jdbc/DefaultKey2StringMapperTest.java 2009-03-04 17:31:09 UTC (rev 7850)
+++ core/branches/flat/src/test/java/org/horizon/loader/jdbc/DefaultKey2StringMapperTest.java 2009-03-04 18:04:58 UTC (rev 7851)
@@ -1,34 +0,0 @@
-package org.horizon.loader.jdbc;
-
-import org.testng.annotations.Test;
-import org.horizon.loader.jdbc.stringbased.DefaultKey2StringMapper;
-
-/**
- * Tester for {@link org.horizon.loader.jdbc.stringbased.Key2StringMapper}.
- *
- * @author Mircea.Markus(a)jboss.com
- */
-@Test(groups = "unit", testName = "loader.jdbc.DefaultKey2StringMapperTest")
-public class DefaultKey2StringMapperTest {
-
- DefaultKey2StringMapper mapper = new DefaultKey2StringMapper();
-
- public void testPrimitivesAreSupported() {
- assert mapper.isSupportedType(Integer.class);
- assert mapper.isSupportedType(Byte.class);
- assert mapper.isSupportedType(Short.class);
- assert mapper.isSupportedType(Long.class);
- assert mapper.isSupportedType(Double.class);
- assert mapper.isSupportedType(Float.class);
- assert mapper.isSupportedType(Boolean.class);
- assert mapper.isSupportedType(String.class);
- }
-
- @SuppressWarnings(value = "all")
- public void testGetStingMapping() {
- Object[] toTest = {new Integer(0), new Byte("1"), new Short("2"), new Long(3), new Double("3.4"), new Float("3.5"), Boolean.FALSE, "some string"};
- for (Object o : toTest) {
- assert mapper.getStringMapping(o).equals(o.toString());
- }
- }
-}
Deleted: core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcStringBasedCacheStoreTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcStringBasedCacheStoreTest.java 2009-03-04 17:31:09 UTC (rev 7850)
+++ core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcStringBasedCacheStoreTest.java 2009-03-04 18:04:58 UTC (rev 7851)
@@ -1,58 +0,0 @@
-package org.horizon.loader.jdbc;
-
-import org.horizon.loader.BaseCacheStoreTest;
-import org.horizon.loader.CacheStore;
-import org.horizon.loader.jdbc.connectionfactory.ConnectionFactory;
-import org.horizon.loader.jdbc.connectionfactory.ConnectionFactoryConfig;
-import org.horizon.loader.jdbc.stringbased.JdbcStringBasedCacheStore;
-import org.horizon.loader.jdbc.stringbased.JdbcStringBasedCacheStoreConfig;
-import org.horizon.marshall.ObjectStreamMarshaller;
-import org.testng.annotations.Test;
-import static org.easymock.classextension.EasyMock.*;
-import static org.easymock.classextension.EasyMock.replay;
-import static org.easymock.classextension.EasyMock.verify;
-
-/**
- * Tester class for {@link org.horizon.loader.jdbc.stringbased.JdbcStringBasedCacheStore}.
- *
- * @author Mircea.Markus(a)jboss.com
- */
-@Test(groups = "functional", testName = "loader.jdbc.JdbcStringBasedCacheStoreTest")
-public class JdbcStringBasedCacheStoreTest extends BaseCacheStoreTest {
-
- protected CacheStore createCacheStore() throws Exception {
- ConnectionFactoryConfig connectionFactoryConfig = UnitTestDatabaseManager.getUniqueConnectionFactoryConfig();
- TableManipulation tm = UnitTestDatabaseManager.buildDefaultTableManipulation();
- JdbcStringBasedCacheStoreConfig config = new JdbcStringBasedCacheStoreConfig(connectionFactoryConfig, tm);
- JdbcStringBasedCacheStore jdbcBucketCacheStore = new JdbcStringBasedCacheStore();
- jdbcBucketCacheStore.init(config, null, new ObjectStreamMarshaller());
- jdbcBucketCacheStore.start();
- return jdbcBucketCacheStore;
- }
-
- public void testNotCreateConnectionFactory() throws Exception {
- JdbcStringBasedCacheStore stringBasedCacheStore = new JdbcStringBasedCacheStore();
- JdbcStringBasedCacheStoreConfig config = new JdbcStringBasedCacheStoreConfig(false);
- config.setCreateTableOnStart(false);
- stringBasedCacheStore.init(config, null, new ObjectStreamMarshaller());
- stringBasedCacheStore.start();
- assert stringBasedCacheStore.getConnectionFactory() == null;
-
- /* this will make sure that if a method like stop is called on the connection then it will barf an exception */
- ConnectionFactory connectionFactory = createMock(ConnectionFactory.class);
- TableManipulation tableManipulation = createMock(TableManipulation.class);
- config.setTableManipulation(tableManipulation);
-
- tableManipulation.start(connectionFactory);
- replay(tableManipulation);
- stringBasedCacheStore.doConnectionFactoryInitialization(connectionFactory);
- verify(tableManipulation);
-
- //stop should be called even if this is an external
- reset(tableManipulation, connectionFactory);
- tableManipulation.stop();
- replay(tableManipulation, connectionFactory);
- stringBasedCacheStore.stop();
- verify(tableManipulation, connectionFactory);
- }
-}
Modified: core/branches/flat/src/test/java/org/horizon/loader/jdbc/UnitTestDatabaseManager.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/jdbc/UnitTestDatabaseManager.java 2009-03-04 17:31:09 UTC (rev 7850)
+++ core/branches/flat/src/test/java/org/horizon/loader/jdbc/UnitTestDatabaseManager.java 2009-03-04 18:04:58 UTC (rev 7851)
@@ -75,7 +75,7 @@
TestingUtil.recursiveFileRemove(toDel);
}
- private static String getDatabaseName(Properties prop) {
+ public static String getDatabaseName(Properties prop) {
StringTokenizer tokenizer = new StringTokenizer(prop.getProperty("cache.jdbc.url"), ":");
tokenizer.nextToken();
tokenizer.nextToken();
Copied: core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/DefaultKey2StringMapperTest.java (from rev 7847, core/branches/flat/src/test/java/org/horizon/loader/jdbc/DefaultKey2StringMapperTest.java)
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/DefaultKey2StringMapperTest.java (rev 0)
+++ core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/DefaultKey2StringMapperTest.java 2009-03-04 18:04:58 UTC (rev 7851)
@@ -0,0 +1,33 @@
+package org.horizon.loader.jdbc.stringbased;
+
+import org.testng.annotations.Test;
+
+/**
+ * Tester for {@link org.horizon.loader.jdbc.stringbased.Key2StringMapper}.
+ *
+ * @author Mircea.Markus(a)jboss.com
+ */
+@Test(groups = "unit", testName = "loader.jdbc.DefaultKey2StringMapperTest")
+public class DefaultKey2StringMapperTest {
+
+ DefaultKey2StringMapper mapper = new DefaultKey2StringMapper();
+
+ public void testPrimitivesAreSupported() {
+ assert mapper.isSupportedType(Integer.class);
+ assert mapper.isSupportedType(Byte.class);
+ assert mapper.isSupportedType(Short.class);
+ assert mapper.isSupportedType(Long.class);
+ assert mapper.isSupportedType(Double.class);
+ assert mapper.isSupportedType(Float.class);
+ assert mapper.isSupportedType(Boolean.class);
+ assert mapper.isSupportedType(String.class);
+ }
+
+ @SuppressWarnings(value = "all")
+ public void testGetStingMapping() {
+ Object[] toTest = {new Integer(0), new Byte("1"), new Short("2"), new Long(3), new Double("3.4"), new Float("3.5"), Boolean.FALSE, "some string"};
+ for (Object o : toTest) {
+ assert mapper.getStringMapping(o).equals(o.toString());
+ }
+ }
+}
Property changes on: core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/DefaultKey2StringMapperTest.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Copied: core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreTest.java (from rev 7848, core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcStringBasedCacheStoreTest.java)
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreTest.java (rev 0)
+++ core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreTest.java 2009-03-04 18:04:58 UTC (rev 7851)
@@ -0,0 +1,56 @@
+package org.horizon.loader.jdbc.stringbased;
+
+import static org.easymock.classextension.EasyMock.*;
+import org.horizon.loader.BaseCacheStoreTest;
+import org.horizon.loader.CacheStore;
+import org.horizon.loader.jdbc.TableManipulation;
+import org.horizon.loader.jdbc.UnitTestDatabaseManager;
+import org.horizon.loader.jdbc.connectionfactory.ConnectionFactory;
+import org.horizon.loader.jdbc.connectionfactory.ConnectionFactoryConfig;
+import org.horizon.marshall.ObjectStreamMarshaller;
+import org.testng.annotations.Test;
+
+/**
+ * Tester class for {@link org.horizon.loader.jdbc.stringbased.JdbcStringBasedCacheStore}.
+ *
+ * @author Mircea.Markus(a)jboss.com
+ */
+@Test(groups = "functional", testName = "loader.jdbc.JdbcStringBasedCacheStoreTest")
+public class JdbcStringBasedCacheStoreTest extends BaseCacheStoreTest {
+
+ protected CacheStore createCacheStore() throws Exception {
+ ConnectionFactoryConfig connectionFactoryConfig = UnitTestDatabaseManager.getUniqueConnectionFactoryConfig();
+ TableManipulation tm = UnitTestDatabaseManager.buildDefaultTableManipulation();
+ JdbcStringBasedCacheStoreConfig config = new JdbcStringBasedCacheStoreConfig(connectionFactoryConfig, tm);
+ JdbcStringBasedCacheStore jdbcBucketCacheStore = new JdbcStringBasedCacheStore();
+ jdbcBucketCacheStore.init(config, null, new ObjectStreamMarshaller());
+ jdbcBucketCacheStore.start();
+ return jdbcBucketCacheStore;
+ }
+
+ public void testNotCreateConnectionFactory() throws Exception {
+ JdbcStringBasedCacheStore stringBasedCacheStore = new JdbcStringBasedCacheStore();
+ JdbcStringBasedCacheStoreConfig config = new JdbcStringBasedCacheStoreConfig(false);
+ config.setCreateTableOnStart(false);
+ stringBasedCacheStore.init(config, null, new ObjectStreamMarshaller());
+ stringBasedCacheStore.start();
+ assert stringBasedCacheStore.getConnectionFactory() == null;
+
+ /* this will make sure that if a method like stop is called on the connection then it will barf an exception */
+ ConnectionFactory connectionFactory = createMock(ConnectionFactory.class);
+ TableManipulation tableManipulation = createMock(TableManipulation.class);
+ config.setTableManipulation(tableManipulation);
+
+ tableManipulation.start(connectionFactory);
+ replay(tableManipulation);
+ stringBasedCacheStore.doConnectionFactoryInitialization(connectionFactory);
+ verify(tableManipulation);
+
+ //stop should be called even if this is an external
+ reset(tableManipulation, connectionFactory);
+ tableManipulation.stop();
+ replay(tableManipulation, connectionFactory);
+ stringBasedCacheStore.stop();
+ verify(tableManipulation, connectionFactory);
+ }
+}
Property changes on: core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreTest.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreTest2.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreTest2.java (rev 0)
+++ core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreTest2.java 2009-03-04 18:04:58 UTC (rev 7851)
@@ -0,0 +1,140 @@
+package org.horizon.loader.jdbc.stringbased;
+
+import org.horizon.loader.CacheLoaderException;
+import org.horizon.loader.CacheStore;
+import org.horizon.loader.StoredEntry;
+import org.horizon.loader.jdbc.JdbcUtil;
+import org.horizon.loader.jdbc.TableManipulation;
+import org.horizon.loader.jdbc.UnitTestDatabaseManager;
+import org.horizon.loader.jdbc.connectionfactory.ConnectionFactoryConfig;
+import org.horizon.marshall.ObjectStreamMarshaller;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.Collections;
+import java.util.Set;
+import java.util.HashSet;
+
+/**
+ * Tester for {@link JdbcStringBasedCacheStore} with an alternative {@link org.horizon.loader.jdbc.stringbased.Key2StringMapper}.
+ *
+ * @author Mircea.Markus(a)jboss.com
+ */
+@Test(groups = "functional", testName = "jdbc.stringbased.JdbcStringBasedCacheStoreTest2")
+public class JdbcStringBasedCacheStoreTest2 {
+
+ CacheStore cacheStore;
+ private ConnectionFactoryConfig cfc;
+ private TableManipulation tableManipulation;
+ private static final Person MIRCEA = new Person("Mircea", "Markus", 28);
+ private static final Person MANIK = new Person("Manik", "Surtani", 18);
+
+ @BeforeTest
+ public void createCacheStore() throws CacheLoaderException {
+ tableManipulation = UnitTestDatabaseManager.buildDefaultTableManipulation();
+ cfc = UnitTestDatabaseManager.getUniqueConnectionFactoryConfig();
+ JdbcStringBasedCacheStoreConfig config = new JdbcStringBasedCacheStoreConfig(cfc, tableManipulation);
+ config.setKey2StringMapperClass(PersonKey2StringMapper.class.getName());
+ cacheStore = new JdbcStringBasedCacheStore();
+ cacheStore.init(config, null, new ObjectStreamMarshaller());
+ cacheStore.start();
+ }
+
+ @AfterMethod
+ public void clearStore() throws Exception {
+ cacheStore.clear();
+ assert rowCount() == 0;
+ }
+
+ @AfterTest
+ public void destroyStore() throws CacheLoaderException {
+ cacheStore.stop();
+ UnitTestDatabaseManager.shutdownInMemoryDatabase(cfc);
+ }
+
+ /**
+ * When trying to perists an unsupported object an exception is expected.
+ */
+ public void persistUnsupportedObject() throws Exception {
+ try {
+ cacheStore.store(newStoredEntry("key", "value"));
+ assert false : "exception is expected as PersonKey2StringMapper does not support strings";
+ } catch (UnsupportedKeyTypeException e) {
+ assert true : "expected";
+ }
+ //just check that an person object will be persisted okay
+ cacheStore.store(newStoredEntry(MIRCEA, "Cluj Napoca"));
+ }
+
+ public void testStoreLoadRemove() throws Exception {
+ assert rowCount() == 0;
+ assert cacheStore.load(MIRCEA) == null : "should not be present in the store";
+ String value = "adsdsadsa";
+ cacheStore.store(newStoredEntry(MIRCEA, value));
+ assert rowCount() == 1;
+ assert cacheStore.load(MIRCEA).getValue().equals(value);
+ assert !cacheStore.remove(MANIK);
+ assert cacheStore.load(MIRCEA).getValue().equals(value);
+ assert rowCount() == 1;
+ assert cacheStore.remove(MIRCEA);
+ assert rowCount() == 0;
+ }
+
+ public void testRemoveAll() throws Exception {
+ assert rowCount() == 0;
+ cacheStore.store(new StoredEntry(MIRCEA, "value"));
+ cacheStore.store(new StoredEntry(MANIK, "value"));
+ assert rowCount() == 2;
+ cacheStore.removeAll(Collections.singleton((Object)MIRCEA));
+ cacheStore.load(MANIK).getValue().equals("value");
+ assert rowCount() == 1;
+ cacheStore.store(new StoredEntry(MIRCEA,"value"));
+ assert rowCount() == 2;
+ Set toRemove = new HashSet();
+ toRemove.add(MIRCEA);
+ toRemove.add(MANIK);
+ cacheStore.removeAll(toRemove);
+ assert rowCount() == 0;
+ }
+
+ public void testClear() throws Exception {
+ assert rowCount() == 0;
+ cacheStore.store(new StoredEntry(MIRCEA, "value"));
+ cacheStore.store(new StoredEntry(MANIK, "value"));
+ assert rowCount() == 2;
+ cacheStore.clear();
+ assert rowCount() == 0;
+ }
+
+ private int rowCount() {
+ JdbcStringBasedCacheStore store = (JdbcStringBasedCacheStore) cacheStore;
+ Connection conn = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try {
+ conn = store.getConnectionFactory().getConnection();
+ String tableName = tableManipulation.getTableName();
+ String sql = "SELECT count(*) FROM " + tableName;
+ statement = conn.prepareStatement(sql);
+ resultSet = statement.executeQuery();
+ resultSet.next();
+ return resultSet.getInt(1);
+ } catch (Exception ex) {
+ throw new RuntimeException(ex);
+ }
+ finally {
+ JdbcUtil.safeClose(resultSet);
+ JdbcUtil.safeClose(statement);
+ store.getConnectionFactory().releaseConnection(conn);
+ }
+ }
+
+ private StoredEntry newStoredEntry(Object key, Object value) {
+ return new StoredEntry(key, value);
+ }
+}
Property changes on: core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreTest2.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/Person.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/Person.java (rev 0)
+++ core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/Person.java 2009-03-04 18:04:58 UTC (rev 7851)
@@ -0,0 +1,54 @@
+package org.horizon.loader.jdbc.stringbased;
+
+import java.io.Serializable;
+
+/**
+ * Pojo used for testing jdbc caches stores.
+ *
+ * @author Mircea.Markus(a)jboss.com
+ */
+public class Person implements Serializable {
+ private String name;
+ private String surname;
+ private int age;
+
+ public Person(String name, String surname, int age) {
+ this.name = name;
+ this.surname = surname;
+ this.age = age;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getSurname() {
+ return surname;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof Person)) return false;
+
+ Person person = (Person) o;
+
+ if (age != person.age) return false;
+ if (name != null ? !name.equals(person.name) : person.name != null) return false;
+ if (surname != null ? !surname.equals(person.surname) : person.surname != null) return false;
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = name != null ? name.hashCode() : 0;
+ result = 31 * result + (surname != null ? surname.hashCode() : 0);
+ result = 31 * result + age;
+ return result;
+ }
+}
Property changes on: core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/Person.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/PersonKey2StringMapper.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/PersonKey2StringMapper.java (rev 0)
+++ core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/PersonKey2StringMapper.java 2009-03-04 18:04:58 UTC (rev 7851)
@@ -0,0 +1,17 @@
+package org.horizon.loader.jdbc.stringbased;
+
+/**
+ * Used for testing jdbc cache stores.
+ *
+ * @author Mircea.Markus(a)jboss.com
+ */
+public class PersonKey2StringMapper implements Key2StringMapper {
+ public boolean isSupportedType(Class keyType) {
+ return keyType == Person.class;
+ }
+
+ public String getStringMapping(Object key) {
+ Person person = (Person) key;
+ return person.getName() + "_" + person.getSurname() + "_" + person.getAge();
+ }
+}
Property changes on: core/branches/flat/src/test/java/org/horizon/loader/jdbc/stringbased/PersonKey2StringMapper.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
15 years, 10 months
JBoss Cache SVN: r7850 - core/branches/flat/src/test/java/org/horizon/statetransfer.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2009-03-04 12:31:09 -0500 (Wed, 04 Mar 2009)
New Revision: 7850
Modified:
core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferCacheLoaderFunctionalTest.java
core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferFunctionalTest.java
Log:
Enabled NBST tests
Modified: core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferCacheLoaderFunctionalTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferCacheLoaderFunctionalTest.java 2009-03-04 17:21:12 UTC (rev 7849)
+++ core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferCacheLoaderFunctionalTest.java 2009-03-04 17:31:09 UTC (rev 7850)
@@ -10,7 +10,7 @@
import org.horizon.test.TestingUtil;
import org.testng.annotations.Test;
-@Test(groups = "functional", testName = "statetransfer.StateTransferCacheLoaderFunctionalTest", enabled = false)
+@Test(groups = "functional", testName = "statetransfer.StateTransferCacheLoaderFunctionalTest", enabled = true)
public class StateTransferCacheLoaderFunctionalTest extends StateTransferFunctionalTest {
int id;
ThreadLocal<Boolean> sharedCacheLoader = new ThreadLocal<Boolean>() {
Modified: core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferFunctionalTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferFunctionalTest.java 2009-03-04 17:21:12 UTC (rev 7849)
+++ core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferFunctionalTest.java 2009-03-04 17:31:09 UTC (rev 7850)
@@ -16,7 +16,7 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
-@Test(groups = "functional", testName = "statetransfer.StateTransferFunctionalTest", enabled = false)
+@Test(groups = "functional", testName = "statetransfer.StateTransferFunctionalTest", enabled = true)
public class StateTransferFunctionalTest extends MultipleCacheManagersTest {
protected static final String ADDRESS_CLASSNAME = Address.class.getName();
15 years, 10 months
JBoss Cache SVN: r7849 - in core/branches/flat/src: test/java/org/horizon/statetransfer and 1 other directory.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2009-03-04 12:21:12 -0500 (Wed, 04 Mar 2009)
New Revision: 7849
Modified:
core/branches/flat/src/main/java/org/horizon/statetransfer/StateTransferManagerImpl.java
core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferCacheLoaderFunctionalTest.java
core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferFunctionalTest.java
Log:
Improved NBST tests
Modified: core/branches/flat/src/main/java/org/horizon/statetransfer/StateTransferManagerImpl.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/statetransfer/StateTransferManagerImpl.java 2009-03-04 17:02:56 UTC (rev 7848)
+++ core/branches/flat/src/main/java/org/horizon/statetransfer/StateTransferManagerImpl.java 2009-03-04 17:21:12 UTC (rev 7849)
@@ -90,8 +90,8 @@
this.interceptorChain = interceptorChain;
}
- @Start(priority = 14)
- // it is imperative that this starts *after* the RPCManager does.
+ @Start(priority = 55)
+ // it is imperative that this starts *after* the RPCManager does, and *after* the cache loader manager (if any) inits and preloads
public void start() throws StateTransferException {
log.trace("Data container is {0}", System.identityHashCode(dataContainer));
cs = clm == null ? null : clm.getCacheStore();
Modified: core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferCacheLoaderFunctionalTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferCacheLoaderFunctionalTest.java 2009-03-04 17:02:56 UTC (rev 7848)
+++ core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferCacheLoaderFunctionalTest.java 2009-03-04 17:21:12 UTC (rev 7849)
@@ -72,21 +72,24 @@
assert !l.containsKey(A_D_NAME);
}
-
public void testSharedLoader() throws Exception {
- sharedCacheLoader.set(true);
- Cache<Object, Object> c1 = createCacheManager().getCache(cacheName);
- writeInitialData(c1);
+ try {
+ sharedCacheLoader.set(true);
+ Cache<Object, Object> c1 = createCacheManager().getCache(cacheName);
+ writeInitialData(c1);
- // starting the second cache would initialize an in-memory state transfer but not a persistent one since the loader is shared
- Cache<Object, Object> c2 = createCacheManager().getCache(cacheName);
+ // starting the second cache would initialize an in-memory state transfer but not a persistent one since the loader is shared
+ Cache<Object, Object> c2 = createCacheManager().getCache(cacheName);
- TestingUtil.blockUntilViewsReceived(60000, c1, c2);
+ TestingUtil.blockUntilViewsReceived(60000, c1, c2);
- verifyInitialDataOnLoader(c1);
- verifyInitialData(c1);
+ verifyInitialDataOnLoader(c1);
+ verifyInitialData(c1);
- verifyNoDataOnLoader(c2);
- verifyNoData(c2);
+ verifyNoDataOnLoader(c2);
+ verifyNoData(c2);
+ } finally {
+ sharedCacheLoader.set(false);
+ }
}
}
Modified: core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferFunctionalTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferFunctionalTest.java 2009-03-04 17:02:56 UTC (rev 7848)
+++ core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferFunctionalTest.java 2009-03-04 17:21:12 UTC (rev 7849)
@@ -110,7 +110,10 @@
try {
if (c % 1000 == 0) {
if (tx) tm.begin();
- for (int i = 0; i < 1000; i++) cache.remove("test" + c);
+ for (int i = 0; i < 1000; i++) {
+ cache.remove("test" + c);
+ c++;
+ }
if (tx) tm.commit();
c = 0;
} else {
@@ -158,7 +161,7 @@
cache2 = createCacheManager().getCache(cacheName);
- cache1.put("delay", new DelayTransfer());
+ cache1.put("delay", new StateTransferFunctionalTest.DelayTransfer());
// Pause to give caches time to see each other
TestingUtil.blockUntilViewsReceived(60000, cache1, cache2);
15 years, 10 months
JBoss Cache SVN: r7848 - in core/branches/flat/src: main/java/org/horizon/loader/jdbc/binary and 2 other directories.
by jbosscache-commits@lists.jboss.org
Author: mircea.markus
Date: 2009-03-04 12:02:56 -0500 (Wed, 04 Mar 2009)
New Revision: 7848
Added:
core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStore.java
core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStoreConfig.java
core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcCacheStoreTest.java
Modified:
core/branches/flat/src/main/java/org/horizon/loader/jdbc/binary/JdbcBinaryCacheStore.java
core/branches/flat/src/main/java/org/horizon/loader/jdbc/binary/JdbcBinaryCacheStoreConfig.java
core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStore.java
core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreConfig.java
core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcBinaryCacheStoreTest.java
core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcStringBasedCacheStoreTest.java
Log:
ongoing jdbc cache store work
Added: core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStore.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStore.java (rev 0)
+++ core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStore.java 2009-03-04 17:02:56 UTC (rev 7848)
@@ -0,0 +1,106 @@
+package org.horizon.loader.jdbc;
+
+import org.horizon.Cache;
+import org.horizon.loader.AbstractCacheStore;
+import org.horizon.loader.CacheLoaderConfig;
+import org.horizon.loader.CacheLoaderException;
+import org.horizon.loader.CacheStore;
+import org.horizon.loader.StoredEntry;
+import org.horizon.loader.jdbc.binary.JdbcBinaryCacheStore;
+import org.horizon.loader.jdbc.connectionfactory.ConnectionFactory;
+import org.horizon.loader.jdbc.connectionfactory.ConnectionFactoryConfig;
+import org.horizon.loader.jdbc.stringbased.JdbcStringBasedCacheStore;
+import org.horizon.marshall.Marshaller;
+
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Set;
+
+/**
+ * // TODO: Mircea: Document this!
+ *
+ * @author
+ */
+public class JdbcCacheStore extends AbstractCacheStore {
+
+ private JdbcCacheStoreConfig config;
+ private JdbcBinaryCacheStore binaryCacheStore = new JdbcBinaryCacheStore();
+ private JdbcStringBasedCacheStore stringBasedCacheStore = new JdbcStringBasedCacheStore();
+ private ConnectionFactory sharedConnectionFactory;
+
+ @Override
+ public void init(CacheLoaderConfig config, Cache cache, Marshaller m) {
+ super.init(config, cache, m);
+ this.config = (JdbcCacheStoreConfig) config;
+ binaryCacheStore.init(this.config.getBinaryCacheStoreConfig(), cache, m);
+ stringBasedCacheStore.init(this.config.getStringCacheStoreConfig(), cache, m);
+ }
+
+ @Override
+ public void start() throws CacheLoaderException {
+ super.start();
+ ConnectionFactoryConfig factoryConfig = config.getConnectionFactoryConfig();
+ sharedConnectionFactory = ConnectionFactory.getConnectionFactory(factoryConfig.getConnectionFactoryClass());
+ sharedConnectionFactory.start(factoryConfig);
+ binaryCacheStore.start();
+ binaryCacheStore.doConnectionFactoryInitialization(sharedConnectionFactory);
+ stringBasedCacheStore.start();
+ stringBasedCacheStore.doConnectionFactoryInitialization(sharedConnectionFactory);
+ }
+
+ @Override
+ public void stop() throws CacheLoaderException {
+ super.stop();
+ binaryCacheStore.stop();
+ stringBasedCacheStore.stop();
+ sharedConnectionFactory.stop();
+ }
+
+ @Override
+ protected void purgeInternal() throws CacheLoaderException {
+ binaryCacheStore.purgeExpired();
+ stringBasedCacheStore.purgeExpired();
+ }
+
+ public StoredEntry load(Object key) throws CacheLoaderException {
+ return getCacheStore(key).load(key);
+ }
+
+ public Set<StoredEntry> loadAll() throws CacheLoaderException {
+ Set<StoredEntry> fromBuckets = binaryCacheStore.loadAll();
+ Set<StoredEntry> fromStrings = stringBasedCacheStore.loadAll();
+ fromBuckets.addAll(fromStrings);
+ return fromBuckets;
+ }
+
+ public void store(StoredEntry ed) throws CacheLoaderException {
+ getCacheStore(ed.getKey()).store(ed);
+ }
+
+ public void fromStream(ObjectInput inputStream) throws CacheLoaderException {
+ binaryCacheStore.fromStream(inputStream);
+ stringBasedCacheStore.fromStream(inputStream);
+ }
+
+ public void toStream(ObjectOutput outputStream) throws CacheLoaderException {
+ binaryCacheStore.toStream(outputStream);
+ stringBasedCacheStore.toStream(outputStream);
+ }
+
+ public boolean remove(Object key) throws CacheLoaderException {
+ return getCacheStore(key).remove(key);
+ }
+
+ public void clear() throws CacheLoaderException {
+ binaryCacheStore.clear();
+ stringBasedCacheStore.clear();
+ }
+
+ public Class<? extends CacheLoaderConfig> getConfigurationClass() {
+ return JdbcCacheStoreConfig.class;
+ }
+
+ private CacheStore getCacheStore(Object key) {
+ return stringBasedCacheStore.supportsKey(key) ? stringBasedCacheStore : binaryCacheStore;
+ }
+}
Property changes on: core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStore.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStoreConfig.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStoreConfig.java (rev 0)
+++ core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStoreConfig.java 2009-03-04 17:02:56 UTC (rev 7848)
@@ -0,0 +1,139 @@
+package org.horizon.loader.jdbc;
+
+import org.horizon.loader.AbstractCacheStoreConfig;
+import org.horizon.loader.jdbc.binary.JdbcBinaryCacheStoreConfig;
+import org.horizon.loader.jdbc.connectionfactory.ConnectionFactoryConfig;
+import org.horizon.loader.jdbc.stringbased.JdbcStringBasedCacheStoreConfig;
+
+/**
+ * // TODO: Mircea: Document this!
+ *
+ * @author
+ */
+public class JdbcCacheStoreConfig extends AbstractCacheStoreConfig {
+
+ private ConnectionFactoryConfig connectionFactoryConfig = new ConnectionFactoryConfig();
+ private TableManipulation binaryTableManipulation = new TableManipulation();
+ private TableManipulation stringsTableManipulation = new TableManipulation();
+
+
+ public JdbcCacheStoreConfig() {
+ this.className = JdbcCacheStore.class.getName();
+ }
+
+ public void setConnectionFactoryConfig(ConnectionFactoryConfig connectionFactoryConfig) {
+ this.connectionFactoryConfig = connectionFactoryConfig;
+ }
+
+ public void setBinaryTableManipulation(TableManipulation binaryTableManipulation) {
+ this.binaryTableManipulation = binaryTableManipulation;
+ }
+
+ public void setStringsTableManipulation(TableManipulation stringsTableManipulation) {
+ this.stringsTableManipulation = stringsTableManipulation;
+ }
+
+ JdbcBinaryCacheStoreConfig getBinaryCacheStoreConfig() {
+ JdbcBinaryCacheStoreConfig cacheStoreConfig = new JdbcBinaryCacheStoreConfig(false);
+ cacheStoreConfig.setTableManipulation(binaryTableManipulation);
+ return cacheStoreConfig;
+ }
+
+ JdbcStringBasedCacheStoreConfig getStringCacheStoreConfig() {
+ JdbcStringBasedCacheStoreConfig config = new JdbcStringBasedCacheStoreConfig(false);
+ config.setTableManipulation(stringsTableManipulation);
+ return config;
+ }
+
+ public void setIdColumnNameForStrings(String idColumnNameForStrings) {
+ this.stringsTableManipulation.setIdColumnName(idColumnNameForStrings);
+ }
+
+ public void setIdColumnTypeForStrings(String idColumnTypeForStrings) {
+ this.stringsTableManipulation.setIdColumnType(idColumnTypeForStrings);
+ }
+
+ public void setTableNameForStrings(String tableNameForStrings) {
+ this.stringsTableManipulation.setTableName(tableNameForStrings);
+ }
+
+ public void setDataColumnNameForStrings(String dataColumnNameForStrings) {
+ this.stringsTableManipulation.setDataColumnName(dataColumnNameForStrings);
+ }
+
+ public void setDataColumnTypeForStrings(String dataColumnTypeForStrings) {
+ this.stringsTableManipulation.setDataColumnType(dataColumnTypeForStrings);
+ }
+
+ public void setTimestampColumnNameForStrings(String timestampColumnNameForStrings) {
+ this.stringsTableManipulation.setTimestampColumnName(timestampColumnNameForStrings);
+ }
+
+ public void setTimestampColumnTypeForStrings(String timestampColumnTypeForStrings) {
+ this.stringsTableManipulation.setTimestampColumnType(timestampColumnTypeForStrings);
+ }
+
+ public void setCreateTableOnStartForStrings(boolean createTableOnStartForStrings) {
+ this.stringsTableManipulation.setCreateTableOnStart(createTableOnStartForStrings);
+ }
+
+ public void setDropTableOnExitForStrings(boolean dropTableOnExitForStrings) {
+ this.stringsTableManipulation.setDropTableOnExit(dropTableOnExitForStrings);
+ }
+
+ public void setIdColumnNameForBinary(String idColumnNameForBinary) {
+ this.binaryTableManipulation.setIdColumnName(idColumnNameForBinary);
+ }
+
+ public void setIdColumnTypeForBinary(String idColumnTypeForBinary) {
+ this.binaryTableManipulation.setIdColumnType(idColumnTypeForBinary);
+ }
+
+ public void setTableNameForBinary(String tableNameForBinary) {
+ this.binaryTableManipulation.setTableName(tableNameForBinary);
+ }
+
+ public void setDataColumnNameForBinary(String dataColumnNameForBinary) {
+ this.binaryTableManipulation.setDataColumnName(dataColumnNameForBinary);
+ }
+
+ public void setDataColumnTypeForBinary(String dataColumnTypeForBinary) {
+ this.binaryTableManipulation.setDataColumnType(dataColumnTypeForBinary);
+ }
+
+ public void setTimestampColumnNameForBinary(String timestampColumnNameForBinary) {
+ this.binaryTableManipulation.setTimestampColumnName(timestampColumnNameForBinary);
+ }
+
+ public void setTimestampColumnTypeForBinary(String timestampColumnTypeForBinary) {
+ this.binaryTableManipulation.setTimestampColumnType(timestampColumnTypeForBinary);
+ }
+
+ public void setCreateTableOnStartForBinary(boolean createTableOnStartForBinary) {
+ this.binaryTableManipulation.setCreateTableOnStart(createTableOnStartForBinary);
+ }
+
+ public void setDropTableOnExitForBinary(boolean dropTableOnExitForBinary) {
+ this.binaryTableManipulation.setDropTableOnExit(dropTableOnExitForBinary);
+ }
+
+ public void setDriverClass(String driverClass) {
+ this.connectionFactoryConfig.setDriverClass(driverClass);
+ }
+
+ public void setConnectionUrl(String connectionUrl) {
+ this.connectionFactoryConfig.setConnectionUrl(connectionUrl);
+ }
+
+ public void setUserName(String userName) {
+ this.connectionFactoryConfig.setUserName(userName);
+ }
+
+ public void setPassword(String password) {
+ this.connectionFactoryConfig.setPassword(password);
+ }
+
+ public ConnectionFactoryConfig getConnectionFactoryConfig() {
+ return connectionFactoryConfig;
+ }
+}
Property changes on: core/branches/flat/src/main/java/org/horizon/loader/jdbc/JdbcCacheStoreConfig.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified: core/branches/flat/src/main/java/org/horizon/loader/jdbc/binary/JdbcBinaryCacheStore.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/jdbc/binary/JdbcBinaryCacheStore.java 2009-03-04 13:32:42 UTC (rev 7847)
+++ core/branches/flat/src/main/java/org/horizon/loader/jdbc/binary/JdbcBinaryCacheStore.java 2009-03-04 17:02:56 UTC (rev 7848)
@@ -62,15 +62,18 @@
public void start() throws CacheLoaderException {
super.start();
String connectionFactoryClass = config.getConnectionFactoryConfig().getConnectionFactoryClass();
- this.connectionFactory = ConnectionFactory.getConnectionFactory(connectionFactoryClass);
- connectionFactory.start(config.getConnectionFactoryConfig());
- tableManipulation = config.getTableManipulation();
- tableManipulation.start(connectionFactory);
+ if (config.isManageConnectionFatory()) {
+ ConnectionFactory factory = ConnectionFactory.getConnectionFactory(connectionFactoryClass);
+ factory.start(config.getConnectionFactoryConfig());
+ doConnectionFactoryInitialization(factory);
+ }
}
public void stop() throws CacheLoaderException {
tableManipulation.stop();
- connectionFactory.stop();
+ if (config.isManageConnectionFatory()) {
+ connectionFactory.stop();
+ }
}
protected void insertBucket(Bucket bucket) throws CacheLoaderException {
@@ -404,4 +407,20 @@
log.error(message, e);
throw new CacheLoaderException(message, e);
}
+
+ public ConnectionFactory getConnectionFactory() {
+ return connectionFactory;
+ }
+
+ /**
+ * Keeps a reference to the connection factory for further use. Also initializes the {@link
+ * org.horizon.loader.jdbc.TableManipulation} that needs connections. This method should be called when you don't
+ * want the store to manage the connection factory, perhaps because it is using an shared connection factory: see
+ * {@link org.horizon.loader.jdbc.JdbcCacheStore} for such an example of this.
+ */
+ public void doConnectionFactoryInitialization(ConnectionFactory connectionFactory) throws CacheLoaderException {
+ this.connectionFactory = connectionFactory;
+ tableManipulation = config.getTableManipulation();
+ tableManipulation.start(connectionFactory);
+ }
}
Modified: core/branches/flat/src/main/java/org/horizon/loader/jdbc/binary/JdbcBinaryCacheStoreConfig.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/jdbc/binary/JdbcBinaryCacheStoreConfig.java 2009-03-04 13:32:42 UTC (rev 7847)
+++ core/branches/flat/src/main/java/org/horizon/loader/jdbc/binary/JdbcBinaryCacheStoreConfig.java 2009-03-04 17:02:56 UTC (rev 7848)
@@ -13,8 +13,14 @@
private ConnectionFactoryConfig connectionFactoryConfig = new ConnectionFactoryConfig();
private TableManipulation tableManipulation = new TableManipulation();
+ private boolean createConnectionFatory = true;
+ public JdbcBinaryCacheStoreConfig(boolean createConnectionFatory) {
+ this.createConnectionFatory = createConnectionFatory;
+ }
+
public JdbcBinaryCacheStoreConfig(ConnectionFactoryConfig connectionFactoryConfig, TableManipulation tm) {
+ this();
this.connectionFactoryConfig = connectionFactoryConfig;
this.tableManipulation = tm;
}
@@ -23,6 +29,10 @@
className = JdbcBinaryCacheStore.class.getName();
}
+ boolean isManageConnectionFatory() {
+ return createConnectionFatory;
+ }
+
/**
* If true, and the table is missing it will be created when starting the cache store. Default to <tt>true</tt>.
*/
@@ -131,4 +141,8 @@
public TableManipulation getTableManipulation() {
return tableManipulation;
}
+
+ public void setTableManipulation(TableManipulation tableManipulation) {
+ this.tableManipulation = tableManipulation;
+ }
}
Modified: core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStore.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStore.java 2009-03-04 13:32:42 UTC (rev 7847)
+++ core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStore.java 2009-03-04 17:02:56 UTC (rev 7848)
@@ -60,17 +60,20 @@
@Override
public void start() throws CacheLoaderException {
super.start();
- String connectionFactoryClass = config.getConnectionFactoryConfig().getConnectionFactoryClass();
- connectionFactory = ConnectionFactory.getConnectionFactory(connectionFactoryClass);
- connectionFactory.start(config.getConnectionFactoryConfig());
- this.tableManipulation = config.getTableManipulation();
- tableManipulation.start(connectionFactory);
+ if (config.isManageConnectionFactory()) {
+ String connectionFactoryClass = config.getConnectionFactoryConfig().getConnectionFactoryClass();
+ ConnectionFactory connectionFactory = ConnectionFactory.getConnectionFactory(connectionFactoryClass);
+ connectionFactory.start(config.getConnectionFactoryConfig());
+ doConnectionFactoryInitialization(connectionFactory);
+ }
this.key2StringMapper = config.getKey2StringMapper();
}
public void stop() throws CacheLoaderException {
tableManipulation.stop();
- connectionFactory.stop();
+ if (config.isManageConnectionFactory()) {
+ connectionFactory.stop();
+ }
}
protected String getLockFromKey(Object key) throws CacheLoaderException {
@@ -307,4 +310,24 @@
log.error(message, e);
throw new CacheLoaderException(message, e);
}
+
+ public boolean supportsKey(Object obj) {
+ return key2StringMapper.isSupportedType(obj);
+ }
+
+ /**
+ * Keeps a reference to the connection factory for further use. Also initializes the {@link
+ * org.horizon.loader.jdbc.TableManipulation} that needs connections. This method should be called when you don't
+ * want the store to manage the connection factory, perhaps because it is using an shared connection factory: see
+ * {@link org.horizon.loader.jdbc.JdbcCacheStore} for such an example of this.
+ */
+ public void doConnectionFactoryInitialization(ConnectionFactory connectionFactory) throws CacheLoaderException {
+ this.connectionFactory = connectionFactory;
+ tableManipulation = config.getTableManipulation();
+ tableManipulation.start(connectionFactory);
+ }
+
+ public Object getConnectionFactory() {
+ return connectionFactory;
+ }
}
Modified: core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreConfig.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreConfig.java 2009-03-04 13:32:42 UTC (rev 7847)
+++ core/branches/flat/src/main/java/org/horizon/loader/jdbc/stringbased/JdbcStringBasedCacheStoreConfig.java 2009-03-04 17:02:56 UTC (rev 7848)
@@ -17,8 +17,10 @@
private ConnectionFactoryConfig connectionFactoryConfig = new ConnectionFactoryConfig();
private TableManipulation tableManipulation = new TableManipulation();
+ private boolean manageConnectionFactory = true;
public JdbcStringBasedCacheStoreConfig(ConnectionFactoryConfig connectionFactoryConfig, TableManipulation tableManipulation) {
+ this();
this.connectionFactoryConfig = connectionFactoryConfig;
this.tableManipulation = tableManipulation;
}
@@ -27,6 +29,11 @@
className = JdbcStringBasedCacheStore.class.getName();
}
+ public JdbcStringBasedCacheStoreConfig(boolean manageConnectionFactory) {
+ this();
+ this.manageConnectionFactory = manageConnectionFactory;
+ }
+
public Key2StringMapper getKey2StringMapper() {
if (key2StringMapper == null) {
try {
@@ -117,8 +124,8 @@
}
/**
- * The name of the driver used for connecting to the database. Mandatory, will be loaded before initiating the
- * first connection.
+ * The name of the driver used for connecting to the database. Mandatory, will be loaded before initiating the first
+ * connection.
*/
public void setDriverClass(String driverClassName) {
this.connectionFactoryConfig.setDriverClass(driverClassName);
@@ -137,4 +144,25 @@
public void setDataColumnType(String dataColumnType) {
this.tableManipulation.setDataColumnType(dataColumnType);
}
+
+ public void setDropTableOnExit(boolean dropTableOnExit) {
+ this.tableManipulation.setDropTableOnExit(dropTableOnExit);
+ }
+
+ public void setCreateTableOnStart(boolean createTableOnStart) {
+ this.tableManipulation.setCreateTableOnStart(createTableOnStart);
+ }
+
+ /**
+ * If this method returns false, then the connection factory should not be created by the {@link
+ * org.horizon.loader.jdbc.stringbased.JdbcStringBasedCacheStore}, but will be injected through {@link
+ * org.horizon.loader.jdbc.stringbased.JdbcStringBasedCacheStore#doConnectionFactoryInitialization(org.horizon.loader.jdbc.connectionfactory.ConnectionFactory)}
+ */
+ public boolean isManageConnectionFactory() {
+ return manageConnectionFactory;
+ }
+
+ public void setTableManipulation(TableManipulation tableManipulation) {
+ this.tableManipulation = tableManipulation;
+ }
}
Modified: core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcBinaryCacheStoreTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcBinaryCacheStoreTest.java 2009-03-04 13:32:42 UTC (rev 7847)
+++ core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcBinaryCacheStoreTest.java 2009-03-04 17:02:56 UTC (rev 7848)
@@ -4,10 +4,11 @@
import org.horizon.loader.CacheStore;
import org.horizon.loader.jdbc.binary.JdbcBinaryCacheStore;
import org.horizon.loader.jdbc.binary.JdbcBinaryCacheStoreConfig;
+import org.horizon.loader.jdbc.connectionfactory.ConnectionFactory;
import org.horizon.loader.jdbc.connectionfactory.ConnectionFactoryConfig;
import org.horizon.marshall.ObjectStreamMarshaller;
-import org.horizon.loader.jdbc.UnitTestDatabaseManager;
import org.testng.annotations.Test;
+import static org.easymock.classextension.EasyMock.*;
/**
* Tester class for {@link JdbcBinaryCacheStore}
@@ -24,6 +25,33 @@
JdbcBinaryCacheStore jdbcBucketCacheStore = new JdbcBinaryCacheStore();
jdbcBucketCacheStore.init(config, null, new ObjectStreamMarshaller());
jdbcBucketCacheStore.start();
+ assert jdbcBucketCacheStore.getConnectionFactory() != null;
return jdbcBucketCacheStore;
}
+
+ public void testNotCreateConnectionFactory() throws Exception {
+ JdbcBinaryCacheStore jdbcBucketCacheStore = new JdbcBinaryCacheStore();
+ JdbcBinaryCacheStoreConfig config = new JdbcBinaryCacheStoreConfig(false);
+ config.setCreateTableOnStart(false);
+ jdbcBucketCacheStore.init(config, null, new ObjectStreamMarshaller());
+ jdbcBucketCacheStore.start();
+ assert jdbcBucketCacheStore.getConnectionFactory() == null;
+
+ /* this will make sure that if a method like stop is called on the connection then it will barf an exception */
+ ConnectionFactory connectionFactory = createMock(ConnectionFactory.class);
+ TableManipulation tableManipulation = createMock(TableManipulation.class);
+ config.setTableManipulation(tableManipulation);
+
+ tableManipulation.start(connectionFactory);
+ replay(tableManipulation);
+ jdbcBucketCacheStore.doConnectionFactoryInitialization(connectionFactory);
+ verify(tableManipulation);
+
+ //stop should be called even if this is an externally managed connection
+ reset(tableManipulation, connectionFactory);
+ tableManipulation.stop();
+ replay(tableManipulation, connectionFactory);
+ jdbcBucketCacheStore.stop();
+ verify(tableManipulation, connectionFactory);
+ }
}
Added: core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcCacheStoreTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcCacheStoreTest.java (rev 0)
+++ core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcCacheStoreTest.java 2009-03-04 17:02:56 UTC (rev 7848)
@@ -0,0 +1,33 @@
+package org.horizon.loader.jdbc;
+
+import org.horizon.loader.BaseCacheStoreTest;
+import org.horizon.loader.CacheStore;
+import org.horizon.loader.jdbc.connectionfactory.ConnectionFactoryConfig;
+import org.testng.annotations.Test;
+
+/**
+ * // TODO: Mircea: Document this!
+ *
+ * @author
+ */
+@Test(groups = "functional", testName = "loader.jdbc.JdbcCacheStoreTest")
+public class JdbcCacheStoreTest extends BaseCacheStoreTest {
+
+ protected CacheStore createCacheStore() throws Exception {
+ JdbcCacheStoreConfig jdbcCacheStoreConfig = new JdbcCacheStoreConfig();
+ TableManipulation stringsTm = UnitTestDatabaseManager.buildDefaultTableManipulation();
+ stringsTm.setTableName("STRINGS_TABLE");
+ TableManipulation binaryTm = UnitTestDatabaseManager.buildDefaultTableManipulation();
+ binaryTm.setTableName("BINARY_TABLE");
+
+ ConnectionFactoryConfig cfc = UnitTestDatabaseManager.getUniqueConnectionFactoryConfig();
+ jdbcCacheStoreConfig.setConnectionFactoryConfig(cfc);
+ jdbcCacheStoreConfig.setStringsTableManipulation(stringsTm);
+ jdbcCacheStoreConfig.setBinaryTableManipulation(binaryTm);
+
+ JdbcCacheStore cacheStore = new JdbcCacheStore();
+ cacheStore.init(jdbcCacheStoreConfig, null, getMarshaller());
+ cacheStore.start();
+ return cacheStore;
+ }
+}
Property changes on: core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcCacheStoreTest.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified: core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcStringBasedCacheStoreTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcStringBasedCacheStoreTest.java 2009-03-04 13:32:42 UTC (rev 7847)
+++ core/branches/flat/src/test/java/org/horizon/loader/jdbc/JdbcStringBasedCacheStoreTest.java 2009-03-04 17:02:56 UTC (rev 7848)
@@ -2,12 +2,15 @@
import org.horizon.loader.BaseCacheStoreTest;
import org.horizon.loader.CacheStore;
+import org.horizon.loader.jdbc.connectionfactory.ConnectionFactory;
import org.horizon.loader.jdbc.connectionfactory.ConnectionFactoryConfig;
import org.horizon.loader.jdbc.stringbased.JdbcStringBasedCacheStore;
import org.horizon.loader.jdbc.stringbased.JdbcStringBasedCacheStoreConfig;
import org.horizon.marshall.ObjectStreamMarshaller;
-import org.horizon.loader.jdbc.UnitTestDatabaseManager;
import org.testng.annotations.Test;
+import static org.easymock.classextension.EasyMock.*;
+import static org.easymock.classextension.EasyMock.replay;
+import static org.easymock.classextension.EasyMock.verify;
/**
* Tester class for {@link org.horizon.loader.jdbc.stringbased.JdbcStringBasedCacheStore}.
@@ -26,4 +29,30 @@
jdbcBucketCacheStore.start();
return jdbcBucketCacheStore;
}
+
+ public void testNotCreateConnectionFactory() throws Exception {
+ JdbcStringBasedCacheStore stringBasedCacheStore = new JdbcStringBasedCacheStore();
+ JdbcStringBasedCacheStoreConfig config = new JdbcStringBasedCacheStoreConfig(false);
+ config.setCreateTableOnStart(false);
+ stringBasedCacheStore.init(config, null, new ObjectStreamMarshaller());
+ stringBasedCacheStore.start();
+ assert stringBasedCacheStore.getConnectionFactory() == null;
+
+ /* this will make sure that if a method like stop is called on the connection then it will barf an exception */
+ ConnectionFactory connectionFactory = createMock(ConnectionFactory.class);
+ TableManipulation tableManipulation = createMock(TableManipulation.class);
+ config.setTableManipulation(tableManipulation);
+
+ tableManipulation.start(connectionFactory);
+ replay(tableManipulation);
+ stringBasedCacheStore.doConnectionFactoryInitialization(connectionFactory);
+ verify(tableManipulation);
+
+ //stop should be called even if this is an external
+ reset(tableManipulation, connectionFactory);
+ tableManipulation.stop();
+ replay(tableManipulation, connectionFactory);
+ stringBasedCacheStore.stop();
+ verify(tableManipulation, connectionFactory);
+ }
}
15 years, 10 months
JBoss Cache SVN: r7847 - core/branches/flat/src/test/java/org/horizon/test/testng.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2009-03-04 08:32:42 -0500 (Wed, 04 Mar 2009)
New Revision: 7847
Modified:
core/branches/flat/src/test/java/org/horizon/test/testng/UnitTestTestNGListener.java
Log:
Improved listener
Modified: core/branches/flat/src/test/java/org/horizon/test/testng/UnitTestTestNGListener.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/test/testng/UnitTestTestNGListener.java 2009-03-04 13:28:08 UTC (rev 7846)
+++ core/branches/flat/src/test/java/org/horizon/test/testng/UnitTestTestNGListener.java 2009-03-04 13:32:42 UTC (rev 7847)
@@ -7,6 +7,8 @@
import org.testng.ITestListener;
import org.testng.ITestResult;
+import java.util.concurrent.atomic.AtomicInteger;
+
/**
* @author dpospisi(a)redhat.com
* @author Mircea.Markus(a)jboss.com
@@ -19,9 +21,9 @@
private ThreadLocal<IClass> threadTestClass = new ThreadLocal<IClass>();
Log log = LogFactory.getLog(UnitTestTestNGListener.class);
- private int failed = 0;
- private int succeded = 0;
- private int skipped = 0;
+ private AtomicInteger failed = new AtomicInteger(0);
+ private AtomicInteger succeded = new AtomicInteger(0);
+ private AtomicInteger skipped = new AtomicInteger(0);
public void onTestStart(ITestResult res) {
log.info("Starting test " + getTestDesc(res));
@@ -29,16 +31,16 @@
}
synchronized public void onTestSuccess(ITestResult arg0) {
- System.out.println(getThreadId() + " Test " + getTestDesc(arg0) + " succeded.");
- log.info("Test succeded " + getTestDesc(arg0) + ".");
- succeded++;
+ System.out.println(getThreadId() + " Test " + getTestDesc(arg0) + " succeeded.");
+ log.info("Test succeeded " + getTestDesc(arg0) + ".");
+ succeded.incrementAndGet();
printStatus();
}
synchronized public void onTestFailure(ITestResult arg0) {
System.out.println(getThreadId() + " Test " + getTestDesc(arg0) + " failed.");
if (arg0.getThrowable() != null) log.error("Test failed " + getTestDesc(arg0), arg0.getThrowable());
- failed++;
+ failed.incrementAndGet();
printStatus();
}
@@ -46,7 +48,7 @@
System.out.println(getThreadId() + " Test " + getTestDesc(arg0) + " skipped.");
log.info(" Test " + getTestDesc(arg0) + " skipped.");
if (arg0.getThrowable() != null) log.error("Test skipped : " + arg0.getThrowable(), arg0.getThrowable());
- skipped++;
+ skipped.incrementAndGet();
printStatus();
}
@@ -68,6 +70,6 @@
}
private void printStatus() {
- System.out.println("Testsuite execution progress: tests succeded " + succeded + ", failed " + failed + ", skipped " + skipped + ".");
+ System.out.println("Test suite progress: tests succeeded: " + succeded.get() + ", failed: " + failed.get() + ", skipped: " + skipped.get() + ".");
}
}
15 years, 10 months
JBoss Cache SVN: r7846 - core/branches/flat/src/test/java/org/horizon/statetransfer.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2009-03-04 08:28:08 -0500 (Wed, 04 Mar 2009)
New Revision: 7846
Modified:
core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferCacheLoaderFunctionalTest.java
core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferFunctionalTest.java
Log:
Updated tests
Modified: core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferCacheLoaderFunctionalTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferCacheLoaderFunctionalTest.java 2009-03-04 13:22:46 UTC (rev 7845)
+++ core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferCacheLoaderFunctionalTest.java 2009-03-04 13:28:08 UTC (rev 7846)
@@ -19,6 +19,10 @@
}
};
+ public StateTransferCacheLoaderFunctionalTest() {
+ cacheName = "nbst-with-loader";
+ }
+
@Override
protected CacheManager createCacheManager() {
// increment the DIMCS store id
Modified: core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferFunctionalTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferFunctionalTest.java 2009-03-04 13:22:46 UTC (rev 7845)
+++ core/branches/flat/src/test/java/org/horizon/statetransfer/StateTransferFunctionalTest.java 2009-03-04 13:28:08 UTC (rev 7846)
@@ -34,7 +34,7 @@
public static final Integer FORTY = 40;
Configuration config;
- protected static final String cacheName = "nbst";
+ protected static String cacheName = "nbst";
private volatile int testCount = 0;
@@ -198,32 +198,32 @@
public void testSTWithThirdWritingNonTxCache() throws Exception {
testCount++;
log.info("testSTWithThirdWritingNonTxCache start - " + testCount);
- thirdWritingCacheTest(false, "nbst1");
+ thirdWritingCacheTest(false);
log.info("testSTWithThirdWritingNonTxCache end - " + testCount);
}
public void testSTWithThirdWritingTxCache() throws Exception {
testCount++;
log.info("testSTWithThirdWritingTxCache start - " + testCount);
- thirdWritingCacheTest(true, "nbst2");
+ thirdWritingCacheTest(true);
log.info("testSTWithThirdWritingTxCache end - " + testCount);
}
public void testSTWithWritingNonTxThread() throws Exception {
testCount++;
log.info("testSTWithWritingNonTxThread start - " + testCount);
- writingThreadTest(false, "nbst3");
+ writingThreadTest(false);
log.info("testSTWithWritingNonTxThread end - " + testCount);
}
public void testSTWithWritingTxThread() throws Exception {
testCount++;
log.info("testSTWithWritingTxThread start - " + testCount);
- writingThreadTest(true, "nbst4");
+ writingThreadTest(true);
log.info("testSTWithWritingTxThread end - " + testCount);
}
- private void thirdWritingCacheTest(boolean tx, String name) throws InterruptedException {
+ private void thirdWritingCacheTest(boolean tx) throws InterruptedException {
Cache<Object, Object> cache1, cache2, cache3;
cache1 = createCacheManager().getCache(cacheName);
cache3 = createCacheManager().getCache(cacheName);
@@ -266,8 +266,8 @@
c.put(A_C_AGE, FORTY);
}
- private void writingThreadTest(boolean tx, String name) throws InterruptedException {
- Cache<Object, Object> cache1 = null, cache2 = null;
+ private void writingThreadTest(boolean tx) throws InterruptedException {
+ Cache<Object, Object> cache1, cache2;
cache1 = createCacheManager().getCache(cacheName);
writeInitialData(cache1);
15 years, 10 months