Merge branch 'master' of https://github.com/AuthMe-Team/AuthMeReloaded into 707-process-as-service

Conflicts:
	src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java
	src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java
This commit is contained in:
ljacqu
2016-05-20 19:48:26 +02:00
29 changed files with 489 additions and 386 deletions
@@ -9,7 +9,9 @@ import fr.xephi.authme.initialization.AuthMeServiceInitializer;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.command.CommandSender;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -53,6 +55,13 @@ public class ReloadCommandTest {
TestHelper.setupLogger();
}
@Before
public void setDefaultSettings() {
// Mock properties retrieved by ConsoleLogger
given(settings.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)).willReturn(false);
given(settings.getProperty(SecuritySettings.USE_LOGGING)).willReturn(false);
}
@Test
public void shouldReload() {
// given
@@ -204,10 +204,11 @@ public abstract class AbstractResourceClosingTest {
}
/**
* Return a list with some test elements that correspond to the given list type's generic type.
* Return a collection of the required type with some test elements that correspond to the
* collection's generic type.
*
* @param type The list type to process and build a test list for
* @return Test list with sample elements of the correct type
* @param type The collection type to process and build a test collection for
* @return Test collection with sample elements of the correct type
*/
private static Collection<?> getTypedCollection(Type type) {
if (type instanceof ParameterizedType) {
@@ -18,8 +18,11 @@ import fr.xephi.authme.initialization.samples.ProvidedClass;
import fr.xephi.authme.initialization.samples.Size;
import fr.xephi.authme.settings.NewSetting;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
@@ -36,6 +39,11 @@ public class AuthMeServiceInitializerTest {
private AuthMeServiceInitializer initializer;
// As we test many cases that throw exceptions, we use JUnit's ExpectedException Rule
// to make sure that we receive the exception we expect
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Before
public void setInitializer() {
initializer = new AuthMeServiceInitializer(ALLOWED_PACKAGE);
@@ -54,15 +62,17 @@ public class AuthMeServiceInitializerTest {
}
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowForInvalidPackage() {
// given / when / then
expectRuntimeExceptionWith("outside of the allowed packages");
initializer.get(InvalidClass.class);
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowForUnregisteredPrimitiveType() {
// given / when / then
expectRuntimeExceptionWith("Primitive types must be provided");
initializer.get(int.class);
}
@@ -85,24 +95,27 @@ public class AuthMeServiceInitializerTest {
assertThat(object.getGammaService(), equalTo(initializer.get(BetaManager.class).getDependencies()[1]));
}
@Test(expected = RuntimeException.class)
@Test
public void shouldRecognizeCircularReferences() {
// given / when / then
expectRuntimeExceptionWith("Found cyclic dependency");
initializer.get(CircularClasses.Circular3.class);
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowForUnregisteredAnnotation() {
// given
initializer.provide(Size.class, 4523);
// when / then
expectRuntimeExceptionWith("must be registered beforehand");
initializer.get(ClassWithAnnotations.class);
}
@Test(expected = RuntimeException.class)
public void shouldThrowForFieldInjectionWithNoDefaultConstructor() {
@Test
public void shouldThrowForFieldInjectionWithoutNoArgsConstructor() {
// given / when / then
expectRuntimeExceptionWith("Did not find injection method");
initializer.get(BadFieldInjection.class);
}
@@ -124,36 +137,41 @@ public class AuthMeServiceInitializerTest {
equalTo(result.getBetaManager().getDependencies()[1]));
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowForAnnotationAsKey() {
// given / when / then
expectRuntimeExceptionWith("Cannot retrieve annotated elements in this way");
initializer.get(Size.class);
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowForSecondRegistration() {
// given / when / then
expectRuntimeExceptionWith("There is already an object present");
initializer.register(ProvidedClass.class, new ProvidedClass(""));
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowForSecondAnnotationRegistration() {
// given
initializer.provide(Size.class, 12);
// when / then
expectRuntimeExceptionWith("already registered");
initializer.provide(Size.class, -8);
}
@Test(expected = NullPointerException.class)
@Test
public void shouldThrowForNullValueAssociatedToAnnotation() {
// given / when / then
expectedException.expect(NullPointerException.class);
initializer.provide(Duration.class, null);
}
@Test(expected = NullPointerException.class)
@Test
public void shouldThrowForRegisterWithNull() {
// given / when / then
expectedException.expect(NullPointerException.class);
initializer.register(String.class, null);
}
@@ -166,31 +184,49 @@ public class AuthMeServiceInitializerTest {
PostConstructTestClass testClass = initializer.get(PostConstructTestClass.class);
// then
assertThat(testClass.werePostConstructsCalled(), equalTo(true));
assertThat(testClass.wasPostConstructCalled(), equalTo(true));
assertThat(testClass.getBetaManager(), not(nullValue()));
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowForInvalidPostConstructMethod() {
// given / when / then
expectRuntimeExceptionWith("@PostConstruct method may not be static or have any parameters");
initializer.get(InvalidPostConstruct.WithParams.class);
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowForStaticPostConstructMethod() {
// given / when / then
expectRuntimeExceptionWith("@PostConstruct method may not be static or have any parameters");
initializer.get(InvalidPostConstruct.Static.class);
}
@Test(expected = RuntimeException.class)
@Test
public void shouldForwardExceptionFromPostConstruct() {
// given / when / then
expectRuntimeExceptionWith("Error executing @PostConstruct method");
initializer.get(InvalidPostConstruct.ThrowsException.class);
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowForMultiplePostConstructMethods() {
// given / when / then
expectRuntimeExceptionWith("Multiple methods with @PostConstruct");
initializer.get(InvalidPostConstruct.MultiplePostConstructs.class);
}
@Test
public void shouldThrowForPostConstructNotReturningVoid() {
// given / when / then
expectRuntimeExceptionWith("@PostConstruct method must have return type void");
initializer.get(InvalidPostConstruct.NotVoidReturnType.class);
}
@Test
public void shouldThrowForAbstractNonRegisteredDependency() {
// given / when / then
expectRuntimeExceptionWith("cannot be instantiated");
initializer.get(ClassWithAbstractDependency.class);
}
@@ -208,12 +244,13 @@ public class AuthMeServiceInitializerTest {
assertThat(cwad.getAlphaService(), not(nullValue()));
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowForAlreadyRegisteredClass() {
// given
initializer.register(BetaManager.class, new BetaManager());
// when / then
expectRuntimeExceptionWith("There is already an object present");
initializer.register(BetaManager.class, new BetaManager());
}
@@ -229,9 +266,10 @@ public class AuthMeServiceInitializerTest {
assertThat(singletonScoped, not(sameInstance(requestScoped)));
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowForStaticFieldInjection() {
// given / when / then
expectRuntimeExceptionWith("is static but annotated with @Inject");
initializer.newInstance(InvalidStaticFieldInjection.class);
}
@@ -271,10 +309,16 @@ public class AuthMeServiceInitializerTest {
assertThat(providedClass.getWasReloaded(), equalTo(true));
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowForNullSetting() {
// given / when / then
expectRuntimeExceptionWith("Settings instance is null");
initializer.performReloadOnServices();
}
private void expectRuntimeExceptionWith(String message) {
expectedException.expect(RuntimeException.class);
expectedException.expectMessage(containsString(message));
}
}
@@ -115,11 +115,9 @@ public class FieldInjectionTest {
}
private static class ThrowingConstructor {
@SuppressWarnings("unused")
@Inject
private ProvidedClass providedClass;
@SuppressWarnings("unused")
public ThrowingConstructor() {
throw new UnsupportedOperationException("Exception in constructor");
}
@@ -65,4 +65,14 @@ public class InstantiationFallbackTest {
assertThat(instantiation, nullValue());
}
@Test
public void shouldReturnNullForClassWithPostConstruct() {
// given / when
Injection<InstantiationFallbackClasses.ClassWithPostConstruct> instantiation =
InstantiationFallback.provide(InstantiationFallbackClasses.ClassWithPostConstruct.class).get();
// then
assertThat(instantiation, nullValue());
}
}
@@ -1,9 +1,10 @@
package fr.xephi.authme.initialization.samples;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
/**
* Sample class - triggers instantiation fallback.
* Sample class - tests various situations for the instantiation fallback.
*/
public abstract class InstantiationFallbackClasses {
@@ -42,4 +43,12 @@ public abstract class InstantiationFallbackClasses {
}
}
// Class with @PostConstruct method should never be instantiated by instantiation fallback
public static final class ClassWithPostConstruct {
@PostConstruct
public void postConstructMethod() {
// --
}
}
}
@@ -9,10 +9,8 @@ import javax.inject.Inject;
public abstract class InvalidPostConstruct {
public static final class WithParams {
@SuppressWarnings("unused")
@Inject
private AlphaService alphaService;
@SuppressWarnings("unused")
@Inject
private ProvidedClass providedClass;
@@ -36,9 +34,36 @@ public abstract class InvalidPostConstruct {
}
public static final class ThrowsException {
@Inject
private ProvidedClass providedClass;
@PostConstruct
public void throwingPostConstruct() {
throw new IllegalStateException("Exception in post construct");
}
}
public static final class NotVoidReturnType {
@Inject
private ProvidedClass providedClass;
@PostConstruct
public int returnsInt() {
return 42;
}
}
public static final class MultiplePostConstructs {
@Inject
private ProvidedClass providedClass;
@PostConstruct
public void postConstruct1() {
// --
}
@PostConstruct
public void postConstruct2() {
// --
}
}
}
@@ -17,22 +17,15 @@ public class PostConstructTestClass implements SettingsDependent {
@Inject
private BetaManager betaManager;
private boolean wasPostConstructCalled = false;
private boolean wasSecondPostConstructCalled = false;
private boolean wasReloaded = false;
@PostConstruct
protected void setFieldToTrue() {
public void postConstructMethod() {
wasPostConstructCalled = true;
}
@PostConstruct
public int otherPostConstructMethod() {
wasSecondPostConstructCalled = true;
return 42;
}
public boolean werePostConstructsCalled() {
return wasPostConstructCalled && wasSecondPostConstructCalled;
public boolean wasPostConstructCalled() {
return wasPostConstructCalled;
}
public BetaManager getBetaManager() {