Fix delta handling in JdbcOperationsSessionRepository

See gh-1070
This commit is contained in:
Craig Andrews
2018-05-10 12:44:54 -04:00
committed by Vedran Pavic
parent 836ea12e93
commit 60581c6427
2 changed files with 87 additions and 3 deletions

View File

@@ -733,13 +733,23 @@ public class JdbcOperationsSessionRepository implements
@Override
public void setAttribute(String attributeName, Object attributeValue) {
if (attributeValue == null) {
this.delta.put(attributeName, DeltaValue.REMOVED);
if (this.delta.get(attributeName) == DeltaValue.ADDED) {
this.delta.remove(attributeName);
}
else {
this.delta.put(attributeName, DeltaValue.REMOVED);
}
}
else if (this.delegate.getAttribute(attributeName) != null) {
else if (this.delta.get(attributeName) != DeltaValue.ADDED && this.delegate.getAttribute(attributeName) != null) {
this.delta.put(attributeName, DeltaValue.UPDATED);
}
else {
this.delta.put(attributeName, DeltaValue.ADDED);
if (this.delta.get(attributeName) == DeltaValue.REMOVED) {
this.delta.put(attributeName, DeltaValue.UPDATED);
}
else {
this.delta.put(attributeName, DeltaValue.ADDED);
}
}
this.delegate.setAttribute(attributeName, attributeValue);
if (PRINCIPAL_NAME_INDEX_NAME.equals(attributeName) ||

View File

@@ -43,12 +43,14 @@ import org.springframework.transaction.TransactionDefinition;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
@@ -338,6 +340,23 @@ public class JdbcOperationsSessionRepositoryTests {
verifyZeroInteractions(this.jdbcOperations);
}
@Test
public void saveUpdatedAddSingleAttributeSetTwice() {
JdbcOperationsSessionRepository.JdbcSession session = this.repository.new JdbcSession("primaryKey",
new MapSession());
session.setAttribute("testName", "testValue");
session.setAttribute("testName", "testValue");
this.repository.save(session);
assertThat(session.isNew()).isFalse();
assertPropagationRequiresNew();
verify(this.jdbcOperations, times(1)).update(
startsWith("INSERT INTO SPRING_SESSION_ATTRIBUTES("),
isA(PreparedStatementSetter.class));
verifyZeroInteractions(this.jdbcOperations);
}
@Test
public void saveUpdatedAddMultipleAttributes() {
JdbcOperationsSessionRepository.JdbcSession session = this.repository.new JdbcSession("primaryKey",
@@ -431,6 +450,61 @@ public class JdbcOperationsSessionRepositoryTests {
verifyZeroInteractions(this.jdbcOperations);
}
@Test
public void saveUpdatedAddThenRemoveSingleAttribute() {
JdbcOperationsSessionRepository.JdbcSession session = this.repository.new JdbcSession("primaryKey",
new MapSession());
session.setAttribute("testName", "testValue");
session.removeAttribute("testName");
this.repository.save(session);
assertThat(session.isNew()).isFalse();
assertPropagationRequiresNew();
verify(this.jdbcOperations, never()).update(
anyString(),
isA(PreparedStatementSetter.class));
verifyZeroInteractions(this.jdbcOperations);
}
@Test
public void saveUpdatedModifyThenRemoveSingleAttribute() {
JdbcOperationsSessionRepository.JdbcSession session = this.repository.new JdbcSession("primaryKey",
new MapSession());
session.setAttribute("testName", "testValue");
session.clearChangeFlags();
session.setAttribute("testName", "testValueModifed");
session.removeAttribute("testName");
this.repository.save(session);
assertThat(session.isNew()).isFalse();
assertPropagationRequiresNew();
verify(this.jdbcOperations, times(1)).update(
startsWith("DELETE FROM SPRING_SESSION_ATTRIBUTES WHERE"),
isA(PreparedStatementSetter.class));
verifyZeroInteractions(this.jdbcOperations);
}
@Test
public void saveUpdatedRemoveThenModifySingleAttribute() {
JdbcOperationsSessionRepository.JdbcSession session = this.repository.new JdbcSession("primaryKey",
new MapSession());
session.setAttribute("testName", "testValue");
session.clearChangeFlags();
session.removeAttribute("testName");
session.setAttribute("testName", "testValueModifed");
this.repository.save(session);
assertThat(session.isNew()).isFalse();
assertPropagationRequiresNew();
verify(this.jdbcOperations, times(1)).update(
startsWith("UPDATE SPRING_SESSION_ATTRIBUTES SET"),
isA(PreparedStatementSetter.class));
verifyZeroInteractions(this.jdbcOperations);
}
@Test
public void saveUpdatedLastAccessedTime() {
JdbcOperationsSessionRepository.JdbcSession session = this.repository.new JdbcSession("primaryKey",