Merge branch 'master' of https://github.com/AuthMe/AuthMeReloaded into 1467-message-keys-hierarchy

This commit is contained in:
ljacqu 2018-01-29 20:04:38 +01:00
commit dfe47066cd
8 changed files with 351 additions and 87 deletions

212
pom.xml
View File

@ -37,12 +37,12 @@
<distributionManagement> <distributionManagement>
<snapshotRepository> <snapshotRepository>
<id>nexus-snapshots</id> <id>codemc-snapshots</id>
<url>https://nexus.authme.eu/repository/maven-snapshots/</url> <url>https://repo.codemc.org/repository/maven-snapshots/</url>
</snapshotRepository> </snapshotRepository>
<repository> <repository>
<id>nexus-releases</id> <id>codemc-releases</id>
<url>https://nexus.authme.eu/repository/maven-releases/</url> <url>https://repo.codemc.org/repository/maven-releases/</url>
</repository> </repository>
</distributionManagement> </distributionManagement>
@ -57,7 +57,8 @@
<properties> <properties>
<!-- Project properties --> <!-- Project properties -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.jdkVersion>1.8</project.jdkVersion> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jdk.version>1.8</jdk.version>
<!-- Output properties --> <!-- Output properties -->
<project.outputName>AuthMe</project.outputName> <project.outputName>AuthMe</project.outputName>
@ -104,6 +105,7 @@
</profiles> </profiles>
<build> <build>
<defaultGoal>clean install</defaultGoal>
<finalName>${project.outputName}-${project.version}</finalName> <finalName>${project.outputName}-${project.version}</finalName>
<resources> <resources>
@ -126,17 +128,44 @@
</resources> </resources>
<plugins> <plugins>
<!-- Maven Java Compiler --> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version> <version>3.7.0</version>
<configuration> <configuration>
<source>${project.jdkVersion}</source> <source>${jdk.version}</source>
<target>${project.jdkVersion}</target> <target>${jdk.version}</target>
</configuration> </configuration>
</plugin> </plugin>
<!-- Test Plugin --> <plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.0</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
@ -149,13 +178,47 @@
</systemPropertyVariables> </systemPropertyVariables>
</configuration> </configuration>
</plugin> </plugin>
<!-- Libs Shading and Relocation --> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>aggregate</goal>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<show>public</show>
<failOnError>false</failOnError>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version> <version>3.1.0</version>
<executions> <executions>
<!-- Run shade goal on package phase -->
<execution> <execution>
<phase>package</phase> <phase>package</phase>
<goals> <goals>
@ -165,7 +228,7 @@
</executions> </executions>
<configuration> <configuration>
<createDependencyReducedPom>false</createDependencyReducedPom> <createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<!-- <!--
Relocate all lib we use in order to fix class loading errors if we use different versions Relocate all lib we use in order to fix class loading errors if we use different versions
than already loaded libs (i.e. by Mojang -> gson) than already loaded libs (i.e. by Mojang -> gson)
@ -220,58 +283,67 @@
</relocations> </relocations>
</configuration> </configuration>
</plugin> </plugin>
<!-- Coverage report generator -->
<plugin> <plugin>
<groupId>org.jacoco</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>jacoco-maven-plugin</artifactId> <artifactId>maven-install-plugin</artifactId>
<version>0.7.9</version> <version>2.5.2</version>
<executions> <executions>
<execution> <execution>
<id>prepare-agent</id> <id>default-install</id>
<phase>none</phase>
</execution>
<execution>
<id>install-custom</id>
<phase>install</phase>
<goals> <goals>
<goal>prepare-agent</goal> <goal>install-file</goal>
</goals> </goals>
</execution> </execution>
</executions> </executions>
</plugin>
<!-- Coverage report uploader -->
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.3.0</version>
<configuration> <configuration>
<failOnServiceError>false</failOnServiceError> <pomFile>pom.xml</pomFile>
<file>target/original-${project.build.finalName}.jar</file>
<javadoc>target/${project.build.finalName}-javadoc.jar</javadoc>
<sources>target/${project.build.finalName}-sources.jar</sources>
</configuration> </configuration>
<!-- The secret token is provided with a command-line parameter! -->
</plugin>
<!-- JavaDocs generator -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<configuration>
<show>public</show>
<failOnError>false</failOnError>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>deploy</phase>
<goals><goal>jar</goal></goals>
</execution>
</executions>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId> <artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version> <version>2.8.2</version>
<executions> <executions>
<execution> <execution>
<id>deploy</id> <id>default-deploy</id>
<phase>none</phase>
</execution>
<execution>
<id>deploy-custom</id>
<phase>deploy</phase> <phase>deploy</phase>
<goals><goal>deploy</goal></goals> <goals>
</execution> <goal>deploy-file</goal>
</executions> </goals>
</execution>
</executions>
<configuration>
<!-- TODO: find a solution to detect release/snapshot -->
<repositoryId>${project.distributionManagement.snapshotRepository.id}</repositoryId>
<url>${project.distributionManagement.snapshotRepository.url}</url>
<pomFile>pom.xml</pomFile>
<file>target/original-${project.build.finalName}.jar</file>
<!-- Already included by the plugin
<javadoc>target/${project.build.finalName}-javadoc.jar</javadoc>
<sources>target/${project.build.finalName}-sources.jar</sources>
-->
</configuration>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.3.0</version>
<configuration>
<!-- The secret token is provided with a command-line parameter! -->
<failOnServiceError>false</failOnServiceError>
</configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
@ -360,7 +432,7 @@
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>23.5-jre</version> <version>23.6-jre</version>
<scope>compile</scope> <scope>compile</scope>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
@ -395,15 +467,15 @@
<dependency> <dependency>
<groupId>com.zaxxer</groupId> <groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId> <artifactId>HikariCP</artifactId>
<version>2.7.4</version> <version>2.7.6</version>
<scope>compile</scope> <scope>compile</scope>
<optional>true</optional>
<exclusions> <exclusions>
<exclusion> <exclusion>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
</exclusion> </exclusion>
</exclusions> </exclusions>
<optional>true</optional>
</dependency> </dependency>
<!-- HikariCP Logger --> <!-- HikariCP Logger -->
<dependency> <dependency>
@ -428,6 +500,8 @@
<groupId>de.mkammerer</groupId> <groupId>de.mkammerer</groupId>
<artifactId>argon2-jvm-nolibs</artifactId> <artifactId>argon2-jvm-nolibs</artifactId>
<version>2.3</version> <version>2.3</version>
<scope>compile</scope>
<optional>true</optional>
</dependency> </dependency>
<!-- Spigot API, http://www.spigotmc.org/ --> <!-- Spigot API, http://www.spigotmc.org/ -->
@ -462,6 +536,21 @@
<!-- Bukkit Libraries --> <!-- Bukkit Libraries -->
<!-- ConfigMe -->
<dependency>
<groupId>ch.jalu</groupId>
<artifactId>configme</artifactId>
<version>0.4.1</version>
<scope>compile</scope>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- bStats metrics --> <!-- bStats metrics -->
<dependency> <dependency>
<groupId>org.bstats</groupId> <groupId>org.bstats</groupId>
@ -501,7 +590,7 @@
<dependency> <dependency>
<groupId>ru.tehkode</groupId> <groupId>ru.tehkode</groupId>
<artifactId>PermissionsEx</artifactId> <artifactId>PermissionsEx</artifactId>
<version>1.23.4</version> <version>1.23.5</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>
@ -680,7 +769,7 @@
<dependency> <dependency>
<groupId>net.ess3</groupId> <groupId>net.ess3</groupId>
<artifactId>Essentials</artifactId> <artifactId>Essentials</artifactId>
<version>2.13-SNAPSHOT</version> <version>2.13.1</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>
@ -736,21 +825,6 @@
</exclusions> </exclusions>
</dependency> </dependency>
<!-- ConfigMe -->
<dependency>
<groupId>ch.jalu</groupId>
<artifactId>configme</artifactId>
<version>0.4.1</version>
<scope>compile</scope>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Unit Testing Libraries --> <!-- Unit Testing Libraries -->
<dependency> <dependency>

View File

@ -167,7 +167,7 @@ public class AuthMeApi {
* @param playerName The name of the player to process * @param playerName The name of the player to process
* *
* @return The date of the last login, or null if the player doesn't exist or has never logged in * @return The date of the last login, or null if the player doesn't exist or has never logged in
* @Deprecated Use Java 8's Instant method {@link #getLastLoginTime(String)} * @deprecated Use Java 8's Instant method {@link #getLastLoginTime(String)}
*/ */
@Deprecated @Deprecated
public Date getLastLogin(String playerName) { public Date getLastLogin(String playerName) {

View File

@ -41,6 +41,7 @@ public class SQLite implements DataSource {
* Constructor for SQLite. * Constructor for SQLite.
* *
* @param settings The settings instance * @param settings The settings instance
* @param dataFolder The data folder
* *
* @throws SQLException when initialization of a SQL datasource failed * @throws SQLException when initialization of a SQL datasource failed
*/ */

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.permission; package fr.xephi.authme.permission;
import com.google.common.annotations.VisibleForTesting;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.Reloadable; import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.listener.JoiningPlayer; import fr.xephi.authme.listener.JoiningPlayer;
@ -41,8 +42,7 @@ public class PermissionsManager implements Reloadable {
private final Server server; private final Server server;
private final PluginManager pluginManager; private final PluginManager pluginManager;
private final Settings settings;
private Settings settings;
/** /**
* The permission handler that is currently in use. * The permission handler that is currently in use.
@ -50,14 +50,8 @@ public class PermissionsManager implements Reloadable {
*/ */
private PermissionHandler handler = null; private PermissionHandler handler = null;
/**
* Constructor.
*
* @param server Server instance
* @param pluginManager Bukkit plugin manager
*/
@Inject @Inject
public PermissionsManager(Server server, PluginManager pluginManager, Settings settings) { PermissionsManager(Server server, PluginManager pluginManager, Settings settings) {
this.server = server; this.server = server;
this.pluginManager = pluginManager; this.pluginManager = pluginManager;
this.settings = settings; this.settings = settings;
@ -76,7 +70,8 @@ public class PermissionsManager implements Reloadable {
* Setup and hook into the permissions systems. * Setup and hook into the permissions systems.
*/ */
@PostConstruct @PostConstruct
private void setup() { @VisibleForTesting
void setup() {
if (settings.getProperty(PluginSettings.FORCE_VAULT_HOOK)) { if (settings.getProperty(PluginSettings.FORCE_VAULT_HOOK)) {
try { try {
PermissionHandler handler = createPermissionHandler(PermissionsSystemType.VAULT); PermissionHandler handler = createPermissionHandler(PermissionsSystemType.VAULT);

View File

@ -36,8 +36,7 @@ public class LuckPermsHandler implements PermissionHandler {
try { try {
luckPermsApi = LuckPerms.getApi(); luckPermsApi = LuckPerms.getApi();
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
e.printStackTrace(); throw new PermissionHandlerException("Could not get api of LuckPerms", e);
throw new PermissionHandlerException("Could not get api of LuckPerms");
} }
} }

View File

@ -9,4 +9,8 @@ public class PermissionHandlerException extends Exception {
public PermissionHandlerException(String message) { public PermissionHandlerException(String message) {
super(message); super(message);
} }
public PermissionHandlerException(String message, Throwable cause) {
super(message, cause);
}
} }

View File

@ -68,7 +68,7 @@ country_banned: '&4Il tuo paese è bandito da questo server!'
not_owner_error: 'Non sei il proprietario di questo account. Per favore scegli un altro nome!' not_owner_error: 'Non sei il proprietario di questo account. Per favore scegli un altro nome!'
kick_fullserver: '&4Il server è attualmente pieno, riprova più tardi!' kick_fullserver: '&4Il server è attualmente pieno, riprova più tardi!'
same_nick: '&4Un giocatore con il tuo stesso nome utente è già connesso sul server!' same_nick: '&4Un giocatore con il tuo stesso nome utente è già connesso sul server!'
invalid_name_case: 'Dovresti entrare con questo nome utente: "%valid", al posto di: "%invalid".' invalid_name_case: 'Dovresti entrare con questo nome utente "%valid", al posto di "%invalid".'
same_ip_online: 'Un giocatore con il tuo stesso IP è già connesso sul server!' same_ip_online: 'Un giocatore con il tuo stesso IP è già connesso sul server!'
# Email # Email
@ -94,15 +94,15 @@ email_cooldown_error: '&cUna email di recupero ti è già stata inviata recentem
# Captcha # Captcha
usage_captcha: '&3Per poterti autenticare devi risolvere un captcha, per favore scrivi: /captcha <theCaptcha>' usage_captcha: '&3Per poterti autenticare devi risolvere un captcha, per favore scrivi: /captcha <theCaptcha>'
wrong_captcha: '&cCaptcha sbagliato, per favore riprova scrivendo: "/captcha THE_CAPTCHA" in chat!' wrong_captcha: '&cCaptcha sbagliato, per favore riprova scrivendo "/captcha THE_CAPTCHA" in chat!'
valid_captcha: '&2Il captcha inserito è valido!' valid_captcha: '&2Il captcha inserito è valido!'
# TODO captcha_for_registration: 'To register you have to solve a captcha first, please use the command: /captcha <theCaptcha>' captcha_for_registration: 'Per poterti registrare devi prima risolvere un captcha, per favore scrivi: /captcha <theCaptcha>'
# TODO register_captcha_valid: '&2Valid captcha! You may now register with /register' register_captcha_valid: '&2Il captcha inserito è valido! Ora puoi eseguire la registrazione con: /register <password> <confermaPassword>'
# Codice di verifica # Codice di verifica
verification_code_required: '&3Questo comando va a modificare dati sensibili e richiede una verifica tramite email! Controlla la tua posta in arrivo e segui le istruzioni nell''email.' verification_code_required: '&3Questo comando va a modificare dati sensibili e richiede una verifica tramite email! Controlla la tua posta in arrivo e segui le istruzioni nell''email.'
usage_verification_code: '&cUtilizzo: /verification <codice>' usage_verification_code: '&cUtilizzo: /verification <codice>'
incorrect_verification_code: '&cCodice sbagliato, per favore riprova scrivendo: "/verification <codice>" in chat, usando il codice che hai ricevuto tramite email' incorrect_verification_code: '&cCodice sbagliato, per favore riprova scrivendo "/verification <codice>" in chat, usando il codice che hai ricevuto tramite email'
verification_code_verified: '&2La tua identità è stata verificata! Ora puoi eseguire tutti i comandi che modificano dati sensibili per questa sessione!' verification_code_verified: '&2La tua identità è stata verificata! Ora puoi eseguire tutti i comandi che modificano dati sensibili per questa sessione!'
verification_code_already_verified: '&2Puoi già eseguire tutti i comandi che modificano dati sensibili per questa sessione!' verification_code_already_verified: '&2Puoi già eseguire tutti i comandi che modificano dati sensibili per questa sessione!'
verification_code_expired: '&3Il tuo codice è scaduto! Esegui nuovamente un comando che modifica dati sensibili per ricevere uno nuovo codice!' verification_code_expired: '&3Il tuo codice è scaduto! Esegui nuovamente un comando che modifica dati sensibili per ricevere uno nuovo codice!'

View File

@ -0,0 +1,191 @@
package fr.xephi.authme.permission;
import com.google.common.collect.ImmutableMap;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.permission.handlers.BPermissionsHandler;
import fr.xephi.authme.permission.handlers.LuckPermsHandler;
import fr.xephi.authme.permission.handlers.PermissionHandler;
import fr.xephi.authme.permission.handlers.PermissionsExHandler;
import fr.xephi.authme.permission.handlers.VaultHandler;
import fr.xephi.authme.permission.handlers.ZPermissionsHandler;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import me.lucko.luckperms.LuckPerms;
import me.lucko.luckperms.api.LuckPermsApi;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicesManager;
import org.junit.AssumptionViolatedException;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;
import static com.google.common.collect.Sets.newHashSet;
import static fr.xephi.authme.permission.PermissionsSystemType.B_PERMISSIONS;
import static fr.xephi.authme.permission.PermissionsSystemType.LUCK_PERMS;
import static fr.xephi.authme.permission.PermissionsSystemType.PERMISSIONS_EX;
import static fr.xephi.authme.permission.PermissionsSystemType.VAULT;
import static fr.xephi.authme.permission.PermissionsSystemType.Z_PERMISSIONS;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
/**
* Tests the initialization of {@link PermissionHandler} in {@link PermissionsManager}.
*/
@RunWith(Parameterized.class)
public class PermissionsManagerInitializationTest {
@Parameterized.Parameter(0)
public PermissionsSystemType permissionsSystemType;
@Parameterized.Parameter(1)
public Class<?> expectedHandlerType;
private Settings settings = mock(Settings.class);
private ServicesManager servicesManager = mock(ServicesManager.class);
private Server server = mock(Server.class);
private PluginManager pluginManager = mock(PluginManager.class);
private PermissionsManager permissionsManager = new PermissionsManager(server, pluginManager, settings);
@BeforeClass
public static void setUpLogger() {
TestHelper.setRealLogger();
}
@Before
public void setUp() {
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
given(server.getServicesManager()).willReturn(servicesManager);
}
@Test
public void shouldInitializeHandler() {
// given
setUpForPermissionSystemTest();
given(settings.getProperty(PluginSettings.FORCE_VAULT_HOOK)).willReturn(false);
Plugin plugin = mock(Plugin.class);
given(plugin.isEnabled()).willReturn(true);
given(pluginManager.getPlugin(permissionsSystemType.getPluginName())).willReturn(plugin);
// when
permissionsManager.setup();
// then
PermissionHandler handler = getHandlerFieldValue();
assertThat(handler, instanceOf(expectedHandlerType));
}
@Test
public void shouldInitializeToVaultIfSoConfigured() {
// given
setUpForVault();
given(settings.getProperty(PluginSettings.FORCE_VAULT_HOOK)).willReturn(true);
Plugin plugin = mock(Plugin.class);
given(plugin.isEnabled()).willReturn(true);
given(pluginManager.getPlugin(VAULT.getPluginName())).willReturn(plugin);
// when
permissionsManager.setup();
// then
PermissionHandler handler = getHandlerFieldValue();
assertThat(handler, instanceOf(VaultHandler.class));
verify(pluginManager, only()).getPlugin(VAULT.getPluginName());
}
@Test
public void shouldNotHookIntoDisabledPlugin() {
// given
given(settings.getProperty(PluginSettings.FORCE_VAULT_HOOK)).willReturn(false);
Plugin plugin = mock(Plugin.class);
given(plugin.isEnabled()).willReturn(false);
given(pluginManager.getPlugin(permissionsSystemType.getPluginName())).willReturn(plugin);
// when
permissionsManager.setup();
// then
assertThat(getHandlerFieldValue(), nullValue());
}
@Test
public void shouldCatchInitializationException() {
// given
given(settings.getProperty(PluginSettings.FORCE_VAULT_HOOK)).willReturn(false);
Plugin plugin = mock(Plugin.class);
// Typically we'd expect a PermissionHandler exception further down the line but we can test it easily like this
given(plugin.isEnabled()).willThrow(new IllegalStateException("Some exception occurred"));
given(pluginManager.getPlugin(permissionsSystemType.getPluginName())).willReturn(plugin);
// when
permissionsManager.setup();
// then
assertThat(getHandlerFieldValue(), nullValue());
}
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> createParameters() {
Map<PermissionsSystemType, Class<?>> handlersByPermissionSystemType = ImmutableMap.of(
LUCK_PERMS, LuckPermsHandler.class,
PERMISSIONS_EX, PermissionsExHandler.class,
B_PERMISSIONS, BPermissionsHandler.class,
Z_PERMISSIONS, ZPermissionsHandler.class,
VAULT, VaultHandler.class);
// Verify that all handlers are present -> reminder to add any new entry here as well
if (!handlersByPermissionSystemType.keySet().equals(newHashSet(PermissionsSystemType.values()))) {
throw new IllegalStateException("Test is not set up with all "
+ PermissionsSystemType.class.getSimpleName() + " entries");
}
// Wrap the above map in a Collection<Object[]> to satisfy JUnit
return handlersByPermissionSystemType.entrySet().stream()
.map(e -> new Object[]{ e.getKey(), e.getValue() })
.collect(Collectors.toList());
}
private void setUpForPermissionSystemTest() {
if (permissionsSystemType == LUCK_PERMS) {
LuckPermsApi api = mock(LuckPermsApi.class);
ReflectionTestUtils.setField(LuckPerms.class, null, "instance", api);
} else if (permissionsSystemType == PERMISSIONS_EX) {
throw new AssumptionViolatedException(
"PermissionsEx instance cannot be mocked because of missing dependencies -- skipping");
} else if (permissionsSystemType == Z_PERMISSIONS) {
ZPermissionsService zPermissionsService = mock(ZPermissionsService.class);
given(servicesManager.load(ZPermissionsService.class)).willReturn(zPermissionsService);
} else if (permissionsSystemType == VAULT) {
setUpForVault();
} else if (permissionsSystemType != B_PERMISSIONS) {
throw new IllegalStateException("Unhandled permission systems type: " + permissionsSystemType);
}
}
private void setUpForVault() {
RegisteredServiceProvider<Permission> registeredServiceProvider = mock(RegisteredServiceProvider.class);
given(servicesManager.getRegistration(Permission.class)).willReturn(registeredServiceProvider);
Permission permission = mock(Permission.class);
given(registeredServiceProvider.getProvider()).willReturn(permission);
}
private PermissionHandler getHandlerFieldValue() {
return ReflectionTestUtils.getFieldValue(PermissionsManager.class, permissionsManager, "handler");
}
}