001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.cli2.application; 018 019import java.util.HashSet; 020import java.util.Set; 021 022import junit.framework.Test; 023import junit.framework.TestCase; 024import junit.framework.TestSuite; 025 026import org.apache.commons.cli2.CommandLine; 027import org.apache.commons.cli2.Group; 028import org.apache.commons.cli2.Option; 029import org.apache.commons.cli2.OptionException; 030import org.apache.commons.cli2.builder.ArgumentBuilder; 031import org.apache.commons.cli2.builder.DefaultOptionBuilder; 032import org.apache.commons.cli2.builder.GroupBuilder; 033import org.apache.commons.cli2.commandline.Parser; 034import org.apache.commons.cli2.validation.EnumValidator; 035 036/** 037 * <p>Test the <code>ls</code> command. Duplicated Option types are not 038 * tested e.g. -a and -d are the same Option type.</p> 039 * 040 * <p>The following is the man output for 'ls'. See 041 * <a href="http://www.rt.com/man/ls.1.html">http://www.rt.com/man/ls.1.html</a>.</p> 042 * 043 * <pre> 044 * LS(1) FSF LS(1) 045 * 046 * NAME ls - list directory contents 047 * 048 * SYNOPSIS ls [OPTION]... [FILE]... 049 * 050 * DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuSUX nor --sort. 051 * 052 * -a, --all do not hide entries starting with . 053 * 054 * -A, --almost-all do not list implied . and .. 055 * 056 * -b, --escape print octal escapes for nongraphic characters 057 * 058 * --block-size=SIZE use SIZE-byte blocks 059 * 060 * -B, --ignore-backups do not list implied entries ending with ~ -c sort by change time; with -l: show ctime -C list entries by columns 061 * 062 * --color[=WHEN] control whether color is used to distinguish file types. WHEN may be `never', `always', or `auto' 063 * 064 * -d, --directory list directory entries instead of contents 065 * 066 * -D, --dired generate output designed for Emacs' dired mode -f do not sort, enable -aU, disable -lst 067 * 068 * -F, --classify append indicator (one of /=@|*) to entries 069 * 070 * --format=WORD across -x, commas -m, horizontal -x, long -l, sin- gle-column -1, verbose -l, vertical -C 071 * 072 * --full-time list both full date and full time -g (ignored) 073 * 074 * -G, --no-group inhibit display of group information 075 * 076 * -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) 077 * 078 * -H, --si likewise, but use powers of 1000 not 1024 079 * 080 * --indicator-style=WORD append indicator with style WORD to entry names: none (default), classify (-F), file-type (-p) 081 * 082 * -i, --inode print index number of each file 083 * 084 * -I, --ignore=PATTERN do not list implied entries matching shell PATTERN 085 * 086 * -k, --kilobytes like --block-size=1024 -l use a long listing format 087 * 088 * -L, --dereference list entries pointed to by symbolic links -m fill width with a comma separated list of entries 089 * 090 * -n, --numeric-uid-gid list numeric UIDs and GIDs instead of names 091 * 092 * -N, --literal print raw entry names (don't treat e.g. control characters specially) -o use long listing format without group info 093 * 094 * -p, --file-type append indicator (one of /=@|) to entries 095 * 096 * -q, --hide-control-chars print ? instead of non graphic characters 097 * 098 * --show-control-chars show non graphic characters as-is (default) 099 * 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 */ 128public 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}