Merge pull request #7318 from isaolmez/BAEL-3014

Bael 3014
This commit is contained in:
Erik Pragt 2019-08-09 08:57:36 +10:00 committed by GitHub
commit abad789f60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 194 additions and 0 deletions

View File

@ -21,6 +21,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, {"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>")));
}
}