/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache.loader;

import junit.framework.Test;
import junit.framework.TestSuite;
import org.jboss.cache.Fqn;

import java.util.Properties;

/**
 * This test runs cache loader tests using Database as the cache loader store. 
 * The default test is configured using Derby embedded framework. 
 * The server and database configuration is read from a properties file located at 
 * /etc/cache-jdbc.properties.
 * 
 * To run this test with any other RDBMS, The appropriate JDBC driver 
 * (i.e mysql-connector-java-3.0.10-stable-bin.jar) 
 * must be in the lib directory.
 * 
 * @author <a href="hmesha@novell.com">Hany Mesha</a>
 * @version <tt>$Revision: 1093 $</tt>
 */
public class JDBCCacheLoaderTest
   extends CacheLoaderTestsBase
{
   protected void configureCache() throws Exception
   {
      Properties prop = new Properties();
      try{
         prop.load(this.getClass().getClassLoader().getResourceAsStream("cache-jdbc.properties"));
      }catch(Exception e){
         System.out.println("Error loading jdbc properties ");
      }
      String props = "cache.jdbc.driver =" + prop.getProperty("cache.jdbc.driver") + "\n" +
                     "cache.jdbc.url=" + prop.getProperty("cache.jdbc.url") +"\n" +
                     "cache.jdbc.user=" + prop.getProperty("cache.jdbc.user") + "\n" +
                     "cache.jdbc.password=" + prop.getProperty("cache.jdbc.password") + "\n" +
                     "cache.jdbc.node.type=" + prop.getProperty("cache.jdbc.node.type");
      cache.setCacheLoaderConfiguration( getSingleCacheLoaderConfig("", "org.jboss.cache.loader.JDBCCacheLoader", props, false, true, false) );
   }
   
   public void testLargeObject()
   {
      try {
    	 String key = "LargeObj";
         // create an object with size bigger than 4k (k=1024 bytes)
   	     StringBuffer text = new StringBuffer("LargeObject");
   	     while (text.toString().getBytes().length < (1024*100)) {
   		    text.append(text);
   	     }
   	     String initialValue = text.toString();
         // insert it into the cache loader
   	     loader.remove(Fqn.fromString("/"));
   	     
   	     Object retVal = loader.put(FQN, key, initialValue);
   	     assertNull(retVal);
   	     addDelay();
         // load the object from the cache loader and validate it
   	     assertEquals(initialValue, (String) loader.get(FQN).get(key));
         // update the object and validate it
   	     String updatedValue = initialValue.concat(("UpdatedValue"));
   	     retVal = loader.put(FQN, key, updatedValue);
   	     assertEquals(initialValue, (String) retVal);
   	     assertEquals(updatedValue, (String) loader.get(FQN).get(key));
	  } catch(Exception e) {
         fail(e.toString());
	  }	   
   }
       

   public static Test suite()
   {
      return new TestSuite(JDBCCacheLoaderTest.class);
   }
   
   public static void main(String[] args)
   {
       junit.textui.TestRunner.run(suite());
   }
}
