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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Aug 10 11:44:50 EDT 2006


Author: tfennelly
Date: 2006-08-10 11:44:44 -0400 (Thu, 10 Aug 2006)
New Revision: 5707

Added:
   labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/SimpleListenerConfig.xml
   labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/ClassUtils.java
   labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/FileUtils.java
   labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/ListenerUtils.java
Modified:
   labs/jbossesb/trunk/qa/junit/build.xml
   labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/SimpleListenersTest.java
   labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/JMSClientUtil.java
Log:
Added some test support utilities and a simple DirPoller test.

Modified: labs/jbossesb/trunk/qa/junit/build.xml
===================================================================
--- labs/jbossesb/trunk/qa/junit/build.xml	2006-08-10 15:43:01 UTC (rev 5706)
+++ labs/jbossesb/trunk/qa/junit/build.xml	2006-08-10 15:44:44 UTC (rev 5707)
@@ -46,13 +46,16 @@
 			<jvmarg value="-Demma.coverage.out.merge=true" />
 
         	<!-- Listener Sys Props... -->
-        	<sysproperty key="org.jboss.soa.esb.paramsRepository.file.root" value="${qa.junit.src}" />
+        	<sysproperty key="org.jboss.soa.esb.paramsRepository.file.root" value="junit/src" />
 
         	<!-- Sys Props from the test.properties file... -->
         	<sysproperty key="org.jboss.soa.esb.jndi.server.type" value="${org.jboss.soa.esb.jndi.server.type}" />
         	<sysproperty key="org.jboss.soa.esb.jndi.server.url" value="${org.jboss.soa.esb.jndi.server.url}" />
         </junit>
 		
+		<!-- Remove all "fixed-up" listener configuration files -->
+		<delete><fileset dir="${qa.junit.src}" includes="**/*.conftmp" /></delete>
+		
 		<!-- Report on the tests (format results) ... -->
         <junitreport todir="${qa.junit.report}">
             <fileset dir="${qa.junit.report}">

Added: labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/SimpleListenerConfig.xml
===================================================================
--- labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/SimpleListenerConfig.xml	2006-08-10 15:43:01 UTC (rev 5706)
+++ labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/SimpleListenerConfig.xml	2006-08-10 15:44:44 UTC (rev 5707)
@@ -0,0 +1,32 @@
+<FileCopierExample
+	commandConnFactoryClass="ConnectionFactory"
+	commandJndiType="jboss"
+	commandJndiURL="localhost"
+	commandIsTopic="false"
+	messageSelector="gpMsgSelector='test'"
+	commandJndiName="queue/A"
+>
+   <PollDirectoryExample
+   	listenerClass="org.jboss.soa.esb.listeners.DirectoryPoller"
+   	actionClass="org.jboss.soa.esb.actions.FileCopier"
+   	pollLatencySecs="2"
+	maxThreads="2"
+	inputDirURI="@qa.build@/inputDir"
+	inputSuffix=".inp"
+	workSuffix=".COPYING"
+	errorSuffix=".COPYERR"
+	postDirURI="@qa.build@/inputDoneDir"
+	postSuffix=".DONE"
+	postDelete="n"
+   > 
+	<CopyTo 
+		copyToDirURI="@qa.build@/copiedTo" 
+		copyToSuffix=".outFromFirst"
+	/>
+	<CopyTo 
+		copyToDirURI="@qa.build@/copiedTo" 
+		copyToSuffix=".outFromSecond"
+	/>
+   </PollDirectoryExample>
+
+</FileCopierExample>

Modified: labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/SimpleListenersTest.java
===================================================================
--- labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/SimpleListenersTest.java	2006-08-10 15:43:01 UTC (rev 5706)
+++ labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/listeners/SimpleListenersTest.java	2006-08-10 15:44:44 UTC (rev 5707)
@@ -21,7 +21,14 @@
  */
 package org.jboss.soa.esb.listeners;
 
+import java.io.File;
+
+import org.jboss.soa.esb.helpers.KeyValuePair;
+import org.jboss.soa.esb.util.ClassUtils;
+import org.jboss.soa.esb.util.FileUtils;
 import org.jboss.soa.esb.util.JMSClientUtil;
+import org.jboss.soa.esb.util.ListenerUtils;
+import org.jboss.soa.esb.util.ListenerUtils.ListenersManagerExecThread;
 
 import junit.framework.TestCase;
 
@@ -30,8 +37,34 @@
  * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
  */
 public class SimpleListenersTest extends TestCase {
+	
+	private ListenersManagerExecThread listenersMgr;
+	File inputDir, inputDoneDir, copiedTo;
 
-	public void test() {
-		JMSClientUtil.sendMessageToQueue("Hello there", JMSClientUtil.QUEUE.A, null);
+	protected void setUp() throws Exception {
+		// Create the directories required by the test...
+		inputDir = FileUtils.createTestDir("inputDir");
+		inputDoneDir = FileUtils.createTestDir("inputDoneDir");
+		copiedTo = FileUtils.createTestDir("copiedTo");
+		
+		// Start the listener...
+		listenersMgr = ListenerUtils.startListeners(ClassUtils.toResourcePath(getClass().getPackage()) + "/SimpleListenerConfig.xml");
 	}
+
+	public void test() throws InterruptedException {
+		// Write the input file...
+		FileUtils.writeDataToFile(new File(inputDir, "infile1.inp"), "Great goal by Maradona!!".getBytes());
+		// Make sure the input get processed to the "inputDoneDir"
+		FileUtils.assertFileExists(new File(inputDoneDir, "infile1.inp.DONE"), 10000);
+		// Make sure the input gets removed from the "inputDoneDir"...
+		FileUtils.assertFileDoesntExists(new File(inputDir, "infile1.inp"), 10000);
+		
+		// Send a Shutdown message to the command queue...
+		JMSClientUtil.sendMessageToQueue("shutdown", JMSClientUtil.QUEUE.A, new KeyValuePair[] {new KeyValuePair("gpMsgSelector", "test")});
+		listenersMgr.assertShutdownOK(10000);
+	}
+
+	protected void tearDown() throws Exception {
+		// No Teardown tasks for this test :-)
+	}
 }

Added: labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/ClassUtils.java
===================================================================
--- labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/ClassUtils.java	2006-08-10 15:43:01 UTC (rev 5706)
+++ labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/ClassUtils.java	2006-08-10 15:44:44 UTC (rev 5707)
@@ -0,0 +1,41 @@
+/*
+ * 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;
+
+/**
+ * Class utilities.
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public abstract class ClassUtils {
+
+	/**
+	 * Get the resource path representation of the specified packageObj.
+	 * @param packageObj Java class Package.
+	 * @return Class Package resource path.
+	 */
+	public static final String toResourcePath(Package packageObj) {
+		if(packageObj == null) {
+			throw new IllegalArgumentException("null 'packageObj' arg.");
+		}
+		return "/" + packageObj.getName().replace('.', '/');
+	}	
+}

Added: 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-10 15:43:01 UTC (rev 5706)
+++ labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/FileUtils.java	2006-08-10 15:44:44 UTC (rev 5707)
@@ -0,0 +1,141 @@
+/*
+ * 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 java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.listeners.GpListener;
+
+/**
+ * File utilities.
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public abstract class FileUtils {
+	
+	private static Logger logger = Logger.getLogger(FileUtils.class);
+
+	/**
+	 * Get the QA environment "build" directory file.
+	 * @return QA "build" dir file.
+	 */
+	public static File getEnvBuildDir() {
+		return new File("./build");
+	}
+	
+	/**
+	 * Creates a directory inside the build dir.
+	 * <p/>
+	 * If the directory already exists, it gets recreated.
+	 * @param name Directory name.
+	 * @return The newly created.recreated directory.
+	 */
+	public static File createTestDir(String name) {
+		File dir = new File(getEnvBuildDir(), name);
+		
+		if(dir.exists()) {
+			dir.delete();
+		}
+		dir.mkdirs();
+		
+		return dir;
+	}
+	
+	/**
+	 * Write the supplied data to the specified file.
+	 * @param file File to write to.
+	 * @param data Data to be written.
+	 */
+	public static void writeDataToFile(File file, byte[] data) {
+		FileOutputStream fileStream = null;
+		
+		try {
+			fileStream = new FileOutputStream(file);
+			fileStream.write(data);
+		} catch (IOException e) {
+			String errorMsg = "Error writing to file: " + file.getAbsolutePath();
+			logger.error(errorMsg, e);
+			TestCase.fail(errorMsg);
+		} finally {
+			if(fileStream != null) {
+				try {
+					fileStream.flush();
+					fileStream.close();
+				} catch (IOException e) {
+					String errorMsg = "Error flushing/closing file: " + file.getAbsolutePath();
+					logger.error(errorMsg, e);
+					TestCase.fail(errorMsg);
+				}
+			}
+		}		
+	}
+	
+	/**
+	 * Assert that the specified file exists.
+	 * @param file The file in question.
+	 * @param maxWait The maximum length of time (ms) to wait for for the file to appear.
+	 */
+	public static void assertFileExists(File file, long maxWait) {
+		long endTime = System.currentTimeMillis() + maxWait;
+		
+		while(System.currentTimeMillis() < endTime) {
+			if(file.exists()) {
+				return;
+			}
+			try {
+				Thread.sleep(100);
+			} catch (InterruptedException e) {
+				logger.error("Thread interupt...", e);
+			}
+		}
+		String errorMsg = "File [" + file.getAbsolutePath() + "] doesn't exist.  Waited for " + maxWait + "ms.";
+		logger.error(errorMsg);
+		TestCase.fail(errorMsg);
+	}
+
+	/**
+	 * Assert that the specified file doesn't exists.
+	 * @param file The file in question.
+	 * @param maxWait The maximum length of time (ms) to wait for for the file to disappear.
+	 */
+	public static void assertFileDoesntExists(File file, long maxWait) {
+		long endTime = System.currentTimeMillis() + maxWait;
+		
+		while(System.currentTimeMillis() < endTime) {
+			if(!file.exists()) {
+				return;
+			}
+			try {
+				Thread.sleep(100);
+			} catch (InterruptedException e) {
+				logger.error("Thread interupt...", e);
+			}
+		}
+		String errorMsg = "File [" + file.getAbsolutePath() + "] exists.  Waited for " + maxWait + "ms.";
+		logger.error(errorMsg);
+		TestCase.fail(errorMsg);
+	}
+}

Modified: labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/JMSClientUtil.java
===================================================================
--- labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/JMSClientUtil.java	2006-08-10 15:43:01 UTC (rev 5706)
+++ labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/JMSClientUtil.java	2006-08-10 15:44:44 UTC (rev 5707)
@@ -43,7 +43,7 @@
 	 * @param properties The String properties to be set on the message.
 	 * @throws Exception
 	 */
-	public static void sendMessageToQueue(Serializable message, QUEUE queue, List<KeyValuePair> properties) {
+	public static void sendMessageToQueue(Serializable message, QUEUE queue, KeyValuePair[] properties) {
 		JMSSendQueueSetup queueSetup = null;
 		
 		if(message == null || queue == null) {
@@ -83,7 +83,7 @@
 		}
 	}
 
-	private static void setStringProperties(Message msg, List<KeyValuePair> properties) throws JMSException {
+	private static void setStringProperties(Message msg, KeyValuePair[] properties) throws JMSException {
 		if(properties == null) {
 			return;
 		}

Added: labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/ListenerUtils.java
===================================================================
--- labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/ListenerUtils.java	2006-08-10 15:43:01 UTC (rev 5706)
+++ labs/jbossesb/trunk/qa/junit/src/org/jboss/soa/esb/util/ListenerUtils.java	2006-08-10 15:44:44 UTC (rev 5707)
@@ -0,0 +1,165 @@
+/*
+ * 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 java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.listeners.GpListener;
+import org.jboss.soa.esb.parameters.ParamFileRepository;
+import org.jboss.soa.esb.parameters.ParamRepositoryException;
+import org.jboss.soa.esb.parameters.ParamRepositoryFactory;
+
+/**
+ * Listener utility methods.
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public abstract class ListenerUtils {
+
+	private static final String CONFIG_TMP_EXT = ".conftmp";
+	private static Logger logger = Logger.getLogger(ListenerUtils.class);
+	
+	/**
+	 * Start a list of Listeners based on the supplied configuration.
+	 * <p/>
+	 * Note the root of the file repos is the root of the junit/src folder.
+	 * @param paramName Config classpath.
+	 * @return Thread of execution for this Listener configs.
+	 */
+	public static ListenersManagerExecThread startListeners(String paramName) throws Exception {
+		ListenersManagerExecThread manager;
+		File fixedConfig = fixUpConfig(paramName);
+		
+		manager = new ListenersManagerExecThread(new GpListener(paramName + CONFIG_TMP_EXT));
+		
+		logger.info("Waiting on Listener Manager the start...");
+		manager.start();
+		while(manager.listenersManager.getState() != GpListener.State.Running) {
+			Thread.sleep(50);
+			if(manager.listenersManager.getState() == GpListener.State.Exception_thrown) {
+				Exception e = manager.listenersManager.getState().getException();
+				logger.error("Failed to start the Listener Manager!", e);
+				TestCase.fail(e.getMessage());
+			}
+		}
+		logger.info("Listener Manager running (Thread: " + manager.getName() + ")!  Note this does not mean all the Listeners are up and running!");
+		
+		return manager;
+	}
+	
+	/**
+	 * Just performs a token replacement on "@qa.build@" tokesn in the Listener config and
+	 * creates a new repository entry for the Listener Manager to read from..
+	 * @param paramName Input config paramName.
+	 * @return The file handle for the new "fixed up" config.
+	 */
+	private static File fixUpConfig(String paramName) throws ParamRepositoryException, IOException {
+		ParamFileRepository repos = (ParamFileRepository)ParamRepositoryFactory.getInstance();
+		String configXml = repos.get(paramName);
+		File paramFile = repos.toParamFile(paramName);
+		File fixedupFile = new File(paramFile.getAbsolutePath() + CONFIG_TMP_EXT);
+		FileOutputStream fixedUpFileStream = new FileOutputStream(fixedupFile);
+		
+		try {
+			// Replace all the "@qa.build@" tokens with the qa "build" dirs file URI, and write to file...
+			configXml = configXml.replaceAll("@qa.build@", FileUtils.getEnvBuildDir().toURI().toString());
+			fixedUpFileStream.write(configXml.getBytes());
+		} finally {
+			fixedUpFileStream.flush();
+			fixedUpFileStream.close();
+		}		
+		
+		return fixedupFile;
+	}
+	
+
+	/**
+	 * Execution thread for the Listener Manager. 
+	 * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+	 */
+	public static class ListenersManagerExecThread extends Thread {
+		
+		private GpListener listenersManager;
+
+		private ListenersManagerExecThread(GpListener listenersManager) {
+			super(listenersManager);
+			this.listenersManager = listenersManager;
+		}
+
+		/**
+		 * Get the {@link GpListener} Listeners Manager class executing in this thread.
+		 * @return The listenersManager property value.
+		 */
+		public GpListener getListenersManager() {
+			return listenersManager;
+		}
+
+		/**
+		 * Assert that the listener Manager is in an Exception state..
+		 */
+		public void asserttInException() {
+			if(listenersManager.getState() != GpListener.State.Exception_thrown) {
+				String errorMsg = "ListenerManager not in Exception state.  Listener Manager Thread: " + this.getName();
+				logger.error(errorMsg);
+				TestCase.fail(errorMsg);
+			}
+		}
+
+		/**
+		 * Assert that the listener Manager is not in an Exception state..
+		 */
+		public void assertNotInException() {
+			if(listenersManager.getState() == GpListener.State.Exception_thrown) {
+				String errorMsg = "ListenerManager in Exception state.  See log.  Listener Manager Thread: " + this.getName();
+				logger.error(errorMsg, listenersManager.getState().getException());
+				TestCase.fail(errorMsg);
+			}
+		}
+
+		/**
+		 * Assert that the listener Manager has shutdown.
+		 * @param maxWait The maximum length of time (ms) to wait for shutdown before failing the test.
+		 */
+		public void assertShutdownOK(long maxWait) {
+			long endTime = System.currentTimeMillis() + maxWait;
+			
+			while(System.currentTimeMillis() < endTime) {
+				if(listenersManager.getState() == GpListener.State.Done_OK) {
+					logger.info("Shutdown was successful.  Listener Manager Thread: " + this.getName());
+					return;
+				}
+				try {
+					Thread.sleep(100);
+				} catch (InterruptedException e) {
+					logger.error("Thread interupt...", e);
+				}
+			}
+			String errorMsg = "ListenerManager failed to shutdown as requested.  Waited for " + maxWait + "ms.  Listener Manager Thread: " + this.getName();
+			logger.error(errorMsg);
+			TestCase.fail(errorMsg);
+		}
+	}
+}
\ No newline at end of file




More information about the jboss-svn-commits mailing list