From 08c47d7e6286329be7f1ffabcbc3ddf460602c70 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 12 May 2003 14:00:10 +0000 Subject: [PATCH] Add nested s to and . git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274562 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 +- .../org/apache/tools/ant/taskdefs/Java.java | 10 ++++ .../taskdefs/optional/junit/JUnitTask.java | 14 ++++++ .../tools/ant/types/CommandlineJava.java | 46 ++++++++++++++++--- .../apache/tools/ant/types/PropertySet.java | 11 +++-- .../tools/ant/types/CommandlineJavaTest.java | 12 ++++- 6 files changed, 85 insertions(+), 11 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 5990ae873..92beb8c24 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -306,7 +306,8 @@ Other changes: defaultexcludes task. Bugzilla Report 12700. * There is a new data type that can be used to collect - properties. + properties. It is supported by , , , + and . * can now control the encoding of the output as well and optionally add new-line characters at the end of files that get concatenated but diff --git a/src/main/org/apache/tools/ant/taskdefs/Java.java b/src/main/org/apache/tools/ant/taskdefs/Java.java index 2d9b9ae5f..8bb3fde07 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Java.java +++ b/src/main/org/apache/tools/ant/taskdefs/Java.java @@ -65,6 +65,7 @@ import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.CommandlineJava; import org.apache.tools.ant.types.Environment; import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.types.PropertySet; import org.apache.tools.ant.types.Reference; /** @@ -303,6 +304,15 @@ public class Java extends Task { cmdl.addSysproperty(sysp); } + /** + * Adds a set of properties as system properties. + * + * @since Ant 1.6 + */ + public void addSyspropertyset(PropertySet sysp) { + cmdl.addSyspropertyset(sysp); + } + /** * If true, then fail if the command exits with a * returncode other than 0 diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java index 3bbee5c18..298cfb5c0 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java @@ -75,6 +75,7 @@ import org.apache.tools.ant.types.CommandlineJava; import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.types.Environment; import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.types.PropertySet; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.LoaderUtils; import junit.framework.AssertionFailedError; @@ -402,6 +403,19 @@ public class JUnitTask extends Task { commandline.addSysproperty(sysp); } + /** + * Adds a set of properties that will be used as system properties + * that tests can access. + * + * This might be useful to tranfer Ant properties to the + * testcases when JVM forking is not enabled. + * + * @since Ant 1.6 + */ + public void addSyspropertyset(PropertySet sysp) { + commandline.addSyspropertyset(sysp); + } + /** * Adds path to classpath used for tests. * diff --git a/src/main/org/apache/tools/ant/types/CommandlineJava.java b/src/main/org/apache/tools/ant/types/CommandlineJava.java index cdda4857f..a55b9c9f7 100644 --- a/src/main/org/apache/tools/ant/types/CommandlineJava.java +++ b/src/main/org/apache/tools/ant/types/CommandlineJava.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2000-2002 The Apache Software Foundation. All rights + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -99,22 +99,37 @@ public class CommandlineJava implements Cloneable { */ public static class SysProperties extends Environment implements Cloneable { Properties sys = null; + private Vector propertySets = new Vector(); public String[] getVariables() throws BuildException { String[] props = super.getVariables(); + Properties p = mergePropertySets(); if (props == null) { - return null; + if (p.size() == 0) { + return null; + } else { + props = new String[0]; + } } - for (int i = 0; i < props.length; i++) { - props[i] = "-D" + props[i]; + String[] result = new String[props.length + p.size()]; + int i = 0; + for (; i < props.length; i++) { + result[i] = "-D" + props[i]; + } + for (Enumeration enum = p.keys(); enum.hasMoreElements();) { + String key = (String) enum.nextElement(); + String value = p.getProperty(key); + result[i++] = "-D" + key + "=" + value; } - return props; + + return result; } public int size() { - return variables.size(); + Properties p = mergePropertySets(); + return variables.size() + p.size(); } public void setSystem() throws BuildException { @@ -125,6 +140,7 @@ public class CommandlineJava implements Cloneable { Object o = e.nextElement(); p.put(o, sys.get(o)); } + p.putAll(mergePropertySets()); for (Enumeration e = variables.elements(); e.hasMoreElements();) { Environment.Variable v = (Environment.Variable) e.nextElement(); p.put(v.getKey(), v.getValue()); @@ -152,12 +168,26 @@ public class CommandlineJava implements Cloneable { try { SysProperties c = (SysProperties) super.clone(); c.variables = (Vector) variables.clone(); + c.propertySets = (Vector) propertySets.clone(); return c; } catch (CloneNotSupportedException e) { return null; } } + public void addSyspropertyset(PropertySet ps) { + propertySets.addElement(ps); + } + + private Properties mergePropertySets() { + Properties p = new Properties(); + for (Enumeration e = propertySets.elements(); + e.hasMoreElements();) { + PropertySet ps = (PropertySet) e.nextElement(); + p.putAll(ps.getProperties()); + } + return p; + } } /** @@ -180,6 +210,10 @@ public class CommandlineJava implements Cloneable { sysProperties.addVariable(sysp); } + public void addSyspropertyset(PropertySet sysp) { + sysProperties.addSyspropertyset(sysp); + } + public void setVm(String vm) { vmCommand.setExecutable(vm); } diff --git a/src/main/org/apache/tools/ant/types/PropertySet.java b/src/main/org/apache/tools/ant/types/PropertySet.java index 9819860e6..410983248 100644 --- a/src/main/org/apache/tools/ant/types/PropertySet.java +++ b/src/main/org/apache/tools/ant/types/PropertySet.java @@ -174,11 +174,11 @@ public class PropertySet extends DataType { } public boolean getDynamic() { - return getRef().dynamic; + return isReference() ? getRef().dynamic : dynamic; } public Mapper getMapper() { - return getRef()._mapper; + return isReference() ? getRef()._mapper : _mapper; } public Properties getProperties() { @@ -187,7 +187,12 @@ public class PropertySet extends DataType { if (getDynamic() || cachedNames == null) { names = new Vector(); // :TODO: should be a Set! - getRef().addPropertyNames(names, prj.getProperties()); + if (isReference()) { + getRef().addPropertyNames(names, prj.getProperties()); + } else { + addPropertyNames(names, prj.getProperties()); + } + if (!getDynamic()) { cachedNames = names; } diff --git a/src/testcases/org/apache/tools/ant/types/CommandlineJavaTest.java b/src/testcases/org/apache/tools/ant/types/CommandlineJavaTest.java index 04830f077..65effc1fd 100644 --- a/src/testcases/org/apache/tools/ant/types/CommandlineJavaTest.java +++ b/src/testcases/org/apache/tools/ant/types/CommandlineJavaTest.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2000-2002 The Apache Software Foundation. All rights + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -143,6 +143,13 @@ public class CommandlineJavaTest extends TestCase { v.setKey("key"); v.setValue("value"); c.addSysproperty(v); + + project.setProperty("key2", "value2"); + PropertySet ps = new PropertySet(); + ps.setProject(project); + ps.appendName("key2"); + c.addSyspropertyset(ps); + try { c.setSystemProperties(); String newClasspath = System.getProperty("java.class.path"); @@ -151,10 +158,13 @@ public class CommandlineJavaTest extends TestCase { assertNotNull(System.getProperty("key")); assertEquals("value", System.getProperty("key")); assertTrue(System.getProperties().containsKey("java.class.path")); + assertNotNull(System.getProperty("key2")); + assertEquals("value2", System.getProperty("key2")); } finally { c.restoreSystemProperties(); } assertNull(System.getProperty("key")); + assertNull(System.getProperty("key2")); } }