Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test refactor and enhancement #1

Merged
merged 4 commits into from
Oct 3, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/test/java/com/hellblazer/process/HelloWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
*/
@SuppressWarnings("restriction")
public class HelloWorld implements RMIExporter {

public static final String STARTUP_MSG = "HelloWorld startup successful";

static void bindJmx() throws Exception {
JMXConnectorServer server;
// Ensure cryptographically strong random number generater used
Expand All @@ -64,6 +67,9 @@ static void bindJmx() throws Exception {
}

public static void main(String[] argv) throws Exception {

System.out.println(STARTUP_MSG);

if (argv[0].equals("-echo")) {
for (int i = 1; i < argv.length; i++) {
System.out.println(argv[i]);
Expand All @@ -84,8 +90,8 @@ public static void main(String[] argv) throws Exception {
System.in));
String line = null;
int i = 0;
while (line == null && i++ < 10) {
Thread.sleep(10);
while (line == null && i++ < 100) {
Thread.sleep(100);
line = reader.readLine();
}
if (line == null) {
Expand Down
244 changes: 80 additions & 164 deletions src/test/java/com/hellblazer/process/JavaProcessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,13 @@
*/
package com.hellblazer.process;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.*;
import java.net.ConnectException;
import java.net.URL;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.TimeUnit;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
Expand All @@ -40,6 +33,7 @@

import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListener;
import org.apache.commons.io.input.TailerListenerAdapter;

import com.hellblazer.process.impl.JavaProcessImpl;
import com.hellblazer.process.impl.ManagedProcessFactoryImpl;
Expand Down Expand Up @@ -110,54 +104,57 @@ protected void setUp() {
testDir = new File(TEST_DIR);
}

public void testClassExecution() throws Exception {
copyTestClassFile();
JavaProcess process = new JavaProcessImpl(processFactory.create());
process.setArguments(new String[] { "-echo", "foo", "bar", "baz" });
process.setJavaClass(HelloWorld.class.getCanonicalName());
assertNull("No jar file set", process.getJarFile());
private void launchProcess(JavaProcess process) throws IOException {
process.setDirectory(testDir);
process.setJavaExecutable(javaBin);

setupJavaClasspath(process);
process.start();
assertEquals("Process exited normally", 0, process.waitFor());
assertTrue("Process not active", !process.isActive());
BufferedReader reader = new BufferedReader(
new InputStreamReader(
process.getStdOut()));
assertTrue("Expected successful process start",
new ProcessStartWatcher(process).waitForSuccessfulStartup());

// Give the process a chance to do its thing before launching into
// evaluating output
try {
System.out.println("Waiting for process before evaluating output...");
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private void validateExpectedEchoLines(BufferedReader reader) throws IOException {
String line;
line = reader.readLine();
assertEquals("foo", line);
line = reader.readLine();
assertEquals("bar", line);
line = reader.readLine();
assertEquals("baz", line);
line = reader.readLine();
assertNull(line);
}

try {
line = reader.readLine();
public void testClassExecution() throws Exception {
copyTestClassFile();
JavaProcess process = new JavaProcessImpl(processFactory.create());
process.setArguments(new String[]{"-echo", "foo", "bar", "baz"});
process.setJavaClass(HelloWorld.class.getCanonicalName());
assertNull("No jar file set", process.getJarFile());

assertEquals("foo", line);
line = reader.readLine();
assertEquals("bar", line);
line = reader.readLine();
assertEquals("baz", line);
line = reader.readLine();
launchProcess(process);

assertNull(line);
} finally {
reader.close();
}
assertEquals("Process exited normally", 0, process.waitFor());
assertTrue("Process not active", !process.isActive());

try {
reader = new BufferedReader(
new InputStreamReader(
process.getStdErr()));
line = reader.readLine();
assertEquals("foo", line);
line = reader.readLine();
assertEquals("bar", line);
line = reader.readLine();
assertEquals("baz", line);
line = reader.readLine();
assertNull(line);
} finally {
reader.close();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getStdErr()))) {
validateExpectedEchoLines(reader);
}

try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getStdOut()))) {
assertEquals(HelloWorld.STARTUP_MSG, reader.readLine());
validateExpectedEchoLines(reader);
}
}

public void testExitValue() throws Exception {
Expand All @@ -167,54 +164,24 @@ public void testExitValue() throws Exception {
process.setJavaClass(HelloWorld.class.getCanonicalName());
process.setDirectory(testDir);
process.setJavaExecutable(javaBin);
process.start();

launchProcess(process);

assertEquals("Process exited abnormally", 66, process.waitFor());
assertTrue("Process not active", !process.isActive());
}

public void testJarExecution() throws Exception {
copyTestJarFile();
JavaProcess process = new JavaProcessImpl(processFactory.create());
process.setArguments(new String[] { "-echo", "foo", "bar", "baz" });
process.setArguments(new String[]{"-echo", "hello"});
process.setJarFile(new File(testDir, TEST_JAR));
assertNull("No java class set", process.getJavaClass());
process.setDirectory(testDir);
process.setJavaExecutable(javaBin);
process.start();
assertEquals("Process exited normally", 0, process.waitFor());
assertTrue("Process not active", !process.isActive());
BufferedReader reader = new BufferedReader(
new InputStreamReader(
process.getStdOut()));
String line;

try {
line = reader.readLine();

assertEquals("foo", line);
line = reader.readLine();
assertEquals("bar", line);
line = reader.readLine();
assertEquals("baz", line);
line = reader.readLine();
assertNull(line);
} finally {
reader.close();
}
launchProcess(process);

reader = new BufferedReader(new InputStreamReader(process.getStdErr()));
try {
line = reader.readLine();
assertEquals("foo", line);
line = reader.readLine();
assertEquals("bar", line);
line = reader.readLine();
assertEquals("baz", line);
line = reader.readLine();
assertNull(line);
} finally {
reader.close();
}
assertEquals("Process exited normally", 0, process.waitFor());
assertTrue("Process not active", !process.isActive());
}

public void testLocalMBeanServerConnection() throws Exception {
Expand All @@ -225,10 +192,8 @@ public void testLocalMBeanServerConnection() throws Exception {
process.setJavaClass(HelloWorld.class.getCanonicalName());
process.setDirectory(testDir);
process.setJavaExecutable(javaBin);
process.start();

assertTrue("process is active", process.isActive());
// Thread.sleep(3000);
launchProcess(process);

Condition condition = new Condition() {
@Override
Expand Down Expand Up @@ -263,100 +228,51 @@ public boolean isTrue() {
assertTrue("Process not active", !process.isActive());
}

public void testStdIn() throws Exception {
copyTestClassFile();
JavaProcess process = new JavaProcessImpl(processFactory.create());
String testLine = "hello";
process.setArguments(new String[] { "-readln", testLine });
process.setJavaClass(HelloWorld.class.getCanonicalName());
process.setDirectory(testDir);
process.setJavaExecutable(javaBin);
process.start();
PrintWriter writer = new PrintWriter(
new OutputStreamWriter(
process.getStdIn()));
try {
writer.println(testLine);
writer.flush();
} finally {
writer.close();
}

assertEquals("Process exited normally", 0, process.waitFor());
assertTrue("Process not active", !process.isActive());
BufferedReader reader = new BufferedReader(
new InputStreamReader(
process.getStdOut()));
try {
String line = reader.readLine();
assertEquals(testLine, line);
} finally {
reader.close();
}
}

public void testTailStdOut() throws Exception {
public void testTailStdInputOutputStreams() throws Exception {
final List<String> lines = new CopyOnWriteArrayList<>();
final AtomicReference<Tailer> t = new AtomicReference<>();
TailerListener listener = new TailerListener() {

@Override
public void init(Tailer tailer) {
t.set(tailer);
}

@Override
public void handle(Exception ex) {
}

TailerListener listener = new TailerListenerAdapter() {
@Override
public void handle(String line) {
lines.add(line);
}

@Override
public void fileRotated() {
}

@Override
public void fileNotFound() {
}
};

copyTestClassFile();
JavaProcess process = new JavaProcessImpl(processFactory.create());
String testLine = "hello";
process.setArguments(new String[] { "-readln", testLine });
process.setJavaClass(HelloWorld.class.getCanonicalName());
process.setDirectory(testDir);
process.setJavaExecutable(javaBin);
process.start();
process.tailStdOut(listener);
assertNotNull(t.get());
Thread tailerThread = new Thread(t.get());
tailerThread.setDaemon(true);
tailerThread.start();
PrintWriter writer = new PrintWriter(
new OutputStreamWriter(
process.getStdIn()));

launchProcess(process);

Tailer tailer = null;
try {
writer.println(testLine);
writer.flush();
} finally {
writer.close();
}
tailer = process.tailStdOut(listener);

assertEquals("Process exited normally", 0, process.waitFor());
assertTrue("Process not active", !process.isActive());
Utils.waitForCondition(1000, new Condition() {
@Override
public boolean isTrue() {
return lines.size() > 0;
try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(process.getStdIn()))) {
writer.println(testLine);
writer.flush();
}
});
assertEquals(2, lines.size());
assertEquals(testLine, lines.get(0));
assertEquals(testLine, lines.get(1));

t.get().stop();
assertEquals("Process exited normally", 0, process.waitFor());
assertTrue("Process not active", !process.isActive());
Utils.waitForCondition(1000, new Condition() {
@Override
public boolean isTrue() {
return lines.size() > 1;
}
});

assertEquals(2, lines.size());
assertEquals(testLine, lines.get(1));

tailer.stop();
} finally {
if (tailer != null) {
tailer.stop();
}
}
}
}
Loading