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.net.URL;
022
023import tech.sirwellington.alchemy.annotations.access.Internal;
024import tech.sirwellington.alchemy.annotations.access.NonInstantiable;
025import tech.sirwellington.alchemy.generator.AlchemyGenerator;
026import tech.sirwellington.alchemy.generator.NetworkGenerators;
027
028import static java.lang.annotation.ElementType.FIELD;
029import static java.lang.annotation.RetentionPolicy.RUNTIME;
030import static tech.sirwellington.alchemy.test.Checks.Internal.checkNotNull;
031import static tech.sirwellington.alchemy.test.Checks.Internal.checkThat;
032
033/*
034 * <pre>
035 * 
036 * {@code
037 * `@RunWith(AlchemyTestRunner.class)
038 * public class ExampleTest
039 * {
040 *   `@GenerateURL(HEXADECIMAL)
041 *   private String username;
042 * 
043 *  ...
044 * }
045 * 
046 * </pre>
047 */
048
049/**
050 * Used in with the {@link AlchemyTestRunner}, this Annotations allows the
051 * Runtime Injection of Generated Strings from the {@link AlchemyGenerator} library.
052 * <p>
053 * Example:
054 * <pre>
055 * {@code
056 * `@RunWith(AlchemyTestRunner.class)
057 * public class ExampleTest
058 * {
059 *   `@GenerateURL
060 *   private URL weblink;
061 *
062 * }
063 * }
064 * </pre>
065 * <p>
066 * Note, '`' (ticks) used to escape Javadocs.
067 *
068 * @author SirWellington
069 * @see GenerateString
070 */
071@Target(FIELD)
072@Retention(RUNTIME)
073public @interface GenerateURL
074{
075
076    String protocol() default "http";
077
078    @Internal
079    @NonInstantiable
080    static class Values
081    {
082
083        private Values() throws IllegalAccessException
084        {
085            throw new IllegalAccessException("cannot instantiate");
086        }
087
088        static AlchemyGenerator<URL> createGeneratorFor(GenerateURL annotation)
089        {
090            checkNotNull(annotation, "annotation is missing");
091
092            String protocol = annotation.protocol();
093            checkNotNull(protocol, "protocol cannot be null");
094            checkThat(!protocol.isEmpty(), "protocol is empty");
095
096            return NetworkGenerators.urlsWithProtocol(protocol);
097        }
098    }
099
100}