Last active
June 7, 2016 10:40
-
-
Save smiklosovic/6481c2c0e78628624dd4231d1034fa03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package net.miklosovic.qa; | |
| import static net.miklosovic.qa.Annotations.Architecture.PPC; | |
| import static net.miklosovic.qa.Annotations.JavaVendor.IBM; | |
| import static net.miklosovic.qa.Annotations.JavaVersion.VERSION_1_8; | |
| import static net.miklosovic.qa.Annotations.OperatingSystem.MAC; | |
| import org.apache.commons.lang3.SystemUtils; | |
| import org.junit.Test; | |
| import java.lang.annotation.Documented; | |
| import java.lang.annotation.ElementType; | |
| import java.lang.annotation.Retention; | |
| import java.lang.annotation.RetentionPolicy; | |
| import java.lang.annotation.Target; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.function.Function; | |
| public class Annotations { | |
| public interface Detectable { | |
| final class TrueDetection implements Detectable { | |
| @Override | |
| public boolean detected() { | |
| return true; | |
| } | |
| } | |
| final class FalseDetection implements Detectable { | |
| @Override | |
| public boolean detected() { | |
| return false; | |
| } | |
| } | |
| boolean detected(); | |
| } | |
| public interface DetectionResolvingStrategy { | |
| boolean resolve(); | |
| } | |
| public class BaseDetectionStrategy { | |
| private List<Detectable> detectables; | |
| public boolean resolve(Function<List<Detectable>, Boolean> resolvingFunction) { | |
| return resolvingFunction.apply(detectables); | |
| } | |
| public BaseDetectionStrategy detectables(List<Detectable> detectables) { | |
| this.detectables = detectables; | |
| return this; | |
| } | |
| } | |
| public class ConjunctionDetectionStrategy extends BaseDetectionStrategy implements DetectionResolvingStrategy { | |
| private Function<List<Detectable>, Boolean> resolvingFunction = detectables -> { | |
| for (Detectable detectable : detectables) { | |
| if (!detectable.detected()) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| }; | |
| @Override | |
| public boolean resolve() { | |
| return super.resolve(resolvingFunction); | |
| } | |
| } | |
| public class DisjunctionDetectionStrategy extends BaseDetectionStrategy implements DetectionResolvingStrategy { | |
| private Function<List<Detectable>, Boolean> resolvingFunction = detectables -> { | |
| for (Detectable detectable : detectables) { | |
| if (detectable.detected()) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| }; | |
| @Override | |
| public boolean resolve() { | |
| return super.resolve(resolvingFunction); | |
| } | |
| } | |
| public enum OperatingSystem implements Detectable { | |
| LINUX { | |
| @Override | |
| public boolean detected() { | |
| return SystemUtils.IS_OS_LINUX; | |
| } | |
| }, | |
| WINDOWS { | |
| @Override | |
| public boolean detected() { | |
| return SystemUtils.IS_OS_WINDOWS; | |
| } | |
| }, | |
| MAC { | |
| @Override | |
| public boolean detected() { | |
| return SystemUtils.IS_OS_MAC_OSX; | |
| } | |
| } | |
| } | |
| public enum JavaVendor implements Detectable { | |
| ORACLE { | |
| @Override | |
| public boolean detected() { | |
| return getVendor().contains(name().toLowerCase()); | |
| } | |
| }, | |
| OPEN_JDK { | |
| @Override | |
| public boolean detected() { | |
| return getVendor().contains(name().toLowerCase()); | |
| } | |
| }, | |
| IBM { | |
| @Override | |
| public boolean detected() { | |
| return getVendor().contains(name().toLowerCase()); | |
| } | |
| }; | |
| public static final String getVendor() { | |
| return System.getProperty("java.vendor"); | |
| } | |
| } | |
| public enum JavaVersion implements Detectable { | |
| VERSION_1_5 { | |
| @Override | |
| public boolean detected() { | |
| return SystemUtils.IS_JAVA_1_5; | |
| } | |
| }, | |
| VERSION_1_6 { | |
| @Override | |
| public boolean detected() { | |
| return SystemUtils.IS_JAVA_1_6; | |
| } | |
| }, | |
| VERSION_1_7 { | |
| @Override | |
| public boolean detected() { | |
| return SystemUtils.IS_JAVA_1_7; | |
| } | |
| }, | |
| VERSION_1_8 { | |
| @Override | |
| public boolean detected() { | |
| return SystemUtils.IS_JAVA_1_8; | |
| } | |
| } | |
| } | |
| public enum Architecture implements Detectable { | |
| x86_32 { | |
| @Override | |
| public boolean detected() { | |
| return new PlatformDetection().arch.equals(PlatformDetection.ARCH_X86_32); | |
| } | |
| }, | |
| x86_64 { | |
| @Override | |
| public boolean detected() { | |
| return new PlatformDetection().arch.equals(PlatformDetection.ARCH_X86_64); | |
| } | |
| }, | |
| PPC { | |
| @Override | |
| public boolean detected() { | |
| return new PlatformDetection().arch.equals(PlatformDetection.ARCH_PPC); | |
| } | |
| }; | |
| private static class PlatformDetection { | |
| private String arch; | |
| public static String ARCH_PPC = "ppc"; | |
| public static String ARCH_X86_32 = "x86_32"; | |
| public static String ARCH_X86_64 = "x86_64"; | |
| public PlatformDetection() { | |
| Map<String, String> archMap = new HashMap<String, String>(); | |
| archMap.put("x86", ARCH_X86_32); | |
| archMap.put("i386", ARCH_X86_32); | |
| archMap.put("i486", ARCH_X86_32); | |
| archMap.put("i586", ARCH_X86_32); | |
| archMap.put("i686", ARCH_X86_32); | |
| archMap.put("x86_64", ARCH_X86_64); | |
| archMap.put("amd64", ARCH_X86_64); | |
| archMap.put("powerpc", ARCH_PPC); | |
| this.arch = archMap.get(SystemUtils.OS_ARCH); | |
| } | |
| } | |
| } | |
| @Retention(RetentionPolicy.RUNTIME) | |
| @Target({ElementType.ANNOTATION_TYPE}) | |
| @Documented | |
| public @interface Governor { | |
| } | |
| @Retention(RetentionPolicy.RUNTIME) | |
| @Target({ElementType.PARAMETER}) | |
| @Documented | |
| public @interface Detected { | |
| OperatingSystem os(); | |
| JavaVersion java(); | |
| JavaVendor javaVendor(); | |
| Architecture arch(); | |
| Class<? extends Detectable>[] customDetections() default Detectable.TrueDetection.class; | |
| Class<? extends DetectionResolvingStrategy> detectionStrategy() default ConjunctionDetectionStrategy.class; | |
| } | |
| @Governor | |
| @Retention(RetentionPolicy.RUNTIME) | |
| @Target({ElementType.METHOD, ElementType.TYPE}) | |
| @Documented | |
| public @interface Jira { | |
| String value() default ""; | |
| boolean force() default false; | |
| Detected detected(); | |
| } | |
| public static final class MyCustomDetectionWith24Databases implements Detectable { | |
| @Override | |
| public boolean detected() { | |
| return true; | |
| } | |
| } | |
| public class MyCustomDetectionStrategy extends BaseDetectionStrategy implements DetectionResolvingStrategy { | |
| private Function<List<Detectable>, Boolean> resolvingFunction = detectables -> myCustomResolving(detectables); | |
| @Override | |
| public boolean resolve() { | |
| return super.resolve(resolvingFunction); | |
| } | |
| private boolean myCustomResolving(List<Detectable> detectables) { | |
| // resolve it here whatever you want | |
| return true; | |
| } | |
| } | |
| @Test | |
| @Jira( | |
| value = "ARQ-123", | |
| detected = @Detected( | |
| os = MAC, | |
| java = VERSION_1_8, | |
| arch = PPC, | |
| javaVendor = IBM, | |
| // custom detections could be set by system property so you would not need to write this all over again | |
| // but it would be instantiated from system property with custom check class name | |
| // string loaded can be delimited by commas so you can instantiate an array of them | |
| customDetections = { | |
| MyCustomDetectionWith24Databases.class | |
| }, | |
| detectionStrategy = MyCustomDetectionStrategy.class | |
| ) | |
| ) | |
| public void testMethod() { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment