All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nohope.test.EnumUtils Maven / Gradle / Ivy

package org.nohope.test;

import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.EnumSet;

import static java.lang.reflect.Modifier.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * @author Ketoth Xupack
 * @since 2012-01-28 18:31
 */
public final class EnumUtils {

    private EnumUtils() {
    }

    /** Tests whatever all enum constructors are private. */
    public static > void assertEnumConstructor(final Class clazz) {
        final Constructor[] cons = clazz.getDeclaredConstructors();

        // hm... seems after code generation enum constructor always private ;)
        for (final Constructor c : cons) {
            final int m = c.getModifiers();
            assertThat("illegal constructor found: " + c,
                    isPrivate(m) || (!isProtected(m) && !isPublic(m)), equalTo(true));
        }
    }

    /**
     * Geek check for calling {@code values()} and {@code valueOf(String)}
     * on constructor.
     */
    public static > void basicAssertions(final Class clazz) {
        final Iterable set = EnumSet.allOf(clazz);

        for (final E e : set) {
            assertThat(Enum.valueOf(clazz, e.toString()), sameInstance(e));
        }
    }

    /**
     * Routine for testing logic which depends on enum order logic.
     *
     * @param values expected order of enums
     */
    @SafeVarargs
    protected static > void assertOrder(final Class clazz, final E... values) {
        final Collection set = EnumSet.allOf(clazz);
        assertThat("All enum values should be passed", set.size(), equalTo(values.length));

        int i = 0;
        for (final E v : values) {
            assertThat("Unexpected order value for " + v, i, equalTo(v.ordinal()));
            i++;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy