• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.command;
20 
21 import com.beust.jcommander.ArgsValidate2;
22 import com.beust.jcommander.JCommander;
23 import com.beust.jcommander.ParameterException;
24 import org.testng.Assert;
25 import org.testng.annotations.Test;
26 
27 import java.io.File;
28 import java.util.Arrays;
29 
30 public class CommandTest {
31   @Test
namedCommandTest1()32   public void namedCommandTest1() {
33     CommandMain cm = new CommandMain();
34     JCommander jc = new JCommander(cm);
35     NamedCommandAdd add = new NamedCommandAdd();
36     jc.addCommand(add);
37     CommandCommit commit = new CommandCommit();
38     jc.addCommand("commit", commit);
39     jc.parse("add", "-i", "A.java");
40 
41     Assert.assertEquals(jc.getParsedCommand(), "add");
42     Assert.assertEquals(add.interactive.booleanValue(), true);
43     Assert.assertEquals(add.patterns, Arrays.asList("A.java"));
44   }
45 
46   @Test(expectedExceptions = ParameterException.class)
shouldComplainIfNoAnnotations()47   public void shouldComplainIfNoAnnotations() {
48     CommandMain cm = new CommandMain();
49     JCommander jc = new JCommander(cm);
50     CommandAdd add = new CommandAdd();
51     jc.addCommand(add);
52   }
53 
54   @Test
commandTest1()55   public void commandTest1() {
56     CommandMain cm = new CommandMain();
57     JCommander jc = new JCommander(cm);
58     CommandAdd add = new CommandAdd();
59     jc.addCommand("add", add);
60     CommandCommit commit = new CommandCommit();
61     jc.addCommand("commit", commit);
62     jc.parse("add", "-i", "A.java");
63 
64     Assert.assertEquals(jc.getParsedCommand(), "add");
65     Assert.assertEquals(add.interactive.booleanValue(), true);
66     Assert.assertEquals(add.patterns, Arrays.asList("A.java"));
67   }
68 
69   @Test
commandTest2()70   public void commandTest2() {
71     CommandMain cm = new CommandMain();
72     JCommander jc = new JCommander(cm);
73     CommandAdd add = new CommandAdd();
74     jc.addCommand("add", add);
75     CommandCommit commit = new CommandCommit();
76     jc.addCommand("commit", commit);
77     jc.parse("-v", "commit", "--amend", "--author=cbeust", "A.java", "B.java");
78 
79     Assert.assertTrue(cm.verbose);
80     Assert.assertEquals(jc.getParsedCommand(), "commit");
81     Assert.assertTrue(commit.amend);
82     Assert.assertEquals(commit.author, "cbeust");
83     Assert.assertEquals(commit.files, Arrays.asList("A.java", "B.java"));
84   }
85 
86     @Test
hiddenCommandTest()87     public void hiddenCommandTest() {
88         CommandMain cm = new CommandMain();
89         JCommander jc = new JCommander(cm);
90         CommandAdd add = new CommandAdd();
91         jc.addCommand("add", add);
92         CommandHidden hidden = new CommandHidden();
93         jc.addCommand("hidden", hidden);
94         jc.parse("hidden", "-i", "A.java");
95 
96         Assert.assertEquals(jc.getParsedCommand(), "hidden");
97         Assert.assertEquals(hidden.interactive.booleanValue(), true);
98         Assert.assertEquals(hidden.patterns, Arrays.asList("A.java"));
99 
100         jc.setProgramName("TestCommander");
101         StringBuilder out = new StringBuilder();
102         jc.usage(out);
103 
104         Assert.assertTrue(out.toString().contains("add      Add file contents to the index"));
105         Assert.assertFalse(out.toString().contains("hidden      Hidden command to add file contents to the index"));
106     }
107 
108   @Test
noParametersAnnotationOnCommandTest()109   public void noParametersAnnotationOnCommandTest() {
110     CommandMain cm = new CommandMain();
111     JCommander jc = new JCommander(cm);
112     CommandNoParametersAnnotation noParametersAnnotation = new CommandNoParametersAnnotation();
113     jc.addCommand("no-annotation", noParametersAnnotation);
114 
115     jc.setProgramName("TestCommander");
116     StringBuilder out = new StringBuilder();
117     jc.usage(out);
118 
119     Assert.assertTrue(out.toString().contains("no-annotation"));
120   }
121 
122   @Test
noTrailingSpaceInUsageTest()123   public void noTrailingSpaceInUsageTest() {
124     CommandMain cm = new CommandMain();
125     JCommander jc = new JCommander(cm);
126     CommandAdd add = new CommandAdd();
127     jc.addCommand("add", add);
128     CommandCommit commit = new CommandCommit();
129     jc.addCommand("commit", commit);
130     jc.parse("-v", "commit", "--amend", "--author=cbeust", "A.java", "B.java");
131     StringBuilder out = new StringBuilder();
132     jc.usage(out);
133     String firstLine = out.toString().split("\n")[0];
134     Assert.assertFalse(firstLine.endsWith(" "), "Usage should not have trailing spaces");
135   }
136 
137   @Test(expectedExceptions = ParameterException.class)
validateSubCommand()138   public void validateSubCommand() throws Exception {
139     JCommander jc = new JCommander(new CommandMain());
140     final ArgsValidate2 sub = new ArgsValidate2();
141     sub.template = null;
142     jc.addCommand("sub", sub);
143     jc.parse("sub", "-template", "foo");
144   }
145 
146   @Test
doNotValidateSubCommand()147   public void doNotValidateSubCommand() throws Exception {
148     JCommander jc = new JCommander(new CommandMain());
149     final ArgsValidate2 sub = new ArgsValidate2();
150     sub.template = null;
151     jc.addCommand("sub", sub);
152     jc.parseWithoutValidation("sub", "-template", "foo");
153     Assert.assertEquals(sub.template, new File("foo"));
154 
155   }
156 
main(String[] args)157   public static void main(String[] args) {
158     new CommandTest().shouldComplainIfNoAnnotations();
159   }
160 }
161