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.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 * 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 * @see GenerateInteger 054 * @see GenerateString 055 * 056 * @author SirWellington 057 */ 058@Target(FIELD) 059@Retention(RUNTIME) 060public @interface GenerateLong 061{ 062 063 Type value() default POSITIVE; 064 065 long min() default 0; 066 067 long max() default 0; 068 069 public enum Type 070 { 071 POSITIVE, 072 NEGATIVE, 073 ANY, 074 RANGE; 075 } 076 077 @Internal 078 @NonInstantiable 079 class Values 080 { 081 082 private Values() throws IllegalAccessException 083 { 084 throw new IllegalAccessException("cannot instantiate"); 085 } 086 087 static AlchemyGenerator<Long> createGeneratorFor(GenerateLong annotation) 088 { 089 checkNotNull(annotation, "missing annotation"); 090 091 Type type = annotation.value(); 092 checkNotNull(type, "@GenerateLong missing value"); 093 094 if (type == RANGE) 095 { 096 long min = annotation.min(); 097 long max = annotation.max(); 098 checkThat(min < max, "@GenerateLong: min must be less than max"); 099 return longs(min, max); 100 } 101 102 //Cover remaining cases 103 switch (type) 104 { 105 case POSITIVE: 106 return positiveLongs(); 107 case NEGATIVE: 108 return longs(Long.MIN_VALUE, 0); 109 default: 110 return longs(Long.MIN_VALUE, Long.MAX_VALUE); 111 } 112 } 113 114 } 115 116}