BAEL-1911 - Fixing author's review comments (#4782)
* Strange git issue with README.MD, wouldn't revert the file * Fixing review comments * BAEL-1911 Refactored SQL code, fixed formatting
This commit is contained in:
parent
3205de68c1
commit
a030afa5ea
@ -10,6 +10,10 @@
|
||||
<name>spring-session-jdbc</name>
|
||||
<description>Spring Session with JDBC tutorial</description>
|
||||
|
||||
<properties>
|
||||
<h2.version>1.4.197</h2.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
@ -18,25 +22,18 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.session</groupId>
|
||||
<artifactId>spring-session-core</artifactId>
|
||||
<artifactId>spring-session-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@ -44,14 +41,6 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.session</groupId>
|
||||
<artifactId>spring-session-jdbc</artifactId>
|
||||
</dependency>
|
||||
<!--<dependency>-->
|
||||
<!--<groupId>org.springframework.boot</groupId>-->
|
||||
<!--<artifactId>spring-boot-starter-security</artifactId>-->
|
||||
<!--</dependency>-->
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@SpringBootApplication
|
||||
public class SpringSessionJdbcApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringSessionJdbcApplication.class, args);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringSessionJdbcApplication.class, args);
|
||||
}
|
||||
}
|
||||
|
@ -24,20 +24,19 @@ public class SpringSessionJdbcController {
|
||||
}
|
||||
|
||||
@PostMapping("/saveColor")
|
||||
public String saveMessage(@RequestParam("color") String color,
|
||||
HttpServletRequest request) {
|
||||
public String saveMessage(@RequestParam("color") String color, HttpServletRequest request) {
|
||||
List<String> favoriteColors = getFavColors(request.getSession());
|
||||
if (!StringUtils.isEmpty(color)) {
|
||||
favoriteColors.add(color);
|
||||
request.getSession().
|
||||
setAttribute("favoriteColors", favoriteColors);
|
||||
request
|
||||
.getSession()
|
||||
.setAttribute("favoriteColors", favoriteColors);
|
||||
}
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
private List<String> getFavColors(HttpSession session) {
|
||||
List<String> favoriteColors = (List<String>) session.
|
||||
getAttribute("favoriteColors");
|
||||
List<String> favoriteColors = (List<String>) session.getAttribute("favoriteColors");
|
||||
if (favoriteColors == null) {
|
||||
favoriteColors = new ArrayList<>();
|
||||
}
|
||||
|
@ -1,9 +1,3 @@
|
||||
spring.session.store-type=jdbc
|
||||
#spring.session.jdbc.initialize-schema=embedded
|
||||
#spring.session.jdbc.table-name=SPRING_SESSION
|
||||
#server.servlet.session.timeout=60s
|
||||
#spring.datasource.url=jdbc:h2:mem:AZ
|
||||
#spring.security.user.name=admin
|
||||
#spring.security.user.password=secret
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2-console
|
@ -1,17 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Spring Session JDBC</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<form th:action="@{/saveColor}" method="post">
|
||||
<input name="color" type="text" placeholder="Enter your favorite color"/>
|
||||
<input type="submit" value="Save"/>
|
||||
</form>
|
||||
</div>
|
||||
<p>Session ID - <span th:text="${sessionId}"/></p>
|
||||
<p>My favorite color(s) - <span th:text="${favoriteColors}"/></p>
|
||||
</body>
|
||||
</html>
|
@ -1,16 +0,0 @@
|
||||
package com.baeldung.springsessionjdbc;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringSessionJdbcApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
package com.baeldung.springsessionjdbc;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInput;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class SpringSessionJdbcIntegrationTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate testRestTemplate;
|
||||
|
||||
@Before
|
||||
public void setup() throws ClassNotFoundException {
|
||||
Class.forName("org.h2.Driver");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenApiHasStarted_whenH2DbIsQueried_thenSessionTablesAreEmpty() throws SQLException {
|
||||
Assert.assertEquals(0, getSessionIdsFromDatabase().size());
|
||||
Assert.assertEquals(0, getSessionAttributeBytesFromDatabase().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGetInvoked_whenH2DbIsQueried_thenOneSessionIsCreated() throws SQLException {
|
||||
assertThat(this.testRestTemplate.getForObject("http://localhost:" + port + "/", String.class)).isNotEmpty();
|
||||
Assert.assertEquals(1, getSessionIdsFromDatabase().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPostInvoked_whenH2DbIsQueried_thenSessionAttributeIsRetrieved() throws ClassNotFoundException, SQLException, IOException {
|
||||
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
||||
map.add("color", "red");
|
||||
this.testRestTemplate.postForObject("http://localhost:" + port + "/saveColor", map, String.class);
|
||||
List<byte[]> queryResponse = getSessionAttributeBytesFromDatabase();
|
||||
Assert.assertEquals(1, queryResponse.size());
|
||||
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(queryResponse.get(0)));
|
||||
List<String> obj = (List<String>) in.readObject(); //Deserialize byte[] to object
|
||||
Assert.assertEquals("red", obj.get(0));
|
||||
}
|
||||
|
||||
private List<String> getSessionIdsFromDatabase() throws SQLException {
|
||||
List<String> result = new ArrayList<>();
|
||||
ResultSet rs = getResultSet("SELECT * FROM SPRING_SESSION");
|
||||
while (rs.next()) {
|
||||
result.add(rs.getString("SESSION_ID"));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<byte[]> getSessionAttributeBytesFromDatabase() throws SQLException {
|
||||
List<byte[]> result = new ArrayList<>();
|
||||
ResultSet rs = getResultSet("SELECT * FROM SPRING_SESSION_ATTRIBUTES");
|
||||
while (rs.next()) {
|
||||
result.add(rs.getBytes("ATTRIBUTE_BYTES"));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private ResultSet getResultSet(String sql) throws SQLException {
|
||||
Connection conn = DriverManager.getConnection("jdbc:h2:mem:testdb", "sa", "");
|
||||
Statement stat = conn.createStatement();
|
||||
return stat.executeQuery(sql);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user