Help message updater: specify the name of the updated file
This commit is contained in:
parent
ecaffbabfc
commit
cff456c285
@ -7,6 +7,7 @@ import fr.xephi.authme.service.HelpTranslationGenerator;
|
|||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -24,8 +25,8 @@ public class UpdateHelpMessagesCommand implements ExecutableCommand {
|
|||||||
@Override
|
@Override
|
||||||
public void executeCommand(CommandSender sender, List<String> arguments) {
|
public void executeCommand(CommandSender sender, List<String> arguments) {
|
||||||
try {
|
try {
|
||||||
helpTranslationGenerator.updateHelpFile();
|
File updatedFile = helpTranslationGenerator.updateHelpFile();
|
||||||
sender.sendMessage("Successfully updated the help file");
|
sender.sendMessage("Successfully updated the help file '" + updatedFile.getName() + "'");
|
||||||
helpMessagesService.reloadMessagesFile();
|
helpMessagesService.reloadMessagesFile();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
sender.sendMessage("Could not update help file: " + e.getMessage());
|
sender.sendMessage("Could not update help file: " + e.getMessage());
|
||||||
|
|||||||
@ -44,15 +44,17 @@ public class HelpTranslationGenerator {
|
|||||||
/**
|
/**
|
||||||
* Updates the help file to contain entries for all commands.
|
* Updates the help file to contain entries for all commands.
|
||||||
*
|
*
|
||||||
|
* @return the help file that has been updated
|
||||||
* @throws IOException if the help file cannot be written to
|
* @throws IOException if the help file cannot be written to
|
||||||
*/
|
*/
|
||||||
public void updateHelpFile() throws IOException {
|
public File updateHelpFile() throws IOException {
|
||||||
String languageCode = settings.getProperty(PluginSettings.MESSAGES_LANGUAGE);
|
String languageCode = settings.getProperty(PluginSettings.MESSAGES_LANGUAGE);
|
||||||
File helpFile = new File(dataFolder, "messages/help_" + languageCode + ".yml");
|
File helpFile = new File(dataFolder, "messages/help_" + languageCode + ".yml");
|
||||||
Map<String, Object> helpEntries = generateHelpMessageEntries();
|
Map<String, Object> helpEntries = generateHelpMessageEntries();
|
||||||
|
|
||||||
String helpEntriesYaml = exportToYaml(helpEntries);
|
String helpEntriesYaml = exportToYaml(helpEntries);
|
||||||
Files.write(helpFile.toPath(), helpEntriesYaml.getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
|
Files.write(helpFile.toPath(), helpEntriesYaml.getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
|
||||||
|
return helpFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String exportToYaml(Map<String, Object> helpEntries) {
|
private static String exportToYaml(Map<String, Object> helpEntries) {
|
||||||
|
|||||||
@ -0,0 +1,70 @@
|
|||||||
|
package fr.xephi.authme.command.executable.authme;
|
||||||
|
|
||||||
|
import fr.xephi.authme.TestHelper;
|
||||||
|
import fr.xephi.authme.command.help.HelpMessagesService;
|
||||||
|
import fr.xephi.authme.service.HelpTranslationGenerator;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link UpdateHelpMessagesCommand}.
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class UpdateHelpMessagesCommandTest {
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private UpdateHelpMessagesCommand command;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private HelpTranslationGenerator helpTranslationGenerator;
|
||||||
|
@Mock
|
||||||
|
private HelpMessagesService helpMessagesService;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUpLogger() {
|
||||||
|
TestHelper.setupLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldUpdateHelpMessage() throws IOException {
|
||||||
|
// given
|
||||||
|
File updatedFile = new File("some/path/help_xx.yml");
|
||||||
|
given(helpTranslationGenerator.updateHelpFile()).willReturn(updatedFile);
|
||||||
|
CommandSender sender = mock(CommandSender.class);
|
||||||
|
|
||||||
|
// when
|
||||||
|
command.executeCommand(sender, Collections.emptyList());
|
||||||
|
|
||||||
|
// then
|
||||||
|
verify(helpMessagesService).reloadMessagesFile();
|
||||||
|
verify(sender).sendMessage("Successfully updated the help file 'help_xx.yml'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCatchAndReportException() throws IOException {
|
||||||
|
// given
|
||||||
|
given(helpTranslationGenerator.updateHelpFile()).willThrow(new IOException("Couldn't do the thing"));
|
||||||
|
CommandSender sender = mock(CommandSender.class);
|
||||||
|
|
||||||
|
// when
|
||||||
|
command.executeCommand(sender, Collections.emptyList());
|
||||||
|
|
||||||
|
// then
|
||||||
|
verify(sender).sendMessage("Could not update help file: Couldn't do the thing");
|
||||||
|
verifyZeroInteractions(helpMessagesService);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user