summaryrefslogtreecommitdiff
path: root/src/test/java/com/beust/jcommander/ParametersDelegateTest.java
blob: 46c7c6afa06e050a473ba1c999e56708480d8bae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.beust.jcommander;

import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author rodionmoiseev
 */
public class ParametersDelegateTest {

  @Test
  public void delegatingEmptyClassHasNoEffect() {
    class EmptyDelegate {
      public String nonParamString = "a";
    }
    class MainParams {
      @Parameter(names = "-a")
      public boolean isA;
      @Parameter(names = {"-b", "--long-b"})
      public String bValue = "";
      @ParametersDelegate
      public EmptyDelegate delegate = new EmptyDelegate();
    }

    MainParams p = new MainParams();
    JCommander cmd = new JCommander(p);
    cmd.parse("-a", "-b", "someValue");
    Assert.assertTrue(p.isA);
    Assert.assertEquals(p.bValue, "someValue");
    Assert.assertEquals(p.delegate.nonParamString, "a");
  }

  @Test
  public void delegatingSetsFieldsOnBothMainParamsAndTheDelegatedParams() {
    class ComplexDelegate {
      @Parameter(names = "-c")
      public boolean isC;
      @Parameter(names = {"-d", "--long-d"})
      public Integer d;
    }
    class MainParams {
      @Parameter(names = "-a")
      public boolean isA;
      @Parameter(names = {"-b", "--long-b"})
      public String bValue = "";
      @ParametersDelegate
      public ComplexDelegate delegate = new ComplexDelegate();
    }

    MainParams p = new MainParams();
    JCommander cmd = new JCommander(p);
    cmd.parse("-c", "--long-d", "123", "--long-b", "bValue");
    Assert.assertFalse(p.isA);
    Assert.assertEquals(p.bValue, "bValue");
    Assert.assertTrue(p.delegate.isC);
    Assert.assertEquals(p.delegate.d, Integer.valueOf(123));
  }

  @Test
  public void combinedAndNestedDelegates() {
    abstract class LeafAbstractDelegate {
      abstract float getFloat();
    }
    class LeafDelegate {
      @Parameter(names = "--list")
      public List<String> list = new ArrayList<String>() {{
        add("value1");
        add("value2");
      }};
      @Parameter(names = "--bool")
      public boolean bool;
    }
    class NestedDelegate1 {
      @ParametersDelegate
      public LeafDelegate leafDelegate = new LeafDelegate();
      @Parameter(names = {"-d", "--long-d"})
      public Integer d;
    }
    class NestedDelegate2 {
      @Parameter(names = "-c")
      public boolean isC;
      @ParametersDelegate
      public NestedDelegate1 nestedDelegate1 = new NestedDelegate1();
      @ParametersDelegate
      public LeafAbstractDelegate anonymousDelegate = new LeafAbstractDelegate() {
        @Parameter(names = "--anon-float")
        public float anon = 999f;

        @Override
        float getFloat() {
          return anon;
        }
      };
    }
    class MainParams {
      @Parameter(names = "-a")
      public boolean isA;
      @Parameter(names = {"-b", "--long-b"})
      public String bValue = "";
      @ParametersDelegate
      public NestedDelegate2 nestedDelegate2 = new NestedDelegate2();
    }

    MainParams p = new MainParams();
    JCommander cmd = new JCommander(p);
    cmd.parse("--anon-float 1.2 -d 234 --list a --list b -a".split(" "));
    Assert.assertEquals(p.nestedDelegate2.anonymousDelegate.getFloat(), 1.2f);
    Assert.assertEquals(p.nestedDelegate2.nestedDelegate1.leafDelegate.list, new ArrayList<String>() {{
      add("a");
      add("b");
    }});
    Assert.assertFalse(p.nestedDelegate2.nestedDelegate1.leafDelegate.bool);
    Assert.assertEquals(p.nestedDelegate2.nestedDelegate1.d, Integer.valueOf(234));
    Assert.assertFalse(p.nestedDelegate2.isC);
    Assert.assertTrue(p.isA);
    Assert.assertEquals(p.bValue, "");
  }

  @Test
  public void commandTest() {
    class Delegate {
      @Parameter(names = "-a")
      public String a = "b";
    }
    class Command {
      @ParametersDelegate
      public Delegate delegate = new Delegate();
    }

    Command c = new Command();

    JCommander cmd = new JCommander();
    cmd.addCommand("command", c);

    cmd.parse("command -a a".split(" "));
    Assert.assertEquals(c.delegate.a, "a");
  }

  @Test
  public void mainParametersTest() {
    class Delegate {
      @Parameter
      public List<String> mainParams = new ArrayList<String>();
    }
    class Command {
      @ParametersDelegate
      public Delegate delegate = new Delegate();
    }

    Command c = new Command();

    JCommander cmd = new JCommander();
    cmd.addCommand("command", c);

    cmd.parse("command main params".split(" "));
    Assert.assertEquals(c.delegate.mainParams, new ArrayList<String>() {{
      add("main");
      add("params");
    }});
  }

  @Test(expectedExceptions = ParameterException.class,
          expectedExceptionsMessageRegExp = ".*delegate.*null.*")
  public void nullDelegatesAreProhibited() {
    class ComplexDelegate {
    }
    class MainParams {
      @ParametersDelegate
      public ComplexDelegate delegate;
    }

    MainParams p = new MainParams();
    JCommander cmd = new JCommander(p);
    cmd.parse();
  }

  @Test(expectedExceptions = ParameterException.class,
          expectedExceptionsMessageRegExp = ".*-a.*")
  public void duplicateDelegateThrowDuplicateOptionException() {
    class Delegate {
      @Parameter(names = "-a")
      public String a;
    }
    class MainParams {
      @ParametersDelegate
      public Delegate d1 = new Delegate();
      @ParametersDelegate
      public Delegate d2 = new Delegate();
    }

    MainParams p = new MainParams();
    JCommander cmd = new JCommander(p);
    cmd.parse("-a value".split(" "));
  }

  @Test(expectedExceptions = ParameterException.class, expectedExceptionsMessageRegExp = "Only one.*is allowed.*")
  public void duplicateMainParametersAreNotAllowed() {
    class Delegate1 {
      @Parameter
      public List<String> mainParams1 = new ArrayList<String>();
    }
    class Delegate2 {
      @Parameter
      public List<String> mainParams2 = new ArrayList<String>();
    }
    class Command {
      @ParametersDelegate
      public Delegate1 delegate1 = new Delegate1();
      @ParametersDelegate
      public Delegate2 delegate2 = new Delegate2();
    }

    Command c = new Command();

    JCommander cmd = new JCommander();
    cmd.addCommand("command", c);

    cmd.parse("command main params".split(" "));
  }

  public static void main(String[] args) {
    new ParametersDelegateTest().commandTest();
  }
}