• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 package com.beust.jcommander;
20 
21 import com.beust.jcommander.args.ArgsDefault;
22 import com.beust.jcommander.defaultprovider.PropertyFileDefaultProvider;
23 
24 import org.testng.Assert;
25 import org.testng.annotations.Test;
26 
27 public class DefaultProviderTest {
28   private static final IDefaultProvider DEFAULT_PROVIDER = new IDefaultProvider() {
29 
30     public String getDefaultValueFor(String optionName) {
31       return "-debug".equals(optionName) ? "false" : "42";
32     }
33 
34   };
35 
defaultProvider(IDefaultProvider provider, String... args)36   private ArgsDefault defaultProvider(IDefaultProvider provider, String... args) {
37     ArgsDefault a = new ArgsDefault();
38     JCommander jc = new JCommander(a);
39     jc.setDefaultProvider(provider);
40 
41     jc.parse(args);
42     return a;
43   }
44 
45   @Test
defaultProvider1()46   public void defaultProvider1() {
47     ArgsDefault a = defaultProvider(DEFAULT_PROVIDER, "f");
48 
49     Assert.assertEquals(a.groups, "42");
50     Assert.assertEquals(a.level, 42);
51     Assert.assertEquals(a.log.intValue(), 42);
52   }
53 
54   @Test
defaultProvider2()55   public void defaultProvider2() {
56     ArgsDefault a = defaultProvider(DEFAULT_PROVIDER, "-groups", "foo", "f");
57 
58     Assert.assertEquals(a.groups, "foo");
59     Assert.assertEquals(a.level, 42);
60     Assert.assertEquals(a.log.intValue(), 42);
61   }
62 
63   @Test
defaultProvider3()64   public void defaultProvider3() {
65     ArgsDefault a = defaultProvider(DEFAULT_PROVIDER, "-groups", "foo", "-level", "13", "f");
66 
67     Assert.assertEquals(a.groups, "foo");
68     Assert.assertEquals(a.level, 13);
69     Assert.assertEquals(a.log.intValue(), 42);
70   }
71 
72   @Test
defaultProvider4()73   public void defaultProvider4() {
74     ArgsDefault a = defaultProvider(DEFAULT_PROVIDER,
75         "-log", "19", "-groups", "foo", "-level", "13", "f");
76 
77     Assert.assertEquals(a.groups, "foo");
78     Assert.assertEquals(a.level, 13);
79     Assert.assertEquals(a.log.intValue(), 19);
80   }
81 
82   @Test
propertyFileDefaultProvider1()83   public void propertyFileDefaultProvider1() {
84     ArgsDefault a = defaultProvider(new PropertyFileDefaultProvider(), "f");
85 
86     Assert.assertEquals(a.groups, "unit");
87     Assert.assertEquals(a.level, 17);
88     Assert.assertEquals(a.log.intValue(), 18);
89   }
90 
91   @Test
propertyFileDefaultProvider2()92   public void propertyFileDefaultProvider2() {
93     ArgsDefault a = defaultProvider(new PropertyFileDefaultProvider(), "-groups", "foo", "f");
94 
95     Assert.assertEquals(a.groups, "foo");
96     Assert.assertEquals(a.level, 17);
97     Assert.assertEquals(a.log.intValue(), 18);
98   }
99 
100   @Test
propertyFileDefaultProvider3()101   public void propertyFileDefaultProvider3() {
102     ArgsDefault a = defaultProvider(new PropertyFileDefaultProvider(),
103         "-groups", "foo", "-level", "13", "f");
104 
105     Assert.assertEquals(a.groups, "foo");
106     Assert.assertEquals(a.level, 13);
107     Assert.assertEquals(a.log.intValue(), 18);
108   }
109 
110   @Test
propertyFileDefaultProvider4()111   public void propertyFileDefaultProvider4() {
112     ArgsDefault a = defaultProvider(new PropertyFileDefaultProvider(),
113         "-log", "19", "-groups", "foo", "-level", "13", "f");
114 
115     Assert.assertEquals(a.groups, "foo");
116     Assert.assertEquals(a.level, 13);
117     Assert.assertEquals(a.log.intValue(), 19);
118   }
119 
120 }
121