Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.virgo.kernel.shutdown/src')
-rw-r--r--org.eclipse.virgo.kernel.shutdown/src/main/java/org/eclipse/virgo/kernel/shutdown/ShutdownClient.java114
-rw-r--r--org.eclipse.virgo.kernel.shutdown/src/main/java/org/eclipse/virgo/kernel/shutdown/ShutdownCommand.java82
-rw-r--r--org.eclipse.virgo.kernel.shutdown/src/main/java/org/eclipse/virgo/kernel/shutdown/ShutdownCommandParser.java80
-rw-r--r--org.eclipse.virgo.kernel.shutdown/src/main/resources/.gitignore0
-rw-r--r--org.eclipse.virgo.kernel.shutdown/src/test/java/.gitignore0
-rw-r--r--org.eclipse.virgo.kernel.shutdown/src/test/java/org/eclipse/virgo/kernel/shutdown/Shutdown.java22
-rw-r--r--org.eclipse.virgo.kernel.shutdown/src/test/java/org/eclipse/virgo/kernel/shutdown/ShutdownClientTests.java309
-rw-r--r--org.eclipse.virgo.kernel.shutdown/src/test/java/org/eclipse/virgo/kernel/shutdown/ShutdownCommandParserTests.java107
-rw-r--r--org.eclipse.virgo.kernel.shutdown/src/test/resources/.gitignore0
9 files changed, 714 insertions, 0 deletions
diff --git a/org.eclipse.virgo.kernel.shutdown/src/main/java/org/eclipse/virgo/kernel/shutdown/ShutdownClient.java b/org.eclipse.virgo.kernel.shutdown/src/main/java/org/eclipse/virgo/kernel/shutdown/ShutdownClient.java
new file mode 100644
index 00000000..bea6c5d3
--- /dev/null
+++ b/org.eclipse.virgo.kernel.shutdown/src/main/java/org/eclipse/virgo/kernel/shutdown/ShutdownClient.java
@@ -0,0 +1,114 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2010 VMware Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VMware Inc. - initial contribution
+ *******************************************************************************/
+
+package org.eclipse.virgo.kernel.shutdown;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import javax.management.MBeanServerConnection;
+import javax.management.ObjectName;
+import javax.management.remote.JMXConnector;
+import javax.management.remote.JMXConnectorFactory;
+import javax.management.remote.JMXServiceURL;
+import javax.naming.ServiceUnavailableException;
+
+/**
+ * A stand-alone client for shutdown of the kernel.
+ * <p />
+ *
+ * <strong>Concurrent Semantics</strong> <br />
+ * Thread-safe.
+ *
+ */
+public class ShutdownClient {
+
+ private static final String OPERATION_IMMEDIATE_SHUTDOWN = "immediateShutdown";
+
+ private static final String OPERATION_SHUTDOWN = "shutdown";
+
+ private static final String PROPERTY_JMX_REMOTE_CREDENTIALS = "jmx.remote.credentials";
+
+ private static final String JMX_SERVICE_URL_TEMPLATE = "service:jmx:rmi:///jndi/rmi://127.0.0.1:%d/jmxrmi";
+
+ public static void main(String[] args) {
+ new ShutdownClient().performShutdown(args);
+ }
+
+ final void performShutdown(String... args) {
+ ShutdownCommand command = ShutdownCommandParser.parse(args);
+
+ if (command != null) {
+ doShutdown(command);
+ } else {
+ displayUsageAndExit();
+ }
+ }
+
+ protected final void doShutdown(ShutdownCommand command) {
+ try {
+ JMXServiceURL jmxServiceURL = new JMXServiceURL(String.format(JMX_SERVICE_URL_TEMPLATE, command.getPort()));
+
+ Map<String, Object> jmxEnvironment = new HashMap<String, Object>();
+ jmxEnvironment.put(PROPERTY_JMX_REMOTE_CREDENTIALS, new String[] { command.getUsername(), command.getPassword() });
+
+ JMXConnector connector = JMXConnectorFactory.connect(jmxServiceURL, jmxEnvironment);
+ MBeanServerConnection connection = connector.getMBeanServerConnection();
+
+ ObjectName shutdownMBeanName = new ObjectName(command.getDomain(), "type", "Shutdown");
+
+ if (command.isImmediate()) {
+ connection.invoke(shutdownMBeanName, OPERATION_IMMEDIATE_SHUTDOWN, null, null);
+ } else {
+ connection.invoke(shutdownMBeanName, OPERATION_SHUTDOWN, null, null);
+ }
+
+ connector.close();
+ } catch (IOException ioe) {
+ Throwable cause = ioe.getCause();
+ if (cause instanceof ServiceUnavailableException) {
+ reportServerUnreachable();
+ } else {
+ reportShutdownFailure(ioe);
+ }
+ } catch (Exception e) {
+ reportShutdownFailure(e);
+ }
+ }
+
+ protected void reportServerUnreachable() {
+ System.out.println("The Server could not be reached, it may already be stopped.");
+ }
+
+ protected void reportShutdownFailure(Exception failure) {
+ failure.printStackTrace();
+ }
+
+ private void displayUsageAndExit() {
+ System.out.println("Usage: shutdown [-options]");
+ System.out.println("Available options:");
+ System.out.println(" -jmxport nnnn Specifies the management port of the kernel");
+ System.out.println(" instance which is to be shutdown.");
+ System.out.println(" -immediate Specifies that the kernel should be shutdown");
+ System.out.println(" immediately.");
+ System.out.println(" -username Specifies the username to use for the");
+ System.out.println(" connection to the kernel.");
+ System.out.println(" -password Specifies the password to use for the");
+ System.out.println(" connection to the kernel.");
+ System.out.println(" -domain Specifies the JMX management domain for the");
+ System.out.println(" kernel.");
+ exit();
+ }
+
+ protected void exit() {
+ System.exit(1);
+ }
+}
diff --git a/org.eclipse.virgo.kernel.shutdown/src/main/java/org/eclipse/virgo/kernel/shutdown/ShutdownCommand.java b/org.eclipse.virgo.kernel.shutdown/src/main/java/org/eclipse/virgo/kernel/shutdown/ShutdownCommand.java
new file mode 100644
index 00000000..fc7a47ef
--- /dev/null
+++ b/org.eclipse.virgo.kernel.shutdown/src/main/java/org/eclipse/virgo/kernel/shutdown/ShutdownCommand.java
@@ -0,0 +1,82 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2010 VMware Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VMware Inc. - initial contribution
+ *******************************************************************************/
+
+package org.eclipse.virgo.kernel.shutdown;
+
+/**
+ * A <code>ShutdownCommand</code> is created by a {@link ShutdownCommandParser} to encapsulate
+ * the parsed shutdown configuration.
+ * <p />
+ *
+ * <strong>Concurrent Semantics</strong> <br />
+ * <strong>Not</strong> thread-safe.
+ *
+ */
+final class ShutdownCommand {
+
+ private static final String DEFAULT_PASSWORD = "springsource";
+
+ private static final String DEFAULT_USERNAME = "admin";
+
+ private static final String DEFAULT_DOMAIN = "org.eclipse.virgo.kernel";
+
+ private static final int DEFAULT_PORT = 9875;
+
+ private String password = DEFAULT_PASSWORD;
+
+ private String username = DEFAULT_USERNAME;
+
+ private String domain = DEFAULT_DOMAIN;
+
+ private int port = DEFAULT_PORT;
+
+ private boolean immediate = false;
+
+ String getPassword() {
+ return password;
+ }
+
+ void setPassword(String password) {
+ this.password = password;
+ }
+
+ String getUsername() {
+ return username;
+ }
+
+ void setUsername(String username) {
+ this.username = username;
+ }
+
+ String getDomain() {
+ return domain;
+ }
+
+ void setDomain(String domain) {
+ this.domain = domain;
+ }
+
+ int getPort() {
+ return port;
+ }
+
+ void setPort(int port) {
+ this.port = port;
+ }
+
+ boolean isImmediate() {
+ return immediate;
+ }
+
+ void setImmediate(boolean immediate) {
+ this.immediate = immediate;
+ }
+}
diff --git a/org.eclipse.virgo.kernel.shutdown/src/main/java/org/eclipse/virgo/kernel/shutdown/ShutdownCommandParser.java b/org.eclipse.virgo.kernel.shutdown/src/main/java/org/eclipse/virgo/kernel/shutdown/ShutdownCommandParser.java
new file mode 100644
index 00000000..713f3df6
--- /dev/null
+++ b/org.eclipse.virgo.kernel.shutdown/src/main/java/org/eclipse/virgo/kernel/shutdown/ShutdownCommandParser.java
@@ -0,0 +1,80 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2010 VMware Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VMware Inc. - initial contribution
+ *******************************************************************************/
+
+package org.eclipse.virgo.kernel.shutdown;
+
+/**
+ * A <code>ShutdownCommandParser</code> is used to parse a series of arguments
+ * into a {@link ShutdownCommand}.
+ *
+ * <p />
+ *
+ * <strong>Concurrent Semantics</strong> <br />
+ * Thread-safe.
+ *
+ */
+final class ShutdownCommandParser {
+
+ private static final String OPTION_DOMAIN = "-domain";
+
+ private static final String OPTION_IMMEDIATE = "-immediate";
+
+ private static final String OPTION_JMXPORT = "-jmxport";
+
+ private static final String OPTION_PASSWORD = "-password";
+
+ private static final String OPTION_USERNAME = "-username";
+
+ static ShutdownCommand parse(String... args) {
+
+ ShutdownCommand command = new ShutdownCommand();
+
+ if (args.length > 0) {
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals(OPTION_USERNAME)) {
+ if (i < args.length - 1) {
+ command.setUsername(args[++i]);
+ } else {
+ return null;
+ }
+ } else if (args[i].equals(OPTION_PASSWORD)) {
+ if (i < args.length - 1) {
+ command.setPassword(args[++i]);
+ } else {
+ return null;
+ }
+ } else if (args[i].equals(OPTION_JMXPORT)) {
+ if (i < args.length - 1) {
+ try {
+ command.setPort(Integer.parseInt(args[++i]));
+ } catch (NumberFormatException nfe) {
+ return null;
+ }
+ } else {
+ return null;
+ }
+ } else if (args[i].equals(OPTION_IMMEDIATE)) {
+ command.setImmediate(true);
+ } else if (args[i].equals(OPTION_DOMAIN)) {
+ if (i < args.length - 1) {
+ command.setDomain(args[++i]);
+ } else {
+ return null;
+ }
+ } else {
+ return null;
+ }
+ }
+ }
+
+ return command;
+ }
+}
diff --git a/org.eclipse.virgo.kernel.shutdown/src/main/resources/.gitignore b/org.eclipse.virgo.kernel.shutdown/src/main/resources/.gitignore
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/org.eclipse.virgo.kernel.shutdown/src/main/resources/.gitignore
diff --git a/org.eclipse.virgo.kernel.shutdown/src/test/java/.gitignore b/org.eclipse.virgo.kernel.shutdown/src/test/java/.gitignore
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/org.eclipse.virgo.kernel.shutdown/src/test/java/.gitignore
diff --git a/org.eclipse.virgo.kernel.shutdown/src/test/java/org/eclipse/virgo/kernel/shutdown/Shutdown.java b/org.eclipse.virgo.kernel.shutdown/src/test/java/org/eclipse/virgo/kernel/shutdown/Shutdown.java
new file mode 100644
index 00000000..70ed946e
--- /dev/null
+++ b/org.eclipse.virgo.kernel.shutdown/src/test/java/org/eclipse/virgo/kernel/shutdown/Shutdown.java
@@ -0,0 +1,22 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2010 VMware Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VMware Inc. - initial contribution
+ *******************************************************************************/
+
+package org.eclipse.virgo.kernel.shutdown;
+
+import javax.management.MXBean;
+
+@MXBean
+public interface Shutdown {
+
+ void shutdown();
+
+ void immediateShutdown();
+}
diff --git a/org.eclipse.virgo.kernel.shutdown/src/test/java/org/eclipse/virgo/kernel/shutdown/ShutdownClientTests.java b/org.eclipse.virgo.kernel.shutdown/src/test/java/org/eclipse/virgo/kernel/shutdown/ShutdownClientTests.java
new file mode 100644
index 00000000..de9d42db
--- /dev/null
+++ b/org.eclipse.virgo.kernel.shutdown/src/test/java/org/eclipse/virgo/kernel/shutdown/ShutdownClientTests.java
@@ -0,0 +1,309 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2010 VMware Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VMware Inc. - initial contribution
+ *******************************************************************************/
+
+package org.eclipse.virgo.kernel.shutdown;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.lang.management.ManagementFactory;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.rmi.RemoteException;
+import java.rmi.registry.LocateRegistry;
+import java.rmi.server.RMIClientSocketFactory;
+import java.rmi.server.RMIServerSocketFactory;
+import java.security.Permission;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectInstance;
+import javax.management.ObjectName;
+import javax.management.remote.JMXConnectorServer;
+import javax.management.remote.JMXConnectorServerFactory;
+import javax.management.remote.JMXServiceURL;
+import javax.management.remote.rmi.RMIConnectorServer;
+
+import org.eclipse.virgo.kernel.shutdown.ShutdownClient;
+import org.eclipse.virgo.kernel.shutdown.ShutdownCommand;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class ShutdownClientTests {
+
+
+ private static SecurityManager securityManager = System.getSecurityManager();
+
+ @BeforeClass
+ public static void installSecurityManager() {
+ System.setSecurityManager(new UnitTestSecurityManager());
+ }
+
+ @AfterClass
+ public static void reinstateSecurityManager() {
+ System.setSecurityManager(securityManager);
+ }
+
+ @Test(expected=RuntimeException.class)
+ public void malformedCommandCausesExit() {
+ UnitTestShutdownClient client = new UnitTestShutdownClient();
+ client.performShutdown("-alpha");
+ }
+
+ @Test
+ public void unreachableMBeanServerReportsServerUnreachable() throws Exception {
+ UnitTestShutdownClient client = new UnitTestShutdownClient();
+
+ ShutdownCommand command = new ShutdownCommand();
+ command.setPort(9999);
+
+ JMXConnectorServer server = bootstrapMBeanServer(9875);
+
+ try {
+ client.doShutdown(command);
+
+ assertTrue(client.serverUnreachable);
+ assertFalse(client.exited);
+ assertFalse(client.shutdownFailureReported);
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ public void missingShutdownMBeanReportsShutdownFailure() throws Exception {
+ UnitTestShutdownClient client = new UnitTestShutdownClient();
+
+ ShutdownCommand command = new ShutdownCommand();
+
+ JMXConnectorServer server = bootstrapMBeanServer(9875);
+
+ try {
+ client.doShutdown(command);
+
+ assertFalse(client.serverUnreachable);
+ assertFalse(client.exited);
+ assertTrue(client.shutdownFailureReported);
+ } finally {
+ server.stop();
+ }
+ }
+
+ @Test
+ public void immediateShutdownInvokesImmediateShutdownOperation() throws Exception {
+ UnitTestShutdownClient client = new UnitTestShutdownClient();
+
+ ShutdownCommand command = new ShutdownCommand();
+ command.setImmediate(true);
+
+ JMXConnectorServer server = bootstrapMBeanServer(9875);
+
+ UnitTestShutdown shutdown = new UnitTestShutdown();
+
+ ObjectInstance shutdownMBean = registerShutdownMBean(shutdown, "org.eclipse.virgo.kernel");
+
+ try {
+ client.doShutdown(command);
+
+ assertFalse(client.serverUnreachable);
+ assertFalse(client.exited);
+ assertFalse(client.shutdownFailureReported);
+
+ assertTrue(shutdown.immediateShutdown);
+ assertFalse(shutdown.shutdown);
+ } finally {
+ server.stop();
+ ManagementFactory.getPlatformMBeanServer().unregisterMBean(shutdownMBean.getObjectName());
+ }
+ }
+
+ @Test
+ public void shutdownInvokesShutdownOperation() throws Exception {
+ UnitTestShutdownClient client = new UnitTestShutdownClient();
+
+ ShutdownCommand command = new ShutdownCommand();
+
+ JMXConnectorServer server = bootstrapMBeanServer(9875);
+
+ UnitTestShutdown shutdown = new UnitTestShutdown();
+
+ ObjectInstance shutdownMBean = registerShutdownMBean(shutdown, "org.eclipse.virgo.kernel");
+
+ try {
+ client.doShutdown(command);
+
+ assertFalse(client.serverUnreachable);
+ assertFalse(client.exited);
+ assertFalse(client.shutdownFailureReported);
+
+ assertFalse(shutdown.immediateShutdown);
+ assertTrue(shutdown.shutdown);
+ } finally {
+ server.stop();
+ ManagementFactory.getPlatformMBeanServer().unregisterMBean(shutdownMBean.getObjectName());
+ }
+ }
+
+ @Test
+ public void clientUsesDomainSpecifiedInCommand() throws Exception {
+ UnitTestShutdownClient client = new UnitTestShutdownClient();
+
+ ShutdownCommand command = new ShutdownCommand();
+ command.setDomain("the.domain");
+
+ JMXConnectorServer server = bootstrapMBeanServer(9875);
+
+ UnitTestShutdown shutdown = new UnitTestShutdown();
+
+ ObjectInstance shutdownMBean = registerShutdownMBean(shutdown, "the.domain");
+
+ try {
+ client.doShutdown(command);
+
+ assertFalse(client.serverUnreachable);
+ assertFalse(client.exited);
+ assertFalse(client.shutdownFailureReported);
+ } finally {
+ server.stop();
+ ManagementFactory.getPlatformMBeanServer().unregisterMBean(shutdownMBean.getObjectName());
+ }
+ }
+
+ @Test
+ public void clientUsesPortSpecifiedInCommand() throws Exception {
+ UnitTestShutdownClient client = new UnitTestShutdownClient();
+
+ ShutdownCommand command = new ShutdownCommand();
+ command.setPort(9999);
+
+ UnitTestShutdown shutdown = new UnitTestShutdown();
+
+ ObjectInstance shutdownMBean = registerShutdownMBean(shutdown, "the.domain");
+
+ client = new UnitTestShutdownClient();
+
+ JMXConnectorServer server = bootstrapMBeanServer(9999);
+
+ try {
+ assertFalse(client.serverUnreachable);
+ assertFalse(client.exited);
+ assertFalse(client.shutdownFailureReported);
+ } finally {
+ server.stop();
+ ManagementFactory.getPlatformMBeanServer().unregisterMBean(shutdownMBean.getObjectName());
+ }
+ }
+
+ private JMXConnectorServer bootstrapMBeanServer(int port) throws Exception {
+
+ createRegistry(port);
+
+ MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+
+ Map<String,Object> env = new HashMap<String,Object>();
+
+ env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, new UnitTestClientSocketFactory());
+
+ env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, new UnitTestServerSocketFactory());
+
+ JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" + port + "/jmxrmi");
+ JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
+ server.start();
+
+ return server;
+ }
+
+ private void createRegistry(int port) {
+ try {
+ LocateRegistry.createRegistry(port);
+ } catch (RemoteException _) {
+
+ }
+ }
+
+ private static final class UnitTestClientSocketFactory implements RMIClientSocketFactory, Serializable {
+
+ private static final long serialVersionUID = 4445230934659082311L;
+
+ public Socket createSocket(String host, int port) throws IOException {
+ return new Socket(host, port);
+ }
+ }
+
+ private static final class UnitTestServerSocketFactory implements RMIServerSocketFactory, Serializable {
+
+ private static final long serialVersionUID = 5261406192116261111L;
+
+ public ServerSocket createServerSocket(int port) throws IOException {
+ return new ServerSocket(port);
+ }
+ }
+
+ private ObjectInstance registerShutdownMBean(Shutdown shutdown, String domain) throws Exception {
+ MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+ return server.registerMBean(shutdown, new ObjectName(domain + ":type=Shutdown"));
+ }
+
+ private final class UnitTestShutdownClient extends ShutdownClient {
+
+ private boolean serverUnreachable = false;
+
+ private boolean exited = false;
+
+ private boolean shutdownFailureReported = false;
+
+ @Override
+ protected void exit() {
+ throw new RuntimeException();
+ }
+
+ @Override
+ protected void reportServerUnreachable() {
+ super.reportServerUnreachable();
+ this.serverUnreachable = true;
+ }
+
+ @Override
+ protected void reportShutdownFailure(Exception failure) {
+ super.reportShutdownFailure(failure);
+ this.shutdownFailureReported = true;
+ }
+ }
+
+ private final class UnitTestShutdown implements Shutdown {
+
+ private boolean immediateShutdown;
+
+ private boolean shutdown;
+
+ public void immediateShutdown() {
+ this.immediateShutdown = true;
+ }
+
+ public void shutdown() {
+ this.shutdown = true;
+ }
+ }
+
+ private static final class UnitTestSecurityManager extends SecurityManager {
+ @Override
+ public void checkPermission(Permission perm, Object context) {
+ }
+
+ @Override
+ public void checkPermission(Permission perm) {
+ }
+ }
+}
diff --git a/org.eclipse.virgo.kernel.shutdown/src/test/java/org/eclipse/virgo/kernel/shutdown/ShutdownCommandParserTests.java b/org.eclipse.virgo.kernel.shutdown/src/test/java/org/eclipse/virgo/kernel/shutdown/ShutdownCommandParserTests.java
new file mode 100644
index 00000000..1805e7e5
--- /dev/null
+++ b/org.eclipse.virgo.kernel.shutdown/src/test/java/org/eclipse/virgo/kernel/shutdown/ShutdownCommandParserTests.java
@@ -0,0 +1,107 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2010 VMware Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VMware Inc. - initial contribution
+ *******************************************************************************/
+
+package org.eclipse.virgo.kernel.shutdown;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.eclipse.virgo.kernel.shutdown.ShutdownCommand;
+import org.eclipse.virgo.kernel.shutdown.ShutdownCommandParser;
+import org.junit.Test;
+
+public class ShutdownCommandParserTests {
+
+ @Test
+ public void nullReturnedForUnrecognisedArgument() {
+ assertNull(ShutdownCommandParser.parse("-foo"));
+ }
+
+ @Test
+ public void nullReturnedForMissingUsername() {
+ assertNull(ShutdownCommandParser.parse("-username"));
+ }
+
+ @Test
+ public void nullReturnedForMissingPassword() {
+ assertNull(ShutdownCommandParser.parse("-password"));
+ }
+
+ @Test
+ public void nullReturnedForJmxPort() {
+ assertNull(ShutdownCommandParser.parse("-jmxport"));
+ }
+
+ @Test
+ public void nullReturnedForMissingDomain() {
+ assertNull(ShutdownCommandParser.parse("-domain"));
+ }
+
+ @Test
+ public void nullReturnedForNonIntegerPort() {
+ assertNull(ShutdownCommandParser.parse("-jmxport", "1alpha345"));
+ }
+
+ @Test
+ public void defaultValuesWhenNoOptionsAreSpecified() {
+ ShutdownCommand command = ShutdownCommandParser.parse();
+ assertNotNull(command);
+
+ assertEquals("org.eclipse.virgo.kernel", command.getDomain());
+ assertEquals("springsource", command.getPassword());
+ assertEquals(9875, command.getPort());
+ assertEquals("admin", command.getUsername());
+ assertFalse(command.isImmediate());
+ }
+
+ @Test
+ public void specificUsername() {
+ ShutdownCommand command = ShutdownCommandParser.parse("-username", "user");
+ assertNotNull(command);
+
+ assertEquals("user", command.getUsername());
+ }
+
+ @Test
+ public void specificPassword() {
+ ShutdownCommand command = ShutdownCommandParser.parse("-password", "secret");
+ assertNotNull(command);
+
+ assertEquals("secret", command.getPassword());
+ }
+
+ @Test
+ public void specificDomain() {
+ ShutdownCommand command = ShutdownCommandParser.parse("-domain", "the.jmx.domain");
+ assertNotNull(command);
+
+ assertEquals("the.jmx.domain", command.getDomain());
+ }
+
+ @Test
+ public void specificPort() {
+ ShutdownCommand command = ShutdownCommandParser.parse("-jmxport", "1234");
+ assertNotNull(command);
+
+ assertEquals(1234, command.getPort());
+ }
+
+ @Test
+ public void immediate() {
+ ShutdownCommand command = ShutdownCommandParser.parse("-immediate");
+ assertNotNull(command);
+
+ assertTrue(command.isImmediate());
+ }
+}
diff --git a/org.eclipse.virgo.kernel.shutdown/src/test/resources/.gitignore b/org.eclipse.virgo.kernel.shutdown/src/test/resources/.gitignore
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/org.eclipse.virgo.kernel.shutdown/src/test/resources/.gitignore

Back to the top