1   package com.lexicalscope.jewel.cli;
2   
3   import static com.google.common.collect.Sets.newHashSet;
4   import static java.util.Arrays.asList;
5   import static org.hamcrest.MatcherAssert.assertThat;
6   import static org.hamcrest.Matchers.*;
7   import static com.lexicalscope.jewel.cli.ConvertTypeOfObject.converterTo;
8   
9   import java.util.Collection;
10  import java.util.List;
11  import java.util.Set;
12  
13  import org.junit.Test;
14  
15  import com.lexicalscope.fluentreflection.TypeToken;
16  import com.lexicalscope.jewel.cli.specification.OptionSpecification;
17  
18  /*
19   * Copyright 2011 Tim Wood
20   *
21   * Licensed under the Apache License, Version 2.0 (the "License");
22   * you may not use this file except in compliance with the License.
23   * You may obtain a copy of the License at
24   *
25   * http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing, software
28   * distributed under the License is distributed on an "AS IS" BASIS,
29   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30   * See the License for the specific language governing permissions and
31   * limitations under the License. 
32   */
33  
34  public class TestConvertTypeOfObject {
35      private final ValidationErrorBuilder validationErrorBuilder = new ValidationErrorBuilderImpl();
36      private OptionSpecification specification;
37  
38      @Test public void argumentConversionTakesPlaceOnGet() throws Exception {
39          assertThat(converterTo(validationErrorBuilder, specification, Integer.class).convert("14"), equalTo(14));
40      }
41  
42      @Test public void argumentConversionCanConvertFromStringToPrimitive() throws Exception {
43          assertThat(converterTo(validationErrorBuilder, specification, int.class).convert("14"), equalTo(14));
44      }
45  
46      @Test public void argumentConversionCanConvertFromStringToChar() throws Exception {
47          assertThat(converterTo(validationErrorBuilder, specification, char.class).convert("c"), equalTo('c'));
48      }
49  
50      @Test(expected = ArgumentValidationException.class) public void argumentConversionCannotConvertFromStringToChar()
51              throws Exception {
52          assertThat(converterTo(validationErrorBuilder, specification, char.class).convert("cc"), equalTo(null));
53  
54          validationErrorBuilder.validate();
55      }
56  
57      @Test public void argumentConversionTakesPlaceOnGetOfIterable() throws Exception {
58          assertThat(
59                  converterTo(validationErrorBuilder, specification, new TypeToken<Iterable<Integer>>() {}).convert(
60                          asList("14", "15", "16")),
61                  contains(14, 15, 16));
62      }
63  
64      @Test public void argumentConversionTakesPlaceOnGetOfCollection() throws Exception {
65          assertThat(
66                  converterTo(validationErrorBuilder, specification, new TypeToken<Collection<Integer>>() {}).convert(
67                          asList("14", "15", "16")),
68                  contains(14, 15, 16));
69      }
70  
71      @Test public void argumentConversionTakesPlaceOnGetOfList() throws Exception {
72          assertThat(
73                  converterTo(validationErrorBuilder, specification, new TypeToken<List<Integer>>() {}).convert(
74                          asList("14", "15", "16")),
75                  contains(14, 15, 16));
76      }
77  
78      @Test public void argumentConversionTakesPlaceOnGetOfSet() throws Exception {
79          assertThat(
80                  converterTo(validationErrorBuilder, specification, new TypeToken<Set<Integer>>() {}).convert(
81                          newHashSet("14", "15", "16")),
82                  contains(14, 15, 16));
83      }
84  }