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