Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow BMScript dir to work on Windows and Linux -- fixes BYTEMAN-254

  • Loading branch information...
commit 62055c07dca4b8698fb0f38e98e392f22395095a 1 parent 742b500
Andrew Dinn authored
13 contrib/bmunit/README.txt
View
@@ -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
129 contrib/bmunit/src/org/jboss/byteman/contrib/bmunit/BMUnit.java
View
@@ -1,6 +1,5 @@
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;
@@ -22,10 +21,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 +68,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 +161,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;
@@ -291,28 +374,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 +442,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 +468,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 +498,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"};
Please sign in to comment.
Something went wrong with that request. Please try again.