410522 - jetty start broken for command line options

+ Adding tests for the different forms of property handling in start.jar
This commit is contained in:
Joakim Erdfelt 2013-06-12 09:14:36 -07:00
parent d93faf589c
commit 5cd478c0fd
4 changed files with 296 additions and 0 deletions

View File

@ -0,0 +1,41 @@
//
// ========================================================================
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.start;
import java.util.Enumeration;
import java.util.Properties;
public class PropertyDump
{
public static void main(String[] args)
{
Properties props = System.getProperties();
Enumeration<?> names = props.propertyNames();
while (names.hasMoreElements())
{
String name = (String)names.nextElement();
// only interested in "test." prefixed properties
if (name.startsWith("test."))
{
System.out.printf("%s=%s%n",name,props.getProperty(name));
}
}
System.exit(0);
}
}

View File

@ -0,0 +1,245 @@
//
// ========================================================================
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.start;
import static org.hamcrest.Matchers.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.toolchain.test.IO;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.OS;
import org.eclipse.jetty.toolchain.test.TestingDir;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
public class PropertyPassingTest
{
private static class ConsoleCapture implements Runnable
{
private String mode;
private BufferedReader reader;
private StringWriter output;
public ConsoleCapture(String mode, InputStream is)
{
this.mode = mode;
this.reader = new BufferedReader(new InputStreamReader(is));
this.output = new StringWriter();
}
@Override
public void run()
{
String line;
try (PrintWriter out = new PrintWriter(output))
{
while ((line = reader.readLine()) != (null))
{
out.println(line);
}
}
catch (IOException ignore)
{
/* ignore */
}
finally
{
IO.close(reader);
}
}
public String getConsoleOutput()
{
return output.toString();
}
public ConsoleCapture start()
{
Thread thread = new Thread(this,"ConsoleCapture/" + mode);
thread.start();
return this;
}
}
@Rule
public TestingDir testingdir = new TestingDir();
@Test
public void testAsJvmArg() throws IOException, InterruptedException
{
File testCfg = MavenTestingUtils.getTestResourceFile("property-dump-start.config");
File bogusXml = MavenTestingUtils.getTestResourceFile("bogus.xml");
// Setup command line
List<String> commands = new ArrayList<>();
commands.add(getJavaBin());
commands.add("-cp");
commands.add(getClassPath());
// addDebug(commands);
commands.add("-Dtest.foo=bar"); // TESTING THIS
commands.add(getStartJarBin());
commands.add("--config=" + testCfg.getAbsolutePath());
commands.add(bogusXml.getAbsolutePath());
// Run command, collect output
String output = collectRunOutput(commands);
// Test for values
Assert.assertThat("output",output,containsString("foo=bar"));
}
@Test
@Ignore("not working yet")
public void testAsCommandLineArg() throws IOException, InterruptedException
{
File testCfg = MavenTestingUtils.getTestResourceFile("property-dump-start.config");
File bogusXml = MavenTestingUtils.getTestResourceFile("bogus.xml");
// Setup command line
List<String> commands = new ArrayList<>();
commands.add(getJavaBin());
commands.add("-cp");
commands.add(getClassPath());
// addDebug(commands);
commands.add(getStartJarBin());
commands.add("test.foo=bar"); // TESTING THIS
commands.add("--config=" + testCfg.getAbsolutePath());
commands.add(bogusXml.getAbsolutePath());
// Run command, collect output
String output = collectRunOutput(commands);
// Test for values
Assert.assertThat("output",output,containsString("foo=bar"));
}
@Test
public void testAsDashDCommandLineArg() throws IOException, InterruptedException
{
File testCfg = MavenTestingUtils.getTestResourceFile("property-dump-start.config");
File bogusXml = MavenTestingUtils.getTestResourceFile("bogus.xml");
// Setup command line
List<String> commands = new ArrayList<>();
commands.add(getJavaBin());
commands.add("-cp");
commands.add(getClassPath());
// addDebug(commands);
commands.add(getStartJarBin());
commands.add("-Dtest.foo=bar"); // TESTING THIS
commands.add("--config=" + testCfg.getAbsolutePath());
commands.add(bogusXml.getAbsolutePath());
// Run command, collect output
String output = collectRunOutput(commands);
// Test for values
Assert.assertThat("output",output,containsString("foo=bar"));
}
private String getClassPath()
{
StringBuilder cp = new StringBuilder();
String pathSep = System.getProperty("path.separator");
cp.append(MavenTestingUtils.getProjectDir("target/classes"));
cp.append(pathSep);
cp.append(MavenTestingUtils.getProjectDir("target/test-classes"));
return cp.toString();
}
protected void addDebug(List<String> commands)
{
commands.add("-Xdebug");
commands.add("-Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=y");
}
private String collectRunOutput(List<String> commands) throws IOException, InterruptedException
{
StringBuilder cline = new StringBuilder();
for (String command : commands)
{
cline.append(command).append(" ");
}
System.out.println("Command line: " + cline);
ProcessBuilder builder = new ProcessBuilder(commands);
Process pid = builder.start();
ConsoleCapture stdOutPump = new ConsoleCapture("STDOUT",pid.getInputStream()).start();
ConsoleCapture stdErrPump = new ConsoleCapture("STDERR",pid.getErrorStream()).start();
int exitCode = pid.waitFor();
if(exitCode != 0) {
System.out.printf("STDERR: [" + stdErrPump.getConsoleOutput() + "]%n");
System.out.printf("STDOUT: [" + stdOutPump.getConsoleOutput() + "]%n");
Assert.assertThat("Exit code",exitCode,is(0));
}
return stdOutPump.getConsoleOutput();
}
private String getStartJarBin()
{
return org.eclipse.jetty.start.Main.class.getName();
}
private String getJavaBin()
{
File javaHome = new File(System.getProperty("java.home"));
if (!javaHome.exists())
{
return null;
}
File javabin = findExecutable(javaHome,"bin/java");
if (javabin != null)
{
return javabin.getAbsolutePath();
}
javabin = findExecutable(javaHome,"bin/java.exe");
if (javabin != null)
{
return javabin.getAbsolutePath();
}
return "java";
}
private File findExecutable(File root, String path)
{
String npath = OS.separators(path);
File exe = new File(root,npath);
if (!exe.exists())
{
return null;
}
return exe;
}
}

View File

@ -0,0 +1,2 @@
<?xml version="1.0"?>
<Configure />

View File

@ -0,0 +1,8 @@
org.eclipse.jetty.start.PropertyDump.class
[*]
$(basedir)/src/test/resources
[default]
$(basedir)/src/test/resources