http://git-wip-us.apache.org/repos/asf/geode/blob/894f3ee7/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteIntegrationTest.java b/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteIntegrationTest.java index 47e512a..cc42a53 100755 --- a/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteIntegrationTest.java +++ b/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteIntegrationTest.java @@ -14,1043 +14,251 @@ */ package org.apache.geode.distributed; -import static org.apache.geode.distributed.ConfigurationProperties.MCAST_PORT; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.apache.geode.distributed.AbstractLauncher.Status.NOT_RESPONDING; +import static org.apache.geode.distributed.AbstractLauncher.Status.ONLINE; +import static org.apache.geode.distributed.AbstractLauncher.Status.STOPPED; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.PrintStream; -import java.lang.management.ManagementFactory; +import java.net.BindException; import java.net.InetAddress; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.atomic.AtomicBoolean; -import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.experimental.categories.Category; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.apache.geode.distributed.AbstractLauncher.Status; import org.apache.geode.distributed.LocatorLauncher.Builder; import org.apache.geode.distributed.LocatorLauncher.LocatorState; -import org.apache.geode.distributed.internal.DistributionConfig; -import org.apache.geode.internal.AvailablePort; -import org.apache.geode.internal.DistributionLocator; -import org.apache.geode.internal.ExitCode; import org.apache.geode.internal.GemFireVersion; -import org.apache.geode.internal.logging.InternalLogWriter; -import org.apache.geode.internal.logging.LocalLogWriter; -import org.apache.geode.internal.net.SocketCreatorFactory; import org.apache.geode.internal.process.ProcessControllerFactory; -import org.apache.geode.internal.process.ProcessStreamReader; -import org.apache.geode.internal.process.ProcessType; -import org.apache.geode.internal.process.ProcessUtils; -import org.apache.geode.test.junit.categories.FlakyTest; import org.apache.geode.test.junit.categories.IntegrationTest; -import org.apache.geode.test.junit.runners.CategoryWithParameterizedRunnerFactory; /** - * Integration tests for launching a Locator in a forked process. + * Integration tests for using {@code LocatorLauncher} as an application main in a forked JVM. * * @since GemFire 8.0 */ @Category(IntegrationTest.class) -@RunWith(Parameterized.class) -@Parameterized.UseParametersRunnerFactory(CategoryWithParameterizedRunnerFactory.class) -public class LocatorLauncherRemoteIntegrationTest - extends AbstractLocatorLauncherRemoteIntegrationTestCase { - - protected volatile Process process; - protected volatile ProcessStreamReader processOutReader; - protected volatile ProcessStreamReader processErrReader; +public class LocatorLauncherRemoteIntegrationTest extends LocatorLauncherRemoteIntegrationTestCase { @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(); - } + public void setUpLocatorLauncherRemoteIntegrationTest() throws Exception { + assertThat(new ProcessControllerFactory().isAttachAPIFound()).isTrue(); } @Test - public void testIsAttachAPIFound() throws Exception { - final ProcessControllerFactory factory = new ProcessControllerFactory(); - assertTrue(factory.isAttachAPIFound()); - } + public void startCreatesPidFile() throws Exception { + startLocator(); - @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 { - * assertIndexDetailsEquals(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(); - * - * assertIndexDetailsEquals(Status.ONLINE, - * locatorLauncher.status().getStatus()); - * assertIndexDetailsEquals(Status.STOPPED, - * locatorLauncher.stop().getStatus()); } - */ + assertThat(getPidFile()).exists(); + } - @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(); + public void pidFileContainsServerPid() throws Exception { + startLocator(); - 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); - } + assertThatPidIsAlive(getLocatorPid()); } - @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()); + public void startCreatesLogFile() throws Exception { + startLocator(); - // 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); - } + assertThat(getLogFile()).exists(); } - @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); + public void startDeletesStaleControlFiles() throws Exception { + File stopRequestFile = givenControlFile(getStopRequestFileName()); + File statusRequestFile = givenControlFile(getStatusRequestFileName()); + File statusFile = givenControlFile(getStatusFileName()); - 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); - } + startLocator(); - // stop the locator - try { - assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); - waitForPidToStop(pid); - } catch (Throwable e) { - this.errorCollector.addError(e); - } + assertDeletionOf(stopRequestFile); + assertDeletionOf(statusRequestFile); + assertDeletionOf(statusFile); } - @Category(FlakyTest.class) // GEODE-764: BindException + /** + * This test takes > 1 minute to run in {@link LocatorLauncherRemoteFileIntegrationTest}. + */ @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); + public void startOverwritesStalePidFile() throws Exception { + givenPidFile(fakePid); - // 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); + startLocator(); - // 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); - } + assertThat(getLocatorPid()).isNotEqualTo(fakePid); } + /** + * This test takes > 1 minute to run in {@link LocatorLauncherRemoteFileIntegrationTest}. + */ @Test - public void testStartUsingPortInUseFails() throws Throwable { - this.socket = SocketCreatorFactory - .createNonDefaultInstance(false, false, null, null, System.getProperties()) - .createServerSocket(this.locatorPort, 50, null, -1); - this.locatorPort = this.socket.getLocalPort(); - - 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(); + public void startWithForceOverwritesExistingPidFile() throws Exception { + givenPidFile(localPid); - 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(); + startLocator(withForce()); - // 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()))); + assertThatPidIsAlive(getLocatorPid()); + assertThat(getLocatorPid()).isNotEqualTo(localPid); } @Test - public void testStartWithDefaultPortInUseFails() throws Throwable { - String expectedString = "java.net.BindException"; - AtomicBoolean outputContainedExpectedString = new AtomicBoolean(); - - this.socket = SocketCreatorFactory - .createNonDefaultInstance(false, false, null, null, System.getProperties()) - .createServerSocket(this.locatorPort, 50, null, -1); - this.locatorPort = this.socket.getLocalPort(); - - 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); - } + public void startWithLocatorPortInUseFailsWithBindException() throws Exception { + givenLocatorPortInUse(nonDefaultLocatorPort); - try { - // check the status - final LocatorState locatorState = dirLauncher.status(); - assertNotNull(locatorState); - assertEquals(Status.NOT_RESPONDING, locatorState.getStatus()); + startLocatorShouldFail(withPort(nonDefaultLocatorPort)); - // 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 + assertThatProcessIsNotAlive(getLocatorProcess()); + assertThatLocatorThrew(BindException.class); + } - // validate that output contained BindException - this.errorCollector.checkThat(outputContainedExpectedString.get(), is(equalTo(true))); + @Test + public void startWithDefaultPortInUseFailsWithBindException() throws Exception { + givenLocatorPortInUse(defaultLocatorPort); - // just in case the launcher started... - LocatorState status = null; - try { - status = dirLauncher.stop(); - } catch (Throwable t) { - // ignore - } + startLocatorShouldFail(); - this.errorCollector.checkThat(status.getStatus(), - is(equalTo(getExpectedStopStatusForNotRunning()))); + assertThatProcessIsNotAlive(getLocatorProcess()); + assertThatLocatorThrew(BindException.class); } @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); - * assertIndexDetailsEquals(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 - */ + public void statusWithPidReturnsOnlineWithDetails() throws Exception { + givenRunningLocator(); + + LocatorState locatorState = new Builder().setPid(getLocatorPid()).build().status(); + + assertThat(locatorState.getStatus()).isEqualTo(ONLINE); + assertThat(locatorState.getClasspath()).isEqualTo(getClassPath()); + assertThat(locatorState.getGemFireVersion()).isEqualTo(GemFireVersion.getGemFireVersion()); + assertThat(locatorState.getHost()).isEqualTo(InetAddress.getLocalHost().getCanonicalHostName()); + assertThat(locatorState.getJavaVersion()).isEqualTo(System.getProperty("java.version")); + assertThat(locatorState.getJvmArguments()).isEqualTo(getJvmArguments()); + assertThat(locatorState.getLogFile()).isEqualTo(getLogFile().getCanonicalPath()); + assertThat(locatorState.getMemberName()).isEqualTo(getUniqueName()); + assertThat(locatorState.getPid().intValue()).isEqualTo(getLocatorPid()); + assertThat(locatorState.getUptime()).isGreaterThan(0); + assertThat(locatorState.getWorkingDirectory()).isEqualTo(getWorkingDirectoryPath()); + } @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); - } + public void statusWithWorkingDirectoryReturnsOnlineWithDetails() throws Exception { + givenRunningLocator(); + + LocatorState locatorState = + new Builder().setWorkingDirectory(getWorkingDirectoryPath()).build().status(); + + assertThat(locatorState.getStatus()).isEqualTo(ONLINE); + assertThat(locatorState.getClasspath()).isEqualTo(getClassPath()); + assertThat(locatorState.getGemFireVersion()).isEqualTo(GemFireVersion.getGemFireVersion()); + assertThat(locatorState.getHost()).isEqualTo(InetAddress.getLocalHost().getCanonicalHostName()); + assertThat(locatorState.getJavaVersion()).isEqualTo(System.getProperty("java.version")); + assertThat(locatorState.getJvmArguments()).isEqualTo(getJvmArguments()); + assertThat(locatorState.getLogFile()).isEqualTo(getLogFile().getCanonicalPath()); + assertThat(locatorState.getMemberName()).isEqualTo(getUniqueName()); + assertThat(locatorState.getPid().intValue()).isEqualTo(readPidFile()); + assertThat(locatorState.getUptime()).isGreaterThan(0); + assertThat(locatorState.getWorkingDirectory()).isEqualTo(getWorkingDirectoryPath()); } - @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()); + public void statusWithEmptyPidFileThrowsIllegalArgumentException() throws Exception { + givenEmptyPidFile(); - assertNotNull(dirLauncher); - assertFalse(dirLauncher.isRunning()); + LocatorLauncher launcher = new Builder().setWorkingDirectory(getWorkingDirectoryPath()).build(); - // 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); - } + assertThatThrownBy(() -> launcher.status()).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Invalid pid 'null' found in"); } @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())); + public void statusWithEmptyWorkingDirectoryReturnsNotRespondingWithDetails() throws Exception { + givenEmptyWorkingDirectory(); + + LocatorState locatorState = + new Builder().setWorkingDirectory(getWorkingDirectoryPath()).build().status(); + + assertThat(locatorState.getStatus()).isEqualTo(NOT_RESPONDING); + assertThat(locatorState.getClasspath()).isNull(); + assertThat(locatorState.getGemFireVersion()).isEqualTo(GemFireVersion.getGemFireVersion()); + assertThat(locatorState.getHost()).isNull(); + assertThat(locatorState.getJavaVersion()).isNull(); + assertThat(locatorState.getLogFile()).isNull(); + assertThat(locatorState.getMemberName()).isNull(); + assertThat(locatorState.getPid()).isNull(); + assertThat(locatorState.getUptime().intValue()).isEqualTo(0); + assertThat(locatorState.getWorkingDirectory()).isEqualTo(getWorkingDirectoryPath()); } + /** + * This test takes > 1 minute to run in {@link LocatorLauncherRemoteFileIntegrationTest}. + */ @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()); - } + public void statusWithStalePidFileReturnsNotResponding() throws Exception { + givenPidFile(fakePid); - @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); + LocatorState locatorState = + new Builder().setWorkingDirectory(getWorkingDirectoryPath()).build().status(); - 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())); + assertThat(locatorState.getStatus()).isEqualTo(NOT_RESPONDING); } @Test - public void testStopUsingPid() throws Throwable { - final List<String> jvmArguments = getJvmArguments(); + public void stopWithPidReturnsStopped() throws Exception { + givenRunningLocator(); - 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"); + LocatorState serverState = new Builder().setPid(getLocatorPid()).build().stop(); - 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); - } + assertThat(serverState.getStatus()).isEqualTo(STOPPED); } - @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); + public void stopWithPidStopsLocatorProcess() throws Exception { + givenRunningLocator(); - // 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)); + new Builder().setPid(getLocatorPid()).build().stop(); - // 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); - } + assertStopOf(getLocatorProcess()); } - /** - * Used only by - * {@link LocatorLauncherRemoteIntegrationTest#testRunningLocatorOutlivesForkingProcess} - */ - public static class LocatorLauncherForkingProcess { + @Test + public void stopWithPidDeletesPidFile() throws Exception { + givenRunningLocator(); - public static void main(final String... args) throws FileNotFoundException { - File file = new File(System.getProperty("user.dir"), - LocatorLauncherForkingProcess.class.getSimpleName().concat(".log")); + new Builder().setPid(getLocatorPid()).build().stop(); - LocalLogWriter logWriter = new LocalLogWriter(InternalLogWriter.ALL_LEVEL, - new PrintStream(new FileOutputStream(file, true))); + assertDeletionOf(getPidFile()); + } - try { - final int port = Integer.parseInt(args[0]); + @Test + public void stopWithWorkingDirectoryReturnsStopped() throws Exception { + givenRunningLocator(); - // 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("-D" + DistributionConfig.GEMFIRE_PREFIX + 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"); + LocatorState serverState = + new Builder().setWorkingDirectory(getWorkingDirectoryPath()).build().stop(); - logWriter.info( - LocatorLauncherForkingProcess.class.getSimpleName() + "#main command: " + command); - logWriter.info(LocatorLauncherForkingProcess.class.getSimpleName() + "#main starting..."); + assertThat(serverState.getStatus()).isEqualTo(STOPPED); + } - Process forkedProcess = new ProcessBuilder(command).start(); + @Test + public void stopWithWorkingDirectoryStopsLocatorProcess() throws Exception { + givenRunningLocator(); - @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(); + new Builder().setWorkingDirectory(getWorkingDirectoryPath()).build().stop(); - logWriter.info(LocatorLauncherForkingProcess.class.getSimpleName() - + "#main waiting for locator to start..."); + assertStopOf(getLocatorProcess()); + } - waitForLocatorToStart(port, TIMEOUT_MILLISECONDS, 10, true); + @Test + public void stopWithWorkingDirectoryDeletesPidFile() throws Exception { + givenRunningLocator(); - logWriter.info(LocatorLauncherForkingProcess.class.getSimpleName() + "#main exiting..."); + new Builder().setWorkingDirectory(getWorkingDirectoryPath()).build().stop(); - ExitCode.NORMAL.doSystemExit(); - } catch (Throwable t) { - logWriter.info(LocatorLauncherForkingProcess.class.getSimpleName() + "#main error: " + t, - t); - System.exit(-1); - } - } + assertDeletionOf(getPidFile()); } }
http://git-wip-us.apache.org/repos/asf/geode/blob/894f3ee7/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteIntegrationTestCase.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteIntegrationTestCase.java b/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteIntegrationTestCase.java new file mode 100644 index 0000000..b74eccc --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteIntegrationTestCase.java @@ -0,0 +1,234 @@ +/* + * 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 org.apache.geode.distributed; + +import static java.util.concurrent.TimeUnit.MINUTES; +import static org.apache.geode.internal.DistributionLocator.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY; +import static org.apache.geode.internal.process.ProcessUtils.isProcessAlive; +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.net.BindException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.junit.After; +import org.junit.Before; + +import org.apache.geode.distributed.AbstractLauncher.Status; +import org.apache.geode.distributed.LocatorLauncher.Builder; +import org.apache.geode.distributed.LocatorLauncher.Command; +import org.apache.geode.distributed.internal.DistributionConfig; +import org.apache.geode.internal.process.ProcessStreamReader; +import org.apache.geode.internal.process.ProcessStreamReader.InputListener; + +/** + * Abstract base class for integration tests of {@link LocatorLauncher} as an application main in a + * forked JVM. + * + * @since GemFire 8.0 + */ +public abstract class LocatorLauncherRemoteIntegrationTestCase + extends LocatorLauncherIntegrationTestCase implements UsesLocatorCommand { + + private final AtomicBoolean threwBindException = new AtomicBoolean(); + + private volatile Process process; + private volatile ProcessStreamReader processOutReader; + private volatile ProcessStreamReader processErrReader; + + private LocatorCommand locatorCommand; + + @Before + public void setUp() throws Exception { + locatorCommand = new LocatorCommand(this); + } + + @After + public void tearDownAbstractLocatorLauncherRemoteIntegrationTestCase() throws Exception { + if (process != null) { + process.destroy(); + } + if (processOutReader != null && processOutReader.isRunning()) { + processOutReader.stop(); + } + if (processErrReader != null && processErrReader.isRunning()) { + processErrReader.stop(); + } + } + + @Override + public List<String> getJvmArguments() { + List<String> jvmArguments = new ArrayList<>(); + jvmArguments.add("-D" + DistributionConfig.GEMFIRE_PREFIX + "log-level=config"); + jvmArguments + .add("-D" + TEST_OVERRIDE_DEFAULT_PORT_PROPERTY + "=" + String.valueOf(defaultLocatorPort)); + return jvmArguments; + } + + @Override + public String getName() { + return getUniqueName(); + } + + protected void assertStopOf(final Process process) { + await().until(() -> assertThat(process.isAlive()).isFalse()); + } + + /** + * Please leave unused parameter throwableClass for improved readability. + */ + protected void assertThatLocatorThrew(Class<? extends Throwable> throwableClass) { + assertThat(threwBindException.get()).isTrue(); + } + + protected void assertThatPidIsAlive(final int pid) { + assertThat(pid).isGreaterThan(0); + assertThat(isProcessAlive(pid)).isTrue(); + } + + protected void assertThatProcessIsNotAlive(final Process process) { + assertThat(process.isAlive()).isFalse(); + } + + protected LocatorLauncher givenRunningLocator() { + return givenRunningLocator(new LocatorCommand(this).withCommand(Command.START)); + } + + protected LocatorLauncher givenRunningLocator(final LocatorCommand command) { + return awaitStart(command); + } + + protected LocatorCommand addJvmArgument(final String arg) { + return locatorCommand.addJvmArgument(arg); + } + + protected LocatorCommand withForce() { + return withForce(true); + } + + protected LocatorCommand withForce(final boolean value) { + return locatorCommand.force(value); + } + + protected LocatorCommand withPort(final int port) { + return locatorCommand.withPort(port); + } + + protected Process getLocatorProcess() { + return process; + } + + @Override + protected LocatorLauncher startLocator() { + return awaitStart(locatorCommand); + } + + protected LocatorLauncher startLocator(final LocatorCommand command) { + return awaitStart(command); + } + + protected LocatorLauncher startLocator(final LocatorCommand command, + final InputListener outListener, final InputListener errListener) { + executeCommandWithReaders(command.create(), outListener, errListener); + LocatorLauncher launcher = awaitStart(getWorkingDirectory()); + assertThat(process.isAlive()).isTrue(); + return launcher; + } + + protected void startLocatorShouldFail(final LocatorCommand command) throws InterruptedException { + awaitStartFail(command, createBindExceptionListener("sysout", threwBindException), + createBindExceptionListener("syserr", threwBindException)); + + } + + protected void startLocatorShouldFail() throws InterruptedException { + startLocatorShouldFail(locatorCommand); + } + + private void assertThatProcessIsNotAlive() { + assertThatProcessIsNotAlive(process); + } + + private void awaitStartFail(final LocatorCommand command, final InputListener outListener, + final InputListener errListener) throws InterruptedException { + executeCommandWithReaders(command.create(), outListener, errListener); + process.waitFor(2, MINUTES); + assertThatProcessIsNotAlive(); + assertThat(process.exitValue()).isEqualTo(1); + } + + private LocatorLauncher awaitStart(final File workingDirectory) { + try { + launcher = new Builder().setWorkingDirectory(workingDirectory.getCanonicalPath()).build(); + awaitStart(launcher); + assertThat(process.isAlive()).isTrue(); + return launcher; + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + private LocatorLauncher awaitStart(final LocatorCommand command) { + executeCommandWithReaders(command); + LocatorLauncher launcher = awaitStart(getWorkingDirectory()); + assertThat(process.isAlive()).isTrue(); + return launcher; + } + + @Override + protected LocatorLauncher awaitStart(final LocatorLauncher launcher) { + await().until(() -> assertThat(launcher.status().getStatus()).isEqualTo(Status.ONLINE)); + assertThat(process.isAlive()).isTrue(); + return launcher; + } + + private InputListener createBindExceptionListener(final String name, + final AtomicBoolean threwBindException) { + return createExpectedListener(name, BindException.class.getName(), threwBindException); + } + + private void executeCommandWithReaders(final List<String> command) { + try { + process = new ProcessBuilder(command).directory(getWorkingDirectory()).start(); + processOutReader = new ProcessStreamReader.Builder(process) + .inputStream(process.getInputStream()).build().start(); + processErrReader = new ProcessStreamReader.Builder(process) + .inputStream(process.getErrorStream()).build().start(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + private void executeCommandWithReaders(final List<String> command, + final InputListener outListener, final InputListener errListener) { + try { + process = new ProcessBuilder(command).directory(getWorkingDirectory()).start(); + processOutReader = new ProcessStreamReader.Builder(process) + .inputStream(process.getInputStream()).inputListener(outListener).build().start(); + processErrReader = new ProcessStreamReader.Builder(process) + .inputStream(process.getErrorStream()).inputListener(errListener).build().start(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + private void executeCommandWithReaders(final LocatorCommand command) { + executeCommandWithReaders(command.create()); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/894f3ee7/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteWithCustomLoggingIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteWithCustomLoggingIntegrationTest.java b/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteWithCustomLoggingIntegrationTest.java index 34e11e0..eb4c17a 100644 --- a/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteWithCustomLoggingIntegrationTest.java +++ b/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherRemoteWithCustomLoggingIntegrationTest.java @@ -15,120 +15,64 @@ package org.apache.geode.distributed; import static org.apache.geode.internal.logging.log4j.custom.CustomConfiguration.CONFIG_LAYOUT_PREFIX; +import static org.apache.geode.internal.logging.log4j.custom.CustomConfiguration.createConfigFileIn; +import static org.apache.logging.log4j.core.config.ConfigurationFactory.CONFIGURATION_FILE_PROPERTY; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.*; import java.io.File; -import java.util.ArrayList; -import java.util.List; +import java.io.IOException; +import java.io.UncheckedIOException; -import org.apache.geode.test.junit.runners.CategoryWithParameterizedRunnerFactory; -import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.SystemOutRule; import org.junit.experimental.categories.Category; -import org.apache.geode.internal.logging.log4j.custom.CustomConfiguration; -import org.apache.geode.internal.process.ProcessStreamReader; -import org.apache.geode.internal.process.ProcessType; -import org.apache.geode.internal.process.ProcessUtils; +import org.apache.geode.distributed.LocatorLauncher.Command; +import org.apache.geode.internal.process.ProcessStreamReader.InputListener; import org.apache.geode.test.junit.categories.IntegrationTest; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; /** - * Integration tests for launching a Locator in a forked process with custom logging configuration + * Integration tests for using {@code LocatorLauncher} as an application main in a forked JVM with + * custom logging configuration. */ @Category(IntegrationTest.class) -@RunWith(Parameterized.class) -@Parameterized.UseParametersRunnerFactory(CategoryWithParameterizedRunnerFactory.class) public class LocatorLauncherRemoteWithCustomLoggingIntegrationTest - extends AbstractLocatorLauncherRemoteIntegrationTestCase { + extends LocatorLauncherRemoteIntegrationTestCase { - private File customConfigFile; + private File customLoggingConfigFile; @Rule public SystemOutRule systemOutRule = new SystemOutRule().enableLog(); @Before public void setUpLocatorLauncherRemoteWithCustomLoggingIntegrationTest() throws Exception { - this.customConfigFile = CustomConfiguration.createConfigFileIn(this.temporaryFolder.getRoot()); + this.customLoggingConfigFile = createConfigFileIn(getWorkingDirectory()); } @Test - public void testStartUsesCustomLoggingConfiguration() throws Throwable { - // build and start the locator - final List<String> jvmArguments = getJvmArguments(); + public void startWithCustomLoggingConfiguration() throws Exception { + startLocator(new LocatorCommand(this) + .addJvmArgument("-D" + CONFIGURATION_FILE_PROPERTY + "=" + getCustomLoggingConfigFilePath()) + .withCommand(Command.START), new ToSystemOut(), new ToSystemOut()); - 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("-D" + ConfigurationFactory.CONFIGURATION_FILE_PROPERTY + "=" - + this.customConfigFile.getCanonicalPath()); - 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(new File(this.workingDirectory)).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; - 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 LocatorLauncher.LocatorState locatorState = this.launcher.status(); - assertNotNull(locatorState); - assertEquals(AbstractLauncher.Status.ONLINE, locatorState.getStatus()); - - assertThat(systemOutRule.getLog()) - .contains("log4j.configurationFile = " + this.customConfigFile.getCanonicalPath()); - assertThat(systemOutRule.getLog()).contains(CONFIG_LAYOUT_PREFIX); - - } catch (Throwable e) { - this.errorCollector.addError(e); - } + assertThat(systemOutRule.getLog()).contains(CONFIG_LAYOUT_PREFIX) + .contains("log4j.configurationFile = " + getCustomLoggingConfigFilePath()); + } - // stop the locator + private String getCustomLoggingConfigFilePath() { try { - assertEquals(AbstractLauncher.Status.STOPPED, this.launcher.stop().getStatus()); - waitForPidToStop(pid); - } catch (Throwable e) { - this.errorCollector.addError(e); + return customLoggingConfigFile.getCanonicalPath(); + } catch (IOException e) { + throw new UncheckedIOException(e); } } - private static class ToSystemOut implements ProcessStreamReader.InputListener { + private static class ToSystemOut implements InputListener { @Override - public void notifyInputLine(String line) { + public void notifyInputLine(final String line) { System.out.println(line); } } - } http://git-wip-us.apache.org/repos/asf/geode/blob/894f3ee7/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherTest.java b/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherTest.java index bc7c371..4206187 100644 --- a/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherTest.java +++ b/geode-core/src/test/java/org/apache/geode/distributed/LocatorLauncherTest.java @@ -14,63 +14,26 @@ */ package org.apache.geode.distributed; -import static org.apache.geode.distributed.ConfigurationProperties.NAME; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import org.apache.geode.distributed.LocatorLauncher.Builder; -import org.apache.geode.distributed.LocatorLauncher.Command; -import org.apache.geode.distributed.internal.DistributionConfig; -import org.apache.geode.distributed.internal.InternalDistributedSystem; -import org.apache.geode.distributed.internal.InternalLocator; -import org.apache.geode.internal.DistributionLocator; -import org.apache.geode.internal.i18n.LocalizedStrings; -import org.apache.geode.test.junit.categories.FlakyTest; -import org.apache.geode.test.junit.categories.UnitTest; -import org.junit.Before; -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; -import java.net.InetAddress; -import java.net.UnknownHostException; -import joptsimple.OptionException; +import org.apache.geode.distributed.internal.InternalLocator; +import org.apache.geode.test.junit.categories.UnitTest; /** - * The LocatorLauncherTest class is a test suite of test cases for testing the contract and - * functionality of launching a GemFire Locator. + * Unit tests for {@link LocatorLauncher}. * - * @see org.apache.geode.distributed.LocatorLauncher - * @see org.apache.geode.distributed.LocatorLauncher.Builder - * @see org.apache.geode.distributed.LocatorLauncher.Command - * @see org.junit.Assert - * @see org.junit.Test * @since GemFire 7.0 */ @Category(UnitTest.class) public class LocatorLauncherTest { - @Rule - public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties(); - - @Rule - public final TestName testName = new TestName(); - - @Before - public void setUp() throws Exception { - assertThat(InternalDistributedSystem.getConnectedInstance()).isNull(); - } - @Test - public void shouldBeMockable() throws Exception { + public void canBeMocked() throws Exception { LocatorLauncher mockLocatorLauncher = mock(LocatorLauncher.class); InternalLocator mockInternalLocator = mock(InternalLocator.class); @@ -80,303 +43,4 @@ public class LocatorLauncherTest { assertThat(mockLocatorLauncher.getLocator()).isSameAs(mockInternalLocator); assertThat(mockLocatorLauncher.getId()).isEqualTo("ID"); } - - @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; - } - } - - @Category(FlakyTest.class) // GEODE-1308 - @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()); - } - - @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(expected = IllegalArgumentException.class) - public void testSetHostnameForClientsWithNullString() { - try { - new Builder().setHostnameForClients(null); - } 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()); - } - - @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(expected = IllegalArgumentException.class) - public void testSetMemberNameWithNullString() { - try { - new Builder().setMemberName(null); - } 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(Integer.valueOf(DistributionLocator.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(Integer.valueOf(DistributionLocator.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(); - - assertThat(launcher).isNotNull(); - 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).set(NAME, "locatorABC").build(); - - assertThat(launcher).isNotNull(); - assertEquals(LocatorLauncher.Command.START, launcher.getCommand()); - assertNull(launcher.getMemberName()); - assertEquals("locatorABC", launcher.getProperties().getProperty(NAME)); - } - - @Test - public void testBuildWithMemberNameSetInSystemPropertiesOnStart() { - System.setProperty(DistributionConfig.GEMFIRE_PREFIX + NAME, "locatorXYZ"); - - LocatorLauncher launcher = new Builder().setCommand(LocatorLauncher.Command.START).build(); - - assertThat(launcher).isNotNull(); - assertEquals(LocatorLauncher.Command.START, launcher.getCommand()); - assertNull(launcher.getMemberName()); - } }