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