use of ch.jalu.injector.Injector in project AuthMeReloaded by AuthMe.
the class EncryptionMethodInfoGatherer method createInitializer.
@SuppressWarnings("unchecked")
private static Injector createInitializer() {
Settings settings = mock(Settings.class);
// Return the default value for any property
when(settings.getProperty(any(Property.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Property<?> property = (Property<?>) invocation.getArguments()[0];
return property.getDefaultValue();
}
});
// By passing some bogus "package" to the constructor, the injector will throw if it needs to
// instantiate any dependency other than what we provide.
Injector injector = new InjectorBuilder().addDefaultHandlers("fr.xephi.authme.security.crypts").create();
injector.register(Settings.class, settings);
return injector;
}
use of ch.jalu.injector.Injector in project AuthMeReloaded by AuthMe.
the class AuthMeInitializationTest method shouldInitializeAllServices.
@Test
public void shouldInitializeAllServices() {
// given
Settings settings = new Settings(dataFolder, mock(PropertyResource.class), null, buildConfigurationData());
Injector injector = new InjectorBuilder().addHandlers(new FactoryDependencyHandler(), new SingletonStoreDependencyHandler()).addDefaultHandlers("fr.xephi.authme").create();
injector.provide(DataFolder.class, dataFolder);
injector.register(Server.class, server);
injector.register(PluginManager.class, pluginManager);
injector.register(AuthMe.class, authMe);
injector.register(Settings.class, settings);
injector.register(DataSource.class, mock(DataSource.class));
injector.register(BukkitService.class, mock(BukkitService.class));
// when
authMe.instantiateServices(injector);
authMe.registerEventListeners(injector);
// then
// Take a few samples and ensure that they are not null
assertThat(injector.getIfAvailable(BlockListener.class), not(nullValue()));
assertThat(injector.getIfAvailable(CommandHandler.class), not(nullValue()));
assertThat(injector.getIfAvailable(Management.class), not(nullValue()));
assertThat(injector.getIfAvailable(NewAPI.class), not(nullValue()));
assertThat(injector.getIfAvailable(PasswordSecurity.class), not(nullValue()));
assertThat(injector.getIfAvailable(PermissionsManager.class), not(nullValue()));
assertThat(injector.getIfAvailable(ProcessSyncPlayerLogin.class), not(nullValue()));
assertThat(injector.getIfAvailable(PurgeService.class), not(nullValue()));
}
use of ch.jalu.injector.Injector in project AuthMeReloaded by AuthMe.
the class PasswordSecurityTest method setUpMocks.
@BeforeInjecting
public void setUpMocks() {
caughtClassInEvent = null;
// When the password encryption event is emitted, replace the encryption method with our mock.
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
if (arguments[0] instanceof PasswordEncryptionEvent) {
PasswordEncryptionEvent event = (PasswordEncryptionEvent) arguments[0];
caughtClassInEvent = event.getMethod() == null ? null : event.getMethod().getClass();
event.setMethod(method);
}
return null;
}
}).when(pluginManager).callEvent(any(Event.class));
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.BCRYPT);
given(settings.getProperty(SecuritySettings.LEGACY_HASHES)).willReturn(Collections.emptySet());
given(settings.getProperty(HooksSettings.BCRYPT_LOG2_ROUND)).willReturn(8);
Injector injector = new InjectorBuilder().addDefaultHandlers("fr.xephi.authme.security.crypts").create();
injector.register(Settings.class, settings);
given(hashAlgorithmFactory.newInstance(any(Class.class))).willAnswer(invocation -> {
Object o = injector.createIfHasDependencies(invocation.getArgument(0));
if (o == null) {
throw new IllegalArgumentException("Cannot create object of class '" + invocation.getArgument(0) + "': missing class that needs to be provided?");
}
return o;
});
}
Aggregations