Home | History | Annotate | Download | only in jcommander
      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.ArgsConverterFactory;
     22 import com.beust.jcommander.args.ArgsMainParameter1;
     23 import com.beust.jcommander.args.ArgsMainParameter2;
     24 import com.beust.jcommander.args.IHostPorts;
     25 
     26 import org.testng.Assert;
     27 import org.testng.annotations.Test;
     28 
     29 import java.util.HashMap;
     30 import java.util.Map;
     31 
     32 /**
     33  * Test the converter factory feature.
     34  *
     35  * @author cbeust
     36  */
     37 public class ConverterFactoryTest {
     38   private static final Map<Class, Class<? extends IStringConverter<?>>> MAP = new HashMap() {{
     39     put(HostPort.class, HostPortConverter.class);
     40   }};
     41 
     42   private static final IStringConverterFactory CONVERTER_FACTORY = new IStringConverterFactory() {
     43 
     44     public Class<? extends IStringConverter<?>> getConverter(Class forType) {
     45       return MAP.get(forType);
     46     }
     47 
     48   };
     49 
     50   @Test
     51   public void parameterWithHostPortParameters() {
     52     ArgsConverterFactory a = new ArgsConverterFactory();
     53     JCommander jc = new JCommander(a);
     54     jc.addConverterFactory(CONVERTER_FACTORY);
     55     jc.parse("-hostport", "example.com:8080");
     56 
     57     Assert.assertEquals(a.hostPort.host, "example.com");
     58     Assert.assertEquals(a.hostPort.port.intValue(), 8080);
     59   }
     60 
     61   /**
     62    * Test that main parameters can be used with string converters,
     63    * either with a factory or from the annotation.
     64    */
     65   private void mainWithHostPortParameters(IStringConverterFactory f, IHostPorts a) {
     66     JCommander jc = new JCommander(a);
     67     if (f != null) jc.addConverterFactory(f);
     68     jc.parse("a.com:10", "b.com:20");
     69     Assert.assertEquals(a.getHostPorts().get(0).host, "a.com");
     70     Assert.assertEquals(a.getHostPorts().get(0).port.intValue(), 10);
     71     Assert.assertEquals(a.getHostPorts().get(1).host, "b.com");
     72     Assert.assertEquals(a.getHostPorts().get(1).port.intValue(), 20);
     73   }
     74 
     75   @Test
     76   public void mainWithoutFactory() {
     77     mainWithHostPortParameters(null, new ArgsMainParameter1());
     78   }
     79 
     80   @Test
     81   public void mainWithFactory() {
     82     mainWithHostPortParameters(CONVERTER_FACTORY, new ArgsMainParameter2());
     83   }
     84 
     85 }
     86 
     87