You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2016/05/02 18:58:42 UTC
[07/12] incubator-geode git commit: Fixing tests
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/cd02af01/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherRemoteJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherRemoteJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherRemoteJUnitTest.java
deleted file mode 100755
index 7eabba6..0000000
--- a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherRemoteJUnitTest.java
+++ /dev/null
@@ -1,1152 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.gemstone.gemfire.distributed;
-
-import static org.junit.Assert.*;
-import static org.hamcrest.CoreMatchers.*;
-
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.PrintStream;
-import java.lang.management.ManagementFactory;
-import java.net.InetAddress;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.atomic.AtomicBoolean;
-
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-import com.gemstone.gemfire.distributed.AbstractLauncher.Status;
-import com.gemstone.gemfire.distributed.LocatorLauncher.Builder;
-import com.gemstone.gemfire.distributed.LocatorLauncher.LocatorState;
-import com.gemstone.gemfire.internal.AvailablePort;
-import com.gemstone.gemfire.internal.DistributionLocator;
-import com.gemstone.gemfire.internal.GemFireVersion;
-import com.gemstone.gemfire.internal.SocketCreator;
-import com.gemstone.gemfire.internal.lang.StringUtils;
-import com.gemstone.gemfire.internal.logging.InternalLogWriter;
-import com.gemstone.gemfire.internal.logging.LocalLogWriter;
-import com.gemstone.gemfire.internal.process.ProcessControllerFactory;
-import com.gemstone.gemfire.internal.process.ProcessStreamReader;
-import com.gemstone.gemfire.internal.process.ProcessType;
-import com.gemstone.gemfire.internal.process.ProcessUtils;
-import com.gemstone.gemfire.test.junit.categories.FlakyTest;
-import com.gemstone.gemfire.test.junit.categories.IntegrationTest;
-
-/**
- * Integration tests for launching a Locator in a forked process.
- *
- * @since 8.0
- */
-@Category(IntegrationTest.class)
-public class LocatorLauncherRemoteJUnitTest extends AbstractLocatorLauncherJUnitTestCase {
-
- protected volatile Process process;
- protected volatile ProcessStreamReader processOutReader;
- protected volatile ProcessStreamReader processErrReader;
-
- @Before
- public final void setUpLocatorLauncherRemoteTest() throws Exception {
- }
-
- @After
- public final void tearDownLocatorLauncherRemoteTest() throws Exception {
- if (this.process != null) {
- this.process.destroy();
- this.process = null;
- }
- if (this.processOutReader != null && this.processOutReader.isRunning()) {
- this.processOutReader.stop();
- }
- if (this.processErrReader != null && this.processErrReader.isRunning()) {
- this.processErrReader.stop();
- }
- }
-
- protected Status getExpectedStopStatusForNotRunning() {
- return Status.NOT_RESPONDING;
- }
-
- @Test
- public void testIsAttachAPIFound() throws Exception {
- final ProcessControllerFactory factory = new ProcessControllerFactory();
- assertTrue(factory.isAttachAPIFound());
- }
-
- @Test
- @Ignore("TRAC bug #52304: test is broken and needs to be reworked")
- public void testRunningLocatorOutlivesForkingProcess() throws Exception {
- }/*
- // TODO: fix up this test
-
- this.temporaryFolder.getRoot() = new File(getUniqueName());
- this.temporaryFolder.getRoot().mkdir();
- assertTrue(this.temporaryFolder.getRoot().isDirectory() && this.temporaryFolder.getRoot().canWrite());
-
- // launch LocatorLauncherForkingProcess which then launches the GemFire Locator
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncherRemoteDUnitTest.class.getName().concat("$").concat(LocatorLauncherForkingProcess.class.getSimpleName()));
- command.add(String.valueOf(this.locatorPort));
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).build().start();
-
- Thread waiting = new Thread(new Runnable() {
- public void run() {
- try {
- assertEquals(0, process.waitFor());
- }
- catch (InterruptedException ignore) {
- logger.error("Interrupted while waiting for process!", ignore);
- }
- }
- });
-
- try {
- waiting.start();
- waiting.join(TIMEOUT_MILLISECONDS);
- assertFalse("Process took too long and timed out!", waiting.isAlive());
- }
- finally {
- if (waiting.isAlive()) {
- waiting.interrupt();
- }
- }
-
- LocatorLauncher locatorLauncher = new Builder().setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()).build();
-
- assertEquals(Status.ONLINE, locatorLauncher.status().getStatus());
- assertEquals(Status.STOPPED, locatorLauncher.stop().getStatus());
- }
- */
-
- @Category(FlakyTest.class) // GEODE-473: random ports, BindException, forks JVM, uses ErrorCollector
- @Test
- public void testStartCreatesPidFile() throws Throwable {
- // build and start the locator
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--port=" + this.locatorPort);
- command.add("--redirect-output");
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).build().start();
-
- int pid = 0;
- this.launcher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- try {
- waitForLocatorToStart(this.launcher);
-
- // validate the pid file and its contents
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- assertTrue(this.pidFile.exists());
- pid = readPid(this.pidFile);
- assertTrue(pid > 0);
- assertTrue(ProcessUtils.isProcessAlive(pid));
-
- final String logFileName = getUniqueName()+".log";
- assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
-
- // check the status
- final LocatorState locatorState = this.launcher.status();
- assertNotNull(locatorState);
- assertEquals(Status.ONLINE, locatorState.getStatus());
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- // stop the locator
- try {
- assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
- waitForPidToStop(pid);
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
- }
-
- private static class ToSystemOut implements ProcessStreamReader.InputListener {
- @Override
- public void notifyInputLine(String line) {
- System.out.println(line);
- }
- }
-
- @Test
- public void testStartUsesCustomLoggingConfiguration() throws Throwable {
- // TODO: create working dir, copy custom xml to that dir and point log4j at it
-
- // build and start the locator
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-Dlog4j.configurationFile=/Users/klund/dev/gemfire/open/geode-core/src/test/resources/com/gemstone/gemfire/internal/logging/log4j/custom/log4j2.xml");
- //command.add("-D" + ConfigurationFactory.CONFIGURATION_FILE_PROPERTY + "=/Users/klund/dev/doesnotexist.xml");
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--port=" + this.locatorPort);
- command.add("--redirect-output");
-
- for (String line : command) {
- System.out.println("KIRK:testStartUsesCustomLoggingConfiguration:stdout: " + line);
- }
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).inputListener(new ToSystemOut()).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).inputListener(new ToSystemOut()).build().start();
-
- int pid = 0;
- String workingDirectory = this.temporaryFolder.getRoot().getCanonicalPath();
- System.out.println("KIRK: workingDirectory=" + workingDirectory);
- this.launcher = new LocatorLauncher.Builder()
- .setWorkingDirectory(workingDirectory)
- .build();
- try {
- waitForLocatorToStart(this.launcher);
-
- // validate the pid file and its contents
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- assertTrue(this.pidFile.exists());
- pid = readPid(this.pidFile);
- assertTrue(pid > 0);
- assertTrue(ProcessUtils.isProcessAlive(pid));
-
- final String logFileName = getUniqueName()+".log";
- assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
-
- // check the status
- final LocatorState locatorState = this.launcher.status();
- assertNotNull(locatorState);
- assertEquals(Status.ONLINE, locatorState.getStatus());
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- // stop the locator
- try {
- assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
- waitForPidToStop(pid);
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
- }
-
- @Test
- public void testStartUsesCustomLoggingConfigurationWithLauncherLifecycleCommands() throws Throwable {
- // TODO: create working dir, copy custom xml to that dir and point log4j at it
-
- // build and start the locator
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-Dlog4j.configurationFile=/Users/klund/dev/gemfire/open/geode-core/src/test/resources/com/gemstone/gemfire/internal/logging/log4j/custom/log4j2.xml");
- //command.add("-D" + ConfigurationFactory.CONFIGURATION_FILE_PROPERTY + "=/Users/klund/dev/doesnotexist.xml");
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--port=" + this.locatorPort);
- command.add("--redirect-output");
-
- for (String line : command) {
- System.out.println("KIRK:testStartUsesCustomLoggingConfiguration:stdout: " + line);
- }
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).inputListener(new ToSystemOut()).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).inputListener(new ToSystemOut()).build().start();
-
- int pid = 0;
- String workingDirectory = this.temporaryFolder.getRoot().getCanonicalPath();
- System.out.println("KIRK: workingDirectory=" + workingDirectory);
- this.launcher = new LocatorLauncher.Builder()
- .setWorkingDirectory(workingDirectory)
- .build();
- try {
- waitForLocatorToStart(this.launcher);
-
- // validate the pid file and its contents
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- assertTrue(this.pidFile.exists());
- pid = readPid(this.pidFile);
- assertTrue(pid > 0);
- assertTrue(ProcessUtils.isProcessAlive(pid));
-
- final String logFileName = getUniqueName()+".log";
- assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
-
- // check the status
- final LocatorState locatorState = this.launcher.status();
- assertNotNull(locatorState);
- assertEquals(Status.ONLINE, locatorState.getStatus());
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- // stop the locator
- try {
- assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
- waitForPidToStop(pid);
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
- }
-
- @Category(FlakyTest.class) // GEODE-530: BindException, random ports
- @Test
- public void testStartDeletesStaleControlFiles() throws Throwable {
- // create existing control files
- this.stopRequestFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getStopRequestFileName());
- this.stopRequestFile.createNewFile();
- assertTrue(this.stopRequestFile.exists());
-
- this.statusRequestFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getStatusRequestFileName());
- this.statusRequestFile.createNewFile();
- assertTrue(this.statusRequestFile.exists());
-
- this.statusFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getStatusFileName());
- this.statusFile.createNewFile();
- assertTrue(this.statusFile.exists());
-
- // build and start the locator
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--port=" + this.locatorPort);
- command.add("--redirect-output");
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).build().start();
-
- // wait for locator to start
- int pid = 0;
- this.launcher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- try {
- waitForLocatorToStart(this.launcher);
-
- // validate the pid file and its contents
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- assertTrue(this.pidFile.exists());
- pid = readPid(this.pidFile);
- assertTrue(pid > 0);
- assertTrue(ProcessUtils.isProcessAlive(pid));
-
- // validate stale control files were deleted
- waitForFileToDelete(this.stopRequestFile);
- waitForFileToDelete(this.statusRequestFile);
- waitForFileToDelete(this.statusFile);
-
- // validate log file was created
- final String logFileName = getUniqueName()+".log";
- assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- // stop the locator
- try {
- assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
- waitForPidToStop(pid);
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
- }
-
- @Category(FlakyTest.class) // GEODE-1229: BindException
- @Test
- public void testStartOverwritesStalePidFile() throws Throwable {
- // create existing pid file
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- writePid(this.pidFile, Integer.MAX_VALUE);
-
- // build and start the locator
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--port=" + this.locatorPort);
- command.add("--redirect-output");
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).build().start();
-
- int pid = 0;
- this.launcher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- try {
- waitForLocatorToStart(this.launcher);
-
- // validate the pid file and its contents
- assertTrue(this.pidFile.exists());
- pid = readPid(this.pidFile);
- assertTrue(pid > 0);
- assertTrue(ProcessUtils.isProcessAlive(pid));
- assertFalse(pid == Integer.MAX_VALUE);
-
- final String logFileName = getUniqueName()+".log";
- assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- // stop the locator
- try {
- assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
- waitForPidToStop(pid);
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
- }
-
- @Category(FlakyTest.class) // GEODE-764: BindException
- @Test
- public void testStartUsingForceOverwritesExistingPidFile() throws Throwable {
- // create existing pid file
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- final int otherPid = getPid();
- assertTrue("Pid " + otherPid + " should be alive", ProcessUtils.isProcessAlive(otherPid));
- writePid(this.pidFile, otherPid);
-
- // build and start the locator
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--port=" + this.locatorPort);
- command.add("--redirect-output");
- command.add("--force");
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).build().start();
-
- // wait for locator to start
- int pid = 0;
- this.launcher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- try {
- waitForLocatorToStart(this.launcher);
-
- // validate the pid file and its contents
- assertTrue(this.pidFile.exists());
- pid = readPid(this.pidFile);
- assertTrue(pid > 0);
- assertTrue(ProcessUtils.isProcessAlive(pid));
- assertTrue(pid != otherPid);
-
- // validate log file was created
- final String logFileName = getUniqueName()+".log";
- assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- // stop the locator
- try {
- assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
- waitForPidToStop(pid);
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
- }
-
- @Test
- public void testStartUsingPortInUseFails() throws Throwable {
- this.socket = SocketCreator.getDefaultInstance().createServerSocket(this.locatorPort, 50, null, -1);
-
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--redirect-output");
- command.add("--port=" + this.locatorPort);
-
- String expectedString = "java.net.BindException";
- AtomicBoolean outputContainedExpectedString = new AtomicBoolean();
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).inputListener(createExpectedListener("sysout", getUniqueName() + "#sysout", expectedString, outputContainedExpectedString)).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).inputListener(createExpectedListener("syserr", getUniqueName() + "#syserr", expectedString, outputContainedExpectedString)).build().start();
-
- // wait for locator to start and fail
- final LocatorLauncher dirLauncher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- try {
- int code = process.waitFor();
- assertEquals("Expected exit code 1 but was " + code, 1, code);
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- try {
- // check the status
- final LocatorState locatorState = dirLauncher.status();
- assertNotNull(locatorState);
- assertEquals(Status.NOT_RESPONDING, locatorState.getStatus());
-
- final String logFileName = getUniqueName()+".log";
- assertFalse("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- // if the following fails, then the SHORTER_TIMEOUT is too short for slow machines
- // or this test needs to use MainLauncher in ProcessWrapper
-
- // validate that output contained BindException
- this.errorCollector.checkThat(outputContainedExpectedString.get(), is(equalTo(true)));
-
- // just in case the launcher started...
- LocatorState status = null;
- try {
- status = dirLauncher.stop();
- } catch (Throwable t) {
- // ignore
- }
-
- this.errorCollector.checkThat(status.getStatus(), is(equalTo(getExpectedStopStatusForNotRunning())));
- }
-
- @Test
- public void testStartWithDefaultPortInUseFails() throws Throwable {
- String expectedString = "java.net.BindException";
- AtomicBoolean outputContainedExpectedString = new AtomicBoolean();
-
- this.socket = SocketCreator.getDefaultInstance().createServerSocket(this.locatorPort, 50, null, -1);
-
- assertFalse(AvailablePort.isPortAvailable(this.locatorPort, AvailablePort.SOCKET));
- assertTrue(this.socket.isBound());
- assertFalse(this.socket.isClosed());
-
- // launch locator
- final List<String> jvmArguments = getJvmArguments();
- jvmArguments.add("-D" + DistributionLocator.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY + "=" + this.locatorPort);
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--redirect-output");
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).inputListener(createExpectedListener("sysout", getUniqueName() + "#sysout", expectedString, outputContainedExpectedString)).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).inputListener(createExpectedListener("syserr", getUniqueName() + "#syserr", expectedString, outputContainedExpectedString)).build().start();
-
- // wait for locator to start up
- final LocatorLauncher dirLauncher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- try {
- int code = process.waitFor(); // TODO: create flavor with timeout in ProcessUtils
- assertEquals("Expected exit code 1 but was " + code, 1, code);
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- try {
- // check the status
- final LocatorState locatorState = dirLauncher.status();
- assertNotNull(locatorState);
- assertEquals(Status.NOT_RESPONDING, locatorState.getStatus());
-
- // creation of log file seems to be random -- look into why sometime
- final String logFileName = getUniqueName()+".log";
- assertFalse("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- // if the following fails, then the SHORTER_TIMEOUT might be too short for slow machines
- // or this test needs to use MainLauncher in ProcessWrapper
-
- // validate that output contained BindException
- this.errorCollector.checkThat(outputContainedExpectedString.get(), is(equalTo(true)));
-
- // just in case the launcher started...
- LocatorState status = null;
- try {
- status = dirLauncher.stop();
- } catch (Throwable t) {
- // ignore
- }
-
- this.errorCollector.checkThat(status.getStatus(), is(equalTo(getExpectedStopStatusForNotRunning())));
- }
-
- @Test
- @Ignore("Need to rewrite this without using dunit.Host")
- public void testStartWithExistingPidFileFails() throws Throwable {
- }/*
- this.temporaryFolder.getRoot() = new File(getUniqueName());
- this.temporaryFolder.getRoot().mkdir();
- assertTrue(this.temporaryFolder.getRoot().isDirectory() && this.temporaryFolder.getRoot().canWrite());
-
- // create existing pid file
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- final int realPid = Host.getHost(0).getVM(3).invoke(() -> ProcessUtils.identifyPid());
- assertFalse("Remote pid shouldn't be the same as local pid " + realPid, realPid == ProcessUtils.identifyPid());
- writePid(this.pidFile, realPid);
-
- // build and start the locator
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--port=" + this.locatorPort);
- command.add("--redirect-output");
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).build().start();
-
- // collect and throw the FIRST failure
- Throwable failure = null;
-
- final LocatorLauncher dirLauncher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- try {
- waitForLocatorToStart(dirLauncher, 10*1000, false);
- } catch (Throwable e) {
- logger.error(e);
- if (failure == null) {
- failure = e;
- }
- }
-
- try {
- // check the status
- final LocatorState locatorState = dirLauncher.status();
- assertNotNull(locatorState);
- assertEquals(Status.NOT_RESPONDING, locatorState.getStatus());
-
- final String logFileName = getUniqueName()+".log";
- assertFalse("Log file should not exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
-
- } catch (Throwable e) {
- logger.error(e);
- if (failure == null) {
- failure = e;
- }
- }
-
- // just in case the launcher started...
- try {
- final LocatorState status = dirLauncher.stop();
- final Status theStatus = status.getStatus();
- assertFalse(theStatus == Status.STARTING);
- assertFalse(theStatus == Status.ONLINE);
- } catch (Throwable e) {
- logger.error(e);
- if (failure == null) {
- failure = e;
- }
- }
-
- if (failure != null) {
- throw failure;
- }
- } // testStartWithExistingPidFileFails
- */
-
- @Test
- public void testStatusUsingPid() throws Throwable {
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--port=" + this.locatorPort);
- command.add("--redirect-output");
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).build().start();
-
- // wait for locator to start
- int pid = 0;
- LocatorLauncher pidLauncher = null;
- final LocatorLauncher dirLauncher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- try {
- waitForLocatorToStart(dirLauncher);
-
- // validate the pid file and its contents
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- assertTrue(this.pidFile.exists());
- pid = readPid(this.pidFile);
- assertTrue(pid > 0);
- assertTrue(ProcessUtils.isProcessAlive(pid));
-
- // validate log file was created
- final String logFileName = getUniqueName()+".log";
- assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
-
- // use launcher with pid
- pidLauncher = new Builder()
- .setPid(pid)
- .build();
-
- assertNotNull(pidLauncher);
- assertFalse(pidLauncher.isRunning());
-
- // validate the status
- final LocatorState actualStatus = pidLauncher.status();
- assertNotNull(actualStatus);
- assertEquals(Status.ONLINE, actualStatus.getStatus());
- assertEquals(pid, actualStatus.getPid().intValue());
- assertTrue(actualStatus.getUptime() > 0);
- assertEquals(this.temporaryFolder.getRoot().getCanonicalPath(), actualStatus.getWorkingDirectory());
- assertEquals(jvmArguments, actualStatus.getJvmArguments());
- assertEquals(ManagementFactory.getRuntimeMXBean().getClassPath(), actualStatus.getClasspath());
- assertEquals(GemFireVersion.getGemFireVersion(), actualStatus.getGemFireVersion());
- assertEquals(System.getProperty("java.version"), actualStatus.getJavaVersion());
- assertEquals(this.temporaryFolder.getRoot().getCanonicalPath() + File.separator + getUniqueName() + ".log", actualStatus.getLogFile());
- assertEquals(InetAddress.getLocalHost().getCanonicalHostName(), actualStatus.getHost());
- assertEquals(getUniqueName(), actualStatus.getMemberName());
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- // stop the locator
- try {
- if (pidLauncher == null) {
- assertEquals(Status.STOPPED, dirLauncher.stop().getStatus());
- } else {
- assertEquals(Status.STOPPED, pidLauncher.stop().getStatus());
- }
- waitForPidToStop(pid);
- waitForFileToDelete(this.pidFile);
-
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
- }
-
- @Category(FlakyTest.class) // GEODE-569: BindException, random ports
- @Test
- public void testStatusUsingWorkingDirectory() throws Throwable {
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--port=" + this.locatorPort);
- command.add("--redirect-output");
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).build().start();
-
- // wait for locator to start
- int pid = 0;
- final LocatorLauncher dirLauncher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- try {
- waitForLocatorToStart(dirLauncher);
-
- // validate the pid file and its contents
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- assertTrue(this.pidFile.exists());
- pid = readPid(this.pidFile);
- assertTrue(pid > 0);
- assertTrue(ProcessUtils.isProcessAlive(pid));
-
- // validate log file was created
- final String logFileName = getUniqueName()+".log";
- assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
-
- assertNotNull(dirLauncher);
- assertFalse(dirLauncher.isRunning());
-
- // validate the status
- final LocatorState actualStatus = dirLauncher.status();
- assertNotNull(actualStatus);
- assertEquals(Status.ONLINE, actualStatus.getStatus());
- assertEquals(pid, actualStatus.getPid().intValue());
- assertTrue(actualStatus.getUptime() > 0);
- assertEquals(this.temporaryFolder.getRoot().getCanonicalPath(), actualStatus.getWorkingDirectory());
- assertEquals(jvmArguments, actualStatus.getJvmArguments());
- assertEquals(ManagementFactory.getRuntimeMXBean().getClassPath(), actualStatus.getClasspath());
- assertEquals(GemFireVersion.getGemFireVersion(), actualStatus.getGemFireVersion());
- assertEquals(System.getProperty("java.version"), actualStatus.getJavaVersion());
- assertEquals(this.temporaryFolder.getRoot().getCanonicalPath() + File.separator + getUniqueName() + ".log", actualStatus.getLogFile());
- assertEquals(InetAddress.getLocalHost().getCanonicalHostName(), actualStatus.getHost());
- assertEquals(getUniqueName(), actualStatus.getMemberName());
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- // stop the locator
- try {
- assertEquals(Status.STOPPED, dirLauncher.stop().getStatus());
- waitForPidToStop(pid);
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
- }
-
- @Test
- public void testStatusWithEmptyPidFile() throws Exception {
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- assertTrue(this.pidFile + " already exists", this.pidFile.createNewFile());
-
- final LocatorLauncher dirLauncher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- final LocatorState actualStatus = dirLauncher.status();
- assertThat(actualStatus, is(notNullValue()));
- assertThat(actualStatus.getStatus(), is(equalTo(Status.NOT_RESPONDING)));
- assertThat(actualStatus.getPid(), is(nullValue()));
- assertThat(actualStatus.getUptime().intValue(), is(equalTo(0)));
- assertThat(actualStatus.getWorkingDirectory(), is(equalTo(this.temporaryFolder.getRoot().getCanonicalPath())));
- assertThat(actualStatus.getClasspath(), is(nullValue()));
- assertThat(actualStatus.getGemFireVersion(), is(equalTo(GemFireVersion.getGemFireVersion())));
- assertThat(actualStatus.getJavaVersion(), is(nullValue()));
- assertThat(actualStatus.getLogFile(), is(nullValue()));
- assertThat(actualStatus.getHost(), is(nullValue()));
- assertThat(actualStatus.getMemberName(), is(nullValue()));
- }
-
- @Test
- public void testStatusWithNoPidFile() throws Exception {
- final LocatorLauncher dirLauncher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- LocatorState locatorState = dirLauncher.status();
- assertEquals(Status.NOT_RESPONDING, locatorState.getStatus());
- }
-
- @Test
- public void testStatusWithStalePidFile() throws Exception {
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- final int pid = 0;
- assertFalse(ProcessUtils.isProcessAlive(pid));
- writePid(this.pidFile, pid);
-
- final LocatorLauncher dirLauncher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- final LocatorState actualStatus = dirLauncher.status();
- assertThat(actualStatus, is(notNullValue()));
- assertThat(actualStatus.getStatus(), is(equalTo(Status.NOT_RESPONDING)));
- assertThat(actualStatus.getPid(), is(nullValue()));
- assertThat(actualStatus.getUptime().intValue(), is(equalTo(0)));
- assertThat(actualStatus.getWorkingDirectory(), is(equalTo(this.temporaryFolder.getRoot().getCanonicalPath())));
- assertThat(actualStatus.getClasspath(), is(nullValue()));
- assertThat(actualStatus.getGemFireVersion(), is(equalTo(GemFireVersion.getGemFireVersion())));
- assertThat(actualStatus.getJavaVersion(), is(nullValue()));
- assertThat(actualStatus.getLogFile(), is(nullValue()));
- assertThat(actualStatus.getHost(), is(nullValue()));
- assertThat(actualStatus.getMemberName(), is(nullValue()));
- }
-
- @Test
- public void testStopUsingPid() throws Throwable {
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--port=" + this.locatorPort);
- command.add("--redirect-output");
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).inputListener(createLoggingListener("sysout", getUniqueName() + "#sysout")).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).inputListener(createLoggingListener("syserr", getUniqueName() + "#syserr")).build().start();
-
- // wait for locator to start
- int pid = 0;
- LocatorLauncher pidLauncher = null;
- final LocatorLauncher dirLauncher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- try {
- waitForLocatorToStart(dirLauncher);
-
- // validate the pid file and its contents
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- assertTrue(this.pidFile.exists());
- pid = readPid(this.pidFile);
- assertTrue(pid > 0);
- assertTrue(ProcessUtils.isProcessAlive(pid));
-
- // validate log file was created
- final String logFileName = getUniqueName()+".log";
- assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
-
- // use launcher with pid
- pidLauncher = new Builder()
- .setPid(pid)
- .build();
-
- assertNotNull(pidLauncher);
- assertFalse(pidLauncher.isRunning());
-
- // validate the status
- final LocatorState status = pidLauncher.status();
- assertNotNull(status);
- assertEquals(Status.ONLINE, status.getStatus());
- assertEquals(pid, status.getPid().intValue());
-
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- // stop the locator
- try {
- if (pidLauncher == null) {
- assertEquals(Status.STOPPED, dirLauncher.stop().getStatus());
- } else {
- assertEquals(Status.STOPPED, pidLauncher.stop().getStatus());
- }
- waitForPidToStop(pid);
- waitForFileToDelete(pidFile);
-
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
- }
-
- @Category(FlakyTest.class) // GEODE-847: random ports, BindException, forks JVM, uses ErrorCollector
- @Test
- public void testStopUsingWorkingDirectory() throws Throwable {
- final List<String> jvmArguments = getJvmArguments();
-
- final List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
- for (String jvmArgument : jvmArguments) {
- command.add(jvmArgument);
- }
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(getUniqueName());
- command.add("--port=" + this.locatorPort);
- command.add("--redirect-output");
-
- this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
- this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).build().start();
- this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).build().start();
-
- // wait for locator to start
- int pid = 0;
- final LocatorLauncher dirLauncher = new LocatorLauncher.Builder()
- .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
- .build();
- try {
- waitForLocatorToStart(dirLauncher);
-
- // validate the pid file and its contents
- this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
- assertTrue(this.pidFile.exists());
- pid = readPid(this.pidFile);
- assertTrue(pid > 0);
- assertTrue(ProcessUtils.isProcessAlive(pid));
-
- // validate log file was created
- final String logFileName = getUniqueName()+".log";
- assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
-
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
-
- try {
- // stop the locator
- assertEquals(Status.STOPPED, dirLauncher.stop().getStatus());
- waitForPidToStop(pid);
- assertFalse("PID file still exists!", this.pidFile.exists());
-
- } catch (Throwable e) {
- this.errorCollector.addError(e);
- }
- }
-
- public static List<String> getJvmArguments() {
- final List<String> jvmArguments = new ArrayList<String>();
- jvmArguments.add("-Dgemfire.log-level=config");
- return jvmArguments;
- }
-
- /**
- * Used only by {@link LocatorLauncherRemoteJUnitTest#testRunningLocatorOutlivesForkingProcess}
- */
- public static class LocatorLauncherForkingProcess {
-
- public static void main(final String... args) throws FileNotFoundException {
- File file = new File(System.getProperty("user.dir"), LocatorLauncherForkingProcess.class.getSimpleName().concat(".log"));
-
- LocalLogWriter logWriter = new LocalLogWriter(InternalLogWriter.ALL_LEVEL, new PrintStream(new FileOutputStream(file, true)));
-
- try {
- final int port = Integer.parseInt(args[0]);
-
- // launch LocatorLauncher
- List<String> command = new ArrayList<String>();
- command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getAbsolutePath());
- command.add("-cp");
- command.add(System.getProperty("java.class.path"));
- command.add("-Dgemfire.mcast-port=0");
- command.add(LocatorLauncher.class.getName());
- command.add(LocatorLauncher.Command.START.getName());
- command.add(LocatorLauncherForkingProcess.class.getSimpleName() + "_Locator");
- command.add("--port=" + port);
- command.add("--redirect-output");
-
- logWriter.info(LocatorLauncherForkingProcess.class.getSimpleName() + "#main command: " + command);
- logWriter.info(LocatorLauncherForkingProcess.class.getSimpleName() + "#main starting...");
-
- Process forkedProcess = new ProcessBuilder(command).start();
-
- @SuppressWarnings("unused")
- ProcessStreamReader processOutReader = new ProcessStreamReader.Builder(forkedProcess).inputStream(forkedProcess.getInputStream()).build().start();
- @SuppressWarnings("unused")
- ProcessStreamReader processErrReader = new ProcessStreamReader.Builder(forkedProcess).inputStream(forkedProcess.getErrorStream()).build().start();
-
- logWriter.info(LocatorLauncherForkingProcess.class.getSimpleName() + "#main waiting for locator to start...");
-
- waitForLocatorToStart(port, TIMEOUT_MILLISECONDS, 10, true);
-
- logWriter.info(LocatorLauncherForkingProcess.class.getSimpleName() + "#main exiting...");
-
- System.exit(0);
- }
- catch (Throwable t) {
- logWriter.info(LocatorLauncherForkingProcess.class.getSimpleName() + "#main error: " + t, t);
- System.exit(-1);
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/cd02af01/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherTest.java b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherTest.java
new file mode 100644
index 0000000..39fdd4d
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherTest.java
@@ -0,0 +1,341 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gemstone.gemfire.distributed;
+
+import static org.junit.Assert.*;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import com.gemstone.gemfire.distributed.LocatorLauncher.Builder;
+import com.gemstone.gemfire.distributed.LocatorLauncher.Command;
+import com.gemstone.gemfire.distributed.internal.DistributionConfig;
+import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+import joptsimple.OptionException;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.contrib.java.lang.system.RestoreSystemProperties;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.TestName;
+
+/**
+ * The LocatorLauncherTest class is a test suite of test cases for testing the contract and functionality of
+ * launching a GemFire Locator.
+ *
+ * @see com.gemstone.gemfire.distributed.LocatorLauncher
+ * @see com.gemstone.gemfire.distributed.LocatorLauncher.Builder
+ * @see com.gemstone.gemfire.distributed.LocatorLauncher.Command
+ * @see org.junit.Assert
+ * @see org.junit.Test
+ * @since 7.0
+ */
+@Category(UnitTest.class)
+public class LocatorLauncherTest {
+
+ @Rule
+ public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();
+
+ @Rule
+ public final TestName testName = new TestName();
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testBuilderParseArgumentsWithNonNumericPort() {
+ try {
+ new Builder().parseArguments("start", "locator1", "--port", "oneTwoThree");
+ }
+ catch (IllegalArgumentException expected) {
+ assertTrue(expected.getCause() instanceof OptionException);
+ assertTrue(expected.getMessage(), expected.getMessage().contains(
+ LocalizedStrings.Launcher_Builder_PARSE_COMMAND_LINE_ARGUMENT_ERROR_MESSAGE.toLocalizedString(
+ "Locator", expected.getCause().getMessage())));
+ throw expected;
+ }
+ }
+
+ @Test
+ public void testForceDefaultsToFalse() {
+ assertFalse(new Builder().getForce());
+ }
+
+ @Test
+ public void testForceSetToTrue() {
+ Builder builder = new Builder();
+
+ builder.parseArguments("start", "--force");
+
+ assertTrue(Boolean.TRUE.equals(builder.getForce()));
+ }
+
+ @Test
+ public void testSetAndGetCommand() {
+ final Builder builder = new Builder();
+
+ assertEquals(Builder.DEFAULT_COMMAND, builder.getCommand());
+ assertSame(builder, builder.setCommand(Command.START));
+ assertEquals(Command.START, builder.getCommand());
+ assertSame(builder, builder.setCommand(Command.STATUS));
+ assertEquals(Command.STATUS, builder.getCommand());
+ assertSame(builder, builder.setCommand(Command.STOP));
+ assertEquals(Command.STOP, builder.getCommand());
+ assertSame(builder, builder.setCommand(null));
+ assertEquals(Builder.DEFAULT_COMMAND, builder.getCommand());
+ }
+
+ @Test
+ public void testSetAndGetBindAddress() throws UnknownHostException {
+ final Builder builder = new Builder();
+
+ assertNull(builder.getBindAddress());
+ assertSame(builder, builder.setBindAddress(null));
+ assertNull(builder.getBindAddress());
+ assertSame(builder, builder.setBindAddress(""));
+ assertNull(builder.getBindAddress());
+ assertSame(builder, builder.setBindAddress(" "));
+ assertNull(builder.getBindAddress());
+ assertSame(builder, builder.setBindAddress(InetAddress.getLocalHost().getCanonicalHostName()));
+ assertEquals(InetAddress.getLocalHost(), builder.getBindAddress());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSetBindAddressToUnknownHost() {
+ try {
+ new Builder().setBindAddress("badhostname.badcompany.bad");
+ }
+ catch (IllegalArgumentException expected) {
+ final String expectedMessage1 = LocalizedStrings.Launcher_Builder_UNKNOWN_HOST_ERROR_MESSAGE.toLocalizedString("Locator");
+ final String expectedMessage2 = "badhostname.badcompany.bad is not an address for this machine.";
+ assertTrue(expected.getMessage().equals(expectedMessage1) || expected.getMessage().equals(expectedMessage2));
+ if (expected.getMessage().equals(expectedMessage1)) {
+ assertTrue(expected.getCause() instanceof UnknownHostException);
+ }
+ throw expected;
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSetBindAddressToNonLocalHost() {
+ try {
+ new Builder().setBindAddress("yahoo.com");
+ }
+ catch (IllegalArgumentException expected) {
+ final String expectedMessage = "yahoo.com is not an address for this machine.";
+ assertEquals(expectedMessage, expected.getMessage());
+ throw expected;
+ }
+ }
+
+ @Test
+ public void testSetBindAddressToLocalHost() throws Exception {
+ String host = InetAddress.getLocalHost().getHostName();
+ new Builder().setBindAddress(host);
+ }
+
+ @Test
+ public void testSetAndGetHostnameForClients() {
+ final Builder builder = new Builder();
+
+ assertNull(builder.getHostnameForClients());
+ assertSame(builder, builder.setHostnameForClients("Pegasus"));
+ assertEquals("Pegasus", builder.getHostnameForClients());
+ assertSame(builder, builder.setHostnameForClients(null));
+ assertNull(builder.getHostnameForClients());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSetHostnameForClientsWithBlankString() {
+ try {
+ new Builder().setHostnameForClients(" ");
+ }
+ catch (IllegalArgumentException expected) {
+ assertEquals(LocalizedStrings.LocatorLauncher_Builder_INVALID_HOSTNAME_FOR_CLIENTS_ERROR_MESSAGE
+ .toLocalizedString(), expected.getMessage());
+ throw expected;
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSetHostnameForClientsWithEmptyString() {
+ try {
+ new Builder().setHostnameForClients("");
+ }
+ catch (IllegalArgumentException expected) {
+ assertEquals(LocalizedStrings.LocatorLauncher_Builder_INVALID_HOSTNAME_FOR_CLIENTS_ERROR_MESSAGE
+ .toLocalizedString(), expected.getMessage());
+ throw expected;
+ }
+ }
+
+ @Test
+ public void testSetAndGetMemberName() {
+ Builder builder = new Builder();
+
+ assertNull(builder.getMemberName());
+ assertSame(builder, builder.setMemberName("locatorOne"));
+ assertEquals("locatorOne", builder.getMemberName());
+ assertSame(builder, builder.setMemberName(null));
+ assertNull(builder.getMemberName());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSetMemberNameWithBlankString() {
+ try {
+ new Builder().setMemberName(" ");
+ }
+ catch (IllegalArgumentException expected) {
+ assertEquals(LocalizedStrings.Launcher_Builder_MEMBER_NAME_ERROR_MESSAGE.toLocalizedString("Locator"),
+ expected.getMessage());
+ throw expected;
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSetMemberNameWithEmptyString() {
+ try {
+ new Builder().setMemberName("");
+ }
+ catch (IllegalArgumentException expected) {
+ assertEquals(LocalizedStrings.Launcher_Builder_MEMBER_NAME_ERROR_MESSAGE.toLocalizedString("Locator"),
+ expected.getMessage());
+ throw expected;
+ }
+ }
+
+ @Test
+ public void testSetAndGetPid() {
+ Builder builder = new Builder();
+
+ assertNull(builder.getPid());
+ assertSame(builder, builder.setPid(0));
+ assertEquals(0, builder.getPid().intValue());
+ assertSame(builder, builder.setPid(1));
+ assertEquals(1, builder.getPid().intValue());
+ assertSame(builder, builder.setPid(1024));
+ assertEquals(1024, builder.getPid().intValue());
+ assertSame(builder, builder.setPid(12345));
+ assertEquals(12345, builder.getPid().intValue());
+ assertSame(builder, builder.setPid(null));
+ assertNull(builder.getPid());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSetPidToInvalidValue() {
+ try {
+ new Builder().setPid(-1);
+ }
+ catch (IllegalArgumentException expected) {
+ assertEquals(LocalizedStrings.Launcher_Builder_PID_ERROR_MESSAGE.toLocalizedString(), expected.getMessage());
+ throw expected;
+ }
+ }
+
+ @SuppressWarnings("deprecation")
+ @Test
+ public void testSetAndGetPort() {
+ Builder builder = new Builder();
+
+ assertEquals(LocatorLauncher.DEFAULT_LOCATOR_PORT, builder.getPort());
+ assertSame(builder, builder.setPort(65535));
+ assertEquals(65535, builder.getPort().intValue());
+ assertSame(builder, builder.setPort(1024));
+ assertEquals(1024, builder.getPort().intValue());
+ assertSame(builder, builder.setPort(80));
+ assertEquals(80, builder.getPort().intValue());
+ assertSame(builder, builder.setPort(1));
+ assertEquals(1, builder.getPort().intValue());
+ assertSame(builder, builder.setPort(0));
+ assertEquals(0, builder.getPort().intValue());
+ assertSame(builder, builder.setPort(null));
+ assertEquals(LocatorLauncher.DEFAULT_LOCATOR_PORT, builder.getPort());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSetPortToOverflow() {
+ try {
+ new Builder().setPort(65536);
+ }
+ catch (IllegalArgumentException expected) {
+ assertEquals(LocalizedStrings.Launcher_Builder_INVALID_PORT_ERROR_MESSAGE.toLocalizedString("Locator"),
+ expected.getMessage());
+ throw expected;
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSetPortToUnderflow() {
+ try {
+ new Builder().setPort(-1);
+ }
+ catch (IllegalArgumentException expected) {
+ assertEquals(LocalizedStrings.Launcher_Builder_INVALID_PORT_ERROR_MESSAGE.toLocalizedString("Locator"),
+ expected.getMessage());
+ throw expected;
+ }
+ }
+
+ @Test
+ public void testBuild() throws Exception {
+ Builder builder = new Builder();
+
+ LocatorLauncher launcher = builder.setCommand(Command.START)
+ .setDebug(true)
+ .setHostnameForClients("beanstock.vmware.com")
+ .setMemberName("Beanstock")
+ .setPort(8192)
+ .build();
+
+ assertNotNull(launcher);
+ assertEquals(builder.getCommand(), launcher.getCommand());
+ assertTrue(launcher.isDebugging());
+ assertEquals(builder.getHostnameForClients(), launcher.getHostnameForClients());
+ assertEquals(builder.getMemberName(), launcher.getMemberName());
+ assertEquals(builder.getPort(), launcher.getPort());
+ assertEquals(builder.getWorkingDirectory(), launcher.getWorkingDirectory());
+ assertFalse(launcher.isHelping());
+ assertFalse(launcher.isRunning());
+ }
+
+ @Test
+ public void testBuildWithMemberNameSetInApiPropertiesOnStart() {
+ LocatorLauncher launcher = new Builder()
+ .setCommand(LocatorLauncher.Command.START)
+ .setMemberName(null)
+ .set(DistributionConfig.NAME_NAME, "locatorABC")
+ .build();
+
+ assertNotNull(launcher);
+ assertEquals(LocatorLauncher.Command.START, launcher.getCommand());
+ assertNull(launcher.getMemberName());
+ assertEquals("locatorABC", launcher.getProperties().getProperty(DistributionConfig.NAME_NAME));
+ }
+
+ @Test
+ public void testBuildWithMemberNameSetInSystemPropertiesOnStart() {
+ System.setProperty(DistributionConfig.GEMFIRE_PREFIX + DistributionConfig.NAME_NAME, "locatorXYZ");
+
+ LocatorLauncher launcher = new Builder()
+ .setCommand(LocatorLauncher.Command.START)
+ .setMemberName(null)
+ .build();
+
+ assertNotNull(launcher);
+ assertEquals(LocatorLauncher.Command.START, launcher.getCommand());
+ assertNull(launcher.getMemberName());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/cd02af01/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorStateJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorStateJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorStateJUnitTest.java
deleted file mode 100755
index 248c39f..0000000
--- a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorStateJUnitTest.java
+++ /dev/null
@@ -1,208 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.gemstone.gemfire.distributed;
-
-import static com.googlecode.catchexception.CatchException.*;
-import static org.assertj.core.api.Assertions.*;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-import com.gemstone.gemfire.distributed.AbstractLauncher.ServiceState;
-import com.gemstone.gemfire.distributed.AbstractLauncher.Status;
-import com.gemstone.gemfire.distributed.LocatorLauncher.LocatorState;
-import com.gemstone.gemfire.management.internal.cli.json.GfJsonException;
-import com.gemstone.gemfire.management.internal.cli.json.GfJsonObject;
-import com.gemstone.gemfire.test.junit.categories.UnitTest;
-
-/**
- * Unit tests for LocatorLauncher.LocatorState
- */
-@Category(UnitTest.class)
-public class LocatorStateJUnitTest {
-
- @Test
- public void fromJsonWithEmptyStringThrowsIllegalArgumentException() throws Exception {
- // given: empty string
- String emptyString = "";
-
- // when: passed to fromJson
- verifyException(this).fromJson(emptyString);
-
- // then: throws IllegalArgumentException with cause of GfJsonException
- assertThat((Exception)caughtException())
- .isInstanceOf(IllegalArgumentException.class)
- .hasCauseInstanceOf(GfJsonException.class);
-
- assertThat(caughtException().getCause())
- .isInstanceOf(GfJsonException.class)
- .hasNoCause();
- }
-
- @Test
- public void fromJsonWithWhiteSpaceStringThrowsIllegalArgumentException() throws Exception {
- // given: white space string
- String whiteSpaceString = " ";
-
- // when: passed to fromJson
- verifyException(this).fromJson(whiteSpaceString);
-
- // then: throws IllegalArgumentException with cause of GfJsonException
- assertThat((Exception)caughtException())
- .isInstanceOf(IllegalArgumentException.class)
- .hasCauseInstanceOf(GfJsonException.class);
-
- assertThat(caughtException().getCause())
- .isInstanceOf(GfJsonException.class)
- .hasNoCause();
- }
-
- @Test
- public void fromJsonWithNullStringThrowsNullPointerException() throws Exception {
- // given: null string
- String nullString = null;
-
- // when: passed to fromJson
- verifyException(this).fromJson(nullString);
-
- // then: throws NullPointerException
- assertThat((Exception)caughtException())
- .isInstanceOf(NullPointerException.class)
- .hasNoCause();
- }
-
- @Test
- public void fromJsonWithValidJsonStringReturnsLocatorState() throws Exception {
- // given: valid json string
- String jsonString = createStatusJson();
-
- // when: passed to fromJson
- LocatorState value = fromJson(jsonString);
-
- // then: return valid instance of LocatorState
- assertThat(value).isInstanceOf(LocatorState.class);
-
- assertThat(value.getClasspath()).isEqualTo(getClasspath());
- assertThat(value.getGemFireVersion()).isEqualTo(getGemFireVersion());
- assertThat(value.getHost()).isEqualTo(getHost());
- assertThat(value.getJavaVersion()).isEqualTo(getJavaVersion());
- assertThat(value.getJvmArguments()).isEqualTo(getJvmArguments());
- assertThat(value.getServiceLocation()).isEqualTo(getServiceLocation());
- assertThat(value.getLogFile()).isEqualTo(getLogFile());
- assertThat(value.getMemberName()).isEqualTo(getMemberName());
- assertThat(value.getPid()).isEqualTo(getPid());
- assertThat(value.getPort()).isEqualTo(getPort());
- assertThat(value.getStatus().getDescription()).isEqualTo(getStatusDescription());
- assertThat(value.getStatusMessage()).isEqualTo(getStatusMessage());
- assertThat(value.getTimestamp().getTime()).isEqualTo(getTimestampTime());
- assertThat(value.getUptime()).isEqualTo(getUptime());
- assertThat(value.getWorkingDirectory()).isEqualTo(getWorkingDirectory());
- }
-
- protected LocatorState fromJson(final String value) {
- return LocatorState.fromJson(value);
- }
-
- private String classpath = "test_classpath";
- private String gemFireVersion = "test_gemfireversion";
- private String host = "test_host";
- private String javaVersion = "test_javaversion";
- private String jvmArguments = "test_jvmarguments";
- private String serviceLocation = "test_location";
- private String logFile = "test_logfile";
- private String memberName = "test_membername";
- private Integer pid = 6396;
- private String port = "test_port";
- private String statusDescription = Status.NOT_RESPONDING.getDescription();
- private String statusMessage = "test_statusmessage";
- private Long timestampTime = 1450728233024L;
- private Long uptime = 1629L;
- private String workingDirectory = "test_workingdirectory";
-
- private String getClasspath() {
- return this.classpath;
- }
- private String getGemFireVersion() {
- return this.gemFireVersion;
- }
- private String getHost() {
- return this.host;
- }
- private String getJavaVersion() {
- return this.javaVersion;
- }
- private List<String> getJvmArguments() {
- List<String> list = new ArrayList<String>();
- list.add(this.jvmArguments);
- return list;
- }
- private String getServiceLocation() {
- return this.serviceLocation;
- }
- private String getLogFile() {
- return this.logFile;
- }
- private String getMemberName() {
- return this.memberName;
- }
- private Integer getPid() {
- return this.pid;
- }
- private String getPort() {
- return this.port;
- }
- private String getStatusDescription() {
- return this.statusDescription;
- }
- private String getStatusMessage() {
- return this.statusMessage;
- }
- private Long getTimestampTime() {
- return this.timestampTime;
- }
- private Long getUptime() {
- return this.uptime;
- }
- private String getWorkingDirectory() {
- return this.workingDirectory;
- }
-
- private String createStatusJson() {
- final Map<String, Object> map = new HashMap<String, Object>();
- map.put(ServiceState.JSON_CLASSPATH, getClasspath());
- map.put(ServiceState.JSON_GEMFIREVERSION, getGemFireVersion());
- map.put(ServiceState.JSON_HOST, getHost());
- map.put(ServiceState.JSON_JAVAVERSION, getJavaVersion());
- map.put(ServiceState.JSON_JVMARGUMENTS, getJvmArguments());
- map.put(ServiceState.JSON_LOCATION, getServiceLocation());
- map.put(ServiceState.JSON_LOGFILE, getLogFile());
- map.put(ServiceState.JSON_MEMBERNAME, getMemberName());
- map.put(ServiceState.JSON_PID, getPid());
- map.put(ServiceState.JSON_PORT, getPort());
- map.put(ServiceState.JSON_STATUS, getStatusDescription());
- map.put(ServiceState.JSON_STATUSMESSAGE, getStatusMessage());
- map.put(ServiceState.JSON_TIMESTAMP, getTimestampTime());
- map.put(ServiceState.JSON_UPTIME, getUptime());
- map.put(ServiceState.JSON_WORKINGDIRECTORY, getWorkingDirectory());
- return new GfJsonObject(map).toString();
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/cd02af01/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorStateTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorStateTest.java b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorStateTest.java
new file mode 100755
index 0000000..55d7d6a
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorStateTest.java
@@ -0,0 +1,208 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gemstone.gemfire.distributed;
+
+import static com.googlecode.catchexception.CatchException.*;
+import static org.assertj.core.api.Assertions.*;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import com.gemstone.gemfire.distributed.AbstractLauncher.ServiceState;
+import com.gemstone.gemfire.distributed.AbstractLauncher.Status;
+import com.gemstone.gemfire.distributed.LocatorLauncher.LocatorState;
+import com.gemstone.gemfire.management.internal.cli.json.GfJsonException;
+import com.gemstone.gemfire.management.internal.cli.json.GfJsonObject;
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+/**
+ * Unit tests for LocatorLauncher.LocatorState
+ */
+@Category(UnitTest.class)
+public class LocatorStateTest {
+
+ @Test
+ public void fromJsonWithEmptyStringThrowsIllegalArgumentException() throws Exception {
+ // given: empty string
+ String emptyString = "";
+
+ // when: passed to fromJson
+ verifyException(this).fromJson(emptyString);
+
+ // then: throws IllegalArgumentException with cause of GfJsonException
+ assertThat((Exception)caughtException())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasCauseInstanceOf(GfJsonException.class);
+
+ assertThat(caughtException().getCause())
+ .isInstanceOf(GfJsonException.class)
+ .hasNoCause();
+ }
+
+ @Test
+ public void fromJsonWithWhiteSpaceStringThrowsIllegalArgumentException() throws Exception {
+ // given: white space string
+ String whiteSpaceString = " ";
+
+ // when: passed to fromJson
+ verifyException(this).fromJson(whiteSpaceString);
+
+ // then: throws IllegalArgumentException with cause of GfJsonException
+ assertThat((Exception)caughtException())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasCauseInstanceOf(GfJsonException.class);
+
+ assertThat(caughtException().getCause())
+ .isInstanceOf(GfJsonException.class)
+ .hasNoCause();
+ }
+
+ @Test
+ public void fromJsonWithNullStringThrowsNullPointerException() throws Exception {
+ // given: null string
+ String nullString = null;
+
+ // when: passed to fromJson
+ verifyException(this).fromJson(nullString);
+
+ // then: throws NullPointerException
+ assertThat((Exception)caughtException())
+ .isInstanceOf(NullPointerException.class)
+ .hasNoCause();
+ }
+
+ @Test
+ public void fromJsonWithValidJsonStringReturnsLocatorState() throws Exception {
+ // given: valid json string
+ String jsonString = createStatusJson();
+
+ // when: passed to fromJson
+ LocatorState value = fromJson(jsonString);
+
+ // then: return valid instance of LocatorState
+ assertThat(value).isInstanceOf(LocatorState.class);
+
+ assertThat(value.getClasspath()).isEqualTo(getClasspath());
+ assertThat(value.getGemFireVersion()).isEqualTo(getGemFireVersion());
+ assertThat(value.getHost()).isEqualTo(getHost());
+ assertThat(value.getJavaVersion()).isEqualTo(getJavaVersion());
+ assertThat(value.getJvmArguments()).isEqualTo(getJvmArguments());
+ assertThat(value.getServiceLocation()).isEqualTo(getServiceLocation());
+ assertThat(value.getLogFile()).isEqualTo(getLogFile());
+ assertThat(value.getMemberName()).isEqualTo(getMemberName());
+ assertThat(value.getPid()).isEqualTo(getPid());
+ assertThat(value.getPort()).isEqualTo(getPort());
+ assertThat(value.getStatus().getDescription()).isEqualTo(getStatusDescription());
+ assertThat(value.getStatusMessage()).isEqualTo(getStatusMessage());
+ assertThat(value.getTimestamp().getTime()).isEqualTo(getTimestampTime());
+ assertThat(value.getUptime()).isEqualTo(getUptime());
+ assertThat(value.getWorkingDirectory()).isEqualTo(getWorkingDirectory());
+ }
+
+ protected LocatorState fromJson(final String value) {
+ return LocatorState.fromJson(value);
+ }
+
+ private String classpath = "test_classpath";
+ private String gemFireVersion = "test_gemfireversion";
+ private String host = "test_host";
+ private String javaVersion = "test_javaversion";
+ private String jvmArguments = "test_jvmarguments";
+ private String serviceLocation = "test_location";
+ private String logFile = "test_logfile";
+ private String memberName = "test_membername";
+ private Integer pid = 6396;
+ private String port = "test_port";
+ private String statusDescription = Status.NOT_RESPONDING.getDescription();
+ private String statusMessage = "test_statusmessage";
+ private Long timestampTime = 1450728233024L;
+ private Long uptime = 1629L;
+ private String workingDirectory = "test_workingdirectory";
+
+ private String getClasspath() {
+ return this.classpath;
+ }
+ private String getGemFireVersion() {
+ return this.gemFireVersion;
+ }
+ private String getHost() {
+ return this.host;
+ }
+ private String getJavaVersion() {
+ return this.javaVersion;
+ }
+ private List<String> getJvmArguments() {
+ List<String> list = new ArrayList<String>();
+ list.add(this.jvmArguments);
+ return list;
+ }
+ private String getServiceLocation() {
+ return this.serviceLocation;
+ }
+ private String getLogFile() {
+ return this.logFile;
+ }
+ private String getMemberName() {
+ return this.memberName;
+ }
+ private Integer getPid() {
+ return this.pid;
+ }
+ private String getPort() {
+ return this.port;
+ }
+ private String getStatusDescription() {
+ return this.statusDescription;
+ }
+ private String getStatusMessage() {
+ return this.statusMessage;
+ }
+ private Long getTimestampTime() {
+ return this.timestampTime;
+ }
+ private Long getUptime() {
+ return this.uptime;
+ }
+ private String getWorkingDirectory() {
+ return this.workingDirectory;
+ }
+
+ private String createStatusJson() {
+ final Map<String, Object> map = new HashMap<String, Object>();
+ map.put(ServiceState.JSON_CLASSPATH, getClasspath());
+ map.put(ServiceState.JSON_GEMFIREVERSION, getGemFireVersion());
+ map.put(ServiceState.JSON_HOST, getHost());
+ map.put(ServiceState.JSON_JAVAVERSION, getJavaVersion());
+ map.put(ServiceState.JSON_JVMARGUMENTS, getJvmArguments());
+ map.put(ServiceState.JSON_LOCATION, getServiceLocation());
+ map.put(ServiceState.JSON_LOGFILE, getLogFile());
+ map.put(ServiceState.JSON_MEMBERNAME, getMemberName());
+ map.put(ServiceState.JSON_PID, getPid());
+ map.put(ServiceState.JSON_PORT, getPort());
+ map.put(ServiceState.JSON_STATUS, getStatusDescription());
+ map.put(ServiceState.JSON_STATUSMESSAGE, getStatusMessage());
+ map.put(ServiceState.JSON_TIMESTAMP, getTimestampTime());
+ map.put(ServiceState.JSON_UPTIME, getUptime());
+ map.put(ServiceState.JSON_WORKINGDIRECTORY, getWorkingDirectory());
+ return new GfJsonObject(map).toString();
+ }
+}