public void testPidUnavailableThrows() { final String name = "Name without PID"; try { ProcessUtils.identifyPid(name); fail("PidUnavailableException should have been thrown for " + name); } catch (PidUnavailableException e) { // passed } } }
public void testExistingPidFileThrows() throws Exception { final File pidFile = new File(getClass().getSimpleName() + File.separator + "testExistingPidFileThrows.pid"); assertTrue(pidFile.createNewFile()); assertTrue(pidFile.exists()); final FileWriter writer = new FileWriter(pidFile); // use a read pid that exists writer.write(String.valueOf(ProcessUtils.identifyPid())); writer.close(); try { new LocalProcessLauncher(pidFile, false); fail("LocalProcessLauncher should have thrown FileAlreadyExistsException"); } catch (FileAlreadyExistsException e) { // passed } }
public void testStalePidFileIsReplaced() throws Exception { final File pidFile = new File(getClass().getSimpleName() + File.separator + "testStalePidFileIsReplaced.pid"); assertTrue(pidFile.createNewFile()); assertTrue(pidFile.exists()); final FileWriter writer = new FileWriter(pidFile); writer.write(String.valueOf(Integer.MAX_VALUE)); writer.close(); try { new LocalProcessLauncher(pidFile, false); } catch (FileAlreadyExistsException e) { fail("LocalProcessLauncher should not have thrown FileAlreadyExistsException"); } final FileReader fr = new FileReader(pidFile); final BufferedReader br = new BufferedReader(fr); final int pid = Integer.parseInt(br.readLine()); br.close(); assertTrue(pid > 0); assertEquals(ProcessUtils.identifyPid(), pid); }
public void testForceReplacesExistingPidFile() throws Exception { assertTrue("testForceReplacesExistingPidFile is broken if PID == Integer.MAX_VALUE", ProcessUtils.identifyPid() != Integer.MAX_VALUE); final File pidFile = new File(getClass().getSimpleName() + File.separator + "testForceReplacesExistingPidFile.pid"); assertTrue(pidFile.createNewFile()); assertTrue(pidFile.exists()); final FileWriter writer = new FileWriter(pidFile); writer.write(String.valueOf(Integer.MAX_VALUE)); writer.close(); try { new LocalProcessLauncher(pidFile, true); } catch (FileAlreadyExistsException e) { fail("LocalProcessLauncher should not have thrown FileAlreadyExistsException"); } final FileReader fr = new FileReader(pidFile); final BufferedReader br = new BufferedReader(fr); final int pid = Integer.parseInt(br.readLine()); br.close(); assertTrue(pid > 0); assertEquals(ProcessUtils.identifyPid(), pid); }