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.validation;
18  
19  import java.util.Arrays;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Set;
23  import java.util.TreeSet;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.commons.cli2.resource.ResourceConstants;
28  import org.apache.commons.cli2.resource.ResourceHelper;
29  
30  public class EnumValidatorTest
31      extends TestCase {
32      private final static ResourceHelper resources = ResourceHelper.getResourceHelper();
33      private final Set enumSet = new TreeSet(Arrays.asList(new Object[] { "red", "green", "blue" }));
34  
35      public void testValidate()
36          throws InvalidArgumentException {
37          final Object[] array = new Object[] { "red", "green" };
38  
39          {
40              final List list = Arrays.asList(array);
41              final EnumValidator validator = new EnumValidator(enumSet);
42              assertEquals("valid values are incorrect", enumSet, validator.getValidValues());
43              validator.validate(list);
44  
45              final Iterator i = list.iterator();
46              assertEquals("red", i.next());
47              assertEquals("green", i.next());
48              assertFalse(i.hasNext());
49          }
50      }
51  
52      public void testNonMember() {
53          final Object[] array = new Object[] { "red", "pink" };
54          final List list = Arrays.asList(array);
55          final EnumValidator validator = new EnumValidator(enumSet);
56  
57          try {
58              validator.validate(list);
59              fail("InvalidArgumentException");
60          } catch (InvalidArgumentException e) {
61              assertEquals(resources.getMessage(ResourceConstants.ENUM_ILLEGAL_VALUE,
62                                                new Object[] { "pink", validator.getValuesAsString() }),
63                           e.getMessage());
64          }
65      }
66  }