Skip to content

Commit

Permalink
Replace "InjectDelayed" extension everywhere with Mockito extension
Browse files Browse the repository at this point in the history
- Remove custom extension and InjectDelayed/BeforeInjecting annotations
- Revert injector version to same stable version as on master
  • Loading branch information
ljacqu committed Dec 20, 2023
1 parent 67ec218 commit 13087f7
Show file tree
Hide file tree
Showing 29 changed files with 299 additions and 305 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@
<dependency>
<groupId>ch.jalu</groupId>
<artifactId>injector</artifactId>
<version>1.1-SNAPSHOT</version>
<version>1.0</version>
<optional>true</optional>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class HelpMessagesService {
private final HelpMessagesFileHandler helpMessagesFileHandler;

@Inject
HelpMessagesService(HelpMessagesFileHandler helpMessagesFileHandler) {
public HelpMessagesService(HelpMessagesFileHandler helpMessagesFileHandler) {
this.helpMessagesFileHandler = helpMessagesFileHandler;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.bukkit.configuration.file.YamlConfiguration;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.File;

import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_LANGUAGE;
Expand All @@ -24,18 +23,16 @@ public abstract class AbstractMessageFileHandler implements Reloadable {

private final ConsoleLogger logger = ConsoleLoggerFactory.get(AbstractMessageFileHandler.class);

@DataFolder
@Inject
private File dataFolder;

@Inject
private Settings settings;
private final File dataFolder;
private final Settings settings;

private String filename;
private FileConfiguration configuration;
private final String defaultFile;

protected AbstractMessageFileHandler() {
protected AbstractMessageFileHandler(@DataFolder File dataFolder, Settings settings) {
this.dataFolder = dataFolder;
this.settings = settings;
this.defaultFile = createFilePath(DEFAULT_LANGUAGE);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package fr.xephi.authme.message;

import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.FileUtils;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import javax.inject.Inject;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

Expand All @@ -21,8 +24,9 @@ public class HelpMessagesFileHandler extends AbstractMessageFileHandler {

private FileConfiguration defaultConfiguration;

@Inject // Trigger injection in the superclass
HelpMessagesFileHandler() {
@Inject
public HelpMessagesFileHandler(@DataFolder File dataFolder, Settings settings) {
super(dataFolder, settings);
}

/**
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/fr/xephi/authme/message/MessagesFileHandler.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package fr.xephi.authme.message;

import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.message.updater.MessageUpdater;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.settings.Settings;

import javax.inject.Inject;
import java.io.File;

import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_LANGUAGE;

Expand All @@ -15,10 +18,12 @@ public class MessagesFileHandler extends AbstractMessageFileHandler {

private final ConsoleLogger logger = ConsoleLoggerFactory.get(MessagesFileHandler.class);

@Inject
private MessageUpdater messageUpdater;
private final MessageUpdater messageUpdater;

MessagesFileHandler() {
@Inject
MessagesFileHandler(@DataFolder File dataFolder, Settings settings, MessageUpdater messageUpdater) {
super(dataFolder, settings);
this.messageUpdater = messageUpdater;
}

@Override
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/fr/xephi/authme/service/HelpTranslationGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@
*/
public class HelpTranslationGenerator {

@Inject
private CommandInitializer commandInitializer;

@Inject
private HelpMessagesService helpMessagesService;
private final CommandInitializer commandInitializer;
private final HelpMessagesService helpMessagesService;
private final Settings settings;
private final File dataFolder;

@Inject
private Settings settings;

@DataFolder
@Inject
private File dataFolder;
HelpTranslationGenerator(CommandInitializer commandInitializer, HelpMessagesService helpMessagesService,
Settings settings, @DataFolder File dataFolder) {
this.commandInitializer = commandInitializer;
this.helpMessagesService = helpMessagesService;
this.settings = settings;
this.dataFolder = dataFolder;
}

/**
* Updates the help file to contain entries for all commands.
Expand Down
29 changes: 14 additions & 15 deletions src/test/java/fr/xephi/authme/command/CommandMapperTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package fr.xephi.authme.command;

import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionExtension;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.command.TestCommandsUtil.TestLoginCommand;
import fr.xephi.authme.command.TestCommandsUtil.TestRegisterCommand;
import fr.xephi.authme.command.TestCommandsUtil.TestUnregisterCommand;
Expand All @@ -11,9 +8,11 @@
import fr.xephi.authme.permission.PermissionsManager;
import org.bukkit.command.CommandSender;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Set;
Expand All @@ -33,16 +32,16 @@
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;

/**
* Test for {@link CommandMapper}.
*/
@ExtendWith(DelayedInjectionExtension.class)
@ExtendWith(MockitoExtension.class)
class CommandMapperTest {

private static List<CommandDescription> commands;

@InjectDelayed
private CommandMapper mapper;

@Mock
Expand All @@ -56,9 +55,10 @@ static void setUpCommandHandler() {
commands = TestCommandsUtil.generateCommands();
}

@BeforeInjecting
void setUpMocks() {
@BeforeEach
void setUpMocksAndMapper() {
given(commandInitializer.getCommands()).willReturn(commands);
mapper = new CommandMapper(commandInitializer, permissionsManager);
}

// -----------
Expand Down Expand Up @@ -106,12 +106,12 @@ void shouldRejectCommandWithTooManyArguments() {
// given
List<String> parts = asList("authme", "register", "pass123", "pass123", "pass123");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);

// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);

// then
verifyNoInteractions(permissionsManager);
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "authme", "register")));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.INCORRECT_ARGUMENTS));
assertThat(result.getDifference(), equalTo(0.0));
Expand All @@ -124,12 +124,12 @@ void shouldRejectCommandWithTooFewArguments() {
// given
List<String> parts = asList("authme", "Reg");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);

// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);

// then
verifyNoInteractions(permissionsManager);
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "authme", "register")));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.INCORRECT_ARGUMENTS));
assertThat(result.getDifference(), equalTo(0.0));
Expand All @@ -142,12 +142,12 @@ void shouldSuggestCommandWithSimilarLabel() {
// given
List<String> parts = asList("authme", "reh", "pass123", "pass123");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);

// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);

// then
verifyNoInteractions(permissionsManager);
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "authme", "register")));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.UNKNOWN_LABEL));
assertThat(result.getDifference() < 0.75, equalTo(true));
Expand All @@ -161,12 +161,12 @@ void shouldSuggestMostSimilarCommand() {
// given
List<String> parts = asList("authme", "asdfawetawty4asdca");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);

// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);

// then
verifyNoInteractions(permissionsManager);
assertThat(result.getCommandDescription(), not(nullValue()));
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.UNKNOWN_LABEL));
assertThat(result.getDifference() > 0.75, equalTo(true));
Expand All @@ -179,12 +179,12 @@ void shouldHandleBaseWithWrongArguments() {
// given
List<String> parts = singletonList("unregister");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);

// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);

// then
verifyNoInteractions(permissionsManager);
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.INCORRECT_ARGUMENTS));
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "unregister")));
assertThat(result.getDifference(), equalTo(0.0));
Expand All @@ -197,12 +197,12 @@ void shouldHandleUnknownBase() {
// given
List<String> parts = asList("bogus", "label1", "arg1");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);

// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);

// then
verifyNoInteractions(permissionsManager);
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.MISSING_BASE_COMMAND));
assertThat(result.getCommandDescription(), nullValue());
}
Expand Down Expand Up @@ -240,12 +240,12 @@ void shouldReturnChildlessBaseCommandWithArgCountError() {
// given
List<String> parts = asList("unregistER", "player1", "wrongArg");
CommandSender sender = mock(CommandSender.class);
given(permissionsManager.hasPermission(eq(sender), any(PermissionNode.class))).willReturn(true);

// when
FoundCommandResult result = mapper.mapPartsToCommand(sender, parts);

// then
verifyNoInteractions(permissionsManager);
assertThat(result.getResultStatus(), equalTo(FoundResultStatus.INCORRECT_ARGUMENTS));
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "unregister")));
assertThat(result.getDifference(), equalTo(0.0));
Expand Down Expand Up @@ -305,7 +305,6 @@ void shouldSupportAuthMePrefix() {
assertThat(result.getCommandDescription(), equalTo(getCommandWithLabel(commands, "unregister")));
}

@SuppressWarnings("unchecked")
@Test
void shouldReturnExecutableCommandClasses() {
// given / when
Expand Down
Loading

0 comments on commit 13087f7

Please sign in to comment.