candr.yoclip.option.OptionPropertiesSetterTest.java Source code

Java tutorial

Introduction

Here is the source code for candr.yoclip.option.OptionPropertiesSetterTest.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 java.lang.reflect.Method;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.Test;

import candr.yoclip.OptionsBadNameException;
import candr.yoclip.OptionsParseException;
import candr.yoclip.annotation.OptionProperties;
import candr.yoclip.annotation.PropertyDescription;

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

public class OptionPropertiesSetterTest {

    @Test(expected = UnsupportedOperationException.class)
    public void initWithWrongArgumentCount() throws NoSuchMethodException {
        final Method setter = TestCase.class.getDeclaredMethod("setWithWrongNumberArgs", String.class);
        final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        new OptionPropertiesSetter<TestCase>(optionProperties, setter);
    }

    @Test(expected = IllegalArgumentException.class)
    public void initWithBadKeyArg() throws NoSuchMethodException {
        final Method setter = TestCase.class.getDeclaredMethod("setWithBadKeyArg", Integer.class, String.class);
        final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        new OptionPropertiesSetter<TestCase>(optionProperties, setter);
    }

    @Test(expected = IllegalArgumentException.class)
    public void initWithBadValueArg() throws NoSuchMethodException {
        final Method setter = TestCase.class.getDeclaredMethod("setWithBadValueArg", String.class, Integer.class);
        final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        new OptionPropertiesSetter<TestCase>(optionProperties, setter);
    }

    @Test(expected = OptionsBadNameException.class)
    public void initWithNoName() throws NoSuchMethodException {
        final Method setter = TestCase.class.getDeclaredMethod("setWithoutPropertyName", String.class,
                String.class);
        final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        new OptionPropertiesSetter<TestCase>(optionProperties, setter);
    }

    @Test
    public void getNames() throws NoSuchMethodException {
        final Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
        final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        final OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties,
                setter);
        assertThat("property name", test.getNames()[0], is("P"));
    }

    @Test
    public void getNamesWithDefault() throws NoSuchMethodException {
        final Method setter = TestCase.class.getDeclaredMethod("setPropertyNoDescription", String.class,
                String.class);
        final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        final OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties,
                setter);
        assertThat("property name", test.getNames()[0], is("D"));
    }

    @Test
    public void testGetUsage() throws NoSuchMethodException {
        final Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
        final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        final OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties,
                setter);
        assertThat("usage", test.getUsage(), is("test usage"));
    }

    @Test
    public void testGetDescription() throws NoSuchMethodException {
        Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
        OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties, setter);
        assertThat("description", test.getDescription(), is("test  description"));

        setter = TestCase.class.getDeclaredMethod("setPropertyNoDescription", String.class, String.class);
        optionProperties = setter.getAnnotation(OptionProperties.class);
        test = new OptionPropertiesSetter<TestCase>(optionProperties, setter);
        assertTrue("expected empty description not " + test.getDescription(),
                StringUtils.isEmpty(test.getDescription()));
    }

    @Test
    public void testIsRequired() throws NoSuchMethodException {
        Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
        OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties, setter);
        assertTrue("expected to be required", test.isRequired());

        setter = TestCase.class.getDeclaredMethod("setPropertyNoDescription", String.class, String.class);
        optionProperties = setter.getAnnotation(OptionProperties.class);
        test = new OptionPropertiesSetter<TestCase>(optionProperties, setter);
        assertFalse("did not expect to be required", test.isRequired());
    }

    @Test
    public void testIsProperties() throws NoSuchMethodException {
        final Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
        final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        final OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties,
                setter);
        assertTrue("expected to be properties", test.isProperties());
    }

    @Test
    public void testGetPropertyDescriptions() throws NoSuchMethodException {
        Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
        OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties, setter);

        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"));

        setter = TestCase.class.getDeclaredMethod("setPropertyNoDescription", String.class, String.class);
        optionProperties = setter.getAnnotation(OptionProperties.class);
        test = new OptionPropertiesSetter<TestCase>(optionProperties, setter);
        assertTrue("did not expect property descriptions", test.getPropertyDescriptions().isEmpty());
    }

    @Test
    public void testSetOption() throws NoSuchMethodException {
        final Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
        final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        final OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties,
                setter);
        final TestCase bean = mock(TestCase.class);

        final String key = "key";
        final String value = "value";
        test.setOption(bean, key + OptionProperties.KEY_VALUE_SEPARATOR + value);
        verify(bean).setProperty(key, value);
    }

    @Test(expected = OptionsParseException.class)
    public void testSetOptionWithBadValue() throws NoSuchMethodException {
        final Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
        final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
        final OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties,
                setter);
        test.setOption(mock(TestCase.class), "value");
    }

    interface TestCase {
        @SuppressWarnings("unused")
        @OptionProperties(name = "P", required = true, usage = { "test", "usage" }, description = { "test", "",
                "description" }, propertyDescriptions = {
                        @PropertyDescription(synopsis = "foo", details = { "foo", "property" }),
                        @PropertyDescription(synopsis = "bar", details = { "bar", "\n", "property" }) })
        void setProperty(String key, String value);

        @SuppressWarnings("unused")
        @OptionProperties()
        void setPropertyNoDescription(String key, String value);

        @SuppressWarnings("unused")
        @OptionProperties(name = "")
        void setWithoutPropertyName(String key, String value);

        @SuppressWarnings("unused")
        @OptionProperties()
        void setWithBadKeyArg(Integer key, String value);

        @SuppressWarnings("unused")
        @OptionProperties()
        void setWithBadValueArg(String key, Integer value);

        @SuppressWarnings("unused")
        @OptionProperties()
        void setWithWrongNumberArgs(String value);
    }
}