[rhq] modules/enterprise
lkrejci
lkrejci at fedoraproject.org
Thu Aug 23 10:22:25 UTC 2012
modules/enterprise/binding/pom.xml | 7
modules/enterprise/binding/src/test/java/org/rhq/bindings/ScriptEngineTest.java | 58 +-
modules/enterprise/binding/src/test/java/org/rhq/bindings/ScriptedTestBase.java | 220 ++++++++
modules/enterprise/binding/src/test/java/org/rhq/bindings/util/ScriptAssertTest.java | 269 +++++++---
4 files changed, 475 insertions(+), 79 deletions(-)
New commits:
commit 8fb35be5c92397b282a582c754c6b71b8b277a60
Author: Lukas Krejci <lkrejci at redhat.com>
Date: Thu Aug 23 12:08:11 2012 +0200
Duplicating all the tests that use scripting language to have both javascript and python versions.
diff --git a/modules/enterprise/binding/pom.xml b/modules/enterprise/binding/pom.xml
index 7d37053..37959d2 100644
--- a/modules/enterprise/binding/pom.xml
+++ b/modules/enterprise/binding/pom.xml
@@ -45,6 +45,13 @@
</dependency>
<dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>rhq-scripting-python</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
<groupId>${project.groupId}</groupId>
<artifactId>rhq-core-domain</artifactId>
<version>${project.version}</version>
diff --git a/modules/enterprise/binding/src/test/java/org/rhq/bindings/ScriptEngineTest.java b/modules/enterprise/binding/src/test/java/org/rhq/bindings/ScriptEngineTest.java
index c3d5450..cf93afb 100644
--- a/modules/enterprise/binding/src/test/java/org/rhq/bindings/ScriptEngineTest.java
+++ b/modules/enterprise/binding/src/test/java/org/rhq/bindings/ScriptEngineTest.java
@@ -40,19 +40,24 @@ import org.rhq.bindings.util.PackageFinder;
*
* @author Lukas Krejci
*/
- at Test
-public class ScriptEngineTest {
+public class ScriptEngineTest extends ScriptedTestBase {
private static StandardBindings EMPTY_BINDINGS = new StandardBindings(new PrintWriter(System.out), new FakeRhqFacade());
@Test
- public void testFactory() throws ScriptException, IOException {
+ public void testFactory_javascript() throws ScriptException, IOException {
ScriptEngine engine = getScriptEngine();
assertNotNull(engine);
}
@Test
- public void testSandbox() throws ScriptException, IOException {
+ public void testFactory_python() throws ScriptException, IOException {
+ ScriptEngine engine = getScriptEngine();
+ assertNotNull(engine);
+ }
+
+ @Test
+ public void testSandbox_javascript() throws ScriptException, IOException {
ScriptEngine sandbox = getSecuredScriptEngine();
try {
@@ -76,7 +81,44 @@ public class ScriptEngineTest {
}
@Test
- public void testStandardBindings() throws ScriptException, IOException {
+ public void testSandbox_python() throws Exception {
+ ScriptEngine sandbox = getSecuredScriptEngine();
+
+ try {
+ sandbox.eval("import java.lang as foo\nfoo.System.exit(1)");
+ } catch (Exception e) {
+ assertSecurityExceptionPresent(e);
+ }
+
+ try {
+ //try hard to get to the System.exit()
+ sandbox.eval(
+ "import java.lang as l\n" +
+ "import java.lang.reflect as r\n" +
+ "import java.beans as b\n" +
+ "cls = l.Class.forName('java.lang.System')\n" +
+ "params = r.Array.newInstance(l.Class.forName('java.lang.Object'), 1)\n" +
+ "params[0] = l.Integer.valueOf('1')\n" +
+ "st = b.Statement(cls, 'exit', params)\n" +
+ "st.execute()");
+
+ } catch (Exception e) {
+ assertSecurityExceptionPresent(e);
+ }
+ }
+
+ @Test
+ public void testStandardBindings_javascript() throws ScriptException, IOException {
+ ScriptEngine scriptEngine = getScriptEngine();
+
+ for(String var : EMPTY_BINDINGS.keySet()) {
+ boolean hasVar = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE).containsKey(var);
+ assertTrue(hasVar, "The variable '" + var + "' is not present in the script context but should be.");
+ }
+ }
+
+ @Test
+ public void testStandardBindings_python() throws Exception {
ScriptEngine scriptEngine = getScriptEngine();
for(String var : EMPTY_BINDINGS.keySet()) {
@@ -86,13 +128,11 @@ public class ScriptEngineTest {
}
private ScriptEngine getScriptEngine() throws ScriptException, IOException {
- return ScriptEngineFactory.getScriptEngine("javascript", new PackageFinder(Collections.<File> emptyList()),
- EMPTY_BINDINGS);
+ return getScriptEngine(new PackageFinder(Collections.<File> emptyList()), EMPTY_BINDINGS);
}
private ScriptEngine getSecuredScriptEngine() throws ScriptException, IOException {
- return ScriptEngineFactory.getSecuredScriptEngine("javascript",
- new PackageFinder(Collections.<File> emptyList()), EMPTY_BINDINGS, new StandardScriptPermissions());
+ return getSecuredScriptEngine(new PackageFinder(Collections.<File> emptyList()), EMPTY_BINDINGS, new StandardScriptPermissions());
}
private void assertSecurityExceptionPresent(Throwable t) {
diff --git a/modules/enterprise/binding/src/test/java/org/rhq/bindings/ScriptedTestBase.java b/modules/enterprise/binding/src/test/java/org/rhq/bindings/ScriptedTestBase.java
new file mode 100644
index 0000000..9bf09b5
--- /dev/null
+++ b/modules/enterprise/binding/src/test/java/org/rhq/bindings/ScriptedTestBase.java
@@ -0,0 +1,220 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2012 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 as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+package org.rhq.bindings;
+
+import static org.testng.Assert.fail;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.security.PermissionCollection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+
+import org.testng.IHookCallBack;
+import org.testng.IHookable;
+import org.testng.ITestContext;
+import org.testng.ITestNGMethod;
+import org.testng.ITestResult;
+import org.testng.annotations.AfterClass;
+
+import org.rhq.bindings.util.PackageFinder;
+
+/**
+ * This is a base class for tests that need to test the some functionality using
+ * the scripting languages.
+ * <p>
+ * It enforces the naming convention for the tests:
+ * <code>
+ * @Test
+ * public void myTestMethod_language() ...
+ * </code>
+ * The language is to be replaced with the name of the scripting language that the test method
+ * uses (e.g. javacript, python). This class then makes sure that each test exists for all
+ * the scripting languages that are present on the classpath.
+ * <p>
+ * This class also provides the {@link #getScriptEngine(PackageFinder, StandardBindings)} and
+ * {@link #getSecuredScriptEngine(PackageFinder, StandardBindings, PermissionCollection)} methods
+ * that, if invoked from within a test method call-chain, will return the script engine for the
+ * correct language under test (determined by the test method name suffix).
+ *
+ * @author Lukas Krejci
+ */
+public abstract class ScriptedTestBase implements IHookable {
+
+ private String currentLanguage;
+
+ @AfterClass
+ public void checkTestImplsForEachLanguage(ITestContext ctx) {
+ Set<Method> methods = getAllTestMethods(getClass(), ctx);
+
+ Set<String> supportedLanguages = ScriptEngineFactory.getSupportedLanguages();
+
+ Map<String, Set<String>> methodBaseNamesByLanguage = new HashMap<String, Set<String>>();
+
+ for (String lang : supportedLanguages) {
+ methodBaseNamesByLanguage.put(lang, new HashSet<String>());
+ }
+
+ Set<Method> invalidTestMethods = new HashSet<Method>();
+
+ for (Method m : methods) {
+ //check that the method name ends with one of the supported language names
+ boolean valid = false;
+ for (String lang : supportedLanguages) {
+ String suffix = "_" + lang;
+
+ if (!m.getName().endsWith(suffix)) {
+ continue;
+ }
+
+ //now put the method "basename" into our mapping array
+ String baseName = m.getName().substring(0, m.getName().lastIndexOf("_"));
+ methodBaseNamesByLanguage.get(lang).add(baseName);
+
+ valid = true;
+ break;
+ }
+
+ if (!valid) {
+ invalidTestMethods.add(m);
+ }
+ }
+
+ Set<String> missingTests = new HashSet<String>();
+
+ //now check that all languages have all test methods
+ for (Map.Entry<String, Set<String>> a : methodBaseNamesByLanguage.entrySet()) {
+ for (Map.Entry<String, Set<String>> b : methodBaseNamesByLanguage.entrySet()) {
+ String alang = a.getKey();
+ String blang = b.getKey();
+ Set<String> amethods = a.getValue();
+ Set<String> bmethods = b.getValue();
+
+ if (alang.equals(blang)) {
+ continue;
+ }
+
+ addMissing(amethods, bmethods, alang, missingTests);
+ addMissing(bmethods, amethods, blang, missingTests);
+ }
+ }
+
+ if (!invalidTestMethods.isEmpty() || !missingTests.isEmpty()) {
+ StringBuilder msg = new StringBuilder("Scripted test " + getClass() + " is invalid:\n");
+
+ if (!invalidTestMethods.isEmpty()) {
+ msg.append("Invalid method names:\n");
+ for (Method m : invalidTestMethods) {
+ msg.append(m.getName()).append("\n");
+ }
+ }
+
+ if (!missingTests.isEmpty()) {
+ msg.append("\nMissing tests for languages:\n");
+ for (String m : missingTests) {
+ msg.append(m).append("\n");
+ }
+ }
+
+ fail(msg.toString());
+ }
+ }
+
+ @Override
+ public final void run(IHookCallBack callBack, ITestResult testResult) {
+ String methodName = testResult.getMethod().getMethodName();
+ int underScoreIdx = methodName.lastIndexOf('_');
+ if (underScoreIdx >= 0 && underScoreIdx < methodName.length() - 1) {
+ currentLanguage = methodName.substring(underScoreIdx + 1);
+ } else {
+ currentLanguage = null;
+ }
+
+ callBack.runTestMethod(testResult);
+
+ currentLanguage = null;
+ }
+
+ /**
+ * Returns a new script engine implementation for the current test method.
+ * <p>
+ * The script engine implementation is determined based on the test method's name
+ * suffix.
+ * <p>
+ * E.g. if the test method ends with "_javascript", this method will return the javascript
+ * script engine.
+ * <p>
+ * This method is calling {@link ScriptEngineFactory#getScriptEngine(String, PackageFinder, StandardBindings)}
+ * but determines the "language" parameter for you.
+ *
+ * @param packageFinder the package finder to use for the script engine initialization
+ * @param bindings the bindings to use in the script engine
+ * @return the script engine
+ * @throws ScriptException
+ * @throws IOException
+ */
+ protected ScriptEngine getScriptEngine(PackageFinder packageFinder, StandardBindings bindings)
+ throws ScriptException, IOException {
+
+ return ScriptEngineFactory.getScriptEngine(currentLanguage, packageFinder, bindings);
+ }
+
+ /**
+ * Similar to {@link #getScriptEngine(PackageFinder, StandardBindings)} but returns the
+ * secured version of the script engine.
+ *
+ * @param packageFinder the package finder to use for the script engine initialization
+ * @param bindings the bindings to use in the script engine
+ * @param perms the permissions to run the scripts with
+ * @return the script engine
+ * @throws ScriptException
+ * @throws IOException
+ */
+ protected ScriptEngine getSecuredScriptEngine(PackageFinder packageFinder, StandardBindings bindings,
+ PermissionCollection perms) throws ScriptException, IOException {
+
+ return ScriptEngineFactory.getSecuredScriptEngine(currentLanguage, packageFinder, bindings, perms);
+ }
+
+ private static Set<Method> getAllTestMethods(Class<?> cls, ITestContext ctx) {
+ HashSet<Method> ret = new HashSet<Method>();
+ for (ITestNGMethod m : ctx.getAllTestMethods()) {
+ if (m.getTestClass().getRealClass().equals(cls)) {
+ ret.add(m.getConstructorOrMethod().getMethod());
+ }
+ }
+
+ return ret;
+ }
+
+ private static void addMissing(Set<String> methods, Set<String> referenceMethods, String lang, Set<String> missing) {
+ for (String m : referenceMethods) {
+ if (!methods.contains(m)) {
+ String fullName = m + "_" + lang;
+ missing.add(fullName);
+ }
+ }
+ }
+}
diff --git a/modules/enterprise/binding/src/test/java/org/rhq/bindings/util/ScriptAssertTest.java b/modules/enterprise/binding/src/test/java/org/rhq/bindings/util/ScriptAssertTest.java
index 1d7f657..97753e3 100644
--- a/modules/enterprise/binding/src/test/java/org/rhq/bindings/util/ScriptAssertTest.java
+++ b/modules/enterprise/binding/src/test/java/org/rhq/bindings/util/ScriptAssertTest.java
@@ -26,118 +26,247 @@ package org.rhq.bindings.util;
import static org.testng.Assert.fail;
import java.io.File;
+import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
-import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.rhq.bindings.FakeRhqFacade;
-import org.rhq.bindings.ScriptEngineFactory;
+import org.rhq.bindings.ScriptedTestBase;
import org.rhq.bindings.StandardBindings;
-public class ScriptAssertTest {
+public class ScriptAssertTest extends ScriptedTestBase {
- private ScriptEngine engine;
+ private ScriptEngine getScriptEngine() {
+ try {
+ return getScriptEngine(new PackageFinder(Collections.<File> emptyList()), new StandardBindings(
+ new PrintWriter(System.out), new FakeRhqFacade()));
+ } catch (ScriptException e) {
+ fail("Could not get the script engine.", e);
+ return null;
+ } catch (IOException e) {
+ fail("Could not get the script engine.", e);
+ return null;
+ }
+ }
- @BeforeTest
- public void verifyScriptEngineIsAvailable() throws Exception {
- StandardBindings bindings = new StandardBindings(new PrintWriter(System.out), new FakeRhqFacade());
- engine = ScriptEngineFactory.getScriptEngine("javascript", new PackageFinder(Collections.<File> emptyList()),
- bindings);
+ @Test
+ public void testAssertExists_javascript() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "var a = 1; assertExists('a');", "assertExists should succeed for a defined variable.");
+ testThrowsAssertion(engine, "assertExists('foo')", "assertExists should fail for an undefined variable.");
+ testWorks(engine, "function func() { return 42 }; assertExists('func');",
+ "assertExists should succeed for a defined function.");
}
@Test
- public void testAssertExists() {
- testWorks("var a = 1; assertExists('a');", "assertExists should succeed for a defined variable.");
- testThrowsAssertion("assertExists('foo')", "assertExists should fail for an undefined variable.");
- testWorks("function func() { return 42 }; assertExists('func');",
+ public void testAssertExists_python() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "a = 1\n" + "assertExists('a')", "assertExists should succeed for a defined variable.");
+ testThrowsAssertion(engine, "assertExists('foo')", "assertExists should fail for an undefined variable.");
+ testWorks(engine, "def func():\n" + " return 42\n" + "assertExists('func')",
"assertExists should succeed for a defined function.");
}
@Test
- public void testAssertTrue() {
- testWorks("var a = true; assertTrue(a);", "assertTrue of a true variable should succeed");
- testWorks("assertTrue(1 == 1)", "assertTrue on a true boolean expression should succeed");
- testThrowsAssertion("var a = false; assertTrue(a)", "assertTrue should fail on a false variable");
- testThrowsAssertion("assertTrue(1 == 2)", "assertTrue should fail on a false boolean expression");
+ public void testAssertTrue_javascript() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "var a = true; assertTrue(a);", "assertTrue of a true variable should succeed");
+ testWorks(engine, "assertTrue(1 == 1)", "assertTrue on a true boolean expression should succeed");
+ testThrowsAssertion(engine, "var a = false; assertTrue(a)", "assertTrue should fail on a false variable");
+ testThrowsAssertion(engine, "assertTrue(1 == 2)", "assertTrue should fail on a false boolean expression");
+ }
+
+ @Test
+ public void testAssertTrue_python() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "a = True\n" + "assertTrue(a)", "assertTrue of a true variable should succeed");
+ testWorks(engine, "assertTrue(1 == 1)", "assertTrue on a true boolean expression should succeed");
+ testThrowsAssertion(engine, "a = False\n" + "assertTrue(a)", "assertTrue should fail on a false variable");
+ testThrowsAssertion(engine, "assertTrue(1 == 2)", "assertTrue should fail on a false boolean expression");
+ }
+
+ @Test
+ public void testAssertFalse_javascript() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "var a = false; assertFalse(a)", "assertFalse of a false variable should succeed");
+ testWorks(engine, "assertFalse(1 == 2)", "assertFalse on a false boolean expression should succeed");
+ testThrowsAssertion(engine, "var a = true; assertFalse(a)", "assertFalse should fail on a true variable");
+ testThrowsAssertion(engine, "assertFalse(1 == 1)", "assertFalse should fail on a true boolean expression");
}
-
+
@Test
- public void testAssertFalse() {
- testWorks("var a = false; assertFalse(a);", "assertFalse of a false variable should succeed");
- testWorks("assertFalse(1 == 2)", "assertFalse on a false boolean expression should succeed");
- testThrowsAssertion("var a = true; assertFalse(a)", "assertFalse should fail on a true variable");
- testThrowsAssertion("assertFalse(1 == 1)", "assertFalse should fail on a true boolean expression");
+ public void testAssertFalse_python() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "a = False\n" + "assertFalse(a)", "assertFalse of a false variable should succeed");
+ testWorks(engine, "assertFalse(1 == 2)", "assertFalse on a false boolean expression should succeed");
+ testThrowsAssertion(engine, "a = True\n" + "assertFalse(a)", "assertFalse should fail on a true variable");
+ testThrowsAssertion(engine, "assertFalse(1 == 1)", "assertFalse should fail on a true boolean expression");
}
-
+
@Test
- public void testAssertNull() {
- testWorks("assertNull(foo)", "assertNull should succeed on an undefined variable");
- testWorks("var foo = null; assertNull(foo);", "assertNull should succeed on a null variable");
- testWorks("assertNull(null)", "assertNull should succeed on a null literal");
- testThrowsAssertion("assertNull(1)", "assertNull should fail on a number");
- testThrowsAssertion("var foo = '1'; assertNull(foo)", "assertNull should fail on a non-null variable");
+ public void testAssertNull_javascript() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "var foo = null; assertNull(foo);", "assertNull should succeed on a null variable");
+ testWorks(engine, "assertNull(null)", "assertNull should succeed on a null literal");
+ testThrowsAssertion(engine, "assertNull(1)", "assertNull should fail on a number");
+ testThrowsAssertion(engine, "var foo = '1'; assertNull(foo)", "assertNull should fail on a non-null variable");
}
-
+
@Test
- public void testAssertNotNull() {
- testWorks("assertNotNull(1)", "assertNotNull should succeed on a number");
- testWorks("var foo = '1'; assertNotNull(foo)", "assertNotNull should succeed on a non-null variable");
- testThrowsAssertion("assertNotNull(foo)", "assertNotNull should fail on an undefined variable");
- testThrowsAssertion("var foo = null; assertNotNull(foo);", "assertNotNull should fail on a null variable");
- testThrowsAssertion("assertNotNull(null)", "assertNotNull should fail on a null literal");
+ public void testAssertNull_python() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "foo = None\n" + "assertNull(foo)", "assertNull should succeed on a null variable");
+ testWorks(engine, "assertNull(None)", "assertNull should succeed on a null literal");
+ testThrowsAssertion(engine, "assertNull(1)", "assertNull should fail on a number");
+ testThrowsAssertion(engine, "foo = '1'\n" + "assertNull(foo)", "assertNull should fail on a non-null variable");
}
-
+
@Test
- public void testAssertEquals_Numbers() {
- testWorks("assertEquals(1, 1)", "1 == 1");
- testWorks("assertEquals(1.0, 1)", "1.0 == 1");
- testWorks("assertEquals(1.0, 1.0)", "1.0 == 1.0");
- testThrowsAssertion("assertEquals(1, 2)", "1 == 2");
+ public void testAssertNotNull_javascript() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "assertNotNull(1)", "assertNotNull should succeed on a number");
+ testWorks(engine, "var foo = '1'; assertNotNull(foo)", "assertNotNull should succeed on a non-null variable");
+ testThrowsAssertion(engine, "assertNotNull(foo)", "assertNotNull should fail on an undefined variable");
+ testThrowsAssertion(engine, "var foo = null; assertNotNull(foo);",
+ "assertNotNull should fail on a null variable");
+ testThrowsAssertion(engine, "assertNotNull(null)", "assertNotNull should fail on a null literal");
}
-
+
@Test
- public void testAssertEquals_Arrays() {
- testWorks("assertEquals(['a', 'b'], ['a', 'b'])", "native array comparison");
- testThrowsAssertion("assertEquals(['a', 'b'], ['c', 'd'])", "native array comparison with difference");
+ public void testAssertNotNull_python() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "assertNotNull(1)", "assertNotNull should succeed on a number");
+ testWorks(engine, "foo = '1'\n" + "assertNotNull(foo)", "assertNotNull should succeed on a non-null variable");
+ testThrowsAssertion(engine, "assertNotNull(foo)", "assertNotNull should fail on an undefined variable");
+ testThrowsAssertion(engine, "foo = None\n" + "assertNotNull(foo);",
+ "assertNotNull should fail on a null variable");
+ testThrowsAssertion(engine, "assertNotNull(None)", "assertNotNull should fail on a null literal");
}
@Test
- public void testAssertEquals_Collections() {
- testWorks("a = new java.util.ArrayList; " + "b = new java.util.ArrayList; " + "a.add('a'); " + "b.add('a'); "
- + "assertEquals(a, b)", "ArrayList comparison");
- testThrowsAssertion("a = new java.util.ArrayList; " + "b = new java.util.ArrayList; " + "a.add('a'); "
+ public void testAssertEquals_Numbers_javascript() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "assertEquals(1, 1)", "1 == 1");
+ testWorks(engine, "assertEquals(1.0, 1)", "1.0 == 1");
+ testWorks(engine, "assertEquals(1.0, 1.0)", "1.0 == 1.0");
+ testThrowsAssertion(engine, "assertEquals(1, 2)", "1 == 2");
+ }
+
+ @Test
+ public void testAssertEquals_Numbers_python() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "assertEquals(1, 1)", "1 == 1");
+ //Python distinguishes between ints and floats, so this
+ //won't work (even though "1.0 == 1" returns true in pure python)
+ //testWorks(engine, "assertEquals(1.0, 1)", "1.0 == 1");
+ testWorks(engine, "assertEquals(1.0, 1.0)", "1.0 == 1.0");
+ testThrowsAssertion(engine, "assertEquals(1, 2)", "1 == 2");
+ }
+
+ @Test
+ public void testAssertEquals_Arrays_javascript() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "assertEquals(['a', 'b'], ['a', 'b'])", "native array comparison");
+ testThrowsAssertion(engine, "assertEquals(['a', 'b'], ['c', 'd'])", "native array comparison with difference");
+ }
+
+ @Test
+ public void testAssertEquals_Arrays_python() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "assertEquals(['a', 'b'], ['a', 'b'])", "native array comparison");
+ testThrowsAssertion(engine, "assertEquals(['a', 'b'], ['c', 'd'])", "native array comparison with difference");
+ }
+
+ @Test
+ public void testAssertEquals_Collections_javascript() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "a = new java.util.ArrayList; " + "b = new java.util.ArrayList; " + "a.add('a'); "
+ + "b.add('a'); " + "assertEquals(a, b)", "ArrayList comparison");
+ testThrowsAssertion(engine, "a = new java.util.ArrayList; " + "b = new java.util.ArrayList; " + "a.add('a'); "
+ "b.add('b'); " + "assertEquals(a, b)", "ArrayList comparison with difference");
}
@Test
- public void testAssertEqualsNoOrder() {
- testWorks("assertEqualsNoOrder(['a', 'b'], ['b', 'a'])", "native array comparison");
+ public void testAssertEquals_Collections_python() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "import java.util as u\n" + "a = u.ArrayList()\n" + "b = u.ArrayList()\n" + "a.add('a')\n"
+ + "b.add('a')\n" + "assertEquals(a, b)", "ArrayList comparison");
+ testThrowsAssertion(engine, "import java.util as u\n" + "a = u.ArrayList()\n" + "b = u.ArrayList()\n" + "a.add('a')\n"
+ + "b.add('b')\n" + "assertEquals(a, b)", "ArrayList comparison with difference");
}
@Test
- public void testAssertSame() {
- testWorks("var a = '1'; assertSame(a, a)", "assertSame should succeed comparing one variable");
- testWorks("var a = '1'; b = a; assertSame(a, b)", "asserSame should succeed comparing 2 references of the same object");
- testWorks("assertSame(null, null);", "assertSame should succeed on null values");
- testThrowsAssertion("var a = '1'; b = '2'; assertSame(a, b)", "assertSame should fail comparing 2 different variables");
- testThrowsAssertion("var a = 1; assertSame(a, null)", "assertSame should fail comparing non-null variable with a null value");
+ public void testAssertEqualsNoOrder_javascript() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "assertEqualsNoOrder(['a', 'b'], ['b', 'a'])", "native array comparison");
}
-
+
+ @Test
+ public void testAssertEqualsNoOrder_python() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "assertEqualsNoOrder(['a', 'b'], ['b', 'a'])", "native array comparison");
+ }
+
@Test
- public void testAssertNotSame() {
- testThrowsAssertion("var a = '1'; assertNotSame(a, a)", "assertNotSame should fail comparing one variable");
- testThrowsAssertion("var a = '1'; b = a; assertNotSame(a, b)", "asserNotSame should fail comparing 2 references of the same object");
- testThrowsAssertion("assertNotSame(null, null);", "assertNotSame should fail on null values");
- testWorks("var a = '1'; b = '2'; assertNotSame(a, b)", "assertNotSame should succeed comparing 2 different variables");
- testWorks("var a = 1; assertNotSame(a, null)", "assertNotSame should succeed comparing non-null variable with a null value");
+ public void testAssertSame_javascript() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "var a = '1'; assertSame(a, a)", "assertSame should succeed comparing one variable");
+ testWorks(engine, "var a = '1'; b = a; assertSame(a, b)",
+ "asserSame should succeed comparing 2 references of the same object");
+ testWorks(engine, "assertSame(null, null);", "assertSame should succeed on null values");
+ testThrowsAssertion(engine, "var a = '1'; b = '2'; assertSame(a, b)",
+ "assertSame should fail comparing 2 different variables");
+ testThrowsAssertion(engine, "var a = 1; assertSame(a, null)",
+ "assertSame should fail comparing non-null variable with a null value");
}
-
- private void testWorks(String script, String message) {
+
+ @Test
+ public void testAssertSame_python() {
+ ScriptEngine engine = getScriptEngine();
+ testWorks(engine, "a = '1'\n" + "assertSame(a, a)", "assertSame should succeed comparing one variable");
+ testWorks(engine, "a = '1'\n" + "b = a\n" + "assertSame(a, b)",
+ "asserSame should succeed comparing 2 references of the same object");
+ testWorks(engine, "assertSame(None, None)", "assertSame should succeed on null values");
+ testThrowsAssertion(engine, "a = '1'\n" + "b = '2'\n" + "assertSame(a, b)",
+ "assertSame should fail comparing 2 different variables");
+ testThrowsAssertion(engine, "a = 1\n" + "assertSame(a, None)",
+ "assertSame should fail comparing non-null variable with a null value");
+ }
+
+ @Test
+ public void testAssertNotSame_javascript() {
+ ScriptEngine engine = getScriptEngine();
+ testThrowsAssertion(engine, "var a = '1'; assertNotSame(a, a)",
+ "assertNotSame should fail comparing one variable");
+ testThrowsAssertion(engine, "var a = '1'; b = a; assertNotSame(a, b)",
+ "asserNotSame should fail comparing 2 references of the same object");
+ testThrowsAssertion(engine, "assertNotSame(null, null);", "assertNotSame should fail on null values");
+ testWorks(engine, "var a = '1'; b = '2'; assertNotSame(a, b)",
+ "assertNotSame should succeed comparing 2 different variables");
+ testWorks(engine, "var a = 1; assertNotSame(a, null)",
+ "assertNotSame should succeed comparing non-null variable with a null value");
+ }
+
+ @Test
+ public void testAssertNotSame_python() {
+ ScriptEngine engine = getScriptEngine();
+ testThrowsAssertion(engine, "a = '1'\n" + "assertNotSame(a, a)",
+ "assertNotSame should fail comparing one variable");
+ testThrowsAssertion(engine, "a = '1'\n" + "b = a\n" + "assertNotSame(a, b)",
+ "asserNotSame should fail comparing 2 references of the same object");
+ testThrowsAssertion(engine, "assertNotSame(None, None)", "assertNotSame should fail on null values");
+ testWorks(engine, "a = '1'\n" + "b = '2'\n" + "assertNotSame(a, b)",
+ "assertNotSame should succeed comparing 2 different variables");
+ testWorks(engine, "a = 1\n" + "assertNotSame(a, None)",
+ "assertNotSame should succeed comparing non-null variable with a null value");
+ }
+
+ private void testWorks(ScriptEngine engine, String script, String message) {
try {
engine.eval(script);
} catch (ScriptException e) {
@@ -145,7 +274,7 @@ public class ScriptAssertTest {
}
}
- private void testThrowsAssertion(String script, String message) {
+ private void testThrowsAssertion(ScriptEngine engine, String script, String message) {
try {
engine.eval(script);
} catch (ScriptException e) {
More information about the rhq-commits
mailing list