001/*
002 * Copyright © 2018. Sir Wellington.
003 * Licensed under the Apache License, Version 2.0 (the "License");
004 * you may not use this file except in compliance with the License.
005 *
006 * You may obtain a copy of the License at
007 *     http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software
010 * distributed under the License is distributed on an "AS IS" BASIS,
011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 * See the License for the specific language governing permissions and
013 * limitations under the License.
014 */
015
016package tech.sirwellington.alchemy.test.junit.runners;
017
018import java.lang.annotation.Retention;
019import java.lang.annotation.Target;
020
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 * <p>
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 * @author SirWellington
057 * @see GenerateString
058 * @see GenerateInstant
059 */
060@Target(FIELD)
061@Retention(RUNTIME)
062public @interface GenerateEnum
063{
064
065    @Internal
066    @NonInstantiable
067    static class Values
068    {
069
070        private Values() throws IllegalAccessException
071        {
072            throw new IllegalAccessException("cannot instantiate");
073        }
074
075        static <E extends Enum> AlchemyGenerator<E> createGeneratorFor(GenerateEnum annotation, Class<E> enumClass) throws IllegalArgumentException
076        {
077            checkNotNull(annotation, "missing annotation");
078            checkNotNull(enumClass, "missing enum class");
079
080            return EnumGenerators.enumValueOf(enumClass);
081        }
082    }
083
084}