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.JCommander; 22 import com.beust.jcommander.ParameterException; 23 24 import org.testng.Assert; 25 import org.testng.annotations.Test; 26 27 import java.util.Arrays; 28 29 public class CommandTest { 30 @Test 31 public void namedCommandTest1() { 32 CommandMain cm = new CommandMain(); 33 JCommander jc = new JCommander(cm); 34 NamedCommandAdd add = new NamedCommandAdd(); 35 jc.addCommand(add); 36 CommandCommit commit = new CommandCommit(); 37 jc.addCommand("commit", commit); 38 jc.parse("add", "-i", "A.java"); 39 40 Assert.assertEquals(jc.getParsedCommand(), "add"); 41 Assert.assertEquals(add.interactive.booleanValue(), true); 42 Assert.assertEquals(add.patterns, Arrays.asList("A.java")); 43 } 44 45 @Test(expectedExceptions = ParameterException.class) 46 public void shouldComplainIfNoAnnotations() { 47 CommandMain cm = new CommandMain(); 48 JCommander jc = new JCommander(cm); 49 CommandAdd add = new CommandAdd(); 50 jc.addCommand(add); 51 } 52 53 @Test 54 public void commandTest1() { 55 CommandMain cm = new CommandMain(); 56 JCommander jc = new JCommander(cm); 57 CommandAdd add = new CommandAdd(); 58 jc.addCommand("add", add); 59 CommandCommit commit = new CommandCommit(); 60 jc.addCommand("commit", commit); 61 jc.parse("add", "-i", "A.java"); 62 63 Assert.assertEquals(jc.getParsedCommand(), "add"); 64 Assert.assertEquals(add.interactive.booleanValue(), true); 65 Assert.assertEquals(add.patterns, Arrays.asList("A.java")); 66 } 67 68 @Test 69 public void commandTest2() { 70 CommandMain cm = new CommandMain(); 71 JCommander jc = new JCommander(cm); 72 CommandAdd add = new CommandAdd(); 73 jc.addCommand("add", add); 74 CommandCommit commit = new CommandCommit(); 75 jc.addCommand("commit", commit); 76 jc.parse("-v", "commit", "--amend", "--author=cbeust", "A.java", "B.java"); 77 78 // jc.setProgramName("TestCommander"); 79 // jc.usage(); 80 // jc.usage("add"); 81 // jc.usage("commit"); 82 83 Assert.assertTrue(cm.verbose); 84 Assert.assertEquals(jc.getParsedCommand(), "commit"); 85 Assert.assertTrue(commit.amend); 86 Assert.assertEquals(commit.author, "cbeust"); 87 Assert.assertEquals(commit.files, Arrays.asList("A.java", "B.java")); 88 } 89 90 @Test 91 public void hiddenCommandTest() { 92 CommandMain cm = new CommandMain(); 93 JCommander jc = new JCommander(cm); 94 CommandAdd add = new CommandAdd(); 95 jc.addCommand("add", add); 96 CommandHidden hidden = new CommandHidden(); 97 jc.addCommand("hidden", hidden); 98 jc.parse("hidden", "-i", "A.java"); 99 100 Assert.assertEquals(jc.getParsedCommand(), "hidden"); 101 Assert.assertEquals(hidden.interactive.booleanValue(), true); 102 Assert.assertEquals(hidden.patterns, Arrays.asList("A.java")); 103 104 jc.setProgramName("TestCommander"); 105 StringBuilder out = new StringBuilder(); 106 jc.usage(out); 107 108 Assert.assertTrue(out.toString().contains("add Add file contents to the index")); 109 Assert.assertFalse(out.toString().contains("hidden Hidden command to add file contents to the index")); 110 } 111 112 public static void main(String[] args) { 113 new CommandTest().shouldComplainIfNoAnnotations(); 114 } 115 } 116