#432 Fix broken tool tasks

This commit is contained in:
ljacqu 2016-05-03 20:44:01 +02:00
parent 3645806edc
commit 67aea654cc
2 changed files with 52 additions and 19 deletions

View File

@ -2,9 +2,14 @@ package tools.commands;
import fr.xephi.authme.command.CommandArgumentDescription; import fr.xephi.authme.command.CommandArgumentDescription;
import fr.xephi.authme.command.CommandDescription; import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.CommandInitializer;
import fr.xephi.authme.command.CommandPermissions; import fr.xephi.authme.command.CommandPermissions;
import fr.xephi.authme.command.CommandUtils; 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 fr.xephi.authme.permission.PermissionNode;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import tools.utils.FileUtils; import tools.utils.FileUtils;
import tools.utils.TagValue.NestedTagValue; import tools.utils.TagValue.NestedTagValue;
import tools.utils.TagValueHolder; import tools.utils.TagValueHolder;
@ -12,10 +17,13 @@ import tools.utils.ToolTask;
import tools.utils.ToolsConstants; import tools.utils.ToolsConstants;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.Scanner; import java.util.Scanner;
import java.util.Set; 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 { public class CommandPageCreater implements ToolTask {
private static final String OUTPUT_FILE = ToolsConstants.DOCS_FOLDER + "commands.md"; private static final String OUTPUT_FILE = ToolsConstants.DOCS_FOLDER + "commands.md";
@ -27,8 +35,7 @@ public class CommandPageCreater implements ToolTask {
@Override @Override
public void execute(Scanner scanner) { public void execute(Scanner scanner) {
// TODO ljacqu 20160427: Fix initialization of commands final Set<CommandDescription> baseCommands = CommandInitializer.buildCommands(getMockInitializer());
final Set<CommandDescription> baseCommands = new HashSet<>();//CommandInitializer.buildCommands();
NestedTagValue commandTags = new NestedTagValue(); NestedTagValue commandTags = new NestedTagValue();
addCommandsInfo(commandTags, baseCommands); addCommandsInfo(commandTags, baseCommands);
@ -75,4 +82,24 @@ public class CommandPageCreater implements ToolTask {
} }
return result.toString(); 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<Object>() {
@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;
}
} }

View File

@ -1,5 +1,6 @@
package tools.hashmethods; package tools.hashmethods;
import fr.xephi.authme.initialization.AuthMeServiceInitializer;
import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.crypts.EncryptionMethod; import fr.xephi.authme.security.crypts.EncryptionMethod;
import fr.xephi.authme.security.crypts.HexSaltedMethod; 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.HasSalt;
import fr.xephi.authme.security.crypts.description.Recommendation; import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.properties.SecuritySettings; import org.mockito.invocation.InvocationOnMock;
import org.mockito.BDDMockito; import org.mockito.stubbing.Answer;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.HashMap; import java.util.HashMap;
@ -18,7 +19,9 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import static com.google.common.collect.Sets.newHashSet; 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.mock;
import static org.mockito.Mockito.when;
/** /**
* Gathers information on {@link EncryptionMethod} implementations based on * Gathers information on {@link EncryptionMethod} implementations based on
@ -30,7 +33,7 @@ public class EncryptionMethodInfoGatherer {
private final static Set<Class<? extends Annotation>> RELEVANT_ANNOTATIONS = private final static Set<Class<? extends Annotation>> RELEVANT_ANNOTATIONS =
newHashSet(HasSalt.class, Recommendation.class, AsciiRestricted.class); newHashSet(HasSalt.class, Recommendation.class, AsciiRestricted.class);
private static NewSetting settings = createSettings(); private static AuthMeServiceInitializer initializer = createInitializer();
private Map<HashAlgorithm, MethodDescription> descriptions; private Map<HashAlgorithm, MethodDescription> descriptions;
@ -54,7 +57,7 @@ public class EncryptionMethodInfoGatherer {
private static MethodDescription createDescription(HashAlgorithm algorithm) { private static MethodDescription createDescription(HashAlgorithm algorithm) {
Class<? extends EncryptionMethod> clazz = algorithm.getClazz(); Class<? extends EncryptionMethod> clazz = algorithm.getClazz();
EncryptionMethod method = null; // TODO ljacqu PasswordSecurity.initializeEncryptionMethod(algorithm, settings); EncryptionMethod method = initializer.newInstance(clazz);
if (method == null) { if (method == null) {
throw new NullPointerException("Method for '" + algorithm + "' is null"); throw new NullPointerException("Method for '" + algorithm + "' is null");
} }
@ -134,19 +137,22 @@ public class EncryptionMethodInfoGatherer {
return key.cast(map.get(key)); return key.cast(map.get(key));
} }
private static NewSetting createSettings() { private static AuthMeServiceInitializer createInitializer() {
// TODO #672 Don't mock settings but instantiate a NewSetting object without any validation / migration
NewSetting settings = mock(NewSetting.class); NewSetting settings = mock(NewSetting.class);
BDDMockito.given(settings.getProperty(HooksSettings.BCRYPT_LOG2_ROUND)).willReturn(8); // Return the default value for any property
BDDMockito.given(settings.getProperty(SecuritySettings.DOUBLE_MD5_SALT_LENGTH)).willReturn(8); when(settings.getProperty(any(Property.class))).thenAnswer(new Answer<Object>() {
return settings; @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"))) { // By not passing any "allowed package" to the constructor, the initializer will throw if it needs to
FileConfiguration configuration = YamlConfiguration.loadConfiguration(isr); // instantiate any dependency other than what we provide.
return new NewSetting(configuration, null, null, null); AuthMeServiceInitializer initializer = new AuthMeServiceInitializer();
} catch (IOException e) { initializer.register(NewSetting.class, settings);
throw new UnsupportedOperationException(e); return initializer;
}*/
} }
} }