Moved Bungee tests to JUnit 5

This commit is contained in:
Rsl1122 2019-08-10 08:15:43 +03:00
parent 6a9fe25f14
commit b5fb947724
4 changed files with 103 additions and 103 deletions

View File

@ -21,45 +21,44 @@ import com.djrapitops.plan.db.SQLiteDB;
import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.DatabaseSettings;
import com.djrapitops.plan.system.settings.paths.ProxySettings;
import com.djrapitops.plan.system.settings.paths.WebserverSettings;
import com.google.common.util.concurrent.MoreExecutors;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import rules.BungeeComponentMocker;
import rules.ComponentMocker;
import utilities.CIProperties;
import utilities.DBPreparer;
import utilities.RandomData;
import utilities.mocks.BungeeMockComponent;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
/**
* Test for Bungee PlanSystem.
*
* @author Rsl1122
*/
@RunWith(MockitoJUnitRunner.Silent.class)
@RunWith(JUnitPlatform.class)
public class BungeeSystemTest {
@ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
private final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
@Rule
public ComponentMocker component = new BungeeComponentMocker(temporaryFolder);
@Rule
public ExpectedException thrown = ExpectedException.none();
private BungeeMockComponent component;
private DBPreparer dbPreparer;
@BeforeEach
void prepareSystem(@TempDir Path temp) throws Exception {
component = new BungeeMockComponent(temp);
dbPreparer = new DBPreparer(component.getPlanSystem(), TEST_PORT_NUMBER);
}
@Test
public void bungeeEnables() throws Exception {
void bungeeEnables() throws Exception {
PlanSystem bungeeSystem = component.getPlanSystem();
try {
PlanConfig config = bungeeSystem.getConfigSystem().getConfig();
@ -79,56 +78,51 @@ public class BungeeSystemTest {
}
@Test
public void bungeeDoesNotEnableWithDefaultIP() throws Exception {
thrown.expect(EnableException.class);
thrown.expectMessage("IP setting still 0.0.0.0 - Configure AlternativeIP/IP that connects to the Proxy server.");
void bungeeDoesNotEnableWithDefaultIP() {
EnableException thrown = assertThrows(EnableException.class, () -> {
PlanSystem bungeeSystem = component.getPlanSystem();
try {
PlanConfig config = bungeeSystem.getConfigSystem().getConfig();
config.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
config.set(ProxySettings.IP, "0.0.0.0");
PlanSystem bungeeSystem = component.getPlanSystem();
try {
PlanConfig config = bungeeSystem.getConfigSystem().getConfig();
config.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
config.set(ProxySettings.IP, "0.0.0.0");
DBSystem dbSystem = bungeeSystem.getDatabaseSystem();
SQLiteDB db = dbSystem.getSqLiteFactory().usingDefaultFile();
db.setTransactionExecutorServiceProvider(MoreExecutors::newDirectExecutorService);
dbSystem.setActiveDatabase(db);
DBSystem dbSystem = bungeeSystem.getDatabaseSystem();
SQLiteDB db = dbSystem.getSqLiteFactory().usingDefaultFile();
db.setTransactionExecutorServiceProvider(MoreExecutors::newDirectExecutorService);
dbSystem.setActiveDatabase(db);
bungeeSystem.enable(); // Throws EnableException
} finally {
bungeeSystem.disable();
}
});
bungeeSystem.enable(); // Throws EnableException
} finally {
bungeeSystem.disable();
}
assertEquals("IP setting still 0.0.0.0 - Configure AlternativeIP/IP that connects to the Proxy server.", thrown.getMessage());
}
@Test
public void testEnableNoMySQL() throws EnableException {
thrown.expect(EnableException.class);
void testEnableNoMySQL() {
assertThrows(EnableException.class, () -> {
PlanSystem bungeeSystem = component.getPlanSystem();
try {
PlanConfig config = bungeeSystem.getConfigSystem().getConfig();
config.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
config.set(ProxySettings.IP, "8.8.8.8");
PlanSystem bungeeSystem = component.getPlanSystem();
try {
PlanConfig config = bungeeSystem.getConfigSystem().getConfig();
config.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
config.set(ProxySettings.IP, "8.8.8.8");
bungeeSystem.enable(); // Throws EnableException
} finally {
bungeeSystem.disable();
}
bungeeSystem.enable(); // Throws EnableException
} finally {
bungeeSystem.disable();
}
});
}
@Test
public void testEnableWithMySQL() throws EnableException {
boolean isCI = Boolean.parseBoolean(System.getenv(CIProperties.IS_CI_SERVICE));
assumeTrue(isCI);
void testEnableWithMySQL() throws Exception {
PlanSystem bungeeSystem = component.getPlanSystem();
try {
PlanConfig config = bungeeSystem.getConfigSystem().getConfig();
config.set(DatabaseSettings.MYSQL_DATABASE, "Plan");
config.set(DatabaseSettings.MYSQL_USER, "travis");
config.set(DatabaseSettings.MYSQL_PASS, "");
config.set(DatabaseSettings.MYSQL_HOST, "127.0.0.1");
config.set(DatabaseSettings.TYPE, "MySQL");
// MySQL settings might not be available.
assumeTrue(dbPreparer.setUpMySQLSettings(config).isPresent());
config.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
config.set(ProxySettings.IP, "8.8.8.8");

View File

@ -14,44 +14,47 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package rules;
package utilities.mocks;
import com.djrapitops.plan.DaggerPlanBungeeComponent;
import com.djrapitops.plan.PlanBungee;
import com.djrapitops.plan.PlanBungeeComponent;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.system.PlanSystem;
import org.junit.rules.ExternalResource;
import org.junit.rules.TemporaryFolder;
import utilities.mocks.PlanBungeeMocker;
public class BungeeComponentMocker extends ExternalResource implements ComponentMocker {
import java.nio.file.Path;
private final TemporaryFolder testFolder;
/**
* Test utility for creating a dagger PlanComponent using a mocked PlanBungee.
*
* @author Rsl1122
*/
public class BungeeMockComponent {
private final Path tempDir;
private PlanBungee planMock;
private PlanBungeeComponent component;
public BungeeComponentMocker(TemporaryFolder testFolder) {
this.testFolder = testFolder;
public BungeeMockComponent(Path tempDir) {
this.tempDir = tempDir;
}
@Override
protected void before() throws Throwable {
PlanBungeeMocker mocker = PlanBungeeMocker.setUp()
.withDataFolder(testFolder.newFolder())
.withPluginDescription()
.withResourceFetchingFromJar()
.withProxy();
planMock = mocker.getPlanMock();
component = DaggerPlanBungeeComponent.builder().plan(planMock).build();
}
public PlanPlugin getPlanMock() {
public PlanBungee getPlanMock() throws Exception {
if (planMock == null) {
planMock = PlanBungeeMocker.setUp()
.withDataFolder(tempDir.toFile())
.withResourceFetchingFromJar()
.withProxy()
.withPluginDescription()
.getPlanMock();
}
return planMock;
}
public PlanSystem getPlanSystem() {
public PlanSystem getPlanSystem() throws Exception {
if (component == null) {
component = DaggerPlanBungeeComponent.builder().plan(getPlanMock()).build();
}
return component.system();
}
}
}

View File

@ -83,23 +83,18 @@ public class PlanBungeeMocker extends Mocker {
return this;
}
public PlanBungeeMocker withDataFolder(File tempFolder) {
PlanBungeeMocker withDataFolder(File tempFolder) {
when(planMock.getDataFolder()).thenReturn(tempFolder);
return this;
}
public PlanBungeeMocker withResourceFetchingFromJar() throws Exception {
PlanBungeeMocker withResourceFetchingFromJar() throws Exception {
withPluginFiles();
return this;
}
@Deprecated
public PlanBungeeMocker withLogging() {
return this;
}
@SuppressWarnings("deprecation")
public PlanBungeeMocker withProxy() {
PlanBungeeMocker withProxy() {
ProxyServer proxyMock = Mockito.mock(ProxyServer.class);
doReturn("1.12.2").when(proxyMock).getVersion();
@ -117,7 +112,7 @@ public class PlanBungeeMocker extends Mocker {
return this;
}
public PlanBungeeMocker withPluginDescription() {
PlanBungeeMocker withPluginDescription() {
File pluginYml = getFile("/bungee.yml");
HashSet<String> empty = new HashSet<>();
PluginDescription pluginDescription = new PluginDescription("Plan", "", "9.9.9", "Rsl1122", empty, empty, pluginYml, "");
@ -125,7 +120,7 @@ public class PlanBungeeMocker extends Mocker {
return this;
}
public PlanBungee getPlanMock() {
PlanBungee getPlanMock() {
return planMock;
}
}

View File

@ -65,7 +65,7 @@ public class DBPreparer {
return db;
}
public Optional<Database> prepareMySQL() throws EnableException {
public Optional<String> setUpMySQLSettings(PlanConfig config) {
String database = System.getenv(CIProperties.MYSQL_DATABASE);
String user = System.getenv(CIProperties.MYSQL_USER);
String pass = System.getenv(CIProperties.MYSQL_PASS);
@ -80,22 +80,30 @@ public class DBPreparer {
String dbName = DBType.MYSQL.getName();
PlanConfig config = system.getConfigSystem().getConfig();
config.set(DatabaseSettings.MYSQL_DATABASE, formattedDatabase);
config.set(DatabaseSettings.MYSQL_USER, user);
config.set(DatabaseSettings.MYSQL_PASS, pass);
config.set(DatabaseSettings.MYSQL_HOST, "127.0.0.1");
config.set(DatabaseSettings.TYPE, dbName);
return Optional.of(formattedDatabase);
}
SQLDB mysql = prepareDBByName(dbName);
mysql.executeTransaction(new Transaction() {
@Override
protected void performOperations() {
execute("DROP DATABASE " + formattedDatabase);
execute("CREATE DATABASE " + formattedDatabase);
execute("USE " + formattedDatabase);
}
});
return Optional.of(mysql);
public Optional<Database> prepareMySQL() throws EnableException {
PlanConfig config = system.getConfigSystem().getConfig();
Optional<String> formattedDB = setUpMySQLSettings(config);
if (formattedDB.isPresent()) {
String formattedDatabase = formattedDB.get();
SQLDB mysql = prepareDBByName(DBType.MYSQL.getName());
mysql.executeTransaction(new Transaction() {
@Override
protected void performOperations() {
execute("DROP DATABASE " + formattedDatabase);
execute("CREATE DATABASE " + formattedDatabase);
execute("USE " + formattedDatabase);
}
});
return Optional.of(mysql);
}
return Optional.empty();
}
}