
/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.test.ha.farm.test;

import java.io.File;
import java.io.FileWriter;
import java.io.Reader;
import java.io.FileReader;

import junit.framework.TestCase;

/**
 *
 * @author  Scott Marlow <smarlow@novell.com>
 *
 */

public class ClusterFileTransferTestCase extends TestCase
{
   // SERVERHOMESTR property is defined in the testsuite/build.xml
   private static String SERVERHOMESTR = System.getProperty( "jboss.internal-server.serverroot");

   private static File SERVERHOME = new File(SERVERHOMESTR);
   private static final File NODE1SERVERFOLDER = new File(SERVERHOME, "node0");
   private static final File NODE1FARMFOLDER = new File(NODE1SERVERFOLDER, "farm");
   private static final File NODE2SERVERFOLDER = new File(SERVERHOME, "node1");
   private static final File NODE2FARMFOLDER = new File(NODE2SERVERFOLDER, "farm");
   private static final String FILEPATTERN1 = "1234567890";

   public ClusterFileTransferTestCase(String testCaseName)
   {
      super(testCaseName);
   }

   public void setUp()
   {

   }

   public void tearDown()
   {
   }

   public void testFarmDeploy() throws Exception
   {
      File node2File = new File(NODE2FARMFOLDER,"farmtestfile.jar");
      node2File.delete();              // ensure target of farm deploy doesn't exist

      File node1File = new File(NODE1FARMFOLDER,"farmtestfile.jar");
      // generate test file to be farm deployed (note the file cannot be deployed but should be propagated across farm)
      generateTestArchive(node1File, FILEPATTERN1);

      int retry = 15;
      while (!node2File.exists() && (retry-- > 0)  ) {
         Thread.sleep(1000);
      }
      assertTrue("farm deployment test file "+node1File +" did not propagate to " + node2File,node2File.exists());
      verifyFilesAreEqual(node1File, node2File);
   }

   public void generateTestArchive(File outputFile, String pattern) throws Exception
   {
      outputFile.delete();
      outputFile.createNewFile();
      FileWriter writer = new FileWriter(outputFile);
      for (int looper = 0; looper < 1024; looper++ )
         writer.write(pattern);
      writer.close();
   }


   public void verifyFilesAreEqual(File file1, File file2) throws Exception
   {
      assertTrue("farm deployment test file1 length ("+file1.length()+") not equal to file2 length("+file2.length()+")", file1.length() == file2.length());
      Reader reader1 = new FileReader(file1);
      Reader reader2 = new FileReader(file2);

      char buf1[] = new char[2024];
      char buf2[] = new char[2024];
      int  buflen1, buflen2;
      while((buflen1 = reader1.read(buf1)) > -1)
      {
         buflen2 = reader2.read(buf2);
         assertTrue("farm deployment test file1 charsRead ("+buflen1+") not equal to file2 charsRead ("+buflen2+")",buflen2 == buflen1);
         assertTrue( "farm deployment test buf1 ("+String.valueOf(buf1)+") not equal to buf2 ("+String.valueOf(buf2)+")",String.valueOf(buf2).equals(String.valueOf(buf1)));
      }

   }

}
