001/*
002 * Copyright © 2018. Sir Wellington.
003 * Licensed under the Apache License, Version 2.0 (the "License");
004 * you may not use this file except in compliance with the License.
005 *
006 * You may obtain a copy of the License at
007 *     http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software
010 * distributed under the License is distributed on an "AS IS" BASIS,
011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 * See the License for the specific language governing permissions and
013 * limitations under the License.
014 */
015
016package tech.sirwellington.alchemy.test.junit.runners;
017
018import java.lang.annotation.Retention;
019import java.lang.annotation.Target;
020import java.net.URL;
021
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/**
049 * Used in with the {@link AlchemyTestRunner}, this Annotations allows the
050 * Runtime Injection of Generated Strings from the {@link AlchemyGenerator} library.
051 * <p>
052 * Example:
053 * <pre>
054 * {@code
055 * `@RunWith(AlchemyTestRunner.class)
056 * public class ExampleTest
057 * {
058 *   `@GenerateURL
059 *   private URL weblink;
060 *
061 * }
062 * }
063 * </pre>
064 * <p>
065 * Note, '`' (ticks) used to escape Javadocs.
066 *
067 * @author SirWellington
068 * @see GenerateString
069 */
070@Target(FIELD)
071@Retention(RUNTIME)
072public @interface GenerateURL
073{
074
075    String protocol() default "http";
076
077    @Internal
078    @NonInstantiable
079    static class Values
080    {
081
082        private Values() throws IllegalAccessException
083        {
084            throw new IllegalAccessException("cannot instantiate");
085        }
086
087        static AlchemyGenerator<URL> createGeneratorFor(GenerateURL annotation)
088        {
089            checkNotNull(annotation, "annotation is missing");
090
091            String protocol = annotation.protocol();
092            checkNotNull(protocol, "protocol cannot be null");
093            checkThat(!protocol.isEmpty(), "protocol is empty");
094
095            return NetworkGenerators.urlsWithProtocol(protocol);
096        }
097    }
098
099}