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