01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
18: package org.apache.commons.betwixt;
19:
20: import junit.framework.TestCase;
21:
22: /**
23: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
24: * @version $Revision: 438373 $
25: */
26: public class TestOptions extends TestCase {
27:
28: private static final int A_SHIFT = 1 << 0;
29: private static final int B_SHIFT = 1 << 1;
30: private static final int C_SHIFT = 1 << 2;
31:
32: public void testGetValue() {
33: Options options = new Options();
34: options.addOption("A", "Alpha");
35: options.addOption("B", "Beta");
36:
37: assertEquals("Get added value", "Alpha", options.getValue("A"));
38: assertNull("Value not added is null", options.getValue("C"));
39:
40: options.addOption("A", "New Value");
41: assertEquals("Lat value set wins", "New Value", options
42: .getValue("A"));
43: }
44:
45: public void testGetNames() {
46: Options options = new Options();
47: options.addOption("A", "Alpha");
48: options.addOption("B", "Beta");
49: options.addOption("C", "Gamma");
50:
51: String[] names = options.getNames();
52: assertEquals("Expected three names", 3, names.length);
53: int flag = (A_SHIFT) + (B_SHIFT) + (C_SHIFT);
54: for (int i = 0; i < 3; i++) {
55: if (names[i].equals("A")) {
56: assertTrue("A named twice", (flag & (A_SHIFT)) > 0);
57: flag -= (A_SHIFT);
58: } else if (names[i].equals("B")) {
59: assertTrue("B named twice", (flag & (B_SHIFT)) > 0);
60: flag -= (B_SHIFT);
61: } else if (names[i].equals("C")) {
62: assertTrue("C named twice", (flag & (C_SHIFT)) > 0);
63: flag -= (C_SHIFT);
64: } else {
65: fail("Unexpected name: " + names[i]);
66: }
67: }
68: }
69:
70: public void testAddOptions() {
71: Options a = new Options();
72: a.addOption("A", "Alpha");
73: a.addOption("B", "Beta");
74: a.addOption("C", "Gamma");
75:
76: Options b = new Options();
77: b.addOption("A", "Apple");
78: b.addOption("C", "Carrot");
79: b.addOption("E", "Egg Plant");
80:
81: a.addOptions(b);
82:
83: // Lat value set wins
84: assertEquals("Apple", a.getValue("A"));
85: assertEquals("Beta", a.getValue("B"));
86: assertEquals("Carrot", a.getValue("C"));
87: assertEquals("Egg Plant", a.getValue("E"));
88: }
89:
90: }
|