diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsController.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsController.java new file mode 100644 index 0000000000..f30b9b2078 --- /dev/null +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsController.java @@ -0,0 +1,33 @@ +package com.baeldung.thymeleaf.controller; + +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.HashSet; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * Controller to test expression utility objects: dates, + * + */ +@Controller +public class ExpressionUtilityObjectsController { + + @RequestMapping(value = "/objects", method = RequestMethod.GET) + public String getDates(Model model) { + model.addAttribute("date", new Date()); + model.addAttribute("calendar", Calendar.getInstance()); + model.addAttribute("num", Math.random() * 10); + model.addAttribute("string", "new text"); + model.addAttribute("emptyString", ""); + model.addAttribute("nullString", null); + model.addAttribute("array", new int[] { 1, 3, 4, 5 }); + model.addAttribute("set", new HashSet(Arrays.asList(1, 3, 8))); + return "objects.html"; + } + +} diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/objects.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/objects.html new file mode 100644 index 0000000000..b2a23a7c0f --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/objects.html @@ -0,0 +1,47 @@ + + + + +Expression utility objects + + +

Date expression utility

+

+

+

+

+

+ +

Calendar expression utility

+

+

+

+

+

+ +

Numbers expression utility

+

+

+

+ +

+

+ +

+ +

Calendar expression utility

+

+

+

+

+

+

+ +

Aggregates expression utility

+

+

+

+

+ + \ No newline at end of file diff --git a/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsControllerTest.java b/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsControllerTest.java new file mode 100644 index 0000000000..f6a79b7570 --- /dev/null +++ b/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsControllerTest.java @@ -0,0 +1,58 @@ +package com.baeldung.thymeleaf.controller; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import javax.servlet.Filter; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.RequestPostProcessor; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.thymeleaf.config.InitSecurity; +import com.baeldung.thymeleaf.config.WebApp; +import com.baeldung.thymeleaf.config.WebMVCConfig; +import com.baeldung.thymeleaf.config.WebMVCSecurity; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class }) +public class ExpressionUtilityObjectsControllerTest { + + @Autowired + WebApplicationContext wac; + @Autowired + MockHttpSession session; + + private MockMvc mockMvc; + + @Autowired + private Filter springSecurityFilterChain; + + protected RequestPostProcessor testUser() { + return user("user1").password("user1Pass").roles("USER"); + } + + @Before + public void setup() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build(); + } + + @Test + public void testGetDates() throws Exception{ + mockMvc.perform(get("/objects").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("objects.html")); + } + +}