diff --git a/src/test/java/tools/commands/CommandPageCreater.java b/src/test/java/tools/commands/CommandPageCreater.java index f74090652..4ba480061 100644 --- a/src/test/java/tools/commands/CommandPageCreater.java +++ b/src/test/java/tools/commands/CommandPageCreater.java @@ -2,9 +2,14 @@ package tools.commands; import fr.xephi.authme.command.CommandArgumentDescription; import fr.xephi.authme.command.CommandDescription; +import fr.xephi.authme.command.CommandInitializer; import fr.xephi.authme.command.CommandPermissions; import fr.xephi.authme.command.CommandUtils; +import fr.xephi.authme.command.ExecutableCommand; +import fr.xephi.authme.initialization.AuthMeServiceInitializer; import fr.xephi.authme.permission.PermissionNode; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import tools.utils.FileUtils; import tools.utils.TagValue.NestedTagValue; import tools.utils.TagValueHolder; @@ -12,10 +17,13 @@ import tools.utils.ToolTask; import tools.utils.ToolsConstants; import java.util.Collection; -import java.util.HashSet; import java.util.Scanner; import java.util.Set; +import static org.mockito.Matchers.isA; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + public class CommandPageCreater implements ToolTask { private static final String OUTPUT_FILE = ToolsConstants.DOCS_FOLDER + "commands.md"; @@ -27,8 +35,7 @@ public class CommandPageCreater implements ToolTask { @Override public void execute(Scanner scanner) { - // TODO ljacqu 20160427: Fix initialization of commands - final Set baseCommands = new HashSet<>();//CommandInitializer.buildCommands(); + final Set baseCommands = CommandInitializer.buildCommands(getMockInitializer()); NestedTagValue commandTags = new NestedTagValue(); addCommandsInfo(commandTags, baseCommands); @@ -75,4 +82,24 @@ public class CommandPageCreater implements ToolTask { } return result.toString(); } + + /** + * Creates an initializer mock that returns mocks of any {@link ExecutableCommand} subclasses passed to it. + * + * @return the initializer mock + */ + private static AuthMeServiceInitializer getMockInitializer() { + AuthMeServiceInitializer initializer = mock(AuthMeServiceInitializer.class); + when(initializer.newInstance(isA(Class.class))).thenAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Class clazz = (Class) invocation.getArguments()[0]; + if (ExecutableCommand.class.isAssignableFrom(clazz)) { + return mock(clazz); + } + throw new IllegalStateException("Unexpected request to instantiate class of type " + clazz.getName()); + } + }); + return initializer; + } } diff --git a/src/test/java/tools/hashmethods/EncryptionMethodInfoGatherer.java b/src/test/java/tools/hashmethods/EncryptionMethodInfoGatherer.java index 47bb8bd6f..ad2ecba4f 100644 --- a/src/test/java/tools/hashmethods/EncryptionMethodInfoGatherer.java +++ b/src/test/java/tools/hashmethods/EncryptionMethodInfoGatherer.java @@ -1,5 +1,6 @@ package tools.hashmethods; +import fr.xephi.authme.initialization.AuthMeServiceInitializer; import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.security.crypts.EncryptionMethod; import fr.xephi.authme.security.crypts.HexSaltedMethod; @@ -7,9 +8,9 @@ import fr.xephi.authme.security.crypts.description.AsciiRestricted; import fr.xephi.authme.security.crypts.description.HasSalt; import fr.xephi.authme.security.crypts.description.Recommendation; import fr.xephi.authme.settings.NewSetting; -import fr.xephi.authme.settings.properties.HooksSettings; -import fr.xephi.authme.settings.properties.SecuritySettings; -import org.mockito.BDDMockito; +import fr.xephi.authme.settings.domain.Property; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import java.lang.annotation.Annotation; import java.util.HashMap; @@ -18,7 +19,9 @@ import java.util.Map; import java.util.Set; import static com.google.common.collect.Sets.newHashSet; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Gathers information on {@link EncryptionMethod} implementations based on @@ -30,7 +33,7 @@ public class EncryptionMethodInfoGatherer { private final static Set> RELEVANT_ANNOTATIONS = newHashSet(HasSalt.class, Recommendation.class, AsciiRestricted.class); - private static NewSetting settings = createSettings(); + private static AuthMeServiceInitializer initializer = createInitializer(); private Map descriptions; @@ -54,7 +57,7 @@ public class EncryptionMethodInfoGatherer { private static MethodDescription createDescription(HashAlgorithm algorithm) { Class clazz = algorithm.getClazz(); - EncryptionMethod method = null; // TODO ljacqu PasswordSecurity.initializeEncryptionMethod(algorithm, settings); + EncryptionMethod method = initializer.newInstance(clazz); if (method == null) { throw new NullPointerException("Method for '" + algorithm + "' is null"); } @@ -134,19 +137,22 @@ public class EncryptionMethodInfoGatherer { return key.cast(map.get(key)); } - private static NewSetting createSettings() { - // TODO #672 Don't mock settings but instantiate a NewSetting object without any validation / migration + private static AuthMeServiceInitializer createInitializer() { NewSetting settings = mock(NewSetting.class); - BDDMockito.given(settings.getProperty(HooksSettings.BCRYPT_LOG2_ROUND)).willReturn(8); - BDDMockito.given(settings.getProperty(SecuritySettings.DOUBLE_MD5_SALT_LENGTH)).willReturn(8); - return settings; + // Return the default value for any property + when(settings.getProperty(any(Property.class))).thenAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Property property = (Property) invocation.getArguments()[0]; + return property.getDefaultValue(); + } + }); - /*try (InputStreamReader isr = new InputStreamReader(getClass().getResourceAsStream("config.yml"))) { - FileConfiguration configuration = YamlConfiguration.loadConfiguration(isr); - return new NewSetting(configuration, null, null, null); - } catch (IOException e) { - throw new UnsupportedOperationException(e); - }*/ + // By not passing any "allowed package" to the constructor, the initializer will throw if it needs to + // instantiate any dependency other than what we provide. + AuthMeServiceInitializer initializer = new AuthMeServiceInitializer(); + initializer.register(NewSetting.class, settings); + return initializer; } }