BaseCollectionTest.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package me.lortseam.completeconfig.data;
  2. import com.google.common.collect.Iterables;
  3. import me.lortseam.completeconfig.api.ConfigContainer;
  4. import me.lortseam.completeconfig.data.text.TranslationIdentifier;
  5. import me.lortseam.completeconfig.exception.IllegalAnnotationTargetException;
  6. import me.lortseam.completeconfig.test.data.containers.*;
  7. import me.lortseam.completeconfig.test.data.groups.EmptyGroup;
  8. import me.lortseam.completeconfig.test.data.listeners.EmptyListener;
  9. import me.lortseam.completeconfig.test.data.listeners.SetterListener;
  10. import nl.altindag.log.LogCaptor;
  11. import org.junit.jupiter.api.AfterEach;
  12. import org.junit.jupiter.api.BeforeEach;
  13. import org.junit.jupiter.api.Test;
  14. import static org.assertj.core.api.Assertions.assertThat;
  15. import static org.junit.jupiter.api.Assertions.*;
  16. import static org.mockito.Mockito.mock;
  17. public class BaseCollectionTest {
  18. private BaseCollection baseCollection;
  19. private final LogCaptor logCaptor = LogCaptor.forRoot();
  20. @BeforeEach
  21. public void beforeEach() {
  22. baseCollection = new BaseCollection(mock(TranslationIdentifier.class)) {};
  23. }
  24. @AfterEach
  25. public void afterEach() {
  26. logCaptor.clearLogs();
  27. }
  28. @Test
  29. public void resolve_includeFieldIfAnnotated() {
  30. baseCollection.resolve(new ContainerWithEntry(), new ContainerWithContainerWithEntry(), new ContainerWithGroupWithEntry());
  31. assertEquals(2, baseCollection.getEntries().size());
  32. assertEquals(1, baseCollection.getCollections().size());
  33. }
  34. @Test
  35. public void resolve_excludeFieldIfNotAnnotated() {
  36. baseCollection.resolve(new ContainerWithField());
  37. assertTrue(baseCollection.isEmpty());
  38. }
  39. @Test
  40. public void resolve_includeFieldInEntries() {
  41. baseCollection.resolve(new EntriesContainerWithEntry());
  42. assertEquals(1, baseCollection.getEntries().size());
  43. }
  44. @Test
  45. public void resolve_excludeFieldInEntriesIfContainer() {
  46. baseCollection.resolve(new EntriesContainerWithEmptyContainer());
  47. assertTrue(baseCollection.isEmpty());
  48. }
  49. @Test
  50. public void resolve_excludeFieldInEntriesIfIgnoreAnnotated() {
  51. baseCollection.resolve(new EntriesContainerWithIgnoredField());
  52. assertTrue(baseCollection.isEmpty());
  53. }
  54. @Test
  55. public void resolve_excludeFieldInEntriesIfTransient() {
  56. baseCollection.resolve(new EntriesContainerWithTransientField());
  57. assertTrue(baseCollection.isEmpty());
  58. }
  59. @Test
  60. public void resolve_includeSuperclassFieldIfNonStatic() {
  61. baseCollection.resolve(new SubclassOfContainerWithEntry(), new SubclassOfContainerWithContainerWithEntry());
  62. assertEquals(2, baseCollection.getEntries().size());
  63. }
  64. @Test
  65. public void resolve_excludeSuperclassFieldIfStatic() {
  66. baseCollection.resolve(new SubclassOfContainerWithStaticEntry(), new SubclassOfContainerWithStaticContainerWithEntry());
  67. assertTrue(baseCollection.isEmpty());
  68. }
  69. @Test
  70. public void resolve_includeFromMethod() {
  71. baseCollection.resolve(new ContainerIncludingContainerWithEntry(), new ContainerIncludingGroupWithEntry());
  72. assertEquals(1, baseCollection.getEntries().size());
  73. assertEquals(1, baseCollection.getCollections().size());
  74. }
  75. @Test
  76. public void resolve_includeNestedIfStatic() {
  77. baseCollection.resolve(new ContainerNestingStaticContainerWithEntry());
  78. assertEquals(1, baseCollection.getEntries().size());
  79. }
  80. @Test
  81. public void resolve_throwIfNestedNonContainer() {
  82. IllegalAnnotationTargetException exception = assertThrows(IllegalAnnotationTargetException.class, () -> baseCollection.resolve(new ContainerNestingStaticClass()));
  83. assertEquals("Transitive " + ContainerNestingStaticClass.Class.class + " must implement " + ConfigContainer.class.getSimpleName(), exception.getMessage());
  84. }
  85. @Test
  86. public void resolve_throwIfNestedNonStatic() {
  87. IllegalAnnotationTargetException exception = assertThrows(IllegalAnnotationTargetException.class, () -> baseCollection.resolve(new ContainerNestingContainerWithEntry()));
  88. assertEquals("Transitive " + ContainerNestingContainerWithEntry.ContainerWithEntry.class + " must be static", exception.getMessage());
  89. }
  90. @Test
  91. public void resolve_logWarningIfEmpty() {
  92. baseCollection.resolve(new EmptyGroup());
  93. assertThat(logCaptor.getWarnLogs()).contains("[CompleteConfig] Group emptyGroup is empty");
  94. }
  95. @Test
  96. public void resolve_listenSetter() {
  97. SetterListener listener = new SetterListener();
  98. baseCollection.resolve(listener);
  99. boolean value = !listener.getValue();
  100. Iterables.getOnlyElement(baseCollection.getEntries()).setValue(value);
  101. assertEquals(value, listener.getValue());
  102. }
  103. @Test
  104. public void resolve_doNotUpdateListenerField() {
  105. EmptyListener listener = new EmptyListener();
  106. baseCollection.resolve(listener);
  107. boolean oldValue = listener.getValue();
  108. Iterables.getOnlyElement(baseCollection.getEntries()).setValue(!oldValue);
  109. assertEquals(oldValue, listener.getValue());
  110. }
  111. }