1 package com.beust.jcommander; 2 3 import com.beust.jcommander.validators.PositiveInteger; 4 5 import org.testng.annotations.Test; 6 7 public class PositiveIntegerTest { 8 9 @Test validateTest()10 public void validateTest() { 11 class Arg { 12 @Parameter(names = { "-p", "--port" }, description = "Shows help", validateWith = PositiveInteger.class) 13 private int port = 0; 14 } 15 Arg arg = new Arg(); 16 JCommander jc = new JCommander(arg); 17 jc.parse("-p", "8080"); 18 19 } 20 21 @Test(expectedExceptions = ParameterException.class) validateTest2()22 public void validateTest2() { 23 class Arg { 24 @Parameter(names = { "-p", "--port" }, description = "Shows help", validateWith = PositiveInteger.class) 25 private int port = 0; 26 } 27 Arg arg = new Arg(); 28 JCommander jc = new JCommander(arg); 29 jc.parse("-p", ""); 30 } 31 32 @Test(expectedExceptions = ParameterException.class) validateTest3()33 public void validateTest3() { 34 class Arg { 35 @Parameter(names = { "-p", "--port" }, description = "Shows help", validateWith = PositiveInteger.class) 36 private int port = 0; 37 } 38 Arg arg = new Arg(); 39 JCommander jc = new JCommander(arg); 40 jc.parse("-p", "-1"); 41 } 42 43 @Test(expectedExceptions = ParameterException.class) validateTest4()44 public void validateTest4() { 45 class Arg { 46 @Parameter(names = { "-p", "--port" }, description = "Port Number", validateWith = PositiveInteger.class) 47 private int port = 0; 48 } 49 Arg arg = new Arg(); 50 JCommander jc = new JCommander(arg); 51 jc.parse("-p", "abc"); 52 } 53 54 @Test(expectedExceptions = ParameterException.class) validateTest5()55 public void validateTest5() { 56 class Arg { 57 @Parameter(names = { "-p", "--port" }, description = "Port Number", validateWith = PositiveInteger.class) 58 private int port = 0; 59 } 60 61 Arg arg = new Arg(); 62 JCommander jc = new JCommander(arg); 63 jc.parse("--port", " "); 64 } 65 }