Refactor message handlers into injectable components (preparation for #1467)
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package fr.xephi.authme;
|
||||
|
||||
import ch.jalu.injector.handlers.postconstruct.PostConstructMethodInvoker;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -102,4 +105,32 @@ public final class ReflectionTestUtils {
|
||||
throw new UnsupportedOperationException("Could not invoke method '" + method + "'", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs all methods annotated with {@link javax.annotation.PostConstruct} on the given instance
|
||||
* (including such methods on superclasses).
|
||||
*
|
||||
* @param instance the instance to process
|
||||
*/
|
||||
public static void invokePostConstructMethods(Object instance) {
|
||||
// Use the implementation of the injector to invoke all @PostConstruct methods the same way
|
||||
new PostConstructMethodInvoker().postProcess(instance, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of the given class, using a no-args constructor (which may be hidden).
|
||||
*
|
||||
* @param clazz the class to instantiate
|
||||
* @param <T> the class' type
|
||||
* @return the created instance
|
||||
*/
|
||||
public static <T> T newInstance(Class<T> clazz) {
|
||||
try {
|
||||
Constructor<T> constructor = clazz.getDeclaredConstructor();
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance();
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new UnsupportedOperationException("Could not invoke no-args constructor of class " + clazz, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +1,55 @@
|
||||
package fr.xephi.authme.command.help;
|
||||
|
||||
import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import com.google.common.io.Files;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.command.CommandDescription;
|
||||
import fr.xephi.authme.command.TestCommandsUtil;
|
||||
import fr.xephi.authme.message.MessageFileHandler;
|
||||
import fr.xephi.authme.message.MessageFileHandlerProvider;
|
||||
import fr.xephi.authme.message.AbstractMessageFileHandler;
|
||||
import fr.xephi.authme.message.HelpMessagesFileHandler;
|
||||
import fr.xephi.authme.permission.DefaultPermission;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static fr.xephi.authme.TestHelper.getJarFile;
|
||||
import static fr.xephi.authme.command.TestCommandsUtil.getCommandWithLabel;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link HelpMessagesService}.
|
||||
*/
|
||||
@RunWith(DelayedInjectionRunner.class)
|
||||
public class HelpMessagesServiceTest {
|
||||
|
||||
private static final String TEST_FILE = "/fr/xephi/authme/command/help/help_test.yml";
|
||||
private static final Collection<CommandDescription> COMMANDS = TestCommandsUtil.generateCommands();
|
||||
|
||||
@InjectDelayed
|
||||
private HelpMessagesService helpMessagesService;
|
||||
|
||||
@Mock
|
||||
private MessageFileHandlerProvider messageFileHandlerProvider;
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
private File dataFolder;
|
||||
|
||||
@BeforeInjecting
|
||||
@SuppressWarnings("unchecked")
|
||||
public void initializeHandler() {
|
||||
MessageFileHandler handler = new MessageFileHandler(getJarFile(TEST_FILE), "messages/messages_en.yml", null);
|
||||
given(messageFileHandlerProvider.initializeHandler(any(Function.class))).willReturn(handler);
|
||||
}
|
||||
@Before
|
||||
public void initializeHandler() throws IOException, InstantiationException, IllegalAccessException {
|
||||
dataFolder = temporaryFolder.newFolder();
|
||||
new File(dataFolder, "messages").mkdirs();
|
||||
File messagesFile = new File(dataFolder, "messages/help_test.yml");
|
||||
Files.copy(TestHelper.getJarFile(TEST_FILE), messagesFile);
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void shouldUseExistingFileAsTextFile() {
|
||||
// given / when / then
|
||||
ArgumentCaptor<Function<String, String>> functionCaptor = ArgumentCaptor.forClass(Function.class);
|
||||
verify(messageFileHandlerProvider).initializeHandler(functionCaptor.capture());
|
||||
Function<String, String> helpFilePathBuilder = functionCaptor.getValue();
|
||||
String defaultFilePath = helpFilePathBuilder.apply("en");
|
||||
assertThat(getClass().getClassLoader().getResource(defaultFilePath), not(nullValue()));
|
||||
HelpMessagesFileHandler helpMessagesFileHandler = createMessagesFileHandler();
|
||||
helpMessagesService = new HelpMessagesService(helpMessagesFileHandler);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -154,4 +145,15 @@ public class HelpMessagesServiceTest {
|
||||
// then
|
||||
assertThat(description, equalTo(command.getDescription()));
|
||||
}
|
||||
|
||||
private HelpMessagesFileHandler createMessagesFileHandler() throws IllegalAccessException, InstantiationException {
|
||||
Settings settings = mock(Settings.class);
|
||||
given(settings.getProperty(PluginSettings.MESSAGES_LANGUAGE)).willReturn("test");
|
||||
|
||||
HelpMessagesFileHandler messagesFileHandler = ReflectionTestUtils.newInstance(HelpMessagesFileHandler.class);
|
||||
ReflectionTestUtils.setField(AbstractMessageFileHandler.class, messagesFileHandler, "settings", settings);
|
||||
ReflectionTestUtils.setField(AbstractMessageFileHandler.class, messagesFileHandler, "dataFolder", dataFolder);
|
||||
ReflectionTestUtils.invokePostConstructMethods(messagesFileHandler);
|
||||
return messagesFileHandler;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,175 +0,0 @@
|
||||
package fr.xephi.authme.message;
|
||||
|
||||
import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import com.google.common.io.Files;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.TypeSafeMatcher;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static fr.xephi.authme.TestHelper.getJarFile;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test for {@link MessageFileHandlerProvider}.
|
||||
*/
|
||||
@RunWith(DelayedInjectionRunner.class)
|
||||
public class MessageFileHandlerProviderTest {
|
||||
|
||||
private static final Function<String, String> MESSAGES_BUILDER = lang -> "messages/messages_" + lang + ".yml";
|
||||
|
||||
@InjectDelayed
|
||||
private MessageFileHandlerProvider handlerProvider;
|
||||
|
||||
@DataFolder
|
||||
private File dataFolder;
|
||||
@Mock
|
||||
private Settings settings;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@BeforeClass
|
||||
public static void initLogger() {
|
||||
TestHelper.setupLogger();
|
||||
}
|
||||
|
||||
@BeforeInjecting
|
||||
public void initDataFolder() throws IOException {
|
||||
this.dataFolder = temporaryFolder.newFolder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnExistingMessagesFile() {
|
||||
// given
|
||||
String language = "fr";
|
||||
// use another language file to make sure we won't copy over it
|
||||
String jarFile = "/messages/messages_it.yml";
|
||||
File existingFile = copyFromJar(MESSAGES_BUILDER.apply(language), jarFile);
|
||||
|
||||
// when
|
||||
File result = handlerProvider.initializeFile(language, MESSAGES_BUILDER);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(existingFile));
|
||||
assertThat(result.exists(), equalTo(true));
|
||||
assertThat(result, equalToFile(getJarFile(jarFile)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCopyFromJarFile() {
|
||||
// given
|
||||
String language = "nl";
|
||||
|
||||
// when
|
||||
File result = handlerProvider.initializeFile(language, MESSAGES_BUILDER);
|
||||
|
||||
// then
|
||||
File expectedFile = new File(dataFolder, MESSAGES_BUILDER.apply(language));
|
||||
assertThat(result, equalTo(expectedFile));
|
||||
assertThat(result.exists(), equalTo(true));
|
||||
assertThat(result, equalToFile(getJarFile("/messages/messages_nl.yml")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCopyDefaultFileForUnknownLanguage() {
|
||||
// given
|
||||
String language = "zxx";
|
||||
|
||||
// when
|
||||
File result = handlerProvider.initializeFile(language, MESSAGES_BUILDER);
|
||||
|
||||
// then
|
||||
File expectedFile = new File(dataFolder, MESSAGES_BUILDER.apply(language));
|
||||
assertThat(result, equalTo(expectedFile));
|
||||
assertThat(result.exists(), equalTo(true));
|
||||
assertThat(result, equalToFile(getJarFile("/messages/messages_en.yml")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForNonExistentDefault() {
|
||||
// given
|
||||
Function<String, String> fileFunction = s -> "bogus";
|
||||
|
||||
// when
|
||||
File result = handlerProvider.initializeFile("gsw", fileFunction);
|
||||
|
||||
// then
|
||||
assertThat(result, nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateHandler() {
|
||||
// given
|
||||
String language = "fr";
|
||||
given(settings.getProperty(PluginSettings.MESSAGES_LANGUAGE)).willReturn(language);
|
||||
|
||||
MessageFileHandlerProvider provider = Mockito.spy(handlerProvider);
|
||||
Function<String, String> fileFunction = lang -> "file_" + lang + ".txt";
|
||||
File file = new File(dataFolder, "some_file.txt");
|
||||
doReturn(file).when(provider).initializeFile(language, fileFunction);
|
||||
|
||||
// when
|
||||
MessageFileHandler handler = provider.initializeHandler(fileFunction);
|
||||
|
||||
// then
|
||||
assertThat(handler, not(nullValue()));
|
||||
verify(settings).getProperty(PluginSettings.MESSAGES_LANGUAGE);
|
||||
verify(provider).initializeFile(language, fileFunction);
|
||||
}
|
||||
|
||||
private File copyFromJar(String path, String jarPath) {
|
||||
File file = new File(dataFolder, path);
|
||||
if (!file.getParentFile().mkdirs()) {
|
||||
throw new IllegalStateException("Could not create folders for '" + file + "'");
|
||||
}
|
||||
try {
|
||||
Files.copy(getJarFile(jarPath), file);
|
||||
return file;
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Matcher<File> equalToFile(File file) {
|
||||
return new TypeSafeMatcher<File>() {
|
||||
@Override
|
||||
protected boolean matchesSafely(File item) {
|
||||
try {
|
||||
return Files.equal(item, file);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("IOException during matcher evaluation", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("Equal to file '" + file + "'");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,21 +1,28 @@
|
||||
package fr.xephi.authme.message;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.io.Files;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import fr.xephi.authme.util.FileUtils;
|
||||
import fr.xephi.authme.util.expiring.Duration;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.hamcrest.Matchers.arrayWithSize;
|
||||
@@ -23,7 +30,6 @@ import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -38,9 +44,12 @@ import static org.mockito.hamcrest.MockitoHamcrest.argThat;
|
||||
public class MessagesIntegrationTest {
|
||||
|
||||
private static final String YML_TEST_FILE = TestHelper.PROJECT_ROOT + "message/messages_test.yml";
|
||||
private static final String YML_DEFAULT_TEST_FILE = "messages/messages_en.yml";
|
||||
private Messages messages;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
private File dataFolder;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
TestHelper.setupLogger();
|
||||
@@ -55,10 +64,15 @@ public class MessagesIntegrationTest {
|
||||
* file that should contain all messages, but again, for testing, it just contains a few.
|
||||
*/
|
||||
@Before
|
||||
public void setUpMessages() {
|
||||
File testFile = TestHelper.getJarFile(YML_TEST_FILE);
|
||||
MessageFileHandlerProvider provider = providerReturning(testFile, YML_DEFAULT_TEST_FILE);
|
||||
messages = new Messages(provider);
|
||||
public void setUpMessages() throws IOException {
|
||||
dataFolder = temporaryFolder.newFolder();
|
||||
File testFile = new File(dataFolder, "messages/messages_test.yml");
|
||||
new File(dataFolder, "messages").mkdirs();
|
||||
FileUtils.create(testFile);
|
||||
Files.copy(TestHelper.getJarFile(YML_TEST_FILE), testFile);
|
||||
|
||||
MessagesFileHandler fileHandler = createMessagesFileHandler();
|
||||
messages = new Messages(fileHandler);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -254,11 +268,14 @@ public class MessagesIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static MessageFileHandlerProvider providerReturning(File file, String defaultFile) {
|
||||
MessageFileHandlerProvider handler = mock(MessageFileHandlerProvider.class);
|
||||
given(handler.initializeHandler(any(Function.class), anyString()))
|
||||
.willReturn(new MessageFileHandler(file, defaultFile, "/authme messages"));
|
||||
return handler;
|
||||
private MessagesFileHandler createMessagesFileHandler() {
|
||||
Settings settings = mock(Settings.class);
|
||||
given(settings.getProperty(PluginSettings.MESSAGES_LANGUAGE)).willReturn("test");
|
||||
|
||||
MessagesFileHandler messagesFileHandler = new MessagesFileHandler();
|
||||
ReflectionTestUtils.setField(AbstractMessageFileHandler.class, messagesFileHandler, "settings", settings);
|
||||
ReflectionTestUtils.setField(AbstractMessageFileHandler.class, messagesFileHandler, "dataFolder", dataFolder);
|
||||
ReflectionTestUtils.invokePostConstructMethods(messagesFileHandler);
|
||||
return messagesFileHandler;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import fr.xephi.authme.command.help.HelpMessage;
|
||||
import fr.xephi.authme.command.help.HelpMessagesService;
|
||||
import fr.xephi.authme.command.help.HelpSection;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.message.MessageFileHandlerProvider;
|
||||
import fr.xephi.authme.message.HelpMessagesFileHandler;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import org.bukkit.configuration.MemorySection;
|
||||
@@ -44,7 +44,7 @@ public class HelpTranslationGeneratorIntegrationTest {
|
||||
@InjectDelayed
|
||||
private HelpMessagesService helpMessagesService;
|
||||
@InjectDelayed
|
||||
private MessageFileHandlerProvider messageFileHandlerProvider;
|
||||
private HelpMessagesFileHandler helpMessagesFileHandler;
|
||||
@InjectDelayed
|
||||
private CommandInitializer commandInitializer;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user