[jboss-svn-commits] JBL Code SVN: r5742 - in labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb: listeners util

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Aug 11 06:57:55 EDT 2006


Author: tfennelly
Date: 2006-08-11 06:57:51 -0400 (Fri, 11 Aug 2006)
New Revision: 5742

Added:
   labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/TestCaseUtils.java
Modified:
   labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/DirectoryPollerTest.java
   labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/SQLTablePollerTest.java
   labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/FileUtils.java
Log:
More tests and utilities


Modified: labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/DirectoryPollerTest.java
===================================================================
--- labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/DirectoryPollerTest.java	2006-08-11 10:21:38 UTC (rev 5741)
+++ labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/DirectoryPollerTest.java	2006-08-11 10:57:51 UTC (rev 5742)
@@ -72,8 +72,8 @@
 
 	protected void tearDown() throws Exception {
 		// Remove the directories required by the test...
-		inputDir.delete();
-		inputDoneDir.delete();
-		copiedTo.delete();
+		FileUtils.assertCanDelete(inputDir, 10000);
+		FileUtils.assertCanDelete(inputDoneDir, 10000);
+		FileUtils.assertCanDelete(copiedTo, 10000);
 	}
 }

Modified: labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/SQLTablePollerTest.java
===================================================================
--- labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/SQLTablePollerTest.java	2006-08-11 10:21:38 UTC (rev 5741)
+++ labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/SQLTablePollerTest.java	2006-08-11 10:57:51 UTC (rev 5742)
@@ -61,6 +61,6 @@
 
 	protected void tearDown() throws Exception {
 		// Remove the directories required by the test...
-		notifyDir.delete();
+		FileUtils.assertCanDelete(notifyDir, 10000);
 	}
 }

Modified: labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/FileUtils.java
===================================================================
--- labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/FileUtils.java	2006-08-11 10:21:38 UTC (rev 5741)
+++ labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/FileUtils.java	2006-08-11 10:57:51 UTC (rev 5742)
@@ -87,9 +87,7 @@
 					fileStream.flush();
 					fileStream.close();
 				} catch (IOException e) {
-					String errorMsg = "Error flushing/closing file: " + file.getAbsolutePath();
-					logger.error(errorMsg, e);
-					TestCase.fail(errorMsg);
+					TestCaseUtils.logAndFail("Error flushing/closing file: " + file.getAbsolutePath(), logger, null);
 				}
 			}
 		}		
@@ -113,9 +111,7 @@
 				logger.error("Thread interupt...", e);
 			}
 		}
-		String errorMsg = "File [" + file.getAbsolutePath() + "] doesn't exist.  Waited for " + maxWait + "ms.";
-		logger.error(errorMsg);
-		TestCase.fail(errorMsg);
+		TestCaseUtils.logAndFail("File [" + file.getAbsolutePath() + "] doesn't exist.  Waited for " + maxWait + "ms.", logger, null);
 	}
 
 	/**
@@ -149,9 +145,8 @@
 				logger.error("Thread interupt...", e);
 			}
 		}
-		String errorMsg = "Files matching the pattern [" + pattern + "] not found in directory [" + dir.getAbsolutePath() + "].  Waited for " + maxWait + "ms.";
-		logger.error(errorMsg);
-		TestCase.fail(errorMsg);
+		TestCaseUtils.logAndFail("Files matching the pattern [" + pattern + "] not found in directory [" 
+				+ dir.getAbsolutePath() + "].  Waited for " + maxWait + "ms.", logger, null);
 	}
 
 	/**
@@ -172,8 +167,36 @@
 				logger.error("Thread interupt...", e);
 			}
 		}
-		String errorMsg = "File [" + file.getAbsolutePath() + "] exists.  Waited for " + maxWait + "ms.";
-		logger.error(errorMsg);
-		TestCase.fail(errorMsg);
+		TestCaseUtils.logAndFail("File [" + file.getAbsolutePath() + "] exists.  Waited for " + maxWait + "ms.", logger, null);
 	}
+
+	/**
+	 * Assert that the file/dir can be deleted.
+	 * <p/>
+	 * Just deletes the file and makes sure the delete worked.  This ensures that nothing has an open
+	 * handle on the file i.e. that code didn't "forget" to close it.  This will work for directories
+	 * too because if there's a handle open on a file in the directory, the delete of the directory 
+	 * will fail.
+	 * @param file The file to be deleted.
+	 */
+	public static void assertCanDelete(File file, long maxWait) {
+		if(!file.exists()) {
+			TestCaseUtils.logAndFail("File [" + file.getAbsolutePath() + "] can't be deleted - it doesn't exists.", logger, null);
+		}
+		file.delete();
+		long endTime = System.currentTimeMillis() + maxWait;
+		
+		while(System.currentTimeMillis() < endTime) {
+			file.delete();
+			if(!file.exists()) {
+				return;
+			}
+			try {
+				Thread.sleep(100);
+			} catch (InterruptedException e) {
+				logger.error("Thread interupt...", e);
+			}
+		}
+		TestCaseUtils.logAndFail("File [" + file.getAbsolutePath() + "] can't be deleted - a handle on it (or one of its children) may be open.  Waited " + maxWait + "ms", logger, null);
+	}
 }

Added: labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/TestCaseUtils.java
===================================================================
--- labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/TestCaseUtils.java	2006-08-11 10:21:38 UTC (rev 5741)
+++ labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/TestCaseUtils.java	2006-08-11 10:57:51 UTC (rev 5742)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.util;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.Logger;
+
+/**
+ * TestCase utility methods.
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public abstract class TestCaseUtils {
+
+
+	/**
+	 * Log a message and fail the test with the same message.
+	 * @param message The message.
+	 * @param logger The Logger instance to use.
+	 * @param cause The cause. can be null.
+	 */
+	public static void logAndFail(String message, Logger logger, Throwable cause) {
+		logger.error(message, cause);
+		TestCase.fail(message);
+	}
+}




More information about the jboss-svn-commits mailing list