[jboss-cvs] JBossAS SVN: r61735 - branches/Branch_4_2/testsuite/src/main/org/jboss/test/util/test.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Mar 27 09:35:31 EDT 2007


Author: stan.silvert at jboss.com
Date: 2007-03-27 09:35:31 -0400 (Tue, 27 Mar 2007)
New Revision: 61735

Added:
   branches/Branch_4_2/testsuite/src/main/org/jboss/test/util/test/TwiddleTestCase.java
Log:
http://jira.jboss.com/jira/browse/JBAS-3726


Added: branches/Branch_4_2/testsuite/src/main/org/jboss/test/util/test/TwiddleTestCase.java
===================================================================
--- branches/Branch_4_2/testsuite/src/main/org/jboss/test/util/test/TwiddleTestCase.java	                        (rev 0)
+++ branches/Branch_4_2/testsuite/src/main/org/jboss/test/util/test/TwiddleTestCase.java	2007-03-27 13:35:31 UTC (rev 61735)
@@ -0,0 +1,129 @@
+/*
+ * 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 TwiddleTestCase extends JBossTestCase
+{
+   public TwiddleTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite() throws Exception
+   {
+      TestSuite suite = new TestSuite();
+      suite.addTest(new TestSuite(TwiddleTestCase.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 [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 (System.getProperty("os.name").toLowerCase().startsWith("windows"))
+      {
+         command.add("cmd");
+         command.add("/C");
+         command.add("twiddle");
+      }
+      else
+      {
+         command.add("/bin/sh");
+         command.add("-c");
+         command.add("twiddle.sh");
+      }
+
+      command.add("-s");
+      command.add(getServerHost());
+
+      command.addAll(Arrays.asList(args));
+
+      ProcessBuilder builder = new ProcessBuilder(command);
+      builder.directory(getTwiddleWorkingDir());
+      Process proc = builder.start();
+      InputStream in = proc.getInputStream();
+      int readByte = 0;
+      StringBuilder buffer = new StringBuilder();
+      while (readByte != -1)
+      {
+        readByte = in.read();
+        buffer.append((char)readByte);
+      }
+
+      return buffer.toString();
+   }
+
+   private File getTwiddleWorkingDir()
+   {
+      // 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");
+      String jbossBin = jbossDist + "/bin";
+      return new File(jbossBin);
+   }
+}




More information about the jboss-cvs-commits mailing list