HADOOP-9869. Configuration.getSocketAddr()/getEnum() should use getTrimmed(). Contributed by Tsuyoshi Ozawa.

This commit is contained in:
Akira Ajisaka 2015-02-12 15:50:48 -08:00
parent 04d09ecd56
commit 9395c76697
3 changed files with 35 additions and 3 deletions

View File

@ -597,6 +597,9 @@ Release 2.7.0 - UNRELEASED
HADOOP-11586. Update use of Iterator to Iterable in HADOOP-11586. Update use of Iterator to Iterable in
AbstractMetricsContext.java. (Ray Chiang via aajisaka) AbstractMetricsContext.java. (Ray Chiang via aajisaka)
HADOOP-9869. Configuration.getSocketAddr()/getEnum() should use
getTrimmed(). (Tsuyoshi Ozawa via aajisaka)
OPTIMIZATIONS OPTIMIZATIONS
HADOOP-11323. WritableComparator#compare keeps reference to byte array. HADOOP-11323. WritableComparator#compare keeps reference to byte array.

View File

@ -1491,13 +1491,14 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
/** /**
* Return value matching this enumerated type. * Return value matching this enumerated type.
* Note that the returned value is trimmed by this method.
* @param name Property name * @param name Property name
* @param defaultValue Value returned if no mapping exists * @param defaultValue Value returned if no mapping exists
* @throws IllegalArgumentException If mapping is illegal for the type * @throws IllegalArgumentException If mapping is illegal for the type
* provided * provided
*/ */
public <T extends Enum<T>> T getEnum(String name, T defaultValue) { public <T extends Enum<T>> T getEnum(String name, T defaultValue) {
final String val = get(name); final String val = getTrimmed(name);
return null == val return null == val
? defaultValue ? defaultValue
: Enum.valueOf(defaultValue.getDeclaringClass(), val); : Enum.valueOf(defaultValue.getDeclaringClass(), val);
@ -1594,6 +1595,7 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
* Get the value of the <code>name</code> property as a <code>Pattern</code>. * Get the value of the <code>name</code> property as a <code>Pattern</code>.
* If no such property is specified, or if the specified value is not a valid * If no such property is specified, or if the specified value is not a valid
* <code>Pattern</code>, then <code>DefaultValue</code> is returned. * <code>Pattern</code>, then <code>DefaultValue</code> is returned.
* Note that the returned value is NOT trimmed by this method.
* *
* @param name property name * @param name property name
* @param defaultValue default value * @param defaultValue default value
@ -2044,7 +2046,7 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
*/ */
public InetSocketAddress getSocketAddr( public InetSocketAddress getSocketAddr(
String name, String defaultAddress, int defaultPort) { String name, String defaultAddress, int defaultPort) {
final String address = get(name, defaultAddress); final String address = getTrimmed(name, defaultAddress);
return NetUtils.createSocketAddr(address, defaultPort, name); return NetUtils.createSocketAddr(address, defaultPort, name);
} }

View File

@ -56,6 +56,7 @@ public class TestConfiguration extends TestCase {
private Configuration conf; private Configuration conf;
final static String CONFIG = new File("./test-config-TestConfiguration.xml").getAbsolutePath(); final static String CONFIG = new File("./test-config-TestConfiguration.xml").getAbsolutePath();
final static String CONFIG2 = new File("./test-config2-TestConfiguration.xml").getAbsolutePath(); final static String CONFIG2 = new File("./test-config2-TestConfiguration.xml").getAbsolutePath();
final static String CONFIG_FOR_ENUM = new File("./test-config-enum-TestConfiguration.xml").getAbsolutePath();
private static final String CONFIG_MULTI_BYTE = new File( private static final String CONFIG_MULTI_BYTE = new File(
"./test-config-multi-byte-TestConfiguration.xml").getAbsolutePath(); "./test-config-multi-byte-TestConfiguration.xml").getAbsolutePath();
private static final String CONFIG_MULTI_BYTE_SAVED = new File( private static final String CONFIG_MULTI_BYTE_SAVED = new File(
@ -76,6 +77,7 @@ public class TestConfiguration extends TestCase {
super.tearDown(); super.tearDown();
new File(CONFIG).delete(); new File(CONFIG).delete();
new File(CONFIG2).delete(); new File(CONFIG2).delete();
new File(CONFIG_FOR_ENUM).delete();
new File(CONFIG_MULTI_BYTE).delete(); new File(CONFIG_MULTI_BYTE).delete();
new File(CONFIG_MULTI_BYTE_SAVED).delete(); new File(CONFIG_MULTI_BYTE_SAVED).delete();
} }
@ -792,6 +794,7 @@ public class TestConfiguration extends TestCase {
conf.setEnum("test.enum", Dingo.FOO); conf.setEnum("test.enum", Dingo.FOO);
assertSame(Dingo.FOO, conf.getEnum("test.enum", Dingo.BAR)); assertSame(Dingo.FOO, conf.getEnum("test.enum", Dingo.BAR));
assertSame(Yak.FOO, conf.getEnum("test.enum", Yak.RAB)); assertSame(Yak.FOO, conf.getEnum("test.enum", Yak.RAB));
conf.setEnum("test.enum", Dingo.FOO);
boolean fail = false; boolean fail = false;
try { try {
conf.setEnum("test.enum", Dingo.BAR); conf.setEnum("test.enum", Dingo.BAR);
@ -802,6 +805,26 @@ public class TestConfiguration extends TestCase {
assertTrue(fail); assertTrue(fail);
} }
public void testEnumFromXml() throws IOException {
out=new BufferedWriter(new FileWriter(CONFIG_FOR_ENUM));
startConfig();
appendProperty("test.enum"," \t \n FOO \t \n");
appendProperty("test.enum2"," \t \n Yak.FOO \t \n");
endConfig();
Configuration conf = new Configuration();
Path fileResource = new Path(CONFIG_FOR_ENUM);
conf.addResource(fileResource);
assertSame(Yak.FOO, conf.getEnum("test.enum", Yak.FOO));
boolean fail = false;
try {
conf.getEnum("test.enum2", Yak.FOO);
} catch (IllegalArgumentException e) {
fail = true;
}
assertTrue(fail);
}
public void testTimeDuration() { public void testTimeDuration() {
Configuration conf = new Configuration(false); Configuration conf = new Configuration(false);
conf.setTimeDuration("test.time.a", 7L, SECONDS); conf.setTimeDuration("test.time.a", 7L, SECONDS);
@ -919,6 +942,10 @@ public class TestConfiguration extends TestCase {
addr = conf.getSocketAddr("myAddress", defaultAddr, defaultPort); addr = conf.getSocketAddr("myAddress", defaultAddr, defaultPort);
assertEquals("host2:3", NetUtils.getHostPortString(addr)); assertEquals("host2:3", NetUtils.getHostPortString(addr));
conf.set("myAddress", " \n \t host4:5 \t \n ");
addr = conf.getSocketAddr("myAddress", defaultAddr, defaultPort);
assertEquals("host4:5", NetUtils.getHostPortString(addr));
boolean threwException = false; boolean threwException = false;
conf.set("myAddress", "bad:-port"); conf.set("myAddress", "bad:-port");
try { try {