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.ArrayList;
      7 import java.util.List;
      8 
      9 /**
     10  * @author rodionmoiseev
     11  */
     12 public class ParametersDelegateTest {
     13 
     14   @Test
     15   public void delegatingEmptyClassHasNoEffect() {
     16     class EmptyDelegate {
     17       public String nonParamString = "a";
     18     }
     19     class MainParams {
     20       @Parameter(names = "-a")
     21       public boolean isA;
     22       @Parameter(names = {"-b", "--long-b"})
     23       public String bValue = "";
     24       @ParametersDelegate
     25       public EmptyDelegate delegate = new EmptyDelegate();
     26     }
     27 
     28     MainParams p = new MainParams();
     29     JCommander cmd = new JCommander(p);
     30     cmd.parse("-a", "-b", "someValue");
     31     Assert.assertTrue(p.isA);
     32     Assert.assertEquals(p.bValue, "someValue");
     33     Assert.assertEquals(p.delegate.nonParamString, "a");
     34   }
     35 
     36   @Test
     37   public void delegatingSetsFieldsOnBothMainParamsAndTheDelegatedParams() {
     38     class ComplexDelegate {
     39       @Parameter(names = "-c")
     40       public boolean isC;
     41       @Parameter(names = {"-d", "--long-d"})
     42       public Integer d;
     43     }
     44     class MainParams {
     45       @Parameter(names = "-a")
     46       public boolean isA;
     47       @Parameter(names = {"-b", "--long-b"})
     48       public String bValue = "";
     49       @ParametersDelegate
     50       public ComplexDelegate delegate = new ComplexDelegate();
     51     }
     52 
     53     MainParams p = new MainParams();
     54     JCommander cmd = new JCommander(p);
     55     cmd.parse("-c", "--long-d", "123", "--long-b", "bValue");
     56     Assert.assertFalse(p.isA);
     57     Assert.assertEquals(p.bValue, "bValue");
     58     Assert.assertTrue(p.delegate.isC);
     59     Assert.assertEquals(p.delegate.d, Integer.valueOf(123));
     60   }
     61 
     62   @Test
     63   public void combinedAndNestedDelegates() {
     64     abstract class LeafAbstractDelegate {
     65       abstract float getFloat();
     66     }
     67     class LeafDelegate {
     68       @Parameter(names = "--list")
     69       public List<String> list = new ArrayList<String>() {{
     70         add("value1");
     71         add("value2");
     72       }};
     73       @Parameter(names = "--bool")
     74       public boolean bool;
     75     }
     76     class NestedDelegate1 {
     77       @ParametersDelegate
     78       public LeafDelegate leafDelegate = new LeafDelegate();
     79       @Parameter(names = {"-d", "--long-d"})
     80       public Integer d;
     81     }
     82     class NestedDelegate2 {
     83       @Parameter(names = "-c")
     84       public boolean isC;
     85       @ParametersDelegate
     86       public NestedDelegate1 nestedDelegate1 = new NestedDelegate1();
     87       @ParametersDelegate
     88       public LeafAbstractDelegate anonymousDelegate = new LeafAbstractDelegate() {
     89         @Parameter(names = "--anon-float")
     90         public float anon = 999f;
     91 
     92         @Override
     93         float getFloat() {
     94           return anon;
     95         }
     96       };
     97     }
     98     class MainParams {
     99       @Parameter(names = "-a")
    100       public boolean isA;
    101       @Parameter(names = {"-b", "--long-b"})
    102       public String bValue = "";
    103       @ParametersDelegate
    104       public NestedDelegate2 nestedDelegate2 = new NestedDelegate2();
    105     }
    106 
    107     MainParams p = new MainParams();
    108     JCommander cmd = new JCommander(p);
    109     cmd.parse("--anon-float 1.2 -d 234 --list a --list b -a".split(" "));
    110     Assert.assertEquals(p.nestedDelegate2.anonymousDelegate.getFloat(), 1.2f);
    111     Assert.assertEquals(p.nestedDelegate2.nestedDelegate1.leafDelegate.list, new ArrayList<String>() {{
    112       add("a");
    113       add("b");
    114     }});
    115     Assert.assertFalse(p.nestedDelegate2.nestedDelegate1.leafDelegate.bool);
    116     Assert.assertEquals(p.nestedDelegate2.nestedDelegate1.d, Integer.valueOf(234));
    117     Assert.assertFalse(p.nestedDelegate2.isC);
    118     Assert.assertTrue(p.isA);
    119     Assert.assertEquals(p.bValue, "");
    120   }
    121 
    122   @Test
    123   public void commandTest() {
    124     class Delegate {
    125       @Parameter(names = "-a")
    126       public String a = "b";
    127     }
    128     class Command {
    129       @ParametersDelegate
    130       public Delegate delegate = new Delegate();
    131     }
    132 
    133     Command c = new Command();
    134 
    135     JCommander cmd = new JCommander();
    136     cmd.addCommand("command", c);
    137 
    138     cmd.parse("command -a a".split(" "));
    139     Assert.assertEquals(c.delegate.a, "a");
    140   }
    141 
    142   @Test
    143   public void mainParametersTest() {
    144     class Delegate {
    145       @Parameter
    146       public List<String> mainParams = new ArrayList<String>();
    147     }
    148     class Command {
    149       @ParametersDelegate
    150       public Delegate delegate = new Delegate();
    151     }
    152 
    153     Command c = new Command();
    154 
    155     JCommander cmd = new JCommander();
    156     cmd.addCommand("command", c);
    157 
    158     cmd.parse("command main params".split(" "));
    159     Assert.assertEquals(c.delegate.mainParams, new ArrayList<String>() {{
    160       add("main");
    161       add("params");
    162     }});
    163   }
    164 
    165   @Test(expectedExceptions = ParameterException.class,
    166           expectedExceptionsMessageRegExp = ".*delegate.*null.*")
    167   public void nullDelegatesAreProhibited() {
    168     class ComplexDelegate {
    169     }
    170     class MainParams {
    171       @ParametersDelegate
    172       public ComplexDelegate delegate;
    173     }
    174 
    175     MainParams p = new MainParams();
    176     JCommander cmd = new JCommander(p);
    177     cmd.parse();
    178   }
    179 
    180   @Test(expectedExceptions = ParameterException.class,
    181           expectedExceptionsMessageRegExp = ".*-a.*")
    182   public void duplicateDelegateThrowDuplicateOptionException() {
    183     class Delegate {
    184       @Parameter(names = "-a")
    185       public String a;
    186     }
    187     class MainParams {
    188       @ParametersDelegate
    189       public Delegate d1 = new Delegate();
    190       @ParametersDelegate
    191       public Delegate d2 = new Delegate();
    192     }
    193 
    194     MainParams p = new MainParams();
    195     JCommander cmd = new JCommander(p);
    196     cmd.parse("-a value".split(" "));
    197   }
    198 
    199   @Test(expectedExceptions = ParameterException.class, expectedExceptionsMessageRegExp = "Only one.*is allowed.*")
    200   public void duplicateMainParametersAreNotAllowed() {
    201     class Delegate1 {
    202       @Parameter
    203       public List<String> mainParams1 = new ArrayList<String>();
    204     }
    205     class Delegate2 {
    206       @Parameter
    207       public List<String> mainParams2 = new ArrayList<String>();
    208     }
    209     class Command {
    210       @ParametersDelegate
    211       public Delegate1 delegate1 = new Delegate1();
    212       @ParametersDelegate
    213       public Delegate2 delegate2 = new Delegate2();
    214     }
    215 
    216     Command c = new Command();
    217 
    218     JCommander cmd = new JCommander();
    219     cmd.addCommand("command", c);
    220 
    221     cmd.parse("command main params".split(" "));
    222   }
    223 
    224   public static void main(String[] args) {
    225     new ParametersDelegateTest().commandTest();
    226   }
    227 }
    228