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; 021 022import tech.sirwellington.alchemy.annotations.access.Internal; 023import tech.sirwellington.alchemy.annotations.access.NonInstantiable; 024import tech.sirwellington.alchemy.generator.AlchemyGenerator; 025import tech.sirwellington.alchemy.generator.EnumGenerators; 026 027import static java.lang.annotation.ElementType.FIELD; 028import static java.lang.annotation.RetentionPolicy.RUNTIME; 029import static tech.sirwellington.alchemy.test.Checks.Internal.checkNotNull; 030 031/** 032 * Used in with the {@link AlchemyTestRunner}, this Annotations allows the Runtime Injection of Enum values, using 033 * {@link EnumGenerators} from the {@link AlchemyGenerator} library. 034 * <p> 035 * Example: 036 * <pre> 037 * {@code 038 * `@RunWith(AlchemyTestRunner.class) 039 * public class ExampleTest 040 * { 041 * enum Role 042 * { 043 * DEVELOPER, 044 * OWNER, 045 * MANGER, 046 * OTHER 047 * } 048 * 049 * `@GenerateEnum 050 * private Role role; 051 * 052 * ... 053 * } 054 * } 055 * </pre> Note, ticks (`) used to escape Javadocs. 056 * 057 * @author SirWellington 058 * @see GenerateString 059 * @see GenerateInstant 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}