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 java.util.UUID;
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.StringGenerators.alphabeticString;
029import static tech.sirwellington.alchemy.generator.StringGenerators.alphanumericString;
030import static tech.sirwellington.alchemy.generator.StringGenerators.hexadecimalString;
031import static tech.sirwellington.alchemy.generator.StringGenerators.uuids;
032import static tech.sirwellington.alchemy.test.Checks.Internal.checkNotNull;
033import static tech.sirwellington.alchemy.test.Checks.Internal.checkThat;
034
035/*
036 * <pre>
037 * 
038 * {@code
039 * `@RunWith(AlchemyTestRunner.class)
040 * public class ExampleTest
041 * {
042 *   `@GenerateString(HEXADECIMAL)
043 *   private String username;
044 * 
045 *  ...
046 * }
047 * 
048 * </pre>
049 */
050/**
051 * Used in with the {@link AlchemyTestRunner}, this Annotations allows the 
052 * Runtime Injection of Generated Strings from the {@link AlchemyGenerator} library.
053 * <p>
054 * Example:
055 * <pre>
056 * {@code 
057 * `@RunWith(AlchemyTestRunner.class)
058 * public class ExampleTest
059 * {
060 *   `@GenerateString(HEXADECIMAL)
061 *   private String username;
062 * 
063 * }
064 * }
065 * </pre>
066 * 
067 * Note, '`' (ticks) used to escape Javadocs. 
068 * @see GenerateInteger
069 * @author SirWellington
070 */
071@Target(FIELD)
072@Retention(RUNTIME)
073public @interface GenerateString
074{
075
076    /*
077     * Named value because it allows for @StringGenerator(ALPHABETIC) instead of @StringGenerator(type = ALPHABETIC)
078     */
079    /**
080     * The type of String to Generate
081     * @return 
082     */
083    Type value() default Type.ALPHABETIC;
084
085    /**
086     * The length of the string, must be {@code > 0}. 
087     * @return 
088     */
089    int length() default 10;
090
091    public static enum Type
092    {
093        ALPHABETIC,
094        ALPHANUMERIC,
095        HEXADECIMAL,
096        /**
097         * For UUIDS, the {@link #length() } property will be ignored, and instead the standard UUID size from {@link UUID#randomUUID()
098         * } is used.
099         */
100        UUID;
101
102    }
103
104    @Internal
105    @NonInstantiable
106    static class Values
107    {
108
109        private Values() throws IllegalAccessException
110        {
111            throw new IllegalAccessException("cannot instantiate");
112        }
113
114        static AlchemyGenerator<String> createGeneratorFor(GenerateString annotation)
115        {
116            checkNotNull(annotation, "annotation is missing");
117
118            int length = annotation.length();
119            checkThat(length > 0, "Invalid @GenerateString use, length must be positive");
120
121            Type type = annotation.value();
122            checkNotNull(type, "@GenerateString Annotation missing type");
123
124            switch (type)
125            {
126                case ALPHABETIC:
127                    return alphabeticString(length);
128                case ALPHANUMERIC:
129                    return alphanumericString(length);
130                case HEXADECIMAL:
131                    return hexadecimalString(length);
132                case UUID:
133                    return uuids;
134                default:
135                    return alphabeticString(length);
136            }
137
138        }
139    }
140
141}