Upgrade to Spring Boot 2.4.5 and latest Spring Session.

This commit is contained in:
Greg L. Turnquist
2021-05-19 12:03:18 -05:00
parent 87e4d5d533
commit 9c1ed981a6
11 changed files with 125 additions and 123 deletions

View File

@@ -12,7 +12,6 @@
<groupId>org.springframework.session.data.mongodb</groupId>
<artifactId>spring-session-data-mongodb-reactive-boot</artifactId>
<packaging>jar</packaging>
<name>Spring Session MongoDB - Examples - Reactive Spring Boot</name>
@@ -34,11 +33,7 @@
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -16,16 +16,54 @@
package org.springframework.session.mongodb.examples;
import lombok.Data;
import java.util.Objects;
/**
* @author Rob Winch
* @author Greg Turnquist
* @since 5.0
*/
@Data
public class SessionAttributeForm {
private String attributeName;
private String attributeValue;
public String getAttributeName() {
return attributeName;
}
public void setAttributeName(String attributeName) {
this.attributeName = attributeName;
}
public String getAttributeValue() {
return attributeValue;
}
public void setAttributeValue(String attributeValue) {
this.attributeValue = attributeValue;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof SessionAttributeForm))
return false;
SessionAttributeForm that = (SessionAttributeForm) o;
return Objects.equals(attributeName, that.attributeName) && Objects.equals(attributeValue, that.attributeValue);
}
@Override
public int hashCode() {
return Objects.hash(attributeName, attributeValue);
}
@Override
public String toString() {
return "SessionAttributeForm{" + "attributeName='" + attributeName + '\'' + ", attributeValue='" + attributeValue
+ '\'' + '}';
}
}

View File

@@ -40,7 +40,7 @@ public class SessionController {
@GetMapping("/")
public String index(Model model, WebSession webSession) {
model.addAttribute("webSession", webSession);
return "index";
}

View File

@@ -16,73 +16,69 @@
package org.springframework.session.mongodb.examples;
import static org.assertj.core.api.Assertions.*;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.session.mongodb.examples.pages.HomePage;
import org.springframework.session.mongodb.examples.pages.HomePage.Attribute;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* @author Eddú Meléndez
* @author Rob Winch
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AttributeTests {
@LocalServerPort
int port;
@LocalServerPort int port;
private WebDriver driver;
@Before
@BeforeEach
public void setup() {
this.driver = new HtmlUnitDriver();
}
@After
@AfterEach
public void tearDown() {
this.driver.quit();
}
@Test
public void home() {
HomePage home = HomePage.go(this.driver, this.port);
home.assertAt();
}
@Test
public void noAttributes() {
HomePage home = HomePage.go(this.driver, this.port);
assertThat(home.attributes()).isEmpty();
}
@Test
public void createAttribute() {
HomePage home = HomePage.go(this.driver, this.port);
// @formatter:off
home = home.form()
.attributeName("a")
.attributeValue("b")
.submit(HomePage.class);
// @formatter:on
home = home.form().attributeName("a").attributeValue("b").submit(HomePage.class);
List<Attribute> attributes = home.attributes();
assertThat(attributes).hasSize(1);
Attribute row = attributes.get(0);
assertThat(row.getAttributeName()).isEqualTo("a");
assertThat(row.getAttributeValue()).isEqualTo("b");
}
}

View File

@@ -20,6 +20,7 @@ import static org.assertj.core.api.Assertions.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
@@ -45,16 +46,19 @@ public class HomePage {
List<Attribute> attributes;
public HomePage(WebDriver driver) {
this.driver = driver;
this.attributes = new ArrayList<>();
}
private static void get(WebDriver driver, int port, String get) {
String baseUrl = "http://localhost:" + port;
driver.get(baseUrl + get);
}
public static HomePage go(WebDriver driver, int port) {
get(driver, port, "/");
return PageFactory.initElements(driver, HomePage.class);
}
@@ -64,11 +68,13 @@ public class HomePage {
}
public List<Attribute> attributes() {
List<Attribute> rows = new ArrayList<>();
for (WebElement tr : this.trs) {
rows.add(new Attribute(tr));
}
List<Attribute> rows = this.trs.stream() //
.map(Attribute::new) //
.collect(Collectors.toList());
this.attributes.addAll(rows);
return this.attributes;
}
@@ -77,6 +83,7 @@ public class HomePage {
}
public class Form {
@FindBy(name = "attributeName")
WebElement attributeName;
@@ -91,22 +98,26 @@ public class HomePage {
}
public Form attributeName(String text) {
this.attributeName.sendKeys(text);
return this;
}
public Form attributeValue(String text) {
this.attributeValue.sendKeys(text);
return this;
}
public <T> T submit(Class<T> page) {
this.submit.click();
return PageFactory.initElements(HomePage.this.driver, page);
}
}
public static class Attribute {
@FindBy(xpath = ".//td[1]")
WebElement attributeName;