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 ANY 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; 024 025import static java.lang.annotation.ElementType.FIELD; 026import static java.lang.annotation.RetentionPolicy.RUNTIME; 027import static tech.sirwellington.alchemy.generator.NumberGenerators.integers; 028import static tech.sirwellington.alchemy.generator.NumberGenerators.negativeIntegers; 029import static tech.sirwellington.alchemy.generator.NumberGenerators.positiveIntegers; 030import static tech.sirwellington.alchemy.test.Checks.Internal.checkNotNull; 031import static tech.sirwellington.alchemy.test.Checks.Internal.checkThat; 032import static tech.sirwellington.alchemy.test.junit.runners.GenerateInteger.Type.POSITIVE; 033import static tech.sirwellington.alchemy.test.junit.runners.GenerateInteger.Type.RANGE; 034 035/** 036 * Used in with the {@link AlchemyTestRunner}, this Annotations allows the 037 * Runtime Injection of Generated Integers from the {@link AlchemyGenerator} library. 038 * 039 * Example: 040 * <pre> 041 * {@code 042 * `@RunWith(AlchemyTestRunner.class) 043 * public class ExampleTest 044 * { 045 * `@GenerateInteger(POSITIVE) 046 * private int size; 047 * 048 * ... 049 * } 050 * } 051 * </pre> 052 * Note, ticks (`) used to escape Javadocs. 053 * 054 * @see GenerateString 055 * @see GenerateLong 056 * @see GenerateDouble 057 * 058 * @author SirWellington 059 */ 060@Target(FIELD) 061@Retention(RUNTIME) 062public @interface GenerateInteger 063{ 064 065 Type value() default POSITIVE; 066 067 int min() default 0; 068 069 int max() default 0; 070 071 public enum Type 072 { 073 POSITIVE, 074 NEGATIVE, 075 ANY, 076 RANGE; 077 } 078 079 @Internal 080 @NonInstantiable 081 class Values 082 { 083 084 private Values() throws IllegalAccessException 085 { 086 throw new IllegalAccessException("cannot instantiate"); 087 } 088 089 static AlchemyGenerator<Integer> createGeneratorFor(GenerateInteger annotation) 090 { 091 checkNotNull(annotation, "missing annotation"); 092 093 Type type = annotation.value(); 094 checkNotNull(type, "@GenerateInteger missing value"); 095 096 if (type == RANGE) 097 { 098 int min = annotation.min(); 099 int max = annotation.max(); 100 checkThat(min < max, "@GenerateInteger: min must be less than max"); 101 return integers(min, max); 102 } 103 104 //Cover remaining cases 105 switch (type) 106 { 107 case POSITIVE: 108 return positiveIntegers(); 109 case NEGATIVE: 110 return negativeIntegers(); 111 default: 112 return integers(Integer.MIN_VALUE, Integer.MAX_VALUE); 113 } 114 } 115 116 } 117 118}