1   package com.lexicalscope.jewel.cli;
2   
3   import static com.lexicalscope.jewel.cli.CliFactory.parseArguments;
4   import static com.lexicalscope.jewel.cli.ArgumentValidationExceptionMatcher.hasValidationException;
5   import static org.hamcrest.Matchers.*;
6   import static org.junit.Assert.*;
7   
8   import java.util.Collections;
9   import java.util.List;
10  
11  import org.junit.Rule;
12  import org.junit.Test;
13  import org.junit.rules.ExpectedException;
14  
15  /*
16   * Copyright 2011 Tim Wood
17   *
18   * Licensed under the Apache License, Version 2.0 (the "License");
19   * you may not use this file except in compliance with the License.
20   * You may obtain a copy of the License at
21   *
22   * http://www.apache.org/licenses/LICENSE-2.0
23   *
24   * Unless required by applicable law or agreed to in writing, software
25   * distributed under the License is distributed on an "AS IS" BASIS,
26   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27   * See the License for the specific language governing permissions and
28   * limitations under the License.
29   */
30  
31  public class TestUnparsedArguments {
32      @Rule public final ExpectedException exception = ExpectedException.none();
33  
34      public interface UnparsedOption {
35          @Unparsed String getName();
36      }
37  
38      public interface UnparsedListOption {
39          @Unparsed() List<String> getNames();
40      }
41  
42      public interface UnparsedListOptionEmptyListDefault {
43          @Unparsed(defaultValue = {}) List<String> getNames();
44      }
45  
46      public interface UnparsedListOptionNullDefault {
47          @Unparsed(defaultToNull = true) List<String> getNames();
48      }
49  
50      public interface OptionalUnparsedOption {
51          @Unparsed String getName();
52  
53          boolean isName();
54      }
55  
56      public interface OptionalUnparsedListOption {
57          @Unparsed List<String> getNames();
58  
59          boolean isNames();
60      }
61  
62      public interface UnparsedListOptionDefaultToEmpty {
63          @Unparsed(defaultValue = {}) List<String> getNames();
64      }
65  
66      public interface UnparsedListOptionDefaultToNull {
67          @Unparsed(defaultToNull = true) List<String> getNames();
68      }
69  
70      public interface UnparsedListOptionDefaultToValues {
71          @Unparsed(defaultValue = { "value0", "value1" }) List<String> getNames();
72      }
73  
74      public interface NoUnparsedOption {
75  
76      }
77  
78      @Test public void testUnparsedOption() throws ArgumentValidationException {
79          assertEquals("value", parseArguments(UnparsedOption.class, "value").getName());
80      }
81  
82      @Test public void testUnparsedOptionMissingValue() throws ArgumentValidationException {
83          exception.expect(ArgumentValidationException.class);
84          exception.expect(hasValidationException(ValidationFailureType.MissingValue));
85  
86          parseArguments(UnparsedOption.class);
87      }
88  
89      @Test public void testUnparsedListOption() throws ArgumentValidationException {
90          assertThat(
91                  parseArguments(UnparsedListOption.class, "value0", "value1").getNames(),
92                  contains("value0", "value1"));
93      }
94  
95      @Test public void testUnparsedListOptionMissingValue() throws ArgumentValidationException {
96          assertThat(parseArguments(UnparsedListOption.class).getNames(),
97                  equalTo(null));
98      }
99  
100     @Test public void missingUnparsedListOptionWithEmptyListDefaultReturnsEmptyList() throws ArgumentValidationException {
101         assertThat(parseArguments(UnparsedListOptionDefaultToEmpty.class).getNames(),
102                 equalTo(Collections.<String>emptyList()));
103     }
104 
105     @Test public void missingUnparsedListOptionWithNullDefaultReturnsNull() throws ArgumentValidationException {
106         assertThat(parseArguments(UnparsedListOptionDefaultToNull.class).getNames(),
107                 equalTo(null));
108     }
109 
110     @Test public void ifNoUnparsedOptionIsSpecifiedButValuesArePresentThenAValidationErrorOccurs()
111             throws ArgumentValidationException {
112         exception.expect(ArgumentValidationException.class);
113         exception
114         .expect(hasValidationException(ValidationFailureType.UnexpectedTrailingValue));
115 
116         parseArguments(NoUnparsedOption.class, "value0");
117     }
118 
119     @Test public void testOptionalUnparsedOption() throws ArgumentValidationException {
120         assertEquals(parseArguments(UnparsedOption.class, "value").getName(), "value");
121     }
122 
123     @Test public void testOptionalUnparsedOptionMissingValue() throws ArgumentValidationException {
124         assertFalse(parseArguments(OptionalUnparsedOption.class).isName());
125     }
126 
127     @Test public void testOptionalUnparsedListOption() throws ArgumentValidationException {
128         final OptionalUnparsedListOption result = parseArguments(
129                 OptionalUnparsedListOption.class,
130                 "value0",
131                 "value1");
132         assertThat(result.getNames(), contains("value0", "value1"));
133         assertTrue(result.isNames());
134     }
135 
136     @Test public void testOptionalUnparsedListOptionMissingValue() throws ArgumentValidationException {
137         final OptionalUnparsedListOption result = parseArguments(OptionalUnparsedListOption.class);
138         assertFalse(result.isNames());
139         assertThat(result.getNames(), nullValue());
140     }
141 
142     @Test public void unparsedListOptionMissingValueDefaultsToEmpty() throws ArgumentValidationException {
143         assertThat(parseArguments(UnparsedListOptionDefaultToEmpty.class).getNames().size(), equalTo(0));
144     }
145 
146     @Test public void unparsedListOptionMissingValueDefaultsToNull() throws ArgumentValidationException {
147         assertThat(parseArguments(UnparsedListOptionDefaultToNull.class).getNames(), nullValue());
148     }
149 
150     @Test public void unparsedListOptionMissingValueDefaultsToValues() throws ArgumentValidationException {
151         assertThat(parseArguments(UnparsedListOptionDefaultToValues.class).getNames(), contains("value0", "value1"));
152     }
153 }