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.commandline; 018 019import java.util.Iterator; 020import java.util.Properties; 021import java.util.Set; 022 023import org.apache.commons.cli2.CommandLine; 024import org.apache.commons.cli2.CommandLineTestCase; 025 026/** 027 * @author Rob Oxspring 028 */ 029public class PropertiesCommandLineTest 030 extends CommandLineTestCase { 031 private Properties props = null; 032 033 protected CommandLine createCommandLine() { 034 props = new Properties(); 035 props.setProperty("--present", "present value"); 036 props.setProperty("--alsopresent", ""); 037 props.setProperty("--multiple", "value 1|value 2|value 3"); 038 props.setProperty("--bool", "true"); 039 040 props.setProperty("present", "present property"); 041 return new PropertiesCommandLine(root, props, '|'); 042 } 043 044 protected CommandLine createCommandLineNoSep() { 045 props = new Properties(); 046 props.setProperty("--present", "present value"); 047 props.setProperty("--alsopresent", ""); 048 props.setProperty("--multiple", "value 1|value 2|value 3"); 049 props.setProperty("--bool", "false"); 050 051 props.setProperty("present", "present property"); 052 return new PropertiesCommandLine(root, props); 053 } 054 055 public void testPropertyValues() { 056 // nothing to test 057 CommandLine cmdline = createCommandLine(); 058 059 assertEquals("wrong value", "present value", cmdline.getValue("--present")); 060 assertEquals("wrong value", "present value", cmdline.getValue("--alsopresent")); 061 assertEquals("wrong # of values", 3, cmdline.getValues("--multiple").size()); 062 assertEquals("wrong value 1", "value 1", cmdline.getValues("--multiple").get(0)); 063 assertEquals("wrong value 2", "value 2", cmdline.getValues("--multiple").get(1)); 064 assertEquals("wrong value 3", "value 3", cmdline.getValues("--multiple").get(2)); 065 } 066 067 public void testNoSeparator() { 068 // nothing to test 069 CommandLine cmdline = createCommandLineNoSep(); 070 071 assertEquals("wrong value", "present value", cmdline.getValue("--present")); 072 assertEquals("wrong value", "present value", cmdline.getValue("--alsopresent")); 073 assertEquals("wrong # of values", 1, cmdline.getValues("--multiple").size()); 074 assertEquals("wrong value", "value 1|value 2|value 3", cmdline.getValue("--multiple")); 075 assertFalse("expected a false", cmdline.getSwitch("--bool").booleanValue()); 076 } 077 078 public void testNullOption() { 079 // nothing to test 080 CommandLine cmdline = createCommandLine(); 081 082 assertFalse("should not find null option", cmdline.hasOption((String) null)); 083 assertTrue("expected a true", cmdline.getSwitch("--bool").booleanValue()); 084 } 085 086 public void testPropertyTriggers() { 087 // nothing to test 088 CommandLine cmdline = createCommandLine(); 089 090 Set triggers = cmdline.getOptionTriggers(); 091 Iterator iter = triggers.iterator(); 092 assertEquals("wrong # of triggers", 4, triggers.size()); 093 assertTrue("cannot find trigger", triggers.contains("--bool")); 094 assertTrue("cannot find trigger", triggers.contains("--present")); 095 assertTrue("cannot find trigger", triggers.contains("--multiple")); 096 assertTrue("cannot find trigger", triggers.contains("--alsopresent")); 097 098 assertFalse("should not find null option", cmdline.hasOption((String) null)); 099 assertTrue("expected a true", cmdline.getSwitch("--bool").booleanValue()); 100 } 101}