Skip to content

JdbcOperationsSessionRepository doesn't resolve delta to correct SQL statement #1070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -733,13 +733,23 @@ public Set<String> getAttributeNames() {
@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) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@
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;
Expand Down Expand Up @@ -338,6 +340,23 @@ public void saveUpdatedAddSingleAttribute() {
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",
Expand Down Expand Up @@ -431,6 +450,61 @@ public void saveUpdatedRemoveMultipleAttributes() {
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",
Expand Down