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