Navigation Menu

Skip to content

Commit

Permalink
Add factories annotations instead of getPriority() and getName()
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Sep 16, 2016
1 parent 67acde4 commit 2becc87
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 157 deletions.
6 changes: 1 addition & 5 deletions README.md
Expand Up @@ -1211,6 +1211,7 @@ A ```WebDriverFactory``` implementation is responsible for creating new instance
For instance, to run your tests on [BrowserStack](https://browserstack.com) For instance, to run your tests on [BrowserStack](https://browserstack.com)


```java ```java
@FactoryName("browserstack")
public class BrowserStackWebDriverFactory implements WebDriverFactory { public class BrowserStackWebDriverFactory implements WebDriverFactory {
@Override @Override
public WebDriver newWebDriver() { public WebDriver newWebDriver() {
Expand All @@ -1233,11 +1234,6 @@ public class BrowserStackWebDriverFactory implements WebDriverFactory {


return new RemoteWebDriver(hubURL, caps); return new RemoteWebDriver(hubURL, caps);
} }

@Override
public String[] getNames() {
return new String[0];
}
} }
``` ```


Expand Down
Expand Up @@ -4,6 +4,7 @@


import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
Expand Down Expand Up @@ -45,7 +46,13 @@ public synchronized T getDefault() {
Collections.sort(factories, new Comparator<T>() { Collections.sort(factories, new Comparator<T>() {
@Override @Override
public int compare(T o1, T o2) { public int compare(T o1, T o2) {
return -Integer.compare(o1.getPriority(), o2.getPriority()); FactoryPriority annotation1 = o1.getClass().getAnnotation(FactoryPriority.class);
int p1 = annotation1 == null ? 0 : annotation1.value();

FactoryPriority annotation2 = o2.getClass().getAnnotation(FactoryPriority.class);
int p2 = annotation2 == null ? 0 : annotation2.value();

return -Integer.compare(p1, p2);
} }
}); });
List<T> filteredFactories = new ArrayList<>(); List<T> filteredFactories = new ArrayList<>();
Expand Down Expand Up @@ -97,23 +104,42 @@ public synchronized T get(String name) {
/** /**
* Register a new factory. * Register a new factory.
* <p> * <p>
* It will also register the factory under names returned by {@link AlternativeNames#getAlternativeNames()} if * It will use {@link FactoryName} value as the default name.
* it implements {@link AlternativeNames}. * <p>
* It will also register the factory under names returned by {@link FactoryNames#getNames()}} if
* it implements {@link FactoryNames}.
* *
* @param factory factory to register * @param factory factory to register
*/ */
public synchronized void register(T factory) { public synchronized void register(T factory) {
if (factories.containsKey(factory.getName())) { FactoryName annotation = factory.getClass().getAnnotation(FactoryName.class);
throw new ConfigurationException("A factory is already registered with this name: " + String annotationName = annotation == null ? null : annotation.value();
factory.getName() + " (" + factories.get(factory.getName()) + ")");
List<String> names = new ArrayList<>();
if (annotationName != null) {
names.add(annotationName);
}
if (factory instanceof FactoryNames) {
names.addAll(Arrays.asList(((FactoryNames) factory).getNames()));
} }
factories.put(factory.getName(), factory);
if (factory instanceof AlternativeNames) {
for (String alternativeName : ((AlternativeNames) factory).getAlternativeNames()) {
if (!factories.containsKey(alternativeName)) {
factories.put(alternativeName, factory);
}


boolean registered = false;

if (names.size() == 0) {
throw new ConfigurationException("Factory " + factory.getClass().getName() + " has no name defined. Use @FactoryName annotation or implement FactoryNames.");
}

for (String name : names) {
if (!registered) {
if (factories.containsKey(name)) {
throw new ConfigurationException("A factory is already registered with this name: " +
name + " (" + factories.get(name) + ")");
}
factories.put(name, factory);
registered = true;
}
if (!factories.containsKey(name)) {
factories.put(name, factory);
} }
} }


Expand Down

This file was deleted.

Expand Up @@ -12,81 +12,53 @@
import java.net.URL; import java.net.URL;


public class DefaultWebDriverFactories { public class DefaultWebDriverFactories {
@FactoryPriority(128)
public static class FirefoxWebDriverFactory extends ReflectiveWebDriverFactory { public static class FirefoxWebDriverFactory extends ReflectiveWebDriverFactory {
public FirefoxWebDriverFactory() { public FirefoxWebDriverFactory() {
super("firefox", "org.openqa.selenium.firefox.FirefoxDriver"); super("firefox", "org.openqa.selenium.firefox.FirefoxDriver");
} }

@Override
public int getPriority() {
return 128;
}
} }


@FactoryPriority(127)
public static class MarionetteWebDriverFactory extends ReflectiveWebDriverFactory { public static class MarionetteWebDriverFactory extends ReflectiveWebDriverFactory {
public MarionetteWebDriverFactory() { public MarionetteWebDriverFactory() {
super("marionette", "org.openqa.selenium.firefox.MarionetteDriver"); super("marionette", "org.openqa.selenium.firefox.MarionetteDriver");
} }

@Override
public int getPriority() {
return 127;
}
} }


@FactoryPriority(64)
public static class ChromeWebDriverFactory extends ReflectiveWebDriverFactory { public static class ChromeWebDriverFactory extends ReflectiveWebDriverFactory {
public ChromeWebDriverFactory() { public ChromeWebDriverFactory() {
super("chrome", "org.openqa.selenium.chrome.ChromeDriver"); super("chrome", "org.openqa.selenium.chrome.ChromeDriver");
} }

@Override
public int getPriority() {
return 64;
}
} }


@FactoryPriority(32)
public static class InternetExplorerWebDriverFactory extends ReflectiveWebDriverFactory { public static class InternetExplorerWebDriverFactory extends ReflectiveWebDriverFactory {
public InternetExplorerWebDriverFactory() { public InternetExplorerWebDriverFactory() {
super("ie", "org.openqa.selenium.ie.InternetExplorerDriver"); super("ie", "org.openqa.selenium.ie.InternetExplorerDriver");
} }

@Override
public int getPriority() {
return 32;
}
} }


@FactoryPriority(31)
public static class EdgeWebDriverFactory extends ReflectiveWebDriverFactory { public static class EdgeWebDriverFactory extends ReflectiveWebDriverFactory {
public EdgeWebDriverFactory() { public EdgeWebDriverFactory() {
super("edge", "org.openqa.selenium.edge.EdgeDriver"); super("edge", "org.openqa.selenium.edge.EdgeDriver");
} }

@Override
public int getPriority() {
return 31;
}
} }


@FactoryPriority(16)
public static class SafaryWebDriverFactory extends ReflectiveWebDriverFactory { public static class SafaryWebDriverFactory extends ReflectiveWebDriverFactory {
public SafaryWebDriverFactory() { public SafaryWebDriverFactory() {
super("safari", "org.openqa.selenium.safari.SafariDriver"); super("safari", "org.openqa.selenium.safari.SafariDriver");
} }

@Override
public int getPriority() {
return 16;
}
} }


@FactoryPriority(8)
public static class PhantomJSWebDriverFactory extends ReflectiveWebDriverFactory { public static class PhantomJSWebDriverFactory extends ReflectiveWebDriverFactory {
public PhantomJSWebDriverFactory() { public PhantomJSWebDriverFactory() {
super("phantomjs", "org.openqa.selenium.phantomjs.PhantomJSDriver"); super("phantomjs", "org.openqa.selenium.phantomjs.PhantomJSDriver");
} }

@Override
public int getPriority() {
return 8;
}
} }


public static class RemoteWebDriverFactory extends ReflectiveWebDriverFactory { public static class RemoteWebDriverFactory extends ReflectiveWebDriverFactory {
Expand Down Expand Up @@ -121,11 +93,6 @@ protected WebDriver newRemoteWebDriver(Object[] args) throws NoSuchMethodExcepti
WebDriver webDriver = ReflectionUtils.getConstructor(webDriverClass, URL.class, Capabilities.class).newInstance(args); WebDriver webDriver = ReflectionUtils.getConstructor(webDriverClass, URL.class, Capabilities.class).newInstance(args);
return new Augmenter().augment(webDriver); return new Augmenter().augment(webDriver);
} }

@Override
public int getPriority() {
return 0;
}
} }


public static class HtmlUnitWebDriverFactory extends ReflectiveWebDriverFactory { public static class HtmlUnitWebDriverFactory extends ReflectiveWebDriverFactory {
Expand All @@ -139,11 +106,6 @@ protected DesiredCapabilities newDefaultCapabilities() {
desiredCapabilities.setJavascriptEnabled(true); desiredCapabilities.setJavascriptEnabled(true);
return desiredCapabilities; return desiredCapabilities;
} }

@Override
public int getPriority() {
return -128;
}
} }


} }
@@ -1,19 +1,11 @@
package org.fluentlenium.configuration; package org.fluentlenium.configuration;


/**
* Marker interface for factories.
*
* @see FactoryPriority
* @see FactoryName
*/
public interface Factory { public interface Factory {
/**
* Primary name of this factory.
* <p>
* To register it with alternative name, use {@link AlternativeNames}.
*
* @return Primary name
*/
String getName();


/**
* Priority of the factory to be grabbed as default Factory.
*
* @return a priority index
*/
int getPriority();
} }
@@ -0,0 +1,17 @@
package org.fluentlenium.configuration;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Define names for a factory
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface FactoryName {
String value();
}
@@ -0,0 +1,15 @@
package org.fluentlenium.configuration;

/**
* Add names to a factory.
* <p>
* {@link Factory} implementations can implement this interface to be registered in registry with those names.
*/
public interface FactoryNames {
/**
* Get the names.
*
* @return array of names
*/
String[] getNames();
}
@@ -0,0 +1,18 @@
package org.fluentlenium.configuration;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/**
* Defines the priority of the factory.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface FactoryPriority {
int value();
}
Expand Up @@ -6,7 +6,7 @@
import java.lang.reflect.Method; import java.lang.reflect.Method;


@IndexIgnore @IndexIgnore
public class MethodInvocationReflectionFactory implements CapabilitiesFactory, AlternativeNames { public class MethodInvocationReflectionFactory implements CapabilitiesFactory, FactoryNames {
private final Method method; private final Method method;
private final Object instance; private final Object instance;
private final Object[] args; private final Object[] args;
Expand All @@ -17,16 +17,6 @@ public MethodInvocationReflectionFactory(Method method, Object instance, Object.
this.args = args; this.args = args;
} }


@Override
public String getName() {
return this.method.getDeclaringClass().getName() + "." + this.method.getName();
}

@Override
public int getPriority() {
return 0;
}

@Override @Override
public Capabilities newCapabilities() { public Capabilities newCapabilities() {
try { try {
Expand All @@ -39,8 +29,9 @@ public Capabilities newCapabilities() {
} }


@Override @Override
public String[] getAlternativeNames() { public String[] getNames() {
return new String[]{ return new String[]{
this.method.getDeclaringClass().getName() + "." + this.method.getName(),
this.method.getDeclaringClass().getSimpleName() + "." + this.method.getName(), this.method.getDeclaringClass().getSimpleName() + "." + this.method.getName(),
this.method.getName() this.method.getName()
}; };
Expand Down
Expand Up @@ -6,11 +6,14 @@
import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.DesiredCapabilities;


import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/** /**
* A simple {@link CapabilitiesFactory} that create {@link Capabilities} instances using reflection. * A simple {@link CapabilitiesFactory} that create {@link Capabilities} instances using reflection.
*/ */
public class ReflectiveCapabilitiesFactory implements CapabilitiesFactory, AlternativeNames, ReflectiveFactory { public class ReflectiveCapabilitiesFactory implements CapabilitiesFactory, FactoryNames, ReflectiveFactory {
private String name; private String name;
private Object[] args; private Object[] args;
private String capabilitiesClassName; private String capabilitiesClassName;
Expand Down Expand Up @@ -62,21 +65,12 @@ public Capabilities newCapabilities() {
} }
} }


@Override public String[] getNames() {
public String getName() { List<String> names = new ArrayList<>(Arrays.asList(name));
return name;
}

@Override
public String[] getAlternativeNames() {
if (capabilitiesClass != null) { if (capabilitiesClass != null) {
return new String[]{capabilitiesClass.getName(), capabilitiesClass.getSimpleName()}; names.add(capabilitiesClass.getName());
names.add(capabilitiesClass.getSimpleName());
} }
return new String[0]; return names.toArray(new String[names.size()]);
}

@Override
public int getPriority() {
return 0;
} }
} }

0 comments on commit 2becc87

Please sign in to comment.