-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PathOptionHandler for automatic parsing of Path objects in java 7
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.kohsuke.args4j.spi; | ||
|
||
import org.kohsuke.args4j.CmdLineException; | ||
import org.kohsuke.args4j.CmdLineParser; | ||
import org.kohsuke.args4j.OptionDef; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* Created with IntelliJ IDEA. | ||
* User: kmahoney | ||
* Date: 6/6/13 | ||
* Time: 8:17 AM | ||
*/ | ||
public class PathOptionHandler extends OneArgumentOptionHandler<Path> { | ||
public PathOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Path> setter) { | ||
super(parser, option, setter); | ||
} | ||
|
||
@Override | ||
protected Path parse(String argument) throws NumberFormatException, CmdLineException { | ||
try { | ||
return Paths.get(argument); | ||
} | ||
catch (Exception e) { | ||
throw new CmdLineException(owner, "Failed to Parse Path: " + argument, e); | ||
} | ||
} | ||
|
||
@Override | ||
public String getDefaultMetaVariable() { | ||
return "PATH"; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
args4j/test/org/kohsuke/args4j/spi/PathOptionHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.kohsuke.args4j.spi; | ||
|
||
import junit.framework.TestCase; | ||
import org.kohsuke.args4j.CmdLineException; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* Created with IntelliJ IDEA. | ||
* User: kmahoney | ||
* Date: 6/7/13 | ||
* Time: 6:09 AM | ||
*/ | ||
|
||
public class PathOptionHandlerTest extends TestCase { | ||
static final String TEST_1 = "/path/test"; | ||
static final String TEST_2 = "bad/path/\0"; | ||
|
||
private PathOptionHandler handler; | ||
|
||
@Override | ||
public void setUp() { | ||
handler = new PathOptionHandler(null, null, null); | ||
} | ||
|
||
public void testParseSuccess() throws Exception { | ||
Path expectedIp = Paths.get(TEST_1); | ||
Path ip = handler.parse(TEST_1); | ||
|
||
assertEquals(expectedIp, ip); | ||
} | ||
|
||
public void testParseFailure() throws Exception { | ||
try { | ||
handler.parse(TEST_2); | ||
} | ||
catch (CmdLineException e) { | ||
//assertEquals("\"a.b.c.d\" must be an IP address", e.getMessage()); | ||
return; | ||
} | ||
fail("Invalid Path Should Have Thrown Exception"); | ||
} | ||
public void testNullParseFailure() throws Exception { | ||
try { | ||
handler.parse(null); | ||
} | ||
catch (CmdLineException e) { | ||
//assertEquals("\"a.b.c.d\" must be an IP address", e.getMessage()); | ||
return; | ||
} | ||
fail("Null Path Should Have Thrown Exception"); | ||
} | ||
} |