1   /*
2    * Created on Nov 15, 2011
3    * Copyright 2010 by Eduard Weissmann (edi.weissmann@gmail.com).
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0 
10   * 
11   * Unless required by applicable law or agreed to in writing, software 
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   */
17  package com.lexicalscope.jewel.cli.examples;
18  
19  import static org.hamcrest.MatcherAssert.assertThat;
20  import static org.hamcrest.Matchers.*;
21  import static org.junit.Assert.*;
22  
23  import java.util.List;
24  
25  import org.junit.Test;
26  
27  import com.lexicalscope.jewel.cli.ArgumentValidationException;
28  import com.lexicalscope.jewel.cli.CliFactory;
29  import com.lexicalscope.jewel.cli.Option;
30  
31  /**
32   * @author Eduard Weissmann
33   */
34  public class TestOptionalMultivaluedOptions {
35      interface ExampleOptions {
36          @Option String getName();
37          boolean isName();
38  
39          @Option List<String> getOptionalMultivaluedOption();
40          boolean isOptionalMultivaluedOption();
41  
42          @Option(defaultValue = {}) List<String> getEmptyDefaultMultivaluedOption();
43          @Option(defaultToNull = true) List<String> getNullDefaultMultivaluedOption();
44      }
45  
46      @Test public void multivaluedOptionalOptionShouldReturnFalseIfOptionIsNotSpecified()
47              throws ArgumentValidationException {
48          final ExampleOptions result = CliFactory.createCli(ExampleOptions.class).parseArguments("--name", "foo");
49          assertTrue(result.isName());
50          assertFalse(result.isOptionalMultivaluedOption());
51      }
52  
53      @Test public void multivaluedOptionalOptionShouldReturnTrueIfOptionIsSpecified()
54              throws ArgumentValidationException {
55          final ExampleOptions result =
56                  CliFactory.createCli(ExampleOptions.class).parseArguments("--optionalMultivaluedOption", "foo");
57          assertThat(result.getOptionalMultivaluedOption(), contains("foo"));
58      }
59  
60      @Test public void multivaluedOptionalOptionShouldReturnFalseIfNothingIsSpecified()
61              throws ArgumentValidationException {
62          final ExampleOptions result = CliFactory.createCli(ExampleOptions.class).parseArguments();
63          assertFalse(result.isName());
64          assertFalse(result.isOptionalMultivaluedOption());
65      }
66  
67      @Test public void missingMultivaluedOptionalOptionShouldReturnNullIfQueried()
68              throws ArgumentValidationException {
69          final ExampleOptions result = CliFactory.createCli(ExampleOptions.class).parseArguments();
70          assertThat(result.getOptionalMultivaluedOption(), nullValue());
71      }
72  
73      @Test public void multivaluedOptionalWithEmptyListDefaultShouldReturnEmptyListWhenQueried()
74              throws ArgumentValidationException {
75          final ExampleOptions result = CliFactory.createCli(ExampleOptions.class).parseArguments();
76          assertThat(result.getEmptyDefaultMultivaluedOption().size(), equalTo(0));
77      }
78  
79      @Test public void multivaluedOptionalWithNullDefaultShouldReturnEmptyListWhenQueried()
80              throws ArgumentValidationException {
81          final ExampleOptions result = CliFactory.createCli(ExampleOptions.class).parseArguments();
82          assertThat(result.getNullDefaultMultivaluedOption(), nullValue());
83      }
84  }