git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274562 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -306,7 +306,8 @@ Other changes: | |||
| defaultexcludes task. Bugzilla Report 12700. | |||
| * There is a new data type <propertyset> that can be used to collect | |||
| properties. | |||
| properties. It is supported by <ant>, <antcall>, <subant>, <java> | |||
| and <junit>. | |||
| * <concat> 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 | |||
| @@ -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 | |||
| @@ -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. | |||
| * | |||
| @@ -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); | |||
| } | |||
| @@ -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; | |||
| } | |||
| @@ -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")); | |||
| } | |||
| } | |||