[jboss-svn-commits] JBL Code SVN: r8134 - in labs/jbossesb/trunk/product/core/rosetta: src/org/jboss/internal/soa/esb/couriers src/org/jboss/soa/esb/couriers tests/src/org/jboss/internal/soa/esb/couriers/tests

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Dec 8 05:01:52 EST 2006


Author: estebanschifman
Date: 2006-12-08 05:01:44 -0500 (Fri, 08 Dec 2006)
New Revision: 8134

Modified:
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/FileCourier.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/couriers/CourierUtil.java
   labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/FileCourierUnitTest.java
Log:
More unit tests for FileCourier

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/FileCourier.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/FileCourier.java	2006-12-08 09:23:07 UTC (rev 8133)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/FileCourier.java	2006-12-08 10:01:44 UTC (rev 8134)
@@ -38,7 +38,15 @@
 import org.jboss.soa.esb.couriers.CourierException;
 import org.jboss.soa.esb.couriers.CourierUtil;
 import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.util.Util;
 
+/**
+ * Internal implementation of a courier to handle Messages in a filesystem 
+ * <p/> Intended to cater for local file system, remote via ftp, ftps or sftp
+ * @author <a href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a>
+ * @since Version 4.0
+ *
+ */
 public class FileCourier implements PickUpOnlyCourier, DeliverOnlyCourier 
 {
 	/**
@@ -72,6 +80,16 @@
 	 */
 	protected void checkEprParms() throws CourierException
 	{
+		_outputSuffix = null;
+		try { _outputSuffix = _epr.getPostSuffix(); }
+		catch (URISyntaxException e) { _logger.warn("Malformed EPR"); }
+		
+		if (Util.isNullString(_outputSuffix))
+		{
+			_outputSuffix = ".esbMessage";
+			_logger.debug("No suffix for delivered messages - using default of <"+_outputSuffix+">");
+		}
+		
 		// Certain things can only be checked in local filesystem
 		try
 		{
@@ -123,6 +141,7 @@
 		{
 			File tmpFile = CourierUtil.messageToLocalFile(dir, message);
 			String name	 = message.getHeader().getCall().getMessageID().toString();
+			name += _outputSuffix;
 			handler.renameFile(tmpFile, new File(dir,name));
 			return true;
 		}
@@ -144,17 +163,19 @@
 			if (null!=files && files.length>0)
 			{
 				File input 	= files[0];
-				File work	= new File(input.getParent(),input.getName()+".esbInProcess");
+				File work	= workFile(input);
 				if (handler.renameFile(input, work))
 					try { result = CourierUtil.messageFromLocalFile(work); }
 					catch (Exception e) 
 					{
-						File error = new File(input.getParent(),input.getName()+".esbError");
-						handler.renameFile(work, error);
+						handler.renameFile(work, errorFile(input));
 						continue;
 					}
-					File done	= new File(input.getParent(),input.getName()+".esbDone");
-					handler.renameFile(work, done);
+					File done = postFile(input);
+					if (null==done)
+						handler.deleteFile(work);
+					else
+						handler.renameFile(work, done);
 					return result;
 			}
 			try { Thread.sleep(200); }
@@ -164,10 +185,80 @@
 		return null;
     } //________________________________
 	
+	protected File workFile(File input)
+	{
+		String sfx = null;
+		try { sfx = _epr.getWorkSuffix(); }
+		catch (URISyntaxException e) 
+			{ _logger.warn("Malformed EPR",e); }
+
+		if (Util.isNullString(sfx))
+		{
+			sfx = ".esbInProcess";
+			_logger.debug("No valid work suffix found in EPR - using default of "+sfx);
+		}
+		return new File(input.getParent(),input.getName()+sfx);
+    } //________________________________
+	
+	protected File errorFile(File input)
+	{
+		String sfx = null;
+//		try { sfx = _epr.getErrorSuffix(); }
+//		catch (URISyntaxException e) 
+//			{ _logger.warn("Malformed EPR",e); }
+
+		if (Util.isNullString(sfx))
+		{
+			sfx = ".esbERROR";
+			_logger.debug("No valid work suffix found in EPR - using default of "+sfx);
+		}
+		return new File(input.getParent(),input.getName()+sfx);
+    } //________________________________
+	
+	protected File postFile(File input)
+	{
+		try 
+		{
+			if (_epr.getPostDelete())
+				return null;
+		}
+		catch (Exception e) 
+			{
+				_logger.warn("Problems in FileEpr",e);
+			}
+
+		String inputDir = input.getParent();
+		String dir = null;
+		try { dir = _epr.getPostDirectory(); }
+		catch (URISyntaxException e) 
+			{ _logger.warn("Malformed EPR",e); }
+		if (Util.isNullString(dir))
+		{
+			dir = inputDir;
+			_logger.debug("No valid post process directory found in EPR - using same as input ("+dir+")");
+		}
+
+		String sfx = null;
+		try { sfx = _epr.getPostSuffix(); }
+		catch (URISyntaxException e) 
+			{ _logger.warn("Malformed EPR",e); }
+		if (Util.isNullString(sfx))
+		{
+			if (dir.equals(inputDir))
+			{
+				sfx = ".esbProcessed";
+				_logger.debug("No valid post suffix found in EPR - using default of "+sfx);
+			}
+		}
+
+		return new File(dir,input.getName()+sfx);
+    } //________________________________
+	
+	protected String			_outputSuffix;
 	protected URL				_url;
 	protected boolean			_receiverOnly;	  
     protected FileEpr			_epr;
     protected LocalFileHandler	_localFhandler;
-    protected Logger			_logger = Logger.getLogger(FileCourier.class);
+    protected static Logger		_logger = Logger.getLogger(FileCourier.class);
 
 } //____________________________________________________________________________

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/couriers/CourierUtil.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/couriers/CourierUtil.java	2006-12-08 09:23:07 UTC (rev 8133)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/couriers/CourierUtil.java	2006-12-08 10:01:44 UTC (rev 8134)
@@ -117,7 +117,7 @@
 	public static File messageToLocalFile(File directory, Message message)
 		throws IOException, ParserConfigurationException
 	{
-		File tmpFile = File.createTempFile("EsbFileCourier_", ".part",directory);
+		File tmpFile = File.createTempFile("EsbFileCourier_", ".__esbPart",directory);
 		Serializable serial = Util.serialize(message);
 		ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(tmpFile));
 		writer.writeObject(serial);

Modified: labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/FileCourierUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/FileCourierUnitTest.java	2006-12-08 09:23:07 UTC (rev 8133)
+++ labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/FileCourierUnitTest.java	2006-12-08 10:01:44 UTC (rev 8134)
@@ -24,12 +24,14 @@
 
 import java.io.File;
 import java.io.FileFilter;
+import java.io.PrintStream;
 import java.net.URI;
 import java.util.UUID;
 
 import junit.framework.Assert;
 import junit.framework.JUnit4TestAdapter;
 
+import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.jboss.soa.esb.addressing.Call;
 import org.jboss.soa.esb.addressing.eprs.FileEpr;
@@ -42,6 +44,12 @@
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+/**
+ * Tests for internal FileCourier class 
+ * @author <a href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a>
+ * @since Version 4.0
+ *
+ */
 public class FileCourierUnitTest 
 {
 	private static Logger _logger		= Logger.getLogger(FileCourierUnitTest.class);
@@ -50,57 +58,77 @@
 	private static final String TMP_DIR = System.getProperty("java.io.tmpdir",DEFAULT_TEMP);
 	
 	static File _tmpDir = new File(TMP_DIR);
-	static URI _uid;
-	static File _theFile;
+	static String TEST_SUFFIX	= ".testMessage";
+	static String ERROR_SUFFIX	= ".esbERROR";  // This is a FIXED value - The FileEpr has no placeholder for it
+	static String WORK_SUFFIX	= ".inProcessMessage";
+	static String DONE_SUFFIX	= ".doneMessage";
 	
 	@BeforeClass
 	public static void setUp() throws Exception
 	{
-//		_logger.setLevel(Level.DEBUG);
+		_logger.setLevel(Level.DEBUG);
 		_logger.debug("tmp directory = <"+_tmpDir+">");
-		_uid = new URI(UUID.randomUUID().toString());
-		_theFile = new File(_tmpDir,_uid.toString());
-		_theFile.delete();
+		purgeStaleFiles();
 	}
 	
 	@AfterClass
 	public static void tearDown() throws Exception
 	{
+		purgeStaleFiles();
+	}
+
+	public static junit.framework.Test suite() {
+		return new JUnit4TestAdapter(FileCourierUnitTest.class);
+	}
+	
+	private static void purgeStaleFiles()
+	{
 		FileFilter ff = new FileFilter() 
 		{
 				public boolean accept(File file)
 				{
-					return (file.getName().startsWith(_uid.toString()));
+					return 
+						(  file.getName().endsWith(TEST_SUFFIX)
+						 ||file.getName().endsWith(WORK_SUFFIX)
+						 ||file.getName().endsWith(DONE_SUFFIX)
+						 ||file.getName().endsWith(ERROR_SUFFIX)
+						);
 				}
 			};
 		for (File file:_tmpDir.listFiles(ff))
 		{
 			_logger.debug("delete of "+file.toString()+" = "+file.delete());
 		}
+		
 	}
 
-	public static junit.framework.Test suite() {
-		return new JUnit4TestAdapter(FileCourierUnitTest.class);
-	}
-
 	@Test
 	public void testStoreRetrieve() throws Exception 
     {
-		String contents = "This is the text that travels all around";
+		String contents = "This is the text that travels in the Message body";
+
+		// toEpr for files must be a directory
 		FileEpr toEpr = new FileEpr("file://"+TMP_DIR);
+		// FileEpr uses the postSuffix as the extension for messages delivered (default = ".esbMessage")
+		toEpr.setPostSuffix(TEST_SUFFIX);
+		Assert.assertEquals(toEpr.getPostSuffix(), TEST_SUFFIX);
+		
 		Message msg = MessageFactory.getInstance().getMessage();
 		msg.getBody().setContents(contents.getBytes());
 
 		Call call = new Call(toEpr);
-		call.setMessageID(_uid);
+		String uid = UUID.randomUUID().toString();
+		call.setMessageID(new URI(uid));
 		msg.getHeader().setCall(call);
-
+		
 		CourierUtil.deliverMessage(msg);
-		Assert.assertTrue(_theFile.exists());
-		_logger.info("Message file "+_theFile.toString()+" successfully created");
+		File theFile = new File(_tmpDir,uid+TEST_SUFFIX);
+		Assert.assertTrue(theFile.exists());
+		_logger.info("Message file "+theFile.toString()+" successfully created");
 				
 		FileEpr fromEpr = new FileEpr(toEpr.getURL());
-		fromEpr.setInputSuffix(_uid.toString());
+		fromEpr.setInputSuffix(TEST_SUFFIX);
+		fromEpr.setPostSuffix(DONE_SUFFIX);
 		
 		TwoWayCourier pickUp = CourierFactory.getPickupCourier(fromEpr);
 		Message retrieved = pickUp.pickup(1000);
@@ -109,7 +137,33 @@
 		String back = new String(retrieved.getBody().getContents());
 		Assert.assertEquals(contents,back);
 		_logger.info("Contents of retrieved msg equal original text <"+back+">");
+		purgeStaleFiles();
+    }
+
+	@Test
+	public void testBadMessage() throws Exception 
+    {
+		String uid = UUID.randomUUID().toString();
+		File theFile = new File(_tmpDir,uid+TEST_SUFFIX);
+		PrintStream out = new PrintStream(theFile);
+		out.print("This is an invalid message");
+		out.close();
+		Assert.assertTrue(theFile.exists());
+		_logger.info("Invalid Message file "+theFile.toString()+" successfully created");
+				
+		FileEpr fromEpr = new FileEpr("file://"+TMP_DIR);
+		fromEpr.setInputSuffix(TEST_SUFFIX);
+		fromEpr.setPostSuffix(DONE_SUFFIX);
 		
+		TwoWayCourier pickUp = CourierFactory.getPickupCourier(fromEpr);
+		Message retrieved = pickUp.pickup(1000);
+		Assert.assertTrue("Null message retrieved",null==retrieved);
+		
+		theFile = new File(_tmpDir,uid+TEST_SUFFIX+ERROR_SUFFIX);
+		Assert.assertTrue(theFile.exists());
+		_logger.info("Message file properly renamed to "+theFile.toString());
+		
+		purgeStaleFiles();
     }
 
 }




More information about the jboss-svn-commits mailing list