|
| 1 | +package io.teaql.core; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertNotSame; |
| 6 | +import static org.junit.Assert.assertNull; |
| 7 | +import static org.junit.Assert.assertSame; |
| 8 | +import static org.junit.Assert.assertTrue; |
| 9 | + |
| 10 | +import java.util.Set; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +public class ChangeSetStackTest { |
| 14 | + |
| 15 | + private static final EntityKey ORDER = new EntityKey("Order", 1L); |
| 16 | + private static final EntityKey OTHER_ORDER = new EntityKey("Order", 2L); |
| 17 | + |
| 18 | + @Test |
| 19 | + public void emptyStackHasNoCurrentOrPoppedChangeSet() { |
| 20 | + ChangeSetStack stack = new ChangeSetStack(); |
| 21 | + |
| 22 | + assertNull(stack.current()); |
| 23 | + assertNull(stack.pop()); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void firstSetLazilyCreatesCurrentChangeSet() { |
| 28 | + ChangeSetStack stack = new ChangeSetStack(); |
| 29 | + |
| 30 | + stack.set(ORDER, "status", "CREATED"); |
| 31 | + |
| 32 | + assertNotNull(stack.current()); |
| 33 | + assertEquals("CREATED", stack.current().get(ORDER, "status")); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void topScopeShadowsLowerScopeAndPopRestoresIt() { |
| 38 | + ChangeSetStack stack = new ChangeSetStack(); |
| 39 | + stack.set(ORDER, "status", "CREATED"); |
| 40 | + stack.push(); |
| 41 | + stack.set(ORDER, "status", "PAID"); |
| 42 | + |
| 43 | + assertEquals("PAID", stack.get(ORDER, "status")); |
| 44 | + |
| 45 | + EntityChangeSet popped = stack.pop(); |
| 46 | + |
| 47 | + assertEquals("PAID", popped.get(ORDER, "status")); |
| 48 | + assertEquals("CREATED", stack.get(ORDER, "status")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void clearCurrentClearsOnlyTopScope() { |
| 53 | + ChangeSetStack stack = new ChangeSetStack(); |
| 54 | + stack.set(ORDER, "status", "CREATED"); |
| 55 | + stack.push(); |
| 56 | + EntityChangeSet top = stack.current(); |
| 57 | + stack.set(ORDER, "status", "PAID"); |
| 58 | + |
| 59 | + stack.clearCurrent(); |
| 60 | + |
| 61 | + assertEquals("CREATED", stack.get(ORDER, "status")); |
| 62 | + assertTrue(stack.current().isEmpty()); |
| 63 | + assertNotSame(top, stack.current()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void changedFieldNamesAreCombinedAcrossScopes() { |
| 68 | + ChangeSetStack stack = new ChangeSetStack(); |
| 69 | + stack.set(ORDER, "status", "CREATED"); |
| 70 | + stack.push(); |
| 71 | + stack.set(ORDER, "total", 100L); |
| 72 | + stack.set(OTHER_ORDER, "comment", "other"); |
| 73 | + |
| 74 | + assertEquals(Set.of("status", "total"), stack.changedFieldNames(ORDER)); |
| 75 | + assertEquals(Set.of("comment"), stack.changedFieldNames(OTHER_ORDER)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void clearEntityRemovesItFromEveryScopeOnly() { |
| 80 | + ChangeSetStack stack = new ChangeSetStack(); |
| 81 | + stack.set(ORDER, "status", "CREATED"); |
| 82 | + stack.set(OTHER_ORDER, "status", "CREATED"); |
| 83 | + stack.push(); |
| 84 | + stack.set(ORDER, "total", 100L); |
| 85 | + stack.set(OTHER_ORDER, "total", 200L); |
| 86 | + |
| 87 | + stack.clearEntity(ORDER); |
| 88 | + |
| 89 | + assertNull(stack.get(ORDER, "status")); |
| 90 | + assertNull(stack.get(ORDER, "total")); |
| 91 | + assertEquals("CREATED", stack.get(OTHER_ORDER, "status")); |
| 92 | + assertEquals(200L, stack.get(OTHER_ORDER, "total")); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void pushCreatesASeparateCurrentChangeSet() { |
| 97 | + ChangeSetStack stack = new ChangeSetStack(); |
| 98 | + EntityChangeSet first = stack.currentMut(); |
| 99 | + |
| 100 | + stack.push(); |
| 101 | + |
| 102 | + assertSame(stack.current(), stack.currentMut()); |
| 103 | + assertNotSame(first, stack.current()); |
| 104 | + } |
| 105 | +} |
0 commit comments