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.validation;
018
019import java.util.Arrays;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Set;
023import java.util.TreeSet;
024
025import junit.framework.TestCase;
026
027import org.apache.commons.cli2.resource.ResourceConstants;
028import org.apache.commons.cli2.resource.ResourceHelper;
029
030public class EnumValidatorTest
031    extends TestCase {
032    private final static ResourceHelper resources = ResourceHelper.getResourceHelper();
033    private final Set enumSet = new TreeSet(Arrays.asList(new Object[] { "red", "green", "blue" }));
034
035    public void testValidate()
036        throws InvalidArgumentException {
037        final Object[] array = new Object[] { "red", "green" };
038
039        {
040            final List list = Arrays.asList(array);
041            final EnumValidator validator = new EnumValidator(enumSet);
042            assertEquals("valid values are incorrect", enumSet, validator.getValidValues());
043            validator.validate(list);
044
045            final Iterator i = list.iterator();
046            assertEquals("red", i.next());
047            assertEquals("green", i.next());
048            assertFalse(i.hasNext());
049        }
050    }
051
052    public void testNonMember() {
053        final Object[] array = new Object[] { "red", "pink" };
054        final List list = Arrays.asList(array);
055        final EnumValidator validator = new EnumValidator(enumSet);
056
057        try {
058            validator.validate(list);
059            fail("InvalidArgumentException");
060        } catch (InvalidArgumentException e) {
061            assertEquals(resources.getMessage(ResourceConstants.ENUM_ILLEGAL_VALUE,
062                                              new Object[] { "pink", validator.getValuesAsString() }),
063                         e.getMessage());
064        }
065    }
066}