candr.yoclip.option.OptionPropertiesFieldTest.java Source code

Java tutorial

Introduction

Here is the source code for candr.yoclip.option.OptionPropertiesFieldTest.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package candr.yoclip.option;

import candr.yoclip.OptionsBadNameException;
import candr.yoclip.OptionsBadTypeException;
import candr.yoclip.OptionsParseException;
import candr.yoclip.annotation.OptionProperties;
import candr.yoclip.annotation.PropertyDescription;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

/**
 * Test cases for the {@link OptionPropertiesField} option wrapper.
 */
public class OptionPropertiesFieldTest {

    final private OptionPropertiesField<OptionPropertiesFieldTest> test;

    @SuppressWarnings("unused")
    @OptionProperties
    private String badProperties;

    @OptionProperties(name = "D", required = true, usage = { "test", "usage" }, description = { "test", "",
            "description" }, propertyDescriptions = {
                    @PropertyDescription(synopsis = "foo", details = { "foo", "property" }),
                    @PropertyDescription(synopsis = "bar", details = { "bar", "\n", "property" }) })
    private Map<String, String> properties;

    @SuppressWarnings("unused")
    @OptionProperties(name = "P")
    private Map<String, String> noPropertyDescriptions;

    public OptionPropertiesFieldTest() throws NoSuchFieldException {
        final Field field = getClass().getDeclaredField("properties");
        final OptionProperties properties = field.getAnnotation(OptionProperties.class);
        test = new OptionPropertiesField<OptionPropertiesFieldTest>(properties, field);
    }

    @Test(expected = OptionsBadTypeException.class)
    public void initBadType() throws NoSuchFieldException {
        final Field field = getClass().getDeclaredField("badProperties");
        final OptionProperties properties = field.getAnnotation(OptionProperties.class);
        new OptionPropertiesField(properties, field);
    }

    @Test(expected = OptionsBadNameException.class)
    public void initBadName() throws NoSuchFieldException {
        final Field field = getClass().getDeclaredField("properties");
        final OptionProperties properties = mock(OptionProperties.class);
        new OptionPropertiesField(properties, field);
    }

    @Test
    public void getNames() {
        final String[] names = test.getNames();
        assertThat("names length", names.length, is(1));
        assertThat("name", names[0], is("D"));
    }

    @Test
    public void getUsage() {
        assertThat("usage", test.getUsage(), is("test usage"));
    }

    @Test
    public void getDescription() {
        assertThat("description", test.getDescription(), is("test  description"));
    }

    @Test
    public void isRequired() {
        assertThat("required", test.isRequired(), is(true));
    }

    @Test
    public void isProperties() {
        assertThat("is properties", test.isProperties(), is(true));
    }

    @Test
    public void isHelp() {
        assertThat("is help", test.isHelp(), is(false));
    }

    @Test
    public void isArguments() {
        assertThat("is arguments", test.isArguments(), is(false));
    }

    @Test
    public void hasValue() {
        assertThat("has value", test.hasValue(), is(false));
    }

    @Test
    public void getDeclaringClass() {
        assertTrue("unexpected declaring class " + test.getDeclaringClass(),
                test.getDeclaringClass().equals(OptionPropertiesFieldTest.class));
    }

    @Test
    public void getType() {
        assertTrue("unexpected type " + test.getType(), test.getType().equals(Map.class));
    }

    @Test
    public void getPropertyDescriptions() {
        final List<Pair<String, String>> propertyDescriptions = test.getPropertyDescriptions();
        assertThat("propertyDescriptions size", propertyDescriptions.size(), is(2));

        assertThat("description[0] rhs", propertyDescriptions.get(0).getLeft(), is("foo"));
        assertThat("description[0] lhs", propertyDescriptions.get(0).getRight(), is("foo property"));

        assertThat("description[1] rhs", propertyDescriptions.get(1).getLeft(), is("bar"));
        assertThat("description[1] lhs", propertyDescriptions.get(1).getRight(), is("bar\nproperty"));
    }

    @Test
    public void getPropertyDescriptionsWithoutDescriptions() throws NoSuchFieldException {
        final Field field = getClass().getDeclaredField("noPropertyDescriptions");
        final OptionProperties properties = field.getAnnotation(OptionProperties.class);
        assertThat("no property descriptions",
                new OptionPropertiesField<OptionPropertiesFieldTest>(properties, field).getPropertyDescriptions()
                        .size(),
                is(0));
    }

    @Test
    public void setOption() {
        try {
            properties = new HashMap<String, String>();

            test.setOption(this, "foo=bar");
            test.setOption(this, "bar=foo");

            assertThat("properties size", properties.size(), is(2));
            assertThat("foo property", properties.get("foo"), is("bar"));
            assertThat("bar property", properties.get("bar"), is("foo"));

        } finally {
            properties = null;
        }
    }

    @Test
    public void setOptionWithBadValue() {
        try {
            test.setOption(this, "foobar");
        } catch (final OptionsParseException e) {
            assertThat("expected bad value", e.getMessage(), is("Yikes!!! Expected key=value not 'foobar'..."));
        }
    }

    @Test
    public void setOptionWithNullMap() {
        try {
            test.setOption(this, "foo=bar");
        } catch (final OptionsParseException e) {
            assertThat("expected null value", e.getMessage(), is("properties is null and cannot be set..."));
        }
    }

    @Test
    public void setOptionWithReadonlyMap() {
        try {
            properties = Collections.emptyMap();
            test.setOption(this, "foo=bar");
        } catch (final OptionsParseException e) {
            assertThat("expected readonly properties", e.getMessage(),
                    is("Yikes!!! Map.put(key,value) threw an exception..."));
        } finally {
            properties = null;
        }
    }
}