Skip to content

Commit bf78a04

Browse files
author
Grzegorz Piwowarek
committed
Refactor Velocity example
1 parent 09d6e18 commit bf78a04

7 files changed

Lines changed: 142 additions & 168 deletions

File tree

‎spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
package com.baeldung.mvc.velocity.controller;
22

3-
import java.util.List;
4-
3+
import com.baeldung.mvc.velocity.domain.Tutorial;
4+
import com.baeldung.mvc.velocity.service.ITutorialsService;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.stereotype.Controller;
77
import org.springframework.ui.Model;
88
import org.springframework.web.bind.annotation.RequestMapping;
99
import org.springframework.web.bind.annotation.RequestMethod;
1010

11-
import com.baeldung.mvc.velocity.domain.Tutorial;
12-
import com.baeldung.mvc.velocity.service.ITutorialsService;
11+
import java.util.List;
1312

14-
@Controller
15-
@RequestMapping("/")
16-
public class MainController {
13+
@Controller @RequestMapping("/") public class MainController {
1714

1815
private final ITutorialsService tutService;
1916

20-
@Autowired
21-
public MainController(ITutorialsService tutService) {
17+
@Autowired public MainController(ITutorialsService tutService) {
2218
this.tutService = tutService;
2319
}
2420

25-
@RequestMapping(method = RequestMethod.GET)
26-
public String listTutorialsPage(Model model) {
21+
@RequestMapping(method = RequestMethod.GET) public String listTutorialsPage(Model model) {
2722
List<Tutorial> list = tutService.listTutorials();
2823
model.addAttribute("tutorials", list);
2924
return "index";

‎spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/domain/Tutorial.java‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,20 @@ public Tutorial(Integer tutId, String title, String description, String author)
1414
this.author = author;
1515
}
1616

17-
public Integer getTutId() {
18-
return tutId;
19-
}
20-
21-
public String getTitle() {
22-
return title;
23-
}
24-
25-
public String getDescription() {
26-
return description;
27-
}
28-
29-
public String getAuthor() {
30-
return author;
31-
}
32-
33-
17+
public Integer getTutId() {
18+
return tutId;
19+
}
20+
21+
public String getTitle() {
22+
return title;
23+
}
24+
25+
public String getDescription() {
26+
return description;
27+
}
28+
29+
public String getAuthor() {
30+
return author;
31+
}
32+
3433
}
Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,39 @@
11
package com.baeldung.mvc.velocity.spring.config;
22

3-
import java.util.Set;
4-
5-
import javax.servlet.ServletContext;
6-
import javax.servlet.ServletException;
7-
import javax.servlet.ServletRegistration;
8-
93
import org.springframework.web.WebApplicationInitializer;
104
import org.springframework.web.context.ContextLoaderListener;
115
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
126
import org.springframework.web.context.support.GenericWebApplicationContext;
137
import org.springframework.web.servlet.DispatcherServlet;
148

9+
import javax.servlet.ServletContext;
10+
import javax.servlet.ServletException;
11+
import javax.servlet.ServletRegistration;
12+
import java.util.Set;
13+
1514
public class MainWebAppInitializer implements WebApplicationInitializer {
1615

17-
/**
18-
* Register and configure all Servlet container components necessary to
19-
* power the web application.
20-
*/
21-
@Override
22-
public void onStartup(final ServletContext sc) throws ServletException {
23-
24-
// Create the 'root' Spring application context
25-
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
26-
root.register(WebConfig.class, SpringConfig.class);
27-
28-
// Manages the lifecycle of the root application context
29-
sc.addListener(new ContextLoaderListener(root));
30-
31-
// Handles requests into the application
32-
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc",
33-
new DispatcherServlet(new GenericWebApplicationContext()));
34-
appServlet.setLoadOnStartup(1);
35-
36-
final Set<String> mappingConflicts = appServlet.addMapping("/");
37-
if (!mappingConflicts.isEmpty()) {
38-
throw new IllegalStateException("'appServlet' could not be mapped to '/' due "
39-
+ "to an existing mapping. This is a known issue under Tomcat versions "
40-
+ "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
41-
}
42-
}
16+
/**
17+
* Register and configure all Servlet container components necessary to
18+
* power the web application.
19+
*/
20+
@Override public void onStartup(final ServletContext sc) throws ServletException {
21+
22+
// Create the 'root' Spring application context
23+
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
24+
root.register(WebConfig.class, SpringConfig.class);
25+
26+
// Manages the lifecycle of the root application context
27+
sc.addListener(new ContextLoaderListener(root));
28+
29+
// Handles requests into the application
30+
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
31+
appServlet.setLoadOnStartup(1);
32+
33+
final Set<String> mappingConflicts = appServlet.addMapping("/");
34+
if (!mappingConflicts.isEmpty()) {
35+
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
36+
}
37+
}
4338

4439
}

‎spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55

66
import com.baeldung.mvc.velocity.service.TutorialsService;
77

8-
@Configuration
9-
public class SpringConfig {
8+
@Configuration public class SpringConfig {
109

11-
@Bean
12-
public TutorialsService tutService(){
13-
return new TutorialsService();
10+
@Bean public TutorialsService tutService() {
11+
return new TutorialsService();
1412
}
1513
}

‎spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/WebConfig.java‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,30 @@
1111
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
1212
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
1313

14-
@Configuration
15-
@EnableWebMvc
16-
@ComponentScan(basePackages={"com.baeldung.mvc.velocity.controller"})
17-
public class WebConfig extends WebMvcConfigurerAdapter {
14+
@Configuration @EnableWebMvc @ComponentScan(basePackages = { "com.baeldung.mvc.velocity.controller" }) public class WebConfig extends WebMvcConfigurerAdapter {
1815

1916
public WebConfig() {
2017
super();
2118
}
2219

23-
@Override
24-
public void addResourceHandlers(ResourceHandlerRegistry registry) {
20+
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) {
2521
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
2622
}
27-
28-
@Override
29-
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
23+
24+
@Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
3025
configurer.enable();
3126
}
3227

33-
@Bean
34-
public ViewResolver viewResolver() {
28+
@Bean public ViewResolver viewResolver() {
3529
final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
3630
bean.setCache(true);
3731
bean.setPrefix("/WEB-INF/views/");
3832
bean.setLayoutUrl("/WEB-INF/layouts/layout.vm");
3933
bean.setSuffix(".vm");
4034
return bean;
4135
}
42-
43-
@Bean
44-
public VelocityConfigurer velocityConfig() {
36+
37+
@Bean public VelocityConfigurer velocityConfig() {
4538
VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
4639
velocityConfigurer.setResourceLoaderPath("/");
4740
return velocityConfigurer;
Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
package com.baeldung.mvc.velocity.test;
22

3-
import static org.hamcrest.Matchers.allOf;
4-
import static org.hamcrest.Matchers.hasItem;
5-
import static org.hamcrest.Matchers.hasProperty;
6-
import static org.hamcrest.Matchers.hasSize;
7-
import static org.hamcrest.Matchers.is;
8-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
9-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
10-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
11-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
12-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
13-
14-
import java.util.Arrays;
15-
3+
import com.baeldung.mvc.velocity.domain.Tutorial;
4+
import com.baeldung.mvc.velocity.service.ITutorialsService;
5+
import com.baeldung.mvc.velocity.spring.config.WebConfig;
6+
import com.baeldung.mvc.velocity.test.config.TestConfig;
167
import org.junit.Before;
178
import org.junit.Test;
189
import org.junit.runner.RunWith;
@@ -25,48 +16,58 @@
2516
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2617
import org.springframework.web.context.WebApplicationContext;
2718

28-
import com.baeldung.mvc.velocity.domain.Tutorial;
29-
import com.baeldung.mvc.velocity.service.ITutorialsService;
30-
import com.baeldung.mvc.velocity.spring.config.WebConfig;
31-
import com.baeldung.mvc.velocity.test.config.TestConfig;
19+
import java.util.Arrays;
20+
21+
import static org.hamcrest.Matchers.allOf;
22+
import static org.hamcrest.Matchers.hasItem;
23+
import static org.hamcrest.Matchers.hasProperty;
24+
import static org.hamcrest.Matchers.hasSize;
25+
import static org.hamcrest.Matchers.is;
26+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
3231

3332
@RunWith(SpringJUnit4ClassRunner.class)
3433
// @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
35-
@ContextConfiguration(classes = { TestConfig.class, WebConfig.class })
36-
@WebAppConfiguration
37-
public class DataContentControllerTest {
34+
@ContextConfiguration(classes = { TestConfig.class, WebConfig.class }) @WebAppConfiguration public class DataContentControllerTest {
3835

39-
private MockMvc mockMvc;
36+
private MockMvc mockMvc;
4037

41-
@Autowired
42-
private ITutorialsService tutServiceMock;
38+
@Autowired private ITutorialsService tutServiceMock;
4339

44-
@Autowired
45-
private WebApplicationContext webApplicationContext;
40+
@Autowired private WebApplicationContext webApplicationContext;
4641

47-
@Before
48-
public void setUp() {
49-
Mockito.reset(tutServiceMock);
42+
@Before public void setUp() {
43+
Mockito.reset(tutServiceMock);
5044

51-
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
52-
}
45+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
46+
}
5347

54-
@Test
55-
public void testModel() throws Exception {
48+
@Test public void testModel() throws Exception {
5649

57-
Mockito.when(tutServiceMock.listTutorials())
58-
.thenReturn(Arrays.asList(new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
59-
new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")));
50+
Mockito.when(tutServiceMock.listTutorials())
51+
.thenReturn(Arrays.asList(
52+
new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
53+
new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")));
6054

61-
mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("index"))
62-
.andExpect(model().attribute("tutorials", hasSize(2)))
63-
.andExpect(model().attribute("tutorials",
64-
hasItem(allOf(hasProperty("tutId", is(1)), hasProperty("author", is("GuavaAuthor")),
65-
hasProperty("title", is("Guava"))))))
66-
.andExpect(model().attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)),
67-
hasProperty("author", is("AndroidAuthor")), hasProperty("title", is("Android"))))));
55+
mockMvc.perform(get("/"))
56+
.andExpect(status().isOk()).andExpect(view().name("index"))
57+
.andExpect(model().attribute("tutorials", hasSize(2)))
58+
.andExpect(model()
59+
.attribute("tutorials",
60+
hasItem(allOf(hasProperty("tutId", is(1)),
61+
hasProperty("author", is("GuavaAuthor")),
62+
hasProperty("title", is("Guava"))))))
63+
.andExpect(model()
64+
.attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)),
65+
hasProperty("author", is("AndroidAuthor")),
66+
hasProperty("title", is("Android"))))));
6867

69-
mockMvc.perform(get("/")).andExpect(xpath("//table").exists());
70-
mockMvc.perform(get("/")).andExpect(xpath("//td[@id='tutId_1']").exists());
71-
}
68+
mockMvc.perform(get("/"))
69+
.andExpect(xpath("//table").exists());
70+
mockMvc.perform(get("/"))
71+
.andExpect(xpath("//td[@id='tutId_1']").exists());
72+
}
7273
}

0 commit comments

Comments
 (0)