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.longs;
028import static tech.sirwellington.alchemy.generator.NumberGenerators.positiveLongs;
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.GenerateLong.Type.POSITIVE;
032import static tech.sirwellington.alchemy.test.junit.runners.GenerateLong.Type.RANGE;
033
034/**
035 * Used in with the {@link AlchemyTestRunner}, this Annotations allows the
036 * Runtime Injection of Generated Longs from the {@link AlchemyGenerator} library.
037 * <p>
038 * Example:
039 * <pre>
040 * {@code
041 * `@RunWith(AlchemyTestRunner.class)
042 * public class ExampleTest
043 * {
044 *   `@GenerateLongs(POSITIVE)
045 *    private long hits;
046 *
047 *    ...
048 * }
049 * }
050 * </pre>
051 * Note, ticks (`) used to escape Javadocs.
052 *
053 * @author SirWellington
054 * @see GenerateInteger
055 * @see GenerateString
056 */
057@Target(FIELD)
058@Retention(RUNTIME)
059public @interface GenerateLong
060{
061
062    Type value() default POSITIVE;
063
064    long min() default 0;
065
066    long 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<Long> createGeneratorFor(GenerateLong annotation)
087        {
088            checkNotNull(annotation, "missing annotation");
089
090            Type type = annotation.value();
091            checkNotNull(type, "@GenerateLong missing value");
092
093            if (type == RANGE)
094            {
095                long min = annotation.min();
096                long max = annotation.max();
097                checkThat(min < max, "@GenerateLong: min must be less than max");
098                return longs(min, max);
099            }
100
101            //Cover remaining cases
102            switch (type)
103            {
104                case POSITIVE:
105                    return positiveLongs();
106                case NEGATIVE:
107                    return longs(Long.MIN_VALUE, 0);
108                default:
109                    return longs(Long.MIN_VALUE, Long.MAX_VALUE);
110            }
111        }
112
113    }
114
115}