001/*
002 * Copyright 2015 SirWellington Tech.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANYTIME KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package tech.sirwellington.alchemy.test.junit.runners;
018
019import java.lang.annotation.Retention;
020import java.lang.annotation.Target;
021import tech.sirwellington.alchemy.annotations.access.Internal;
022import tech.sirwellington.alchemy.annotations.access.NonInstantiable;
023import tech.sirwellington.alchemy.generator.AlchemyGenerator;
024import tech.sirwellington.alchemy.generator.EnumGenerators;
025
026import static java.lang.annotation.ElementType.FIELD;
027import static java.lang.annotation.RetentionPolicy.RUNTIME;
028import static tech.sirwellington.alchemy.test.Checks.Internal.checkNotNull;
029
030/**
031 * Used in with the {@link AlchemyTestRunner}, this Annotations allows the Runtime Injection of Enum values, using
032 * {@link EnumGenerators} from the {@link AlchemyGenerator} library.
033 *
034 * Example:
035 * <pre>
036 * {@code
037 * `@RunWith(AlchemyTestRunner.class)
038 *  public class ExampleTest
039 *  {
040 *   enum Role
041 *   {
042 *    DEVELOPER,
043 *    OWNER,
044 *    MANGER,
045 *    OTHER
046 *   }
047 *
048 *    `@GenerateEnum
049 *     private Role role;
050 *
051 *    ...
052 *  }
053 * }
054 * </pre> Note, ticks (`) used to escape Javadocs.
055 *
056 * @see GenerateString
057 * @see GenerateInstant
058 *
059 * @author SirWellington
060 */
061@Target(FIELD)
062@Retention(RUNTIME)
063public @interface GenerateEnum
064{
065
066    @Internal
067    @NonInstantiable
068    static class Values
069    {
070
071        private Values() throws IllegalAccessException
072        {
073            throw new IllegalAccessException("cannot instantiate");
074        }
075
076        static <E extends Enum> AlchemyGenerator<E> createGeneratorFor(GenerateEnum annotation, Class<E> enumClass) throws IllegalArgumentException
077        {
078            checkNotNull(annotation, "missing annotation");
079            checkNotNull(enumClass, "missing enum class");
080
081            return EnumGenerators.enumValueOf(enumClass);
082        }
083    }
084
085}