Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Dec 04, 2013
@adinn Allow BMScript dir to work on Windows and Linux -- fixes BYTEMAN-254 62055c0
Commits on Dec 05, 2013
@adinn added extra fallback for obtaining pid on Windows -- fixes BTEMAN-255
n.b many thanks very much to antoine vianey (antoine dot vianey at
gmail dot com) for contributing code for this fix
e5efcb1
Showing with 139 additions and 24 deletions.
  1. +12 −1 contrib/bmunit/README.txt
  2. +127 −23 contrib/bmunit/src/org/jboss/byteman/contrib/bmunit/BMUnit.java
View
13 contrib/bmunit/README.txt
@@ -144,7 +144,7 @@ that @BMScript rules are always loaded before rules specified via @BMRule.
File lookup employs the computed test name and/or the test class name to locate
the rule script, trying various alternative combinations of these two values. If you
have configured a lookup directory then files are searched for below that directory.
-Otherwise, System property org.jboss.byteman.contrib.bmunit.script.directory will be
+Otherwise, System property org.jboss.byteman.contrib.bmunit.load.directory will be
checked and, if set, used as the search directory. Failing that the search will proceed
using the working directory of the test.
@@ -167,6 +167,17 @@ Look for
7) <dir>/TestCaseClass.btm
8) <dir>/TestCaseClass.txt
+Files are also searched for as classloader resources. If you have configured
+a lookup directory then that directory is used as a prefix for the resource name.
+Otherwise, System property org.jboss.byteman.contrib.bmunit.load.directory will be
+checked and, if set, used as the resource name prefix. If this property is unset
+then the value of org.jboss.byteman.contrib.bmunit.load.directory will be used
+instead. If neither is set then no resource prefix will be used.
+
+Note that when running on Windows any '/' separator occurring in a file name
+will be substituted with a'\' character. If you wnat your tests to run on both
+Windows and Linux/Unix then you should specify dir paths usng '/' as a separator.
+
JUnit 3 Style Tests
-------------------
If your test class inherits from BMTestCase then your test will load the Byteman
View
150 contrib/bmunit/src/org/jboss/byteman/contrib/bmunit/BMUnit.java
@@ -1,13 +1,13 @@
package org.jboss.byteman.contrib.bmunit;
-import com.sun.org.apache.xpath.internal.operations.Bool;
import com.sun.tools.attach.AgentInitializationException;
import org.jboss.byteman.agent.install.Install;
import org.jboss.byteman.agent.install.VMInfo;
import org.jboss.byteman.agent.submit.ScriptText;
import org.jboss.byteman.agent.submit.Submit;
import java.io.*;
+import java.lang.management.ManagementFactory;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
@@ -22,10 +22,17 @@
public class BMUnit
{
/**
- * System property which identifies the directory from which to start searching
- * for rule script. If unset the current working directory of the test is used.
+ * System property which identifies the directory from which to
+ * start searching for rule script. If unset the current working
+ * directory of the test is used.
*/
- public final static String LOAD_DIRECTORY = "org.jboss.byteman.contrib.bmunit.script.directory";
+ public final static String LOAD_DIRECTORY = "org.jboss.byteman.contrib.bmunit.load.directory";
+
+ /**
+ * System property which identifies the resource load directory
+ * from which to start searching for rule script.
+ */
+ public final static String RESOURCE_LOAD_DIRECTORY = "org.jboss.byteman.contrib.bmunit.resource.load.directory";
/**
* System property specifying the port to be used when starting the agent and when submitting
@@ -62,12 +69,28 @@
private final static boolean verbose = (System.getProperty(VERBOSE) != null);
/**
- * the directory in which to look for rule scripts. this can be configured by setting system property
+ * the file separator character used by the native file system
+ */
+ private static char fs = File.separatorChar;
+
+ /**
+ * the directory in which to look for rule scripts. this can be
+ * configured by setting system property
* org.jboss.byteman.contrib.bmunit.load.directory
*/
private static String defaultLoadDirectory = initDefaultLoadDirectory();
/**
+ * the resource path used to look for rule scripts. this can be
+ * configured by setting system property
+ * org.jboss.byteman.contrib.bmunit.resource.load.directory
+ *
+ * if that property is unset then it will be set from property
+ * org.jboss.byteman.contrib.bmunit.load.directory
+ */
+ private static String defaultResourceLoadDirectory = initDefaultResourceLoadDirectory();
+
+ /**
* hash table used to maintain association between test cases and rule files
*/
private static HashMap<String, String> fileTable = new HashMap<String, String>();
@@ -139,15 +162,76 @@ public static boolean getPolicy()
return policy;
}
/**
- * computes the default load directory from system property org.jboss.byteman.contrib.bmunit.load.directory
- * or defaults it to "."
+ * computes the default load directory from system property
+ * org.jboss.byteman.contrib.bmunit.load.directory or defaults it
+ * to ""
* @return the load directory
*/
private static String initDefaultLoadDirectory()
{
String dir = System.getProperty(LOAD_DIRECTORY);
- if (dir == null || dir.length() == 0) {
- dir = ".";
+ if (dir == null) {
+ return "";
+ }
+ return normalize(dir, true);
+ }
+
+ /**
+ * computes the default resource load directory from system
+ * property
+ * org.jboss.byteman.contrib.bmunit.resource.load.directory or
+ * defaults it to the load directory
+ * @return the resource load directory
+ */
+ private static String initDefaultResourceLoadDirectory()
+ {
+ String dir = System.getProperty(RESOURCE_LOAD_DIRECTORY);
+ if (dir == null) {
+ dir = System.getProperty(LOAD_DIRECTORY);
+ if (dir == null) {
+ dir = "";
+ }
+ }
+ int l = dir.length();
+ if (l > 0 && dir.charAt(l) != '/') {
+ dir = dir + "/";
+ }
+ return dir;
+ }
+
+ /**
+ * transform the supplied directory string if necessary to employ
+ * the file separator appropriate to the current file system,
+ * including a separator at the end if requested and not present.
+ *
+ * BMUnit assumes that all supplied paths are specified in Unix
+ * format i.e. with a '/' separator. So, transformation of '/' to
+ * '\' is only performed on Windows systems.
+ *
+ * @dir the directory string to be checked
+ * @endWithSeparator true if the returned directory must terminate
+ * wiht a file separator
+ */
+ private static String normalize(String dir, boolean endWithSeparator)
+ {
+ int l = dir.length();
+ if (l == 0) {
+ // don't worry about appending a separator to an empty
+ // string as endWithSeparator is only used for the
+ // relative load path and "" is used to specify a relative
+ // path
+ return dir;
+ }
+
+ if (fs == '\\' && dir.indexOf('/', 0) >= 0) {
+ dir = dir.replace('/', '\\');
+ }
+
+ if (endWithSeparator && dir.charAt(l - 1) != fs) {
+ StringBuilder sb = new StringBuilder(l+1);
+ sb.append(dir);
+ sb.append(fs);
+ return sb.toString();
}
return dir;
@@ -203,6 +287,26 @@ private static synchronized void loadAgent() throws Exception
break;
}
}
+ // last ditch effort to obtain pid on Windows where the availableVMs list may be empty
+ if (id == null) {
+ String processName = ManagementFactory.getRuntimeMXBean().getName();
+ if (processName != null && processName.contains("@")) {
+ id = processName.substring(0, processName.indexOf("@"));
+ // check we actually have an integer
+ try {
+ Integer.parseInt(id);
+ // well, it's a number so now check it identifies the current VM
+ String value = Install.getSystemProperty(id, prop);
+ if (!unique.equals(value)) {
+ // nope, not the right process
+ id = null;
+ }
+ } catch (NumberFormatException e) {
+ // nope, not a number
+ id = null;
+ }
+ }
+ }
// make sure we found a process
if (id == null) {
throw new Exception("BMUnit : Unable to identify test JVM process during agent load");
@@ -291,28 +395,25 @@ public static void loadScriptFile(Class<?> clazz, String dir) throws Exception
*/
public static void loadScriptFile(Class<?> clazz, String testName, String dir) throws Exception
{
- String loadDirectory = dir;
- if (loadDirectory == null) {
- loadDirectory = defaultLoadDirectory;
- }
// turn '.' characters into file separator characters
String className = clazz.getName();
if (testName == null) {
testName = "";
}
String key = className + "#" + testName;
- className = className.replace('.', File.separatorChar);
- int index = className.lastIndexOf(File.separatorChar);
+ className = className.replace('.', '/');
+ int index = className.lastIndexOf('/');
// we can also use the class name without package qualifier
String bareClassName = (index < 0 ? null : className.substring(index + 1));
String filename = null;
File file = null;
// first try for rule file based on test name or class name plus test name
- filename=findScript(loadDirectory,
+ filename=findScript(dir,
testName,
className + "-" + testName,
- className, bareClassName);
+ className, bareClassName,
+ bareClassName + "-" + testName);
if(filename != null)
file=new File(filename);
@@ -362,7 +463,7 @@ public static void unloadScriptFile(Class<?> clazz, String testName) throws Exce
/**
* loads a script supplied as a text String rather than via a file on disk
* @param clazz the test class
- * @param testName the test name
+ * @param testname the test name
* @param scriptText the text of the rule or rules contained in the script
*/
public static void loadScriptText(Class<?> clazz, String testname, String scriptText) throws Exception
@@ -388,7 +489,6 @@ public static void loadScriptText(Class<?> clazz, String testname, String script
* unloads a script previously supplied as a text String
* @param clazz the test class
* @param testName the test name
- * @param scriptText the text of the rule or rules contained in the script
*/
public static void unloadScriptText(Class<?> clazz, String testName) throws Exception
{
@@ -419,13 +519,17 @@ public static void unloadScriptText(Class<?> clazz, String testName) throws Exce
* @return The fully qualified name of the file, or null if not found
*/
protected static String findScript(String dir, String name) {
- String filename=name;
+ if(name == null) return null;
+ String filename=normalize(name, false);
String resourceName = name;
- if(filename == null) return null;
if(dir != null && dir.length() > 0) {
- filename=dir + File.separator + filename;
+ filename=normalize(dir, true) + filename;
resourceName=dir + "/" + resourceName;
- }
+ } else {
+ // n.b. defaults either are "" or end with correct separator
+ filename = defaultLoadDirectory + filename;
+ resourceName = defaultResourceLoadDirectory + filename;
+ }
final String[] filenames={filename, filename + ".btm", filename + ".txt"};
final String[] resourceNames={resourceName, resourceName + ".btm", resourceName + ".txt"};

No commit comments for this range

Something went wrong with that request. Please try again.