View Javadoc

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;
18  
19  import org.apache.commons.cli2.option.ArgumentTest;
20  import org.apache.commons.cli2.option.PropertyOption;
21  
22  /**
23   * @author Rob Oxspring
24   */
25  public abstract class WriteableCommandLineTestCase extends CommandLineTestCase {
26  
27  	private WriteableCommandLine writeable;
28  
29  	protected abstract WriteableCommandLine createWriteableCommandLine();
30  
31  	/* (non-Javadoc)
32  	 * @see org.apache.commons.cli2.CommandLineTest#createCommandLine()
33  	 */
34  	protected final CommandLine createCommandLine() {
35  		final WriteableCommandLine cl = createWriteableCommandLine();
36  		cl.addOption(present);
37  		cl.addProperty(new PropertyOption(), "present","present property");
38  		cl.addSwitch(bool,true);
39  		cl.addValue(present,"present value");
40  		cl.addOption(multiple);
41  		cl.addValue(multiple,"value 1");
42  		cl.addValue(multiple,"value 2");
43  		cl.addValue(multiple,"value 3");
44  		return cl;
45  	}
46  
47  	/*
48  	 * @see CommandLineTest#setUp()
49  	 */
50  	public void setUp() throws Exception {
51  		super.setUp();
52  		writeable = createWriteableCommandLine();
53  	}
54  	public final void testAddOption() {
55  		assertFalse(writeable.hasOption(present));
56  		writeable.addOption(present);
57  		assertTrue(writeable.hasOption(present));
58  	}
59  	public final void testAddValue() {
60  		assertFalse(writeable.hasOption(present));
61  		assertTrue(writeable.getValues(present).isEmpty());
62  		writeable.addValue(present,"value");
63  		assertContentsEqual(list("value"),writeable.getValues(present));
64  
65  		// most options shouldn't appear due to adding values
66  		assertFalse(writeable.hasOption(present));
67  
68  		final Argument arg = ArgumentTest.buildHostArgument();
69  
70  		assertFalse(writeable.hasOption(arg));
71  		assertTrue(writeable.getValues(arg).isEmpty());
72  		writeable.addValue(arg,"value");
73  		assertContentsEqual(list("value"),writeable.getValues(arg));
74  
75  		// Arguments should force the option present
76  		assertTrue(writeable.hasOption(arg));
77  	}
78  	public final void testAddSwitch() {
79  		assertFalse(writeable.hasOption(present));
80  		assertNull(writeable.getSwitch(present));
81  		writeable.addSwitch(present,true);
82  		assertEquals(Boolean.TRUE,writeable.getSwitch(present));
83  		assertTrue(writeable.hasOption(present));
84  	}
85  	public final void testAddProperty() {
86  		assertNull(writeable.getProperty(new PropertyOption(), "present"));
87  		writeable.addProperty(new PropertyOption(), "present","present value");
88  		assertEquals("present value",writeable.getProperty(new PropertyOption(), "present"));
89  	}
90  	public final void testLooksLikeOption() {
91  		//TODO Implement looksLikeOption().
92  	}
93  }