Skip to content

Commit

Permalink
Cukes Http Plugins were not registered properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Gursky authored and Igor Gursky committed Feb 11, 2019
1 parent b483e1a commit a8dff14
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

import com.google.inject.AbstractModule;
import com.google.inject.multibindings.Multibinder;
import lv.ctco.cukes.core.CukesOptions;
import lv.ctco.cukes.core.CukesRuntimeException;
import org.apache.commons.lang3.ArrayUtils;

import java.net.URL;
import java.util.Properties;

import static lv.ctco.cukes.core.internal.helpers.Files.createCukesPropertyFileUrl;

public abstract class AbstractCukesModule extends AbstractModule {

Expand All @@ -10,4 +18,31 @@ protected <T extends CukesPlugin> void registerPlugin(Class<T> pluginClass) {
baseMultibinder.addBinding().to(pluginClass);
}

@SuppressWarnings("unchecked")
protected void bindPlugins(Class clazz) {
try {
Multibinder multibinder = Multibinder.newSetBinder(binder(), clazz);

// add user configured plugins
ClassLoader classLoader = AbstractCukesModule.class.getClassLoader();

Properties properties = new Properties();
URL url = createCukesPropertyFileUrl(classLoader);
if (url == null) return;
properties.load(url.openStream());

String plugins = properties.getProperty(CukesOptions.PROPERTIES_PREFIX + CukesOptions.PLUGINS);
if (plugins == null) return;

String[] pluginClasses = plugins.split(CukesOptions.DELIMITER);
for (String pluginClass : pluginClasses) {
Class aClass = classLoader.loadClass(pluginClass);
if (ArrayUtils.contains(aClass.getInterfaces(), clazz)) {
multibinder.addBinding().to(aClass);
}
}
} catch (Exception e) {
throw new CukesRuntimeException("Binding of Cukes plugins failed");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package lv.ctco.cukes.core.internal.di;

import com.google.inject.matcher.Matchers;
import com.google.inject.multibindings.Multibinder;
import lv.ctco.cukes.core.CukesOptions;
import lv.ctco.cukes.core.CukesRuntimeException;
import lv.ctco.cukes.core.extension.AbstractCukesModule;
import lv.ctco.cukes.core.extension.CukesPlugin;
import lv.ctco.cukes.core.internal.context.CaptureContext;
Expand All @@ -15,10 +12,6 @@
import org.aopalliance.intercept.MethodInterceptor;

import java.lang.annotation.Annotation;
import java.net.URL;
import java.util.Properties;

import static lv.ctco.cukes.core.internal.helpers.Files.createCukesPropertyFileUrl;

public class CukesGuiceModule extends AbstractCukesModule {

Expand All @@ -28,38 +21,12 @@ protected void configure() {
bindInterceptor(new CaptureContextInterceptor(), CaptureContext.class);
bindInterceptor(new SwitchedByInterceptor(), SwitchedBy.class);

bindPlugins();
bindPlugins(CukesPlugin.class);
}

private void bindInterceptor(MethodInterceptor interceptor, Class<? extends Annotation> annotationType) {
requestInjection(interceptor);
bindInterceptor(Matchers.annotatedWith(annotationType), Matchers.any(), interceptor);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(annotationType), interceptor);
}

@SuppressWarnings("unchecked")
private void bindPlugins() {
try {
Multibinder<CukesPlugin> multibinder = Multibinder.newSetBinder(binder(), CukesPlugin.class);

// add user configured plugins
ClassLoader classLoader = CukesGuiceModule.class.getClassLoader();

Properties properties = new Properties();
URL url = createCukesPropertyFileUrl(classLoader);
if (url == null) return;
properties.load(url.openStream());

String plugins = properties.getProperty(CukesOptions.PROPERTIES_PREFIX + CukesOptions.PLUGINS);
if (plugins == null) return;

String[] pluginClasses = plugins.split(CukesOptions.DELIMITER);
for (String pluginClass : pluginClasses) {
Class<? extends CukesPlugin> aClass = (Class<? extends CukesPlugin>) classLoader.loadClass(pluginClass);
multibinder.addBinding().to(aClass);
}
} catch (Exception e) {
throw new CukesRuntimeException("Binding of Cukes plugins failed");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lv.ctco.cukes.core.facade.VariableFacade;
import lv.ctco.cukes.core.facade.VariableFacadeImpl;
import lv.ctco.cukes.http.extension.AbstractCukesHttpModule;
import lv.ctco.cukes.http.extension.CukesHttpPlugin;
import lv.ctco.cukes.http.facade.HttpAssertionFacade;
import lv.ctco.cukes.http.facade.HttpAssertionFacadeImpl;
import lv.ctco.cukes.http.logging.HttpLoggingPlugin;
Expand All @@ -15,6 +16,7 @@

@CukesInjectableModule
public class CukesHttpGuiceModule extends AbstractCukesHttpModule {

@Override
protected void configure() {
boolean isLoadRunnedEnabled = Boolean.parseBoolean(System.getProperty(PROPERTIES_PREFIX + LOADRUNNER_FILTER_BLOCKS_REQUESTS));
Expand All @@ -24,5 +26,7 @@ protected void configure() {
bind(RandomGeneratorFacade.class).to(RandomGeneratorFacadeImpl.class);
}
registerHttpPlugin(HttpLoggingPlugin.class);

bindPlugins(CukesHttpPlugin.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.inject.multibindings.Multibinder;
import lv.ctco.cukes.core.extension.AbstractCukesModule;
import lv.ctco.cukes.core.extension.CukesPlugin;

public abstract class AbstractCukesHttpModule extends AbstractCukesModule {

Expand Down

0 comments on commit a8dff14

Please sign in to comment.