BAEL-3014: Added sample code for lists

This commit is contained in:
isaolmez 2019-07-15 12:44:59 +03:00
parent ca53b93176
commit 306c37cfc4
8 changed files with 204 additions and 4 deletions

View File

@ -5,11 +5,17 @@
<name>spring-thymeleaf-2</name>
<packaging>war</packaging>
<!-- <parent>-->
<!-- <groupId>com.baeldung</groupId>-->
<!-- <artifactId>parent-boot-2</artifactId>-->
<!-- <version>0.0.1-SNAPSHOT</version>-->
<!-- <relativePath>../parent-boot-2</relativePath>-->
<!-- </parent>-->
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
@ -21,6 +27,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,64 @@
package com.baeldung.thymeleaf.lists;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/lists")
public class ListsController {
@GetMapping("/toList")
public String usingToList(Model model) {
List<String> colors = getColors();
String[] colorsArray = colors.toArray(new String[0]);
model.addAttribute("myArray", colorsArray);
return "lists/toList";
}
@GetMapping("/contains")
public String usingContains(Model model) {
model.addAttribute("myList", getColors());
model.addAttribute("others", getOtherColors());
return "lists/contains";
}
@GetMapping("/size")
public String usingSize(Model model) {
model.addAttribute("myList", getColors());
return "lists/size";
}
@GetMapping("/isEmpty")
public String usingIsEmpty(Model model) {
model.addAttribute("myList", getColors());
return "lists/isEmpty";
}
@GetMapping("/sort")
public String usingSort(Model model) {
model.addAttribute("myList", getColors());
model.addAttribute("reverse", Comparator.reverseOrder());
return "lists/sort";
}
private List<String> getColors() {
List<String> colors = new ArrayList<>();
colors.add("green");
colors.add("yellow");
colors.add("red");
colors.add("blue");
return colors;
}
private List<String> getOtherColors() {
List<String> colors = new ArrayList<>();
colors.add("green");
colors.add("blue");
return colors;
}
}

View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Lists Utility Class in Thymeleaf</title>
</head>
<body>
myList contains red: <span th:text="${#lists.contains(myList, 'red')}"/>
myList contains red and green: <span th:text='${#lists.containsAll(myList, new String[]{"red", "green"})}'/>
</body>
</html>

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Lists Utility Class in Thymeleaf</title>
</head>
<body>
isEmpty Check : <span th:text="${#lists.isEmpty(myList)}"/>
<span th:unless="${#lists.isEmpty(myList)}">List is not empty</span>
</body>
</html>

View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Lists Utility Class in Thymeleaf</title>
</head>
<body>
size: <span th:text="${#lists.size(myList)}"/>
</body>
</html>

View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Lists Utility Class in Thymeleaf</title>
</head>
<body>
sort: <span th:text="${#lists.sort(myList)}"/>
sort with Comparator: <span th:text="${#lists.sort(myList, reverse)}"/>
</body>
</html>

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Lists Utility Class in Thymeleaf</title>
</head>
<body>
<span th:with="convertedList=${#lists.toList(myArray)}">
converted list size: <span th:text="${#lists.size(convertedList)}"/>
</span>
</body>
</html>

View File

@ -0,0 +1,60 @@
package com.baeldung.thymeleaf.lists;
import static org.hamcrest.CoreMatchers.containsString;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc(printOnlyOnFailure = false)
public class ListsControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
public void whenCalledToList_ThenConvertsToList() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/lists/toList"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("converted list size: <span>4</span>")));
}
@Test
public void whenCalledContains_ThenChecksMembership() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/lists/contains"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("myList contains red: <span>true</span>")))
.andExpect(content().string(containsString("myList contains red and green: <span>true</span>")));
}
@Test
public void whenCalledSize_ThenReturnsSize() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/lists/size"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("size: <span>4</span>")));
}
@Test
public void whenCalledSort_ThenSortsItems() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/lists/sort"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("sort: <span>[blue, green, red, yellow]</span>")))
.andExpect(content().string(containsString("sort with Comparator: <span>[yellow, red, green, blue]</span>")));
}
@Test
public void whenCalledIsEmpty_ThenChecksAnyMembers() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/lists/isEmpty"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("isEmpty Check : <span>false</span>")));
}
}