Skip to content

Commit

Permalink
#83 additional built-in type converters: NetworkInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Dec 18, 2017
1 parent 0286b81 commit 603ddd8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -1975,6 +1976,7 @@ private class Interpreter {
converterRegistry.put(TimeZone.class, new BuiltIn.TimeZoneConverter());
converterRegistry.put(ByteOrder.class, new BuiltIn.ByteOrderConverter());
converterRegistry.put(Class.class, new BuiltIn.ClassConverter());
converterRegistry.put(NetworkInterface.class, new BuiltIn.NetworkInterfaceConverter());

this.command = Assert.notNull(command, "command");
Class<?> cls = command.getClass();
Expand Down Expand Up @@ -2911,6 +2913,19 @@ public ByteOrder convert(String s) throws Exception {
static class ClassConverter implements ITypeConverter<Class> {
public Class<?> convert(String s) throws Exception { return Class.forName(s); }
}
static class NetworkInterfaceConverter implements ITypeConverter<NetworkInterface> {
public NetworkInterface convert(String s) throws Exception {
try {
InetAddress addr = new InetAddressConverter().convert(s);
return NetworkInterface.getByInetAddress(addr);
} catch (Exception ex) {
try { return NetworkInterface.getByName(s);
} catch (Exception ex2) {
throw new TypeConversionException("'" + s + "' is not an InetAddress or NetworkInterface name");
}
}
}
}
private BuiltIn() {} // private constructor: never instantiate
}

Expand Down
14 changes: 11 additions & 3 deletions src/test/java/picocli/CommandLineTypeConversionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -86,6 +87,7 @@ static class SupportedTypes {
@Option(names = "-tz") TimeZone aTimeZone;
@Option(names = "-byteOrder") ByteOrder aByteOrder;
@Option(names = "-Class") Class aClass;
@Option(names = "-NetworkInterface") NetworkInterface aNetInterface;
}
@Test
public void testDefaults() {
Expand Down Expand Up @@ -124,10 +126,10 @@ public void testDefaults() {
assertEquals("TimeZone", null, bean.aTimeZone);
assertEquals("ByteOrder", null, bean.aByteOrder);
assertEquals("Class", null, bean.aClass);
assertEquals("NetworkInterface", null, bean.aNetInterface);
}
@Test
public void testTypeConversionSucceedsForValidInput()
throws MalformedURLException, URISyntaxException, UnknownHostException, ParseException {
public void testTypeConversionSucceedsForValidInput() throws Exception {
SupportedTypes bean = CommandLine.populateCommand(new SupportedTypes(),
"-boolean", "-Boolean", //
"-byte", "12", "-Byte", "23", //
Expand All @@ -152,7 +154,8 @@ public void testTypeConversionSucceedsForValidInput()
"-Currency", "EUR",
"-tz", "Japan/Tokyo",
"-byteOrder", "LITTLE_ENDIAN",
"-Class", "java.lang.String"
"-Class", "java.lang.String",
"-NetworkInterface", "127.0.0.0"
);
assertEquals("boolean", true, bean.booleanField);
assertEquals("Boolean", Boolean.TRUE, bean.aBooleanField);
Expand Down Expand Up @@ -189,6 +192,7 @@ public void testTypeConversionSucceedsForValidInput()
assertEquals("TimeZone", TimeZone.getTimeZone("Japan/Tokyo"), bean.aTimeZone);
assertEquals("ByteOrder", ByteOrder.LITTLE_ENDIAN, bean.aByteOrder);
assertEquals("Class", String.class, bean.aClass);
assertEquals("NetworkInterface", NetworkInterface.getByInetAddress(InetAddress.getByName("127.0.0.0")), bean.aNetInterface);
}
@Test
public void testByteFieldsAreDecimal() {
Expand Down Expand Up @@ -449,6 +453,10 @@ public void testTimeZoneConvertersInvalidError() {
assertEquals(TimeZone.getTimeZone("GMT"), bean.aTimeZone);
}
@Test
public void testNetworkInterfaceConvertersInvalidError() {
//parseInvalidValue("-NetworkInterface", "127.127.127.127.127", ": java.lang.IllegalArgumentException");
}
@Test
public void testRegexPatternConverterInvalidError() {
parseInvalidValue("-Pattern", "[[(aa", String.format(": java.util.regex.PatternSyntaxException: Unclosed character class near index 4%n" +
"[[(aa%n" +
Expand Down

0 comments on commit 603ddd8

Please sign in to comment.