<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><style type="text/css"><!--
#msg DL { border : 1px #006 solid; background-color : #369; padding : 6px; color : #fff; }
#msg DT { float : left; width : 6em; font-weight : bold; }
#msg DL, #msg DT, #msg UL, #msg LI { font-family : arial,helvetica,sans-serif; font-size : 10pt; }
h3 { font-family : arial,helvetica,sans-serif; font-size : 10pt; font-weight : bold; }
#msg PRE { overflow : auto; white-space : normal; background-color : #ffc; border : 1px #fc0 solid; padding : 6px; }
#msg UL, PRE, .diff { overflow : auto; }
#patch h4 { font-family : arial,helvetica,sans-serif; font-size : 10pt; }
#patch h4 { padding: 8px; background : #369; color : #fff; margin : 0; }
#patch .propset h4, #patch .binary h4 {margin: 0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {background:#eeeeee;padding: 0 0 10px 0;}
#patch .propset .diff, #patch .binary .diff {padding: 10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch .add {background:#ddffdd;}
#patch .rem {background:#ffdddd;}
#patch .lines, .info {color:#888888;background:#ffffff;}
.diff { width : 100%; }
#msg DL { border : 1px #006 solid; background-color : #369; padding : 6px; color : #fff; }
#msg DT { float : left; width : 6em; font-weight : bold; }
#msg DL, #msg DT, #msg UL, #msg LI { font-family : arial,helvetica,sans-serif; font-size : 10pt; }
h3 { font-family : arial,helvetica,sans-serif; font-size : 10pt; font-weight : bold; }
#msg PRE { overflow : auto; white-space : normal; background-color : #ffc; border : 1px #fc0 solid; padding : 6px; }
#msg UL, PRE, .diff { overflow : auto; }
#patch h4 { font-family : arial,helvetica,sans-serif; font-size : 10pt; }
#patch h4 { padding: 8px; background : #369; color : #fff; margin : 0; }
#patch .propset h4, #patch .binary h4 {margin: 0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {background:#eeeeee;padding: 0 0 10px 0;}
#patch .propset .diff, #patch .binary .diff {padding: 10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch .add {background:#ddffdd;}
#patch .rem {background:#ffdddd;}
#patch .lines, .info {color:#888888;background:#ffffff;}
.diff { width : 100%; }
--></style>
<title>[rhq-project.org rhq] [4626] Adding TestNG to the runtime classpath of the CLI so that its assertion utilities can be made available to scripts.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd>4626</dd>
<dt>Author</dt> <dd>jsanda</dd>
<dt>Date</dt> <dd>2009-07-29 13:34:58 -0500 (Wed, 29 Jul 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>Adding TestNG to the runtime classpath of the CLI so that its assertion utilities can be made available to scripts. Initial commit for ScriptAssert which (right now) is delegates to TestNG's Assert class. Generalized a helper method in ScriptCommand for generating and binding global functions in the script context. In addition to the methods in ScriptUtil, global methods are created and bound for ScriptAssert as well.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#rhqtrunkmodulesenterpriseremotingclipomxml">rhq/trunk/modules/enterprise/remoting/cli/pom.xml</a></li>
<li><a href="#rhqtrunkmodulesenterpriseremotingclisrcmainjavaorgrhqenterpriseclientcommandsScriptCommandjava">rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/commands/ScriptCommand.java</a></li>
<li><a href="#rhqtrunkmodulesenterpriseremotingclisrcmainjavaorgrhqenterpriseclientutilityScriptUtiljava">rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/utility/ScriptUtil.java</a></li>
<li><a href="#rhqtrunkmodulesenterpriseremotingclisrcmainscriptsrhqclientbuildxml">rhq/trunk/modules/enterprise/remoting/cli/src/main/scripts/rhq-client.build.xml</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#rhqtrunkmodulesenterpriseremotingclisrcmainjavaorgrhqenterpriseclientutilityScriptAssertjava">rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/utility/ScriptAssert.java</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="rhqtrunkmodulesenterpriseremotingclipomxml"></a>
<div class="modfile"><h4>Modified: rhq/trunk/modules/enterprise/remoting/cli/pom.xml (4625 => 4626)</h4>
<pre class="diff">
<span class="info">--- rhq/trunk/modules/enterprise/remoting/cli/pom.xml 2009-07-29 13:42:17 UTC (rev 4625)
+++ rhq/trunk/modules/enterprise/remoting/cli/pom.xml 2009-07-29 18:34:58 UTC (rev 4626)
</span><span class="lines">@@ -30,6 +30,7 @@
</span><span class="cx"> <persistence-api.version>1.0</persistence-api.version>
<hibernate-all.version>1.0.0.Alpha9</hibernate-all.version>
<opencsv.version>1.8</opencsv.version>
</span><span class="add">+ <testng.version>5.9</testng.version>
</span><span class="cx"> </properties>
<dependencies>
</span><span class="lines">@@ -139,6 +140,13 @@
</span><span class="cx"> <artifactId>hibernate-annotations</artifactId>
</dependency>
</span><span class="add">+ <dependency>
+ <groupId>org.testng</groupId>
+ <artifactId>testng</artifactId>
+ <version>${testng.version}</version>
+ <classifier>jdk15</classifier>
+ </dependency>
+
</span><span class="cx"> </dependencies>
<build>
</span><span class="lines">@@ -251,6 +259,9 @@
</span><span class="cx"> name="log4j.version"
value="${log4j.version}" />
<property
</span><span class="add">+ name="testng.version"
+ value="${testng.version}" />
+ <property
</span><span class="cx"> name="persistence-api.version"
value="${persistence-api.version}" />
<property
</span></pre></div>
<a id="rhqtrunkmodulesenterpriseremotingclisrcmainjavaorgrhqenterpriseclientcommandsScriptCommandjava"></a>
<div class="modfile"><h4>Modified: rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/commands/ScriptCommand.java (4625 => 4626)</h4>
<pre class="diff">
<span class="info">--- rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/commands/ScriptCommand.java 2009-07-29 13:42:17 UTC (rev 4625)
+++ rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/commands/ScriptCommand.java 2009-07-29 18:34:58 UTC (rev 4626)
</span><span class="lines">@@ -34,6 +34,7 @@
</span><span class="cx"> import org.rhq.enterprise.client.utility.PackageFinder;
import org.rhq.enterprise.client.utility.ResourceClientProxy;
import org.rhq.enterprise.client.utility.ScriptUtil;
</span><span class="add">+import org.rhq.enterprise.client.utility.ScriptAssert;
</span><span class="cx">
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
</span><span class="lines">@@ -48,6 +49,7 @@
</span><span class="cx"> import java.io.Reader;
import java.util.Arrays;
import java.util.List;
</span><span class="add">+import java.lang.reflect.Method;
</span><span class="cx">
/**
* @author Greg Hinkle
</span><span class="lines">@@ -178,7 +180,6 @@
</span><span class="cx"> // These are prepared on every call in case the user logs out and logs into another server
if (controller.getSubject() != null) {
jsEngine.put("subject", controller.getSubject());
</span><span class="rem">- //sem.getBindings().put("subject", controller.getSubject());
</span><span class="cx"> sem.getBindings().putAll(controller.getManagers());
}
TabularWriter tw = new TabularWriter(client.getPrintWriter());
</span><span class="lines">@@ -186,28 +187,26 @@
</span><span class="cx"> sem.getBindings().put("pretty", tw);
sem.getBindings().put("ProxyFactory", new ResourceClientProxy.Factory(client.getRemoteClient()));
</span><span class="rem">- bindScriptUtils();
</span><span class="cx">
</span><span class="rem">- jsEngine.put("rhq", controller);
</span><span class="add">+ bindObjectAndGlobalFuctions(controller, "rhq");
+ bindObjectAndGlobalFuctions(new ScriptUtil(), "scriptUtil");
+ bindObjectAndGlobalFuctions(new ScriptAssert(jsEngine), "Assert");
</span><span class="cx"> }
</span><span class="rem">- private void bindScriptUtils() {
- final String BIND_NAME = "scriptUtil";
-
- ScriptUtil scriptUtil = new ScriptUtil(jsEngine);
- jsEngine.put(BIND_NAME, scriptUtil);
-
</span><span class="add">+ private void bindObjectAndGlobalFuctions(Object object, String bindingName) {
+ jsEngine.put(bindingName, object);
</span><span class="cx"> try {
</span><span class="rem">- BeanInfo beanInfo = Introspector.getBeanInfo(ScriptUtil.class, Object.class);
</span><span class="add">+ BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
</span><span class="cx"> MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
</span><span class="add">+
</span><span class="cx"> for (MethodDescriptor methodDescriptor : methodDescriptors) {
</span><span class="rem">- java.lang.reflect.Method method = methodDescriptor.getMethod();
</span><span class="add">+ Method method = methodDescriptor.getMethod();
</span><span class="cx"> String methodName = method.getName();
int argCount = method.getParameterTypes().length;
StringBuilder functionBuilder = new StringBuilder();
functionBuilder.append(methodName).append("(");
</span><span class="rem">- for (int i = 0; i < argCount; i++) {
</span><span class="add">+ for (int i = 0; i < argCount; ++i) {
</span><span class="cx"> if (i != 0) {
functionBuilder.append(", ");
}
</span><span class="lines">@@ -215,20 +214,21 @@
</span><span class="cx"> }
functionBuilder.append(")");
String functionFragment = functionBuilder.toString();
</span><span class="add">+ boolean returnsVoid = method.getReturnType().equals(Void.TYPE);
</span><span class="cx">
</span><span class="rem">- boolean returnsVoid = method.getReturnType().equals(Void.TYPE);
</span><span class="cx"> String functionDefinition = "function " + functionFragment + " { " + (returnsVoid ? "" : "return ")
</span><span class="rem">- + BIND_NAME + "." + functionFragment + "; }";
</span><span class="add">+ + bindingName + "." + functionFragment + "; }";
</span><span class="cx">
</span><span class="rem">- log.info("Binding... \"" + functionDefinition + "\"");
</span><span class="add">+ log.info("Binding global function --> " + functionDefinition);
</span><span class="cx"> try {
jsEngine.eval(functionDefinition);
} catch (ScriptException e) {
</span><span class="rem">- log.warn("Unable to bind script utility function " + functionFragment, e);
</span><span class="add">+ log.warn("Unable to bind global function " + functionFragment, e);
</span><span class="cx"> }
}
</span><span class="rem">- } catch (IntrospectionException ie) {
- log.warn("Could not bind any script utility functions", ie);
</span><span class="add">+ } catch (IntrospectionException e) {
+ // TODO Should we altogether remove the object from the script engine bindings?
+ log.warn("Could not bind " + object.getClass().getName() + " into script engine.");
</span><span class="cx"> }
}
</span></pre></div>
<a id="rhqtrunkmodulesenterpriseremotingclisrcmainjavaorgrhqenterpriseclientutilityScriptAssertjava"></a>
<div class="addfile"><h4>Added: rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/utility/ScriptAssert.java (0 => 4626)</h4>
<pre class="diff">
<span class="info">--- rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/utility/ScriptAssert.java (rev 0)
+++ rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/utility/ScriptAssert.java 2009-07-29 18:34:58 UTC (rev 4626)
</span><span class="lines">@@ -0,0 +1,215 @@
</span><span class="add">+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2008 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation, and/or the GNU Lesser
+ * General Public License, version 2.1, also as published by the Free
+ * Software Foundation.
+ *
+ * This program 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 General Public License and the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * and the GNU Lesser General Public License along with this program;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.rhq.enterprise.client.utility;
+
+import org.testng.Assert;
+
+import javax.script.ScriptEngine;
+import java.util.Collection;
+
+public class ScriptAssert {
+
+ private ScriptEngine scriptEngine;
+
+ public ScriptAssert(ScriptEngine scriptEngine) {
+ this.scriptEngine = scriptEngine;
+ }
+
+ public void assertTrue(boolean condition, String msg) {
+ Assert.assertTrue(condition, msg);
+ }
+
+ public void assertTrue(boolean condition) {
+ Assert.assertTrue(condition);
+ }
+
+ public void assertFalse(boolean condition, String msg) {
+ Assert.assertFalse(condition, msg);
+ }
+
+ public void assertFalse(boolean condition) {
+ Assert.assertFalse(condition);
+ }
+
+ public void fail(String msg, Throwable throwable) {
+ Assert.fail(msg, throwable);
+ }
+
+ public void fail(String msg) {
+ Assert.fail(msg);
+ }
+
+ public void fail() {
+ Assert.fail();
+ }
+
+ public void assertEquals(Object actual, Object expected, String msg) {
+ Assert.assertEquals(actual, expected, msg);
+ }
+
+ public void assertEquals(Object actual, Object expected) {
+ Assert.assertEquals(actual, expected);
+ }
+
+ public void assertEquals(String actual, String expected, String msg) {
+ Assert.assertEquals(actual, expected, msg);
+ }
+
+ public void assertEquals(String actual, String expected) {
+ Assert.assertEquals(actual, expected);
+ }
+
+ public void assertEquals(double actual, double expected, double delta, String msg) {
+ Assert.assertEquals(actual, expected, delta, msg);
+ }
+
+ public void assertEquals(double actual, double expected, double delta) {
+ Assert.assertEquals(actual, expected, delta);
+ }
+
+ public void assertEquals(float actual, float expected, float delta, String msg) {
+ Assert.assertEquals(actual, expected, delta, msg);
+ }
+
+ public void assertEquals(float actual, float expected, float delta) {
+ Assert.assertEquals(actual, expected, delta);
+ }
+
+ public void assertEquals(long actual, long expected, String msg) {
+ Assert.assertEquals(actual, expected, msg);
+ }
+
+ public void assertEquals(long actual, long expected) {
+ Assert.assertEquals(actual, expected);
+ }
+
+ public void assertEquals(boolean actual, boolean expected, String msg) {
+ Assert.assertEquals(actual, expected, msg);
+ }
+
+ public void assertEquals(boolean actual, boolean expected) {
+ Assert.assertEquals(actual, expected);
+ }
+
+ public void assertEquals(byte actual, byte expected, String msg) {
+ Assert.assertEquals(actual, expected, msg);
+ }
+
+ public void assertEquals(byte actual, byte expected) {
+ Assert.assertEquals(actual, expected);
+ }
+
+ public void assertEquals(char actual, char expected, String msg) {
+ Assert.assertEquals(actual, expected, msg);
+ }
+
+ public void assertEquals(char actual, char expected) {
+ Assert.assertEquals(actual, expected);
+ }
+
+ public void assertEquals(short actual, short expected, String msg) {
+ Assert.assertEquals(actual, expected, msg);
+ }
+
+ public void assertEquals(short actual, short expected) {
+ Assert.assertEquals(actual, expected);
+ }
+
+ public void assertEquals(int actual, int expected, String msg) {
+ Assert.assertEquals(actual, expected, msg);
+ }
+
+ public void assertEquals(int actual, int expected) {
+ Assert.assertEquals(actual, expected);
+ }
+
+ public void assertNotNull(Object object) {
+ Assert.assertNotNull(object);
+ }
+
+ public void assertNotNull(Object object, String msg) {
+ Assert.assertNotNull(object, msg);
+ }
+
+ public void assertNull(Object object) {
+ Assert.assertNull(object);
+ }
+
+ public void assertNull(Object object, String msg) {
+ Assert.assertNull(object, msg);
+ }
+
+ public void assertSame(Object actual, Object expected, String msg) {
+ Assert.assertSame(actual, expected, msg);
+ }
+
+ public void assertSame(Object actual, Object expected) {
+ Assert.assertSame(actual, expected);
+ }
+
+ public void assertNotSame(Object actual, Object expected, String msg) {
+ Assert.assertNotSame(actual, expected, msg);
+ }
+
+ public void assertNotSame(Object actual, Object expected) {
+ Assert.assertNotSame(actual, expected);
+ }
+
+ public void assertEquals(Collection actual, Collection expected) {
+ Assert.assertEquals(actual, expected);
+ }
+
+ public void assertEquals(Collection actual, Collection expected, String msg) {
+ Assert.assertEquals(actual, expected, msg);
+ }
+
+ public void assertEquals(Object[] actual, Object[] expected, String msg) {
+ Assert.assertEquals(actual, expected, msg);
+ }
+
+ public void assertEqualsNoOrder(Object[] actual, Object[] expected, String msg) {
+ Assert.assertEqualsNoOrder(actual, expected, msg);
+ }
+
+ public void assertEquals(Object[] actual, Object[] expected) {
+ Assert.assertEquals(actual, expected);
+ }
+
+ public void assertEqualsNoOrder(Object[] actual, Object[] expected) {
+ Assert.assertEqualsNoOrder(actual, expected);
+ }
+
+ public void assertEquals(byte[] actual, byte[] expected) {
+ Assert.assertEquals(actual, expected);
+ }
+
+ public void assertEquals(byte[] actual, byte[] expected, String msg) {
+ Assert.assertEquals(actual, expected, msg);
+ }
+
+ public void assertExists(String identifier) {
+ assertNotNull(scriptEngine.get(identifier), identifier + " is not defined");
+ }
+
+}
</span><span class="cx">Property changes on: rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/utility/ScriptAssert.java
___________________________________________________________________
</span></pre></div>
<a id="svnmimetype"></a>
<div class="addfile"><h4>Added: svn:mime-type ( => )</h4>
<pre class="diff">
<span class="info">Added: svn:keywords
+ Date Author Id Revision HeadURL
</span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4>Added: svn:eol-style ( => )</h4>
<pre class="diff">
<span class="info">
Modified: rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/utility/ScriptUtil.java
</span><span class="cx">===================================================================
</span><span class="rem">--- rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/utility/ScriptUtil.java 2009-07-29 13:42:17 UTC (rev 4625)
</span><span class="add">+++ rhq/trunk/modules/enterprise/remoting/cli/src/main/java/org/rhq/enterprise/client/utility/ScriptUtil.java 2009-07-29 18:34:58 UTC (rev 4626)
</span><span class="lines">@@ -9,40 +9,6 @@
</span><span class="cx">
public class ScriptUtil {
</span><span class="rem">- private ScriptEngine scriptEngine;
-
- public ScriptUtil(ScriptEngine scriptEngine) {
- this.scriptEngine = scriptEngine;
- }
-
- public void assertEqual(Object lefty, Object righty) {
- assertEqual(lefty, righty, null);
- }
-
- public void assertEqual(Object lefty, Object righty, String errorMessage) {
- boolean result;
- if (lefty == null) {
- if (righty == null) {
- result = true; // both null
- } else {
- result = false; // only lefty is null
- }
- } else {
- if (righty == null) {
- result = false; // only righty is null
- } else {
- result = lefty.equals(righty) && righty.equals(lefty);
- }
- }
- if (result == false) {
- if (errorMessage == null) {
- throw new AssertionError("values were not equal");
- } else {
- throw new AssertionError(errorMessage);
- }
- }
- }
-
</span><span class="cx"> public byte[] getFileBytes(String fileName) {
File file = new File(fileName);
long length = file.length();
</span><span class="lines">@@ -81,12 +47,6 @@
</span><span class="cx"> return bytes;
}
</span><span class="rem">- public void assertExists(String identifier) {
- if (scriptEngine.get(identifier) == null) {
- throw new AssertionError(identifier + " was not defined");
- }
- }
-
</span><span class="cx"> public void sleep(long millis) {
try {
Thread.sleep(millis);
</span></pre></div>
<a id="rhqtrunkmodulesenterpriseremotingclisrcmainscriptsrhqclientbuildxml"></a>
<div class="modfile"><h4>Modified: rhq/trunk/modules/enterprise/remoting/cli/src/main/scripts/rhq-client.build.xml (4625 => 4626)</h4>
<pre class="diff">
<span class="info">--- rhq/trunk/modules/enterprise/remoting/cli/src/main/scripts/rhq-client.build.xml 2009-07-29 13:42:17 UTC (rev 4625)
+++ rhq/trunk/modules/enterprise/remoting/cli/src/main/scripts/rhq-client.build.xml 2009-07-29 18:34:58 UTC (rev 4626)
</span><span class="lines">@@ -61,6 +61,7 @@
</span><span class="cx"> <copy file="${settings.localRepository}/javax/persistence/persistence-api/${persistence-api.version}/persistence-api-${persistence-api.version}.jar" tofile="${lib.home}/persistence-api-${persistence-api.version}.jar" verbose="true" />
<copy file="${basedir}/target/${project.artifactId}-${project.version}.jar" tofile="${lib.home}/${project.artifactId}-${project.version}.jar" verbose="true"/>
<copy file="${settings.localRepository}/net/sf/opencsv/opencsv/${opencsv.version}/opencsv-${opencsv.version}.jar" tofile="${lib.home}/opencsv-${opencsv.version}.jar" verbose="true"/>
</span><span class="add">+ <copy file="${settings.localRepository}/org/testng/testng/${testng.version}/testng-${testng.version}-jdk15.jar" tofile="${lib.home}/testng-${testng.version}-jdk15.jar" verbose="true"/>
</span><span class="cx"> </target>
<target name="prepare-tests">
</span>
</pre>
</div>
</div>
</body>
</html>
|