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.option; 018 019import java.util.HashSet; 020import java.util.Iterator; 021import java.util.List; 022import java.util.ListIterator; 023import java.util.Set; 024 025import org.apache.commons.cli2.DisplaySetting; 026import org.apache.commons.cli2.HelpLine; 027import org.apache.commons.cli2.Option; 028import org.apache.commons.cli2.OptionException; 029import org.apache.commons.cli2.WriteableCommandLine; 030import org.apache.commons.cli2.commandline.WriteableCommandLineImpl; 031 032/** 033 * @author Rob Oxspring 034 */ 035public class PropertyOptionTest extends OptionTestCase { 036 037 /* 038 * (non-Javadoc) 039 * 040 * @see org.apache.commons.cli2.OptionTestCase#testCanProcess() 041 */ 042 public void testCanProcess() { 043 final Option option = new PropertyOption(); 044 assertTrue(option.canProcess(new WriteableCommandLineImpl(option,null), "-Dmyprop=myval")); 045 } 046 047 public void testCanProcess_Null() { 048 final Option option = new PropertyOption(); 049 assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), (String) null)); 050 } 051 052 public void testCanProcess_TooShort() { 053 final Option option = new PropertyOption(); 054 assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), "-D")); 055 } 056 057 public void testCanProcess_BadMatch() { 058 final Option option = new PropertyOption(); 059 assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null),"-dump")); 060 } 061 062 /* 063 * (non-Javadoc) 064 * 065 * @see org.apache.commons.cli2.OptionTestCase#testPrefixes() 066 */ 067 public void testPrefixes() { 068 final Option option = new PropertyOption(); 069 assertContentsEqual(list("-D"), option.getPrefixes()); 070 } 071 072 /* 073 * (non-Javadoc) 074 * 075 * @see org.apache.commons.cli2.OptionTestCase#testProcess() 076 */ 077 public void testProcess() throws OptionException { 078 final PropertyOption option = new PropertyOption(); 079 final List args = list("-Dmyprop=myvalue"); 080 final WriteableCommandLine commandLine = commandLine(option, args); 081 final ListIterator iterator = args.listIterator(); 082 083 option.process(commandLine, iterator); 084 assertEquals("myvalue", commandLine.getProperty(option, "myprop")); 085 assertFalse(iterator.hasNext()); 086 assertEquals(1, commandLine.getProperties(option).size()); 087 } 088 089 public void testProcess_UnexpectedOptionException() { 090 final Option option = new PropertyOption(); 091 final List args = list("--help"); 092 final WriteableCommandLine commandLine = commandLine(option, args); 093 final ListIterator iterator = args.listIterator(); 094 095 try { 096 option.process(commandLine, iterator); 097 fail("UnexpectedOption"); 098 } 099 catch (final OptionException uoe) { 100 assertEquals(option, uoe.getOption()); 101 assertEquals( 102 "Unexpected --help while processing -Dproperty=value", 103 uoe.getMessage()); 104 } 105 } 106 107 public void testProcess_BadPropertyException() throws OptionException { 108 final PropertyOption option = new PropertyOption(); 109 final List args = list("-Dmyprop"); 110 final WriteableCommandLine commandLine = commandLine(option, args); 111 final ListIterator iterator = args.listIterator(); 112 113 option.process(commandLine, iterator); 114 115 assertEquals("true", commandLine.getProperty(option, "myprop")); 116 } 117 118 public void testProcess_SetToEmpty() throws OptionException { 119 final PropertyOption option = new PropertyOption(); 120 final List args = list("-Dmyprop="); 121 final WriteableCommandLine commandLine = commandLine(option, args); 122 final ListIterator iterator = args.listIterator(); 123 124 option.process(commandLine, iterator); 125 assertEquals("", commandLine.getProperty(option, "myprop")); 126 assertFalse(iterator.hasNext()); 127 assertEquals(1, commandLine.getProperties(option).size()); 128 } 129 130 /* 131 * (non-Javadoc) 132 * 133 * @see org.apache.commons.cli2.OptionTestCase#testTriggers() 134 */ 135 public void testTriggers() { 136 final Option option = new PropertyOption(); 137 138 assertContentsEqual(list("-D"), option.getTriggers()); 139 } 140 141 /* 142 * (non-Javadoc) 143 * 144 * @see org.apache.commons.cli2.OptionTestCase#testValidate() 145 */ 146 public void testValidate() throws OptionException { 147 final Option option = new PropertyOption(); 148 final List args = list("-Dproperty=value"); 149 final WriteableCommandLine commandLine = commandLine(option, args); 150 final ListIterator iterator = args.listIterator(); 151 152 option.process(commandLine, iterator); 153 154 option.validate(commandLine); 155 } 156 157 /* 158 * (non-Javadoc) 159 * 160 * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage() 161 */ 162 public void testAppendUsage() { 163 final Option option = new PropertyOption(); 164 final StringBuffer buffer = new StringBuffer(); 165 option.appendUsage(buffer, DisplaySetting.ALL, null); 166 167 assertEquals("-D<property>=<value>", buffer.toString()); 168 } 169 170 public void testAppendUsage_Hidden() { 171 final Option option = new PropertyOption(); 172 final StringBuffer buffer = new StringBuffer(); 173 final Set settings = new HashSet(DisplaySetting.ALL); 174 settings.remove(DisplaySetting.DISPLAY_PROPERTY_OPTION); 175 option.appendUsage(buffer, settings, null); 176 177 assertEquals("", buffer.toString()); 178 } 179 180 /* 181 * (non-Javadoc) 182 * 183 * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName() 184 */ 185 public void testGetPreferredName() { 186 final Option option = new PropertyOption(); 187 assertEquals("-D", option.getPreferredName()); 188 } 189 190 /* 191 * (non-Javadoc) 192 * 193 * @see org.apache.commons.cli2.OptionTestCase#testGetDescription() 194 */ 195 public void testGetDescription() { 196 final Option option = new PropertyOption(); 197 assertEquals( 198 "Passes properties and values to the application", 199 option.getDescription()); 200 } 201 202 /* 203 * (non-Javadoc) 204 * 205 * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() 206 */ 207 public void testHelpLines() { 208 final Option option = new PropertyOption(); 209 final List lines = option.helpLines(0, DisplaySetting.ALL, null); 210 final Iterator i = lines.iterator(); 211 212 final HelpLine line1 = (HelpLine)i.next(); 213 assertEquals(0, line1.getIndent()); 214 assertEquals(option, line1.getOption()); 215 216 assertFalse(i.hasNext()); 217 } 218 219 /* 220 * (non-Javadoc) 221 * 222 * @see org.apache.commons.cli2.OptionTestCase#testHelpLines() 223 */ 224 public void testHelpLines_NoDisplay() { 225 final Option option = new PropertyOption(); 226 final Set settings = new HashSet(DisplaySetting.ALL); 227 settings.remove(DisplaySetting.DISPLAY_PROPERTY_OPTION); 228 final List lines = option.helpLines(0, settings, null); 229 final Iterator i = lines.iterator(); 230 231 assertFalse(i.hasNext()); 232 } 233}