Home | History | Annotate | Download | only in jcommander
      1 package com.beust.jcommander;
      2 
      3 import org.testng.Assert;
      4 import org.testng.annotations.Test;
      5 
      6 import java.util.List;
      7 
      8 /**
      9  * Tests for @Parameter on top of methods.
     10  */
     11 @Test
     12 public class MethodSetterTest {
     13   public void arityStringsSetter() {
     14     class ArgsArityStringSetter {
     15 
     16       @Parameter(names = "-pairs", arity = 2, description = "Pairs")
     17       public void setPairs(List<String> pairs) {
     18         this.pairs = pairs;
     19       }
     20       public List<String> getPairs() {
     21         return this.pairs;
     22       }
     23       public List<String> pairs;
     24 
     25       @Parameter(description = "Rest")
     26       public void setRest(List<String> rest) {
     27         this.rest = rest;
     28       }
     29 //      public List<String> getRest() {
     30 //        return this.rest;
     31 //      }
     32       public List<String> rest;
     33     }
     34     ArgsArityStringSetter args = new ArgsArityStringSetter();
     35     String[] argv = { "-pairs", "pair0", "pair1", "rest" };
     36     new JCommander(args, argv);
     37 
     38     Assert.assertEquals(args.pairs.size(), 2);
     39     Assert.assertEquals(args.pairs.get(0), "pair0");
     40     Assert.assertEquals(args.pairs.get(1), "pair1");
     41     Assert.assertEquals(args.rest.size(), 1);
     42     Assert.assertEquals(args.rest.get(0), "rest");
     43   }
     44 
     45   public void setterThatThrows() {
     46     class Arg {
     47       @Parameter(names = "--host")
     48       public void setHost(String host) {
     49         throw new ParameterException("Illegal host");
     50       }
     51     }
     52     boolean passed = false;
     53     try {
     54       new JCommander(new Arg(), new String[] { "--host", "host" });
     55     } catch(ParameterException ex) {
     56       Assert.assertEquals(ex.getCause(), null);
     57       passed = true;
     58     }
     59     Assert.assertTrue(passed, "Should have thrown an exception");
     60   }
     61 
     62   public void getterReturningNonString() {
     63     class Arg {
     64       private Integer port;
     65 
     66       @Parameter(names = "--port")
     67       public void setPort(String port) {
     68         this.port = Integer.parseInt(port);
     69       }
     70 
     71       public Integer getPort() {
     72         return port;
     73       }
     74     }
     75     Arg arg = new Arg();
     76     new JCommander(arg, new String[] { "--port", "42" });
     77 
     78     Assert.assertEquals(arg.port, new Integer(42));
     79   }
     80 
     81   public void noGetterButWithField() {
     82     class Arg {
     83       private Integer port = 43;
     84 
     85       @Parameter(names = "--port")
     86       public void setPort(String port) {
     87         this.port = Integer.parseInt(port);
     88       }
     89     }
     90     Arg arg = new Arg();
     91     JCommander jc = new JCommander(arg, new String[] { "--port", "42" });
     92     ParameterDescription pd = jc.getParameters().get(0);
     93     Assert.assertEquals(pd.getDefault(), 43);
     94   }
     95 
     96   @Test(enabled = false)
     97   public static void main(String[] args) throws Exception {
     98     new MethodSetterTest().noGetterButWithField();
     99   }
    100 }
    101