1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.cli2.application; 18 19 import java.util.HashSet; 20 import java.util.Set; 21 22 import junit.framework.Test; 23 import junit.framework.TestCase; 24 import junit.framework.TestSuite; 25 26 import org.apache.commons.cli2.CommandLine; 27 import org.apache.commons.cli2.Group; 28 import org.apache.commons.cli2.Option; 29 import org.apache.commons.cli2.OptionException; 30 import org.apache.commons.cli2.builder.ArgumentBuilder; 31 import org.apache.commons.cli2.builder.DefaultOptionBuilder; 32 import org.apache.commons.cli2.builder.GroupBuilder; 33 import org.apache.commons.cli2.commandline.Parser; 34 import org.apache.commons.cli2.validation.EnumValidator; 35 36 /** 37 * <p>Test the <code>ls</code> command. Duplicated Option types are not 38 * tested e.g. -a and -d are the same Option type.</p> 39 * 40 * <p>The following is the man output for 'ls'. See 41 * <a href="http://www.rt.com/man/ls.1.html">http://www.rt.com/man/ls.1.html</a>.</p> 42 * 43 * <pre> 44 * LS(1) FSF LS(1) 45 * 46 * NAME ls - list directory contents 47 * 48 * SYNOPSIS ls [OPTION]... [FILE]... 49 * 50 * DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuSUX nor --sort. 51 * 52 * -a, --all do not hide entries starting with . 53 * 54 * -A, --almost-all do not list implied . and .. 55 * 56 * -b, --escape print octal escapes for nongraphic characters 57 * 58 * --block-size=SIZE use SIZE-byte blocks 59 * 60 * -B, --ignore-backups do not list implied entries ending with ~ -c sort by change time; with -l: show ctime -C list entries by columns 61 * 62 * --color[=WHEN] control whether color is used to distinguish file types. WHEN may be `never', `always', or `auto' 63 * 64 * -d, --directory list directory entries instead of contents 65 * 66 * -D, --dired generate output designed for Emacs' dired mode -f do not sort, enable -aU, disable -lst 67 * 68 * -F, --classify append indicator (one of /=@|*) to entries 69 * 70 * --format=WORD across -x, commas -m, horizontal -x, long -l, sin- gle-column -1, verbose -l, vertical -C 71 * 72 * --full-time list both full date and full time -g (ignored) 73 * 74 * -G, --no-group inhibit display of group information 75 * 76 * -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) 77 * 78 * -H, --si likewise, but use powers of 1000 not 1024 79 * 80 * --indicator-style=WORD append indicator with style WORD to entry names: none (default), classify (-F), file-type (-p) 81 * 82 * -i, --inode print index number of each file 83 * 84 * -I, --ignore=PATTERN do not list implied entries matching shell PATTERN 85 * 86 * -k, --kilobytes like --block-size=1024 -l use a long listing format 87 * 88 * -L, --dereference list entries pointed to by symbolic links -m fill width with a comma separated list of entries 89 * 90 * -n, --numeric-uid-gid list numeric UIDs and GIDs instead of names 91 * 92 * -N, --literal print raw entry names (don't treat e.g. control characters specially) -o use long listing format without group info 93 * 94 * -p, --file-type append indicator (one of /=@|) to entries 95 * 96 * -q, --hide-control-chars print ? instead of non graphic characters 97 * 98 * --show-control-chars show non graphic characters as-is (default) 99 * 100 * -Q, --quote-name enclose entry names in double quotes 101 * 102 * --quoting-style=WORD use quoting style WORD for entry names: literal, shell, shell-always, c, escape 103 * 104 * -r, --reverse reverse order while sorting 105 * 106 * -R, --recursive list subdirectories recursively 107 * 108 * -s, --size print size of each file, in blocks -S sort by file size 109 * 110 * --sort=WORD extension -X, none -U, size -S, time -t, version -v status -c, time -t, atime -u, access -u, use -u 111 * 112 * --time=WORD show time as WORD instead of modification time: atime, access, use, ctime or status; use specified time as sort key if --sort=time -t sort by modification time 113 * 114 * -T, --tabsize=COLS assume tab stops at each COLS instead of 8 -u sort by last access time; with -l: show atime -U do not sort; list entries in directory order -v sort by version 115 * 116 * -w, --width=COLS assume screen width instead of current value -x list entries by lines instead of by columns -X sort alphabetically by entry extension -1 list one file per line 117 * 118 * --help display this help and exit 119 * 120 * --version output version information and exit 121 * 122 * By default, color is not used to distinguish types of files. That is equivalent to using --color=none. Using the --color option without the optional WHEN argument is equivalent to using --color=always. With --color=auto, color codes are output only if standard output is con- nected to a terminal (tty). 123 * </pre> 124 * 125 * @author Rob Oxspring 126 * @author John Keyes 127 */ 128 public class LsTest extends TestCase { 129 130 /** Option Builder */ 131 private static final DefaultOptionBuilder oBuilder = 132 new DefaultOptionBuilder(); 133 134 /** Argument Builder */ 135 private static final ArgumentBuilder aBuilder = new ArgumentBuilder(); 136 137 /** Group Builder */ 138 private static final GroupBuilder gBuilder = new GroupBuilder(); 139 140 private static Group options; 141 142 public static Test suite() { 143 return new TestSuite(LsTest.class); 144 } 145 146 /** 147 * Required ctor. 148 * 149 * @param name 150 * the name of the TestCase 151 */ 152 public LsTest(final String name) { 153 super(name); 154 } 155 156 public void setUp() { 157 if (LsTest.options == null) { 158 final Option a = 159 oBuilder 160 .withShortName("a") 161 .withLongName("all") 162 .withDescription("do not hide entries starting with .") 163 .create(); 164 165 final Option blockSize = 166 oBuilder 167 .withLongName("block-size") 168 .withRequired(false) 169 .withDescription("use SIZE-byte blocks") 170 .withArgument( 171 aBuilder 172 .withMaximum(1) 173 .withMinimum(1) 174 .withInitialSeparator('=') 175 .create()) 176 .create(); 177 178 final Option c = 179 oBuilder 180 .withShortName("c") 181 .withRequired(false) 182 .withDescription("with -lt: sort by, and show, ctime (time of last modification of file status information) with -l:show ctime and sort by name otherwise: sort by ctime") 183 .create(); 184 185 final Set colors = new HashSet(); 186 colors.add("never"); 187 colors.add("always"); 188 colors.add("auto"); 189 final Option color = 190 oBuilder 191 .withLongName("color") 192 .withRequired(false) 193 .withDescription("control whether color is used to distinguish file types. WHEN may be `never', `always', or `auto'") 194 .withArgument( 195 aBuilder 196 .withMaximum(1) 197 .withMinimum(1) 198 .withInitialSeparator('=') 199 .withValidator(new EnumValidator(colors)) 200 .create()) 201 .create(); 202 203 LsTest.options = 204 gBuilder 205 .withOption(a) 206 .withOption(blockSize) 207 .withOption(c) 208 .withOption(color) 209 .create(); 210 } 211 } 212 213 public void testLs() throws OptionException { 214 // create the command line parser 215 Parser parser = new Parser(); 216 parser.setGroup(options); 217 CommandLine line = 218 parser.parse(new String[] { "--block-size=10", "--color=never" }); 219 220 assertTrue(line.hasOption("--block-size")); 221 assertEquals(line.getValue("--block-size"), "10"); 222 assertFalse(line.hasOption("--ignore-backups")); 223 } 224 }