ConfigTest.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package me.lortseam.completeconfig.data;
  2. import me.lortseam.completeconfig.api.ConfigContainer;
  3. import me.lortseam.completeconfig.test.data.containers.EmptyContainer;
  4. import nl.altindag.log.LogCaptor;
  5. import org.junit.jupiter.api.AfterEach;
  6. import org.junit.jupiter.api.BeforeEach;
  7. import org.junit.jupiter.api.Nested;
  8. import org.junit.jupiter.api.Test;
  9. import static org.assertj.core.api.Assertions.assertThat;
  10. import static org.junit.jupiter.api.Assertions.*;
  11. public class ConfigTest {
  12. @Test
  13. public void builder_throwExceptionIfModIDNull() {
  14. NullPointerException exception = assertThrows(NullPointerException.class, () -> Config.builder(null));
  15. assertEquals("modID is marked non-null but is null", exception.getMessage());
  16. }
  17. @Nested
  18. public class Builder {
  19. private static final String MOD_ID = "test";
  20. private Config.Builder builder;
  21. private final LogCaptor logCaptor = LogCaptor.forName("CompleteConfig");
  22. @BeforeEach
  23. public void beforeEach() {
  24. builder = Config.builder(MOD_ID);
  25. }
  26. @AfterEach
  27. public void afterEach() {
  28. logCaptor.clearLogs();
  29. }
  30. @Test
  31. public void setBranch_throwIfBranchNull() {
  32. NullPointerException exception = assertThrows(NullPointerException.class, () -> builder.setBranch(null));
  33. assertEquals("branch is marked non-null but is null", exception.getMessage());
  34. }
  35. @Test
  36. public void setBranch_throwIfBranchContainsNullElement() {
  37. assertThrows(NullPointerException.class, () -> builder.setBranch(new String[]{null}));
  38. }
  39. @Test
  40. public void add_throwIfContainersNull() {
  41. NullPointerException exception = assertThrows(NullPointerException.class, () -> builder.add((ConfigContainer[]) null));
  42. assertEquals("containers is marked non-null but is null", exception.getMessage());
  43. }
  44. @Test
  45. public void add_throwIfContainersContainNullElement() {
  46. assertThrows(NullPointerException.class, () -> builder.add((ConfigContainer) null));
  47. }
  48. @Test
  49. public void build_logWarningAndReturnNullIfChildrenEmpty() {
  50. assertNull(builder.build());
  51. assertThat(logCaptor.getWarnLogs()).contains("Mod " + MOD_ID + " tried to create an empty config");
  52. }
  53. @Test
  54. public void build_logWarningAndReturnNullIfEmpty() {
  55. assertNull(builder.add(new EmptyContainer()).build());
  56. assertThat(logCaptor.getWarnLogs()).containsExactly("Config of ConfigSource(modID=" + MOD_ID + ", branch=[]) is empty");
  57. }
  58. }
  59. }