[jboss-cvs] JBossAS SVN: r62108 - in trunk/testsuite: src/main/org/jboss/test/util/test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 4 21:39:17 EDT 2007


Author: stan.silvert at jboss.com
Date: 2007-04-04 21:39:16 -0400 (Wed, 04 Apr 2007)
New Revision: 62108

Added:
   trunk/testsuite/src/main/org/jboss/test/util/test/TwiddleUnitTestCase.java
Modified:
   trunk/testsuite/build.xml
Log:
JBAS-3726


Modified: trunk/testsuite/build.xml
===================================================================
--- trunk/testsuite/build.xml	2007-04-05 00:16:20 UTC (rev 62107)
+++ trunk/testsuite/build.xml	2007-04-05 01:39:16 UTC (rev 62108)
@@ -2001,6 +2001,7 @@
          <jvmarg value="${junit.jvm.options}"/>
          <!-- Used for JGroups -->
          <jvmarg value="-Dbind.address=${node0}"/>
+         <sysproperty key="jboss.dist" value="${jboss.dist}"/>
          <sysproperty key="jbosstest.deploy.dir" file="${build.lib}"/>
          <sysproperty key="build.testlog" value="${build.testlog}"/>
          <sysproperty key="log4j.configuration" value="file:${build.resources}/log4j.xml"/>
@@ -3353,6 +3354,7 @@
          <jvmarg value="-Djava.security.policy==${build.resources}/client.policy"/>
          <!-- Used for JGroups -->
          <jvmarg value="-Dbind.address=${node0}"/>
+         <sysproperty key="jboss.dist" value="${jboss.dist}"/>
          <sysproperty key="jbosstest.deploy.dir" value="${build.lib}"/>
          <sysproperty key="build.testlog" value="${build.testlog}"/>
          <sysproperty key="log4j.configuration" value="file:${build.resources}/log4j.xml"/>
@@ -3407,6 +3409,7 @@
          jvm="${junit.jvm}">
 
          <jvmarg value="${junit.jvm.options}"/>
+         <sysproperty key="jboss.dist" value="${jboss.dist}"/>
          <sysproperty key="jbosstest.deploy.dir" file="${build.lib}"/>
          <sysproperty key="build.testlog" value="${build.testlog}"/>
          <sysproperty key="log4j.configuration" value="file:${build.resources}/log4j.xml"/>
@@ -3482,6 +3485,7 @@
 
          <!-- Used for JGroups -->
          <jvmarg value="-Dbind.address=${node0}"/>
+         <sysproperty key="jboss.dist" value="${jboss.dist}"/>
          <sysproperty key="java.endorsed.dirs" value="${jboss.dist}/lib/endorsed"/>
          <!-- [JBAS-3969] IOException: unknown protocol: vfsfile -->
          <sysproperty key="java.protocol.handler.pkgs" value="org.jboss.virtual.protocol"/>
@@ -3545,6 +3549,7 @@
         <jvmarg value="${junit.jvm.options}"/>
         <!-- Used for JGroups -->
         <jvmarg value="-Dbind.address=${node0}"/>
+        <sysproperty key="jboss.dist" value="${jboss.dist}"/>
         <sysproperty key="java.endorsed.dirs" value="${jboss.dist}/lib/endorsed"/>
         <sysproperty key="jbosstest.deploy.dir" file="${build.lib}"/>
         <sysproperty key="java.naming.provider.url" value="${node0.jndi.url}"/>

Added: trunk/testsuite/src/main/org/jboss/test/util/test/TwiddleUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/util/test/TwiddleUnitTestCase.java	                        (rev 0)
+++ trunk/testsuite/src/main/org/jboss/test/util/test/TwiddleUnitTestCase.java	2007-04-05 01:39:16 UTC (rev 62108)
@@ -0,0 +1,181 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.test.util.test;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.jboss.test.JBossTestCase;
+
+/**
+ * Unit tests for the Twiddle command line utility.
+ *
+ * @author  <a href="mailto:stan at jboss.org">Stan Silvert</a>
+ */
+public class TwiddleUnitTestCase extends JBossTestCase
+{
+  public TwiddleUnitTestCase(String name)
+  {
+     super(name);
+  }
+
+  public static Test suite() throws Exception
+  {
+     TestSuite suite = new TestSuite();
+     suite.addTest(new TestSuite(TwiddleUnitTestCase.class));
+     return suite;
+  }
+
+  /**
+   * This really just tests that twiddle can be invoked.
+   */
+  public void testHelp() throws Exception
+  {
+     String result = runTwiddle("-h");
+     assertTrue(result.contains("usage: twiddle"));
+     assertTrue(result.contains("[options] <command> [command_arguments]"));
+  }
+
+  /**
+   *  Test getting an attribute value from an MBean.
+   */
+  public void testGet() throws Exception
+  {
+     String result = runTwiddle("get", "\"jboss.system:type=Server\"", "Started");
+     assertTrue(result.startsWith("Started=true"));
+  }
+
+  /**
+   * Run twiddle with the given arguments. This method relies on finding the twiddle
+   * bat or sh file using the jboss.dist system property. It will automatically pass
+   * in the -s parameter for the host of the server being used for testing.
+   *
+   * @param args The arguments passed to twiddle.  These should not include the -s
+   *             argument.
+   */
+  protected String runTwiddle(String... args) throws IOException, InterruptedException
+  {
+     List<String> command = new ArrayList<String>();
+
+     if (isWindows())
+     {
+        command.add("cmd");
+        command.add("/C");
+        command.add("twiddle");
+        //command.add("-s");
+        //command.add(getServerHost());
+        command.addAll(Arrays.asList(args));
+     }
+     else
+     {
+        command.add("/bin/sh");
+        command.add("-c");
+        String twiddleCmd = "./twiddle.sh ";
+        //twiddleCmd += "-s ";
+        //twiddleCmd += getServerHost();
+        twiddleCmd += makeTwiddleArgs(args);
+        command.add(twiddleCmd);
+     }
+
+     ProcessBuilder builder = new ProcessBuilder(command);
+     builder.directory(getTwiddleWorkingDir());
+     builder.environment().put("JBOSS_HOME", getJBossHome());
+     Process proc = builder.start();
+     StringBuilder buffer = readStream(proc.getInputStream());
+
+     if (log.isDebugEnabled()) debugTwiddle(builder, buffer, proc.getErrorStream());
+
+     return buffer.toString();
+  }
+
+  protected StringBuilder readStream(InputStream in) throws IOException
+  {
+     StringBuilder buffer = new StringBuilder();
+     int readByte = 0;
+     while (readByte != -1)
+     {
+       readByte = in.read();
+       if (readByte != -1)
+       {
+          buffer.append((char)readByte);
+       }
+     }
+     return buffer;
+  }
+
+  protected String makeTwiddleArgs(String[] args)
+  {
+     String result = "";
+     for (int i=0; i < args.length; i++)
+     {
+        result += " ";
+        result += args[i];
+     }
+     return result;
+  }
+
+  protected void debugTwiddle(ProcessBuilder builder,  
+                              StringBuilder buffer, 
+                              InputStream errorStream) throws IOException
+  {
+     String command = "";
+     for (String param: builder.command() )
+     {
+        command += param;
+        command += " ";
+     }
+
+     log.debug("executed: " + command);
+     log.debug("returned: " + buffer.toString());
+     log.debug("error stream: " + readStream(errorStream).toString());
+  }
+
+  protected boolean isWindows()
+  {
+     return System.getProperty("os.name").toLowerCase().startsWith("windows");
+  }
+
+  protected String getJBossHome()
+  {
+     // usually, the jboss.dist system property is set in the ant <junit> task using
+     // <sysproperty key="jboss.dist" value="${jboss.dist}"/>
+     String jbossDist = System.getProperty("jboss.dist");
+     if (jbossDist == null) throw new IllegalStateException("jboss.dist System property is not set");
+     return jbossDist;
+  }
+
+  protected File getTwiddleWorkingDir()
+  {
+     String jbossBin = getJBossHome() + "/bin";
+     log.debug("Twiddle working dir = " + jbossBin);
+     return new File(jbossBin);
+  }
+}




More information about the jboss-cvs-commits mailing list