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   
18  package org.apache.commons.betwixt;
19  
20  import junit.framework.TestCase;
21  
22  /**
23   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
24   * @version $Revision: 561314 $
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      
33      public void testGetValue() {
34          Options options = new Options();
35          options.addOption("A", "Alpha");
36          options.addOption("B", "Beta");
37         
38          assertEquals("Get added value", "Alpha", options.getValue("A"));
39          assertNull("Value not added is null", options.getValue("C"));
40          
41          options.addOption("A", "New Value");
42          assertEquals("Lat value set wins", "New Value", options.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              {
57                  assertTrue("A named twice", (flag & (A_SHIFT))>0);
58                  flag -= (A_SHIFT);
59              }
60              else if (names[i].equals("B"))
61              {
62                  assertTrue("B named twice", (flag & (B_SHIFT))>0);
63                  flag -= (B_SHIFT);
64              }
65              else if (names[i].equals("C"))
66              {
67                  assertTrue("C named twice", (flag & (C_SHIFT))>0);
68                  flag -= (C_SHIFT);
69              }           
70              else
71              {
72                  fail("Unexpected name: " + names[i]);
73              }
74          }
75      }
76      
77      public void testAddOptions() {
78          Options a = new Options();
79          a.addOption("A", "Alpha");
80          a.addOption("B", "Beta");
81          a.addOption("C", "Gamma");
82          
83          Options b = new Options();
84          b.addOption("A", "Apple");
85          b.addOption("C", "Carrot");
86          b.addOption("E", "Egg Plant");
87          
88          a.addOptions(b);
89          
90          // Lat value set wins
91          assertEquals("Apple",a.getValue("A"));
92          assertEquals("Beta",a.getValue("B"));
93          assertEquals("Carrot",a.getValue("C"));
94          assertEquals("Egg Plant",a.getValue("E"));
95      }
96      
97  }