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.doubles;
028import static tech.sirwellington.alchemy.generator.NumberGenerators.positiveDoubles;
029import static tech.sirwellington.alchemy.test.Checks.Internal.checkNotNull;
030import static tech.sirwellington.alchemy.test.Checks.Internal.checkThat;
031import static tech.sirwellington.alchemy.test.junit.runners.GenerateDouble.Type.POSITIVE;
032import static tech.sirwellington.alchemy.test.junit.runners.GenerateDouble.Type.RANGE;
033
034/**
035 * Used in with the {@link AlchemyTestRunner}, this Annotations allows the 
036 * Runtime Injection of Generated Doubles from the {@link AlchemyGenerator} library.
037 * 
038 * Example:
039 * <pre>
040 * {@code
041 * `@RunWith(AlchemyTestRunner.class)
042 * public class ExampleTest
043 * {
044 *   `@GenerateDouble(POSITIVE)
045 *    private double percentage;
046 * 
047 *    ...
048 * }
049 * }
050 * </pre>
051 * Note, ticks (`) used to escape Javadocs.
052 * 
053 * @see GenerateInteger
054 * @see GenerateLong
055 * @see GenerateString
056 * 
057 * @author SirWellington
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}