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