add test case and fix logical error

This commit is contained in:
ascrutae 2017-06-26 22:52:27 +08:00
parent 9efff4bc63
commit 6e1d0d602f
2 changed files with 55 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import org.skywalking.apm.agent.core.plugin.exception.IllegalPluginDefineExcepti
import org.skywalking.apm.util.StringUtil;
public class PluginDefine {
public static final String PLUGIN_OFF_PREFIX = "[OFF]";
/**
* Plugin name.
*/
@ -38,17 +39,20 @@ public class PluginDefine {
String pluginName = pluginDefine[0];
String defineClass = pluginDefine[1];
if (pluginName.toUpperCase().startsWith("[OFF]")) {
return new PluginDefine(pluginName.substring(5), defineClass, State.OFF);
if (pluginName.toUpperCase().startsWith(PLUGIN_OFF_PREFIX)) {
return new PluginDefine(pluginName.substring(PLUGIN_OFF_PREFIX.length()), defineClass, State.OFF);
} else {
return new PluginDefine(pluginName, defineClass, State.ON);
}
}
public boolean enable() {
return !Config.Plugin.DISABLED_PLUGINS.contains(name) || forceEnable();
return !forceDisable() || forceEnable();
}
private boolean forceDisable() {
return state != State.ON || Config.Plugin.DISABLED_PLUGINS.contains(name);
}
private boolean forceEnable() {
return state == State.OFF && Config.Plugin.FORCE_ENABLE_PLUGINS.contains(name);

View File

@ -0,0 +1,48 @@
package org.skywalking.apm.agent.core.plugin;
import org.junit.Test;
import org.skywalking.apm.agent.core.conf.Config;
import org.skywalking.apm.agent.core.plugin.exception.IllegalPluginDefineException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class PluginDefineTest {
private static final String TEST_PLUGIN = "test_plugin";
private static final String TEST_DEFINE_CLASS = "test_define_class";
@Test(expected = IllegalPluginDefineException.class)
public void testIllegalPluginDefine() throws IllegalPluginDefineException {
PluginDefine.build("illegal_plugin_define");
}
@Test(expected = IllegalPluginDefineException.class)
public void testEmptyPluginDefine() throws IllegalPluginDefineException {
PluginDefine.build("");
}
@Test
public void testOffStatePlugin() throws IllegalPluginDefineException {
PluginDefine pluginDefine = PluginDefine.build(PluginDefine.PLUGIN_OFF_PREFIX + TEST_PLUGIN + "=" + TEST_DEFINE_CLASS);
assertFalse(pluginDefine.enable());
assertEquals(TEST_DEFINE_CLASS, pluginDefine.getDefineClass());
}
@Test
public void testDefaultStatePlugin() throws IllegalPluginDefineException {
PluginDefine pluginDefine = PluginDefine.build(TEST_PLUGIN + "=" + TEST_DEFINE_CLASS);
assertTrue(pluginDefine.enable());
assertEquals(TEST_DEFINE_CLASS, pluginDefine.getDefineClass());
}
@Test
public void testForceEnablePlugin() throws IllegalPluginDefineException {
Config.Plugin.FORCE_ENABLE_PLUGINS.add(TEST_PLUGIN);
PluginDefine pluginDefine = PluginDefine.build(PluginDefine.PLUGIN_OFF_PREFIX + TEST_PLUGIN + "=" + TEST_DEFINE_CLASS);
assertTrue(pluginDefine.enable());
assertEquals(TEST_DEFINE_CLASS, pluginDefine.getDefineClass());
Config.Plugin.FORCE_ENABLE_PLUGINS.clear();
}
}