1/**
2 * Copyright (C) 2010 the original author or authors.
3 * See the notice.md file distributed with this work for additional
4 * information regarding copyright ownership.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package com.beust.jcommander;
20
21import com.beust.jcommander.internal.Lists;
22import com.beust.jcommander.internal.Sets;
23import org.testng.Assert;
24import org.testng.annotations.Test;
25
26import java.util.Iterator;
27import java.util.List;
28import java.util.Set;
29
30/**
31 * Test behaviour of default parameter values
32 * @author rodionmoiseev
33 */
34public class DefaultValueTest {
35  @Test
36  public void emptyDefaultValueForListParameterStaysEmptyIfNotAssignedOrIsSetOtherwise() {
37    MyOptsWithEmptyDefaults opts = new MyOptsWithEmptyDefaults();
38    JCommander cmd = new JCommander(opts);
39    cmd.parse(new String[]{"-a", "anotherValue"});
40    Assert.assertEquals(opts.list.size(), 1);
41    Assert.assertEquals(opts.list.get(0), "anotherValue");
42    Assert.assertEquals(opts.set.size(), 0);
43  }
44
45  @Test
46  public void defaultValueForListParametersGetsOverwrittenWithSpecifiedValueOrStaysAsDefaultOtherwise() {
47    MyOptsWithDefaultValues opts = new MyOptsWithDefaultValues();
48    JCommander cmd = new JCommander(opts);
49    cmd.parse(new String[]{"-a", "anotherValue"});
50    Assert.assertEquals(opts.list.size(), 1);
51    Assert.assertEquals(opts.list.get(0), "anotherValue");
52    Assert.assertEquals(opts.set.size(), 1);
53    Assert.assertEquals(opts.set.iterator().next(), "defaultValue");
54  }
55
56  @Test
57  public void anyNumberOfValuesCanBeSetToListParameters_ForEmptyDefaults(){
58    MyOptsWithEmptyDefaults opts = new MyOptsWithEmptyDefaults();
59    testSettingMultipleValuesToListTypeParameters(opts);
60  }
61
62  @Test
63  public void anyNumberOfValuesCanBeSetToListParameters_ForNonEmptyDefaults(){
64    MyOptsWithDefaultValues opts = new MyOptsWithDefaultValues();
65    testSettingMultipleValuesToListTypeParameters(opts);
66  }
67
68  private void testSettingMultipleValuesToListTypeParameters(MyOpts opts) {
69    JCommander cmd = new JCommander(opts);
70    cmd.parse(new String[]{"-a", "anotherValue", "-a", "anotherValue2",
71                           "-b", "anotherValue3", "-b", "anotherValue4"});
72    Assert.assertEquals(opts.list.size(), 2);
73    Assert.assertEquals(opts.list.get(0), "anotherValue");
74    Assert.assertEquals(opts.list.get(1), "anotherValue2");
75    Assert.assertEquals(opts.set.size(), 2);
76    Iterator<String> arg2it = opts.set.iterator();
77    Assert.assertEquals(arg2it.next(), "anotherValue3");
78    Assert.assertEquals(arg2it.next(), "anotherValue4");
79  }
80
81  public static class MyOpts {
82    @Parameter(names = "-a")
83    public List<String> list;
84    @Parameter(names = "-b")
85    public Set<String> set;
86  }
87
88  public static final class MyOptsWithDefaultValues extends MyOpts {
89    public MyOptsWithDefaultValues(){
90      this.list = singletonList("defaultValue");
91      this.set = singletonSet("defaultValue");
92    }
93  }
94
95  public static final class MyOptsWithEmptyDefaults extends MyOpts {
96    public MyOptsWithEmptyDefaults(){
97      this.list = Lists.newArrayList();
98      this.set = Sets.newLinkedHashSet();
99    }
100  }
101
102  public static final List<String> singletonList(String value) {
103    List<String> list = Lists.newArrayList();
104    list.add(value);
105    return list;
106  }
107
108  public static final Set<String> singletonSet(String value){
109    Set<String> set = Sets.newLinkedHashSet();
110    set.add(value);
111    return set;
112  }
113}
114