1package com.beust.jcommander;
2
3import com.beust.jcommander.validators.PositiveInteger;
4
5import org.testng.annotations.Test;
6
7public class PositiveIntegerTest {
8
9  @Test
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(new String[] { "-p", "8080" });
18
19  }
20
21  @Test(expectedExceptions = ParameterException.class)
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(new String[] { "-p", "" });
30  }
31
32  @Test(expectedExceptions = ParameterException.class)
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(new String[] { "-p", "-1" });
41  }
42
43  @Test(expectedExceptions = ParameterException.class)
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(new String[] { "-p", "abc" });
52  }
53
54  @Test(expectedExceptions = ParameterException.class)
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(new String[] { "--port", " " });
64  }
65}