Skip to content

Instantly share code, notes, and snippets.

@smiklosovic
Last active June 7, 2016 12:55
Show Gist options
  • Select an option

  • Save smiklosovic/e8a9586cccdf5187feb0696a97c3f68e to your computer and use it in GitHub Desktop.

Select an option

Save smiklosovic/e8a9586cccdf5187feb0696a97c3f68e to your computer and use it in GitHub Desktop.
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 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 AndStrategy 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 OrStrategy 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 static final class OperatingSystem {
public static final class Linux implements Detectable {
@Override
public boolean detected() {
return SystemUtils.IS_OS_LINUX;
}
}
public static final class Windows implements Detectable {
@Override
public boolean detected() {
return SystemUtils.IS_OS_WINDOWS;
}
}
public static final class Mac implements Detectable {
@Override
public boolean detected() {
return SystemUtils.IS_OS_MAC_OSX;
}
}
}
public static final class JavaVendor {
public static final class Oracle implements Detectable {
@Override
public boolean detected() {
return getVendor().contains(getClass().getSimpleName().toLowerCase());
}
}
public static final class OpenJDK implements Detectable {
@Override
public boolean detected() {
return getVendor().contains(getClass().getSimpleName().toLowerCase());
}
}
public static final class IBM implements Detectable {
@Override
public boolean detected() {
return getVendor().contains(getClass().getSimpleName().toLowerCase());
}
}
public static final String getVendor() {
return System.getProperty("java.vendor");
}
}
public static final class JavaVersion {
public static final class VERSION_1_5 implements Detectable {
@Override
public boolean detected() {
return SystemUtils.IS_JAVA_1_5;
}
}
public static final class VERSION_1_6 implements Detectable {
@Override
public boolean detected() {
return SystemUtils.IS_JAVA_1_6;
}
}
public static final class VERSION_1_7 implements Detectable {
@Override
public boolean detected() {
return SystemUtils.IS_JAVA_1_7;
}
}
public static final class VERSION_1_8 implements Detectable {
@Override
public boolean detected() {
return SystemUtils.IS_JAVA_1_8;
}
}
}
public static final class Architecture {
public static final class X86_32 implements Detectable {
@Override
public boolean detected() {
return new PlatformDetection().arch.equals(PlatformDetection.ARCH_X86_32);
}
}
public static final class X86_64 implements Detectable {
@Override
public boolean detected() {
return new PlatformDetection().arch.equals(PlatformDetection.ARCH_X86_64);
}
}
public static final class PPC implements Detectable {
@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 {
}
@Governor
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
public @interface Jira {
String value() default "";
boolean force() default false;
Detector detector();
}
@Governor
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
@Documented
public @interface Detector {
Class<? extends Detectable>[] value() default Detectable.TrueDetection.class;
Class<? extends DetectionResolvingStrategy> detectionStrategy() default AndStrategy.class;
}
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;
}
}
// when you are using default detection strategy, you can omit 'value'
@Test
@Jira(value = "ARQ-123", detector = @Detector({Mac.class, VERSION_1_8.class, IBM.class, PPC.class}))
public void testMethod() {
}
@Test
@Jira(value = "ARQ-123", detector = @Detector(value = {Mac.class, VERSION_1_8.class, IBM.class, PPC.class}, detectionStrategy = AndStrategy.class))
public void testMethod() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment