On Mar 8, 2009, at 5:34 PM, David Blevins wrote:
Technically speaking the java.util.Properties class will do this
already (which I only discovered a few months ago), but you have to
pass the parent Properties instance in the constructor of the child
properties object which we aren't doing just yet. We could
definitely do this for the properties in the system instance that
default to the system properties.
On second thought it doesn't look like such a good idea to rely on the
java.util.Properties object inheritance. The Properties specific
methods do the parent delegation nicely, but all java.util.Map methods
(size, get, set, keySet, entrySet, values) do not reflect the parent's
data. Seems very error prone.
Here's a little test case:
public void testPropertiesInheritance() {
Properties system = new Properties();
system.setProperty("color", "red");
system.setProperty("shape", "round");
system.setProperty("texture", "matte");
Properties systemInstance = new Properties(system);
systemInstance.setProperty("color", "orange");
systemInstance.setProperty("weight", "15");
systemInstance.setProperty("height", "2");
// The good parts
assertEquals("SystemInstance.getProperty(\"shape\")",
"round", systemInstance.getProperty("shape"));
assertEquals("SystemInstance.getProperty(\"texture\")",
"matte", systemInstance.getProperty("texture"));
assertEquals("SystemInstance.getProperty(\"color\")",
"orange", systemInstance.getProperty("color"));
assertEquals("SystemInstance.getProperty(\"weight\")", "15",
systemInstance.getProperty("weight"));
assertEquals("SystemInstance.getProperty(\"height\")", "2",
systemInstance.getProperty("height"));
ArrayList<?> names =
Collections.list(systemInstance.propertyNames());
assertEquals("Names.size()", 5, names.size());
// update "system" and check "systemInstance"
system.setProperty("shape", "square");
assertEquals("SystemInstance.getProperty(\"shape\")",
"square", systemInstance.getProperty("shape"));
// The bad, all java.util.Map methods do not reflect this
inheritance
assertEquals("SystemInstance.size()", 3,
systemInstance.size());
assertEquals("SystemInstance.get(\"shape\")", null,
systemInstance.get("shape"));
assertEquals("SystemInstance.get(\"texture\")", null,
systemInstance.get("texture"));
assertEquals("SystemInstance.get(\"color\")", "orange",
systemInstance.get("color"));
assertEquals("SystemInstance.get(\"weight\")", "15",
systemInstance.get("weight"));
assertEquals("SystemInstance.get(\"height\")", "2",
systemInstance.get("height"));
}
-David