modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/Action.java | 5
modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangAbstractComponent.java | 9
modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangComponent.java | 58 +-
modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptExecutorService.java | 32 +
modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptExecutorServiceImpl.java | 66 +++
modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptResolverFactory.java | 30 +
modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptResolverFactoryImpl.java | 31 +
modules/plugins/alt-lang/alt-lang-plugin/src/test/java/org/rhq/plugins/altlang/AltLangComponentTest.java | 196 ++++++++++
8 files changed, 402 insertions(+), 25 deletions(-)
New commits:
commit c12e2f4fbffb12a4b90bc8497605499e2afb8eab
Author: John Sanda <john(a)localhost.localdomain>
Date: Wed Dec 30 12:17:28 2009 -0500
Refactoring stop() and getAvailability() methods and adding unit tests for them.
diff --git a/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangComponent.java b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangComponent.java
index b74f3e6..4ec45d0 100644
--- a/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangComponent.java
+++ b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangComponent.java
@@ -50,6 +50,16 @@ public class AltLangComponent extends AltLangAbstractComponent
this.scriptExecutor = scriptExecutor;
}
+ /**
+ * This is just a test hook for unit tests. The resourceContext field is initialized in the
+ * {@link #start(org.rhq.core.pluginapi.inventory.ResourceContext)} method.
+ *
+ * @param resourceContext
+ */
+ void setResourceContext(ResourceContext resourceContext) {
+ this.resourceContext = resourceContext;
+ }
+
public void start(ResourceContext resourceContext) throws InvalidPluginConfigurationException, Exception {
this.resourceContext = resourceContext;
@@ -64,34 +74,27 @@ public class AltLangComponent extends AltLangAbstractComponent
}
public void stop() {
- try {
- Action action = createAction("resource_component", "stop");
- ScriptMetadata metadata = resolveScript(resourceContext.getPluginConfiguration(), action);
+ Action action = createAction("resource_component", "stop");
+ ScriptMetadata metadata = resolveScript(resourceContext.getPluginConfiguration(), action);
- ScriptEngine scriptEngine = createScriptEngine(metadata);
- setBindings(scriptEngine, action, null);
+ Map<String, Object> bindings = new HashMap<String, Object>();
+ bindings.put("action", action);
+ bindings.put("resourceContext", resourceContext);
- scriptEngine.eval(loadScript(metadata));
- }
- catch (ScriptException e) {
- throw new RuntimeException(e);
- }
+ scriptExecutor.executeScript(metadata, bindings);
}
public AvailabilityType getAvailability() {
- try {
- Action action = createAction("resource_component", "get_availability");
- ScriptMetadata metadata = resolveScript(resourceContext.getPluginConfiguration(), action);
+ Action action = createAction("resource_component", "get_availability");
+ ScriptMetadata metadata = resolveScript(resourceContext.getPluginConfiguration(), action);
- ScriptEngine scriptEngine = createScriptEngine(metadata);
- setBindings(scriptEngine, action, null);
+ Map<String, Object> bindings = new HashMap<String, Object>();
+ bindings.put("action", action);
+ bindings.put("resourceContext", resourceContext);
- AvailabilityType availabilityType = (AvailabilityType) scriptEngine.eval(loadScript(metadata));
- return availabilityType;
- }
- catch (ScriptException e) {
- throw new RuntimeException(e);
- }
+ AvailabilityType availabilityType = scriptExecutor.executeScript(metadata, bindings);
+
+ return availabilityType;
}
public OperationResult invokeOperation(String name, Configuration parameters)
diff --git a/modules/plugins/alt-lang/alt-lang-plugin/src/test/java/org/rhq/plugins/altlang/AltLangComponentTest.java b/modules/plugins/alt-lang/alt-lang-plugin/src/test/java/org/rhq/plugins/altlang/AltLangComponentTest.java
index 8d9b01c..21bbd71 100644
--- a/modules/plugins/alt-lang/alt-lang-plugin/src/test/java/org/rhq/plugins/altlang/AltLangComponentTest.java
+++ b/modules/plugins/alt-lang/alt-lang-plugin/src/test/java/org/rhq/plugins/altlang/AltLangComponentTest.java
@@ -23,17 +23,20 @@
package org.rhq.plugins.altlang;
+import static org.testng.Assert.*;
import static org.hamcrest.core.AllOf.*;
import static org.hamcrest.collection.IsMapContaining.*;
import org.hamcrest.Matcher;
import org.jmock.Expectations;
import org.rhq.core.domain.configuration.Configuration;
+import org.rhq.core.domain.measurement.AvailabilityType;
import org.rhq.core.domain.resource.Resource;
import org.rhq.core.domain.resource.ResourceType;
import org.rhq.core.pluginapi.inventory.ResourceContext;
import org.rhq.test.JMockTest;
import org.rhq.test.jmock.PropertyMatcher;
+import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.HashMap;
@@ -41,35 +44,58 @@ import java.util.Map;
public class AltLangComponentTest extends JMockTest {
- @Test
- public void testStart() throws Exception {
- AltLangComponent component = new AltLangComponent();
+ AltLangComponent component;
+
+ ScriptResolverFactory scriptResolverFactory;
+
+ ScriptResolver scriptResolver;
+
+ ScriptExecutorService scriptExecutor;
+
+ ResourceContext resourceContext;
+
+ ScriptMetadata metadata;
+
+ Action action;
- final ResourceContext resourceContext = new FakeResourceContext(createResource());
+ @BeforeMethod
+ public void setup() {
+ component = new AltLangComponent();
- final ScriptResolverFactory scriptResolverFactory = context.mock(ScriptResolverFactory.class);
+ scriptResolverFactory = context.mock(ScriptResolverFactory.class);
component.setScriptResolverFactory(scriptResolverFactory);
- final ScriptResolver scriptResolver = context.mock(ScriptResolver.class);
+ scriptResolver = context.mock(ScriptResolver.class);
- final ScriptExecutorService scriptExecutor = context.mock(ScriptExecutorService.class);
+ scriptExecutor = context.mock(ScriptExecutorService.class);
component.setScriptExecutor(scriptExecutor);
- final Action action = new Action("resource_component", "start", resourceContext.getResourceType());
+ resourceContext = new FakeResourceContext(createResource());
+
+ metadata = new ScriptMetadata();
+
+ action = null;
+ }
+
+ @Test
+ public void scriptToStartComponentShouldBeCalledWithCorrectBindings() throws Exception {
+ action = new Action("resource_component", "start", resourceContext.getResourceType());
final Map<String, Object> bindings = new HashMap<String, Object>();
bindings.put("resourceContext", resourceContext);
bindings.put("action", action);
- final ScriptMetadata metadata = new ScriptMetadata();
-
context.checking(new Expectations() {{
allowing(scriptResolverFactory).getScriptResolver(); will(returnValue(scriptResolver));
atLeast(1).of(scriptResolver).resolveScript(with(resourceContext.getPluginConfiguration()),
with(matchingAction(action))); will(returnValue(metadata));
- oneOf(scriptExecutor).executeScript(with(aNonNull(ScriptMetadata.class)),
+ // This expectation verifies that scriptExecutor is passes the correct arguments. First, it checks that
+ // the metadata argument matches and then it checks the bindings argument. The bindings argument is a map
+ // of objects to be bound as script variables. This expectation verifies that the minimum, required
+ // variables are bound.
+ oneOf(scriptExecutor).executeScript(with(matchingMetadata(metadata)),
with(allOf(hasEntry(equal("action"), matchingAction(action)),
hasEntry(equal("resourceContext"), same(resourceContext)))));
}});
@@ -77,10 +103,78 @@ public class AltLangComponentTest extends JMockTest {
component.start(resourceContext);
}
+ @Test
+ public void scriptToStopComponentShouldBeCalledWithCorrectBindings() throws Exception {
+ component.setResourceContext(resourceContext);
+
+ action = new Action("resource_component", "stop", resourceContext.getResourceType());
+
+ final Map<String, Object> bindings = new HashMap<String, Object>();
+ bindings.put("resourceContext", resourceContext);
+ bindings.put("action", action);
+
+ context.checking(new Expectations() {{
+ allowing(scriptResolverFactory).getScriptResolver(); will(returnValue(scriptResolver));
+
+ atLeast(1).of(scriptResolver).resolveScript(with(resourceContext.getPluginConfiguration()),
+ with(matchingAction(action))); will(returnValue(metadata));
+
+ // This expectation verifies that scriptExecutor is passes the correct arguments. First, it checks that
+ // the metadata argument matches and then it checks the bindings argument. The bindings argument is a map
+ // of objects to be bound as script variables. This expectation verifies that the minimum, required
+ // variables are bound.
+ oneOf(scriptExecutor).executeScript(with(matchingMetadata(metadata)),
+ with(allOf(hasEntry(equal("action"), matchingAction(action)),
+ hasEntry(equal("resourceContext"), same(resourceContext)))));
+ }});
+
+ component.stop();
+ }
+
+ @Test
+ public void scriptToCheckAvailabilityShouldBeCalledWithCorrectBindings() throws Exception {
+ component.setResourceContext(resourceContext);
+
+ action = new Action("resource_component", "get_availability", resourceContext.getResourceType());
+
+ final Map<String, Object> bindings = new HashMap<String, Object>();
+ bindings.put("resourceContext", resourceContext);
+ bindings.put("action", action);
+
+ context.checking(new Expectations() {{
+ allowing(scriptResolverFactory).getScriptResolver(); will(returnValue(scriptResolver));
+
+ atLeast(1).of(scriptResolver).resolveScript(with(resourceContext.getPluginConfiguration()),
+ with(matchingAction(action))); will(returnValue(metadata));
+
+ // This expectation verifies that scriptExecutor is passes the correct arguments. First, it checks that
+ // the metadata argument matches and then it checks the bindings argument. The bindings argument is a map
+ // of objects to be bound as script variables. This expectation verifies that the minimum, required
+ // variables are bound.
+ oneOf(scriptExecutor).executeScript(with(matchingMetadata(metadata)),
+ with(allOf(hasEntry(equal("action"), matchingAction(action)),
+ hasEntry(equal("resourceContext"), same(resourceContext)))));
+ will(returnValue(AvailabilityType.UP));
+ }});
+
+ AvailabilityType expectedAvailability = AvailabilityType.UP;
+ AvailabilityType actualAvailability = component.getAvailability();
+
+ assertEquals(
+ actualAvailability,
+ expectedAvailability,
+ "Expected to get back the availability returned from the script"
+ );
+ }
+
public static Matcher<Action> matchingAction(Action expected) {
return new PropertyMatcher<Action>(expected);
}
+ public static Matcher<ScriptMetadata> matchingMetadata(ScriptMetadata expected) {
+ return new PropertyMatcher<ScriptMetadata>(expected);
+ }
+
private Resource createResource() {
Resource resource = new Resource();
resource.setPluginConfiguration(new Configuration());
commit 8562eace9eca73995ae8970e5e84544590ef0d0a
Author: John Sanda <john(a)localhost.localdomain>
Date: Wed Dec 30 10:19:59 2009 -0500
Starting to refactor plugin to add unit tests (which should have been done from the start)
With this commit, I have a unit test in place for AltLangComponent.start(). This is the
initial commit for ScriptExecutorService and ScriptExecutorServiceImpl. ScriptExecutorService
handles the work of executing scripts. It will later also do some validation and can
also provide hooks for pre- and post-processing of scripts. This is also the initial
commit for ScriptResolverFactory. Currently there is only a single ScriptResolver
implementation, but there will be more as the plugin evolves and the factory will encapsulate
the logic of determining which resolver to use.
diff --git a/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/Action.java b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/Action.java
index 0e01aef..0e52d02 100644
--- a/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/Action.java
+++ b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/Action.java
@@ -65,4 +65,9 @@ public class Action {
public void setName(String name) {
this.name = name;
}
+
+ @Override
+ public String toString() {
+ return "Action[type=" + type + ", name=" + name + ", resourceType=" + resourceType + "]";
+ }
}
diff --git a/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangAbstractComponent.java b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangAbstractComponent.java
index c4ae18b..917be7c 100644
--- a/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangAbstractComponent.java
+++ b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangAbstractComponent.java
@@ -31,8 +31,15 @@ import java.io.Reader;
public abstract class AltLangAbstractComponent {
+ private ScriptResolverFactory scriptResolverFactory = new ScriptResolverFactoryImpl();
+
+ public void setScriptResolverFactory(ScriptResolverFactory scriptResolverFactory) {
+ this.scriptResolverFactory = scriptResolverFactory;
+ }
+
protected ScriptMetadata resolveScript(Configuration pluginConfiguration, Action action) {
- ScriptResolver resolver = new ScriptPerActionTypeResolver();
+// ScriptResolver resolver = new ScriptPerActionTypeResolver();
+ ScriptResolver resolver = scriptResolverFactory.getScriptResolver();
ScriptMetadata scriptMetadata = resolver.resolveScript(pluginConfiguration, action);
return scriptMetadata;
diff --git a/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangComponent.java b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangComponent.java
index 3cf8cb0..b74f3e6 100644
--- a/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangComponent.java
+++ b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/AltLangComponent.java
@@ -44,16 +44,23 @@ public class AltLangComponent extends AltLangAbstractComponent
private ResourceContext resourceContext;
+ private ScriptExecutorService scriptExecutor = new ScriptExecutorServiceImpl();
+
+ void setScriptExecutor(ScriptExecutorService scriptExecutor) {
+ this.scriptExecutor = scriptExecutor;
+ }
+
public void start(ResourceContext resourceContext) throws InvalidPluginConfigurationException, Exception {
this.resourceContext = resourceContext;
Action action = createAction("resource_component", "start");
ScriptMetadata metadata = resolveScript(resourceContext.getPluginConfiguration(), action);
- ScriptEngine scriptEngine = createScriptEngine(metadata);
- setBindings(scriptEngine, action, null);
+ Map<String, Object> bindings = new HashMap<String, Object>();
+ bindings.put("action", action);
+ bindings.put("resourceContext", resourceContext);
- scriptEngine.eval(loadScript(metadata));
+ scriptExecutor.executeScript(metadata, bindings);
}
public void stop() {
diff --git a/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptExecutorService.java b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptExecutorService.java
new file mode 100644
index 0000000..ceef5ef
--- /dev/null
+++ b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptExecutorService.java
@@ -0,0 +1,32 @@
+/*
+ * 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.plugins.altlang;
+
+import java.util.Map;
+
+public interface ScriptExecutorService {
+
+ <T> T executeScript(ScriptMetadata scriptMetadata, Map<String, ?> scriptBindings);
+
+}
diff --git a/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptExecutorServiceImpl.java b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptExecutorServiceImpl.java
new file mode 100644
index 0000000..ec16d59
--- /dev/null
+++ b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptExecutorServiceImpl.java
@@ -0,0 +1,66 @@
+/*
+ * 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.plugins.altlang;
+
+import javax.script.Bindings;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.Map;
+
+public class ScriptExecutorServiceImpl implements ScriptExecutorService {
+
+ public <T> T executeScript(ScriptMetadata scriptMetadata, Map<String, ?> scriptBindings) {
+ ScriptEngineManager scriptEngineMrg = new ScriptEngineManager();
+ ScriptEngine scriptEngine = scriptEngineMrg.getEngineByName(scriptMetadata.getLang());
+ Bindings bindings = scriptEngine.createBindings();
+
+ setBindings(scriptBindings, scriptEngine, bindings);
+ Reader scriptReader = loadScript(scriptMetadata);
+
+ try {
+ return (T) scriptEngine.eval(scriptReader);
+ }
+ catch (ScriptException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private Reader loadScript(ScriptMetadata metadata) {
+ InputStream stream = getClass().getResourceAsStream(metadata.getScriptPath());
+ return new InputStreamReader(stream);
+ }
+
+ private <T> void setBindings(Map<String, ?> scriptBindings, ScriptEngine scriptEngine, Bindings bindings) {
+ if (scriptBindings != null) {
+ bindings.putAll(scriptBindings);
+ }
+ scriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
+ }
+
+}
diff --git a/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptResolverFactory.java b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptResolverFactory.java
new file mode 100644
index 0000000..c3b41de
--- /dev/null
+++ b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptResolverFactory.java
@@ -0,0 +1,30 @@
+/*
+ * 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.plugins.altlang;
+
+public interface ScriptResolverFactory {
+
+ ScriptResolver getScriptResolver();
+
+}
diff --git a/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptResolverFactoryImpl.java b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptResolverFactoryImpl.java
new file mode 100644
index 0000000..c13423e
--- /dev/null
+++ b/modules/plugins/alt-lang/alt-lang-plugin/src/main/java/org/rhq/plugins/altlang/ScriptResolverFactoryImpl.java
@@ -0,0 +1,31 @@
+/*
+ * 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.plugins.altlang;
+
+public class ScriptResolverFactoryImpl implements ScriptResolverFactory {
+
+ public ScriptResolver getScriptResolver() {
+ return new ScriptPerActionTypeResolver();
+ }
+}
diff --git a/modules/plugins/alt-lang/alt-lang-plugin/src/test/java/org/rhq/plugins/altlang/AltLangComponentTest.java b/modules/plugins/alt-lang/alt-lang-plugin/src/test/java/org/rhq/plugins/altlang/AltLangComponentTest.java
new file mode 100644
index 0000000..8d9b01c
--- /dev/null
+++ b/modules/plugins/alt-lang/alt-lang-plugin/src/test/java/org/rhq/plugins/altlang/AltLangComponentTest.java
@@ -0,0 +1,102 @@
+/*
+ * 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.plugins.altlang;
+
+import static org.hamcrest.core.AllOf.*;
+import static org.hamcrest.collection.IsMapContaining.*;
+
+import org.hamcrest.Matcher;
+import org.jmock.Expectations;
+import org.rhq.core.domain.configuration.Configuration;
+import org.rhq.core.domain.resource.Resource;
+import org.rhq.core.domain.resource.ResourceType;
+import org.rhq.core.pluginapi.inventory.ResourceContext;
+import org.rhq.test.JMockTest;
+import org.rhq.test.jmock.PropertyMatcher;
+import org.testng.annotations.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class AltLangComponentTest extends JMockTest {
+
+ @Test
+ public void testStart() throws Exception {
+ AltLangComponent component = new AltLangComponent();
+
+ final ResourceContext resourceContext = new FakeResourceContext(createResource());
+
+ final ScriptResolverFactory scriptResolverFactory = context.mock(ScriptResolverFactory.class);
+ component.setScriptResolverFactory(scriptResolverFactory);
+
+ final ScriptResolver scriptResolver = context.mock(ScriptResolver.class);
+
+ final ScriptExecutorService scriptExecutor = context.mock(ScriptExecutorService.class);
+ component.setScriptExecutor(scriptExecutor);
+
+ final Action action = new Action("resource_component", "start", resourceContext.getResourceType());
+
+ final Map<String, Object> bindings = new HashMap<String, Object>();
+ bindings.put("resourceContext", resourceContext);
+ bindings.put("action", action);
+
+ final ScriptMetadata metadata = new ScriptMetadata();
+
+ context.checking(new Expectations() {{
+ allowing(scriptResolverFactory).getScriptResolver(); will(returnValue(scriptResolver));
+
+ atLeast(1).of(scriptResolver).resolveScript(with(resourceContext.getPluginConfiguration()),
+ with(matchingAction(action))); will(returnValue(metadata));
+
+ oneOf(scriptExecutor).executeScript(with(aNonNull(ScriptMetadata.class)),
+ with(allOf(hasEntry(equal("action"), matchingAction(action)),
+ hasEntry(equal("resourceContext"), same(resourceContext)))));
+ }});
+
+ component.start(resourceContext);
+ }
+
+ public static Matcher<Action> matchingAction(Action expected) {
+ return new PropertyMatcher<Action>(expected);
+ }
+
+ private Resource createResource() {
+ Resource resource = new Resource();
+ resource.setPluginConfiguration(new Configuration());
+ resource.setResourceType(new ResourceType());
+
+ return resource;
+ }
+
+ static class FakeResourceContext extends ResourceContext {
+ private Configuration pluginConfiguration;
+ private ResourceType resourceType;
+
+ public FakeResourceContext(Resource resource) {
+ super(resource, null, null, null, null, null, null, null, null, null, null, null);
+ }
+
+ }
+
+}