PluginManagerTest: Ensure config keyNames are not duplicated
This adds a new test to ensure plugin configurations do not duplicate the keyName annotation argument to prevent runtime errors.
This commit is contained in:
@@ -38,6 +38,7 @@ import java.applet.Applet;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@@ -48,6 +49,8 @@ import net.runelite.api.Client;
|
|||||||
import net.runelite.client.RuneLite;
|
import net.runelite.client.RuneLite;
|
||||||
import net.runelite.client.RuneLiteModule;
|
import net.runelite.client.RuneLiteModule;
|
||||||
import net.runelite.client.eventbus.EventBus;
|
import net.runelite.client.eventbus.EventBus;
|
||||||
|
import net.runelite.client.config.Config;
|
||||||
|
import net.runelite.client.config.ConfigItem;
|
||||||
import net.runelite.client.rs.ClientUpdateCheckMode;
|
import net.runelite.client.rs.ClientUpdateCheckMode;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -75,6 +78,7 @@ public class PluginManagerTest
|
|||||||
public Client client;
|
public Client client;
|
||||||
|
|
||||||
private Set<Class> pluginClasses;
|
private Set<Class> pluginClasses;
|
||||||
|
private Set<Class> configClasses;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() throws IOException
|
public void before() throws IOException
|
||||||
@@ -85,8 +89,9 @@ public class PluginManagerTest
|
|||||||
|
|
||||||
RuneLite.setInjector(injector);
|
RuneLite.setInjector(injector);
|
||||||
|
|
||||||
// Find plugins we expect to have
|
// Find plugins and configs we expect to have
|
||||||
pluginClasses = new HashSet<>();
|
pluginClasses = new HashSet<>();
|
||||||
|
configClasses = new HashSet<>();
|
||||||
Set<ClassInfo> classes = ClassPath.from(getClass().getClassLoader()).getTopLevelClassesRecursive(PLUGIN_PACKAGE);
|
Set<ClassInfo> classes = ClassPath.from(getClass().getClassLoader()).getTopLevelClassesRecursive(PLUGIN_PACKAGE);
|
||||||
for (ClassInfo classInfo : classes)
|
for (ClassInfo classInfo : classes)
|
||||||
{
|
{
|
||||||
@@ -95,6 +100,12 @@ public class PluginManagerTest
|
|||||||
if (pluginDescriptor != null)
|
if (pluginDescriptor != null)
|
||||||
{
|
{
|
||||||
pluginClasses.add(clazz);
|
pluginClasses.add(clazz);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Config.class.isAssignableFrom(clazz))
|
||||||
|
{
|
||||||
|
configClasses.add(clazz);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,4 +166,37 @@ public class PluginManagerTest
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ensureNoDuplicateConfigKeyNames()
|
||||||
|
{
|
||||||
|
for (final Class clazz : configClasses)
|
||||||
|
{
|
||||||
|
final Set<String> configKeyNames = new HashSet<>();
|
||||||
|
|
||||||
|
for (final Method method : clazz.getMethods())
|
||||||
|
{
|
||||||
|
if (!method.isDefault())
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
final ConfigItem annotation = method.getAnnotation(ConfigItem.class);
|
||||||
|
|
||||||
|
if (annotation == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String configKeyName = annotation.keyName();
|
||||||
|
|
||||||
|
if (configKeyNames.contains(configKeyName))
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException("keyName " + configKeyName + " is duplicated in " + clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
configKeyNames.add(configKeyName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user