JAVA-12731: Move spring-session into spring-web-modules

This commit is contained in:
sampadawagde
2022-06-30 22:13:57 +05:30
parent 670cc728f8
commit 22d38db84a
29 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package com.baeldung.springsessionjdbc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringSessionJdbcApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSessionJdbcApplication.class, args);
}
}

View File

@@ -0,0 +1,45 @@
package com.baeldung.springsessionjdbc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
@Controller
public class SpringSessionJdbcController {
@GetMapping("/")
public String index(Model model, HttpSession session) {
List<String> favoriteColors = getFavColors(session);
model.addAttribute("favoriteColors", favoriteColors);
model.addAttribute("sessionId", session.getId());
return "index";
}
@PostMapping("/saveColor")
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);
}
return "redirect:/";
}
private List<String> getFavColors(HttpSession session) {
List<String> favoriteColors = (List<String>) session.getAttribute("favoriteColors");
if (favoriteColors == null) {
favoriteColors = new ArrayList<>();
}
return favoriteColors;
}
}

View File

@@ -0,0 +1,4 @@
spring.datasource.generate-unique-name=false
spring.session.store-type=jdbc
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -0,0 +1,17 @@
package com.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.springsessionjdbc.SpringSessionJdbcApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringSessionJdbcApplication.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@@ -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);
}
}