From 09d6e1852fade8408e23e9f4c223f92761c52994 Mon Sep 17 00:00:00 2001 From: Raquel Garrido Date: Tue, 26 Jul 2016 08:04:19 +0200 Subject: [PATCH] Fix last PR (#533) * Test Model Content * Test get * test get * Modified mvc-velocity to java based configuration * Added failOnMissingWebXml to false * Fix config error * fix tests * no message * Revert "fix tests" This reverts commit f2b39424cf718d274d7dc82a4e5fc89e703b8aaf. * Fix PR --- spring-mvc-velocity/pom.xml | 3 + .../velocity/controller/MainController.java | 13 ++- .../spring/config/MainWebAppInitializer.java | 44 ++++++++ .../velocity/spring/config/SpringConfig.java | 15 +++ .../mvc/velocity/spring/config/WebConfig.java | 49 ++++++++ .../src/main/webapp/WEB-INF/layouts/layout.vm | 2 +- .../src/main/webapp/WEB-INF/views/index.vm | 8 +- .../webapp/WEB-INF/{web.xml => web_old.xml} | 0 .../test/DataContentControllerTest.java | 106 ++++++++---------- .../test/NavigationControllerTest.java | 28 +++-- .../mvc/velocity/test/config/TestConfig.java | 38 +++++++ 11 files changed, 223 insertions(+), 83 deletions(-) create mode 100644 spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/MainWebAppInitializer.java create mode 100644 spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java create mode 100644 spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/WebConfig.java rename spring-mvc-velocity/src/main/webapp/WEB-INF/{web.xml => web_old.xml} (100%) create mode 100644 spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/config/TestConfig.java diff --git a/spring-mvc-velocity/pom.xml b/spring-mvc-velocity/pom.xml index 597e638cf7..6c63e0be18 100644 --- a/spring-mvc-velocity/pom.xml +++ b/spring-mvc-velocity/pom.xml @@ -128,6 +128,9 @@ org.apache.maven.plugins maven-war-plugin ${maven-war-plugin.version} + + false + diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java index fe88705b90..1362bf99b3 100644 --- a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java @@ -1,23 +1,24 @@ package com.baeldung.mvc.velocity.controller; -import com.baeldung.mvc.velocity.domain.Tutorial; -import com.baeldung.mvc.velocity.service.TutorialsService; +import java.util.List; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import java.util.List; +import com.baeldung.mvc.velocity.domain.Tutorial; +import com.baeldung.mvc.velocity.service.ITutorialsService; @Controller @RequestMapping("/") public class MainController { - private final TutorialsService tutService; + private final ITutorialsService tutService; @Autowired - public MainController(TutorialsService tutService) { + public MainController(ITutorialsService tutService) { this.tutService = tutService; } @@ -28,7 +29,7 @@ public class MainController { return "index"; } - public TutorialsService getTutService() { + public ITutorialsService getTutService() { return tutService; } } \ No newline at end of file diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/MainWebAppInitializer.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/MainWebAppInitializer.java new file mode 100644 index 0000000000..3cc1251e88 --- /dev/null +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/MainWebAppInitializer.java @@ -0,0 +1,44 @@ +package com.baeldung.mvc.velocity.spring.config; + +import java.util.Set; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; + +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.ContextLoaderListener; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.context.support.GenericWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +public class MainWebAppInitializer implements WebApplicationInitializer { + + /** + * Register and configure all Servlet container components necessary to + * power the web application. + */ + @Override + public void onStartup(final ServletContext sc) throws ServletException { + + // Create the 'root' Spring application context + final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); + root.register(WebConfig.class, SpringConfig.class); + + // Manages the lifecycle of the root application context + sc.addListener(new ContextLoaderListener(root)); + + // Handles requests into the application + final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", + new DispatcherServlet(new GenericWebApplicationContext())); + appServlet.setLoadOnStartup(1); + + final Set mappingConflicts = appServlet.addMapping("/"); + if (!mappingConflicts.isEmpty()) { + 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"); + } + } + +} diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java new file mode 100644 index 0000000000..a024db543d --- /dev/null +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java @@ -0,0 +1,15 @@ +package com.baeldung.mvc.velocity.spring.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.mvc.velocity.service.TutorialsService; + +@Configuration +public class SpringConfig { + + @Bean + public TutorialsService tutService(){ + return new TutorialsService(); + } +} diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/WebConfig.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/WebConfig.java new file mode 100644 index 0000000000..4cc8e0dca1 --- /dev/null +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/WebConfig.java @@ -0,0 +1,49 @@ +package com.baeldung.mvc.velocity.spring.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.velocity.VelocityConfigurer; +import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver; + +@Configuration +@EnableWebMvc +@ComponentScan(basePackages={"com.baeldung.mvc.velocity.controller"}) +public class WebConfig extends WebMvcConfigurerAdapter { + + public WebConfig() { + super(); + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + configurer.enable(); + } + + @Bean + public ViewResolver viewResolver() { + final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver(); + bean.setCache(true); + bean.setPrefix("/WEB-INF/views/"); + bean.setLayoutUrl("/WEB-INF/layouts/layout.vm"); + bean.setSuffix(".vm"); + return bean; + } + + @Bean + public VelocityConfigurer velocityConfig() { + VelocityConfigurer velocityConfigurer = new VelocityConfigurer(); + velocityConfigurer.setResourceLoaderPath("/"); + return velocityConfigurer; + } +} diff --git a/spring-mvc-velocity/src/main/webapp/WEB-INF/layouts/layout.vm b/spring-mvc-velocity/src/main/webapp/WEB-INF/layouts/layout.vm index 203e675cfb..3bac96aaae 100644 --- a/spring-mvc-velocity/src/main/webapp/WEB-INF/layouts/layout.vm +++ b/spring-mvc-velocity/src/main/webapp/WEB-INF/layouts/layout.vm @@ -1,6 +1,6 @@ - Spring & Velocity + Spring with Velocity
diff --git a/spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm b/spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm index d1ae0b02cb..9e06a09e4f 100644 --- a/spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm +++ b/spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm @@ -10,10 +10,10 @@ #foreach($tut in $tutorials) - $tut.tutId - $tut.title - $tut.description - $tut.author + $tut.tutId + $tut.title + $tut.description + $tut.author #end \ No newline at end of file diff --git a/spring-mvc-velocity/src/main/webapp/WEB-INF/web.xml b/spring-mvc-velocity/src/main/webapp/WEB-INF/web_old.xml similarity index 100% rename from spring-mvc-velocity/src/main/webapp/WEB-INF/web.xml rename to spring-mvc-velocity/src/main/webapp/WEB-INF/web_old.xml diff --git a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java index 36453eef92..6c62a3fa07 100644 --- a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java +++ b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java @@ -1,8 +1,18 @@ package com.baeldung.mvc.velocity.test; -import com.baeldung.mvc.velocity.domain.Tutorial; -import com.baeldung.mvc.velocity.service.ITutorialsService; -import com.baeldung.mvc.velocity.service.TutorialsService; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath; + +import java.util.Arrays; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -15,72 +25,48 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import java.util.Arrays; - -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasProperty; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +import com.baeldung.mvc.velocity.domain.Tutorial; +import com.baeldung.mvc.velocity.service.ITutorialsService; +import com.baeldung.mvc.velocity.spring.config.WebConfig; +import com.baeldung.mvc.velocity.test.config.TestConfig; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) +// @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) +@ContextConfiguration(classes = { TestConfig.class, WebConfig.class }) @WebAppConfiguration public class DataContentControllerTest { - - private MockMvc mockMvc; + private MockMvc mockMvc; - @Autowired - private ITutorialsService tutServiceMock; + @Autowired + private ITutorialsService tutServiceMock; - @Autowired - private WebApplicationContext webApplicationContext; - - @Before - public void setUp() { - tutServiceMock = Mockito.mock(TutorialsService.class); - Mockito.reset(tutServiceMock); + @Autowired + private WebApplicationContext webApplicationContext; + + @Before + public void setUp() { + Mockito.reset(tutServiceMock); + + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + } - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - } - @Test - public void testModel() throws Exception{ + public void testModel() throws Exception { - Mockito.when(tutServiceMock.listTutorials()).thenReturn(Arrays.asList( - new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), - new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor") - )); - - mockMvc.perform(get("/")) - .andExpect(status().isOk()) - .andExpect(view().name("index")) - .andExpect(content().string(containsString("GuavaAuthor"))) - .andExpect(content().string(containsString("Introduction to Guava"))) - .andExpect(content().string(containsString("AndroidAuthor"))) - .andExpect(content().string(containsString("Introduction to Android"))) - .andExpect(model().attribute("tutorials", hasSize(2))) - .andExpect(model().attribute("tutorials", hasSize(2))) - .andExpect(model().attribute("tutorials", hasItem( - allOf( - hasProperty("tutId", is(1)), - hasProperty("author", is("GuavaAuthor")), - hasProperty("title", is("Guava")) - ) - ))) - .andExpect(model().attribute("tutorials", hasItem( - allOf( - hasProperty("tutId", is(2)), - hasProperty("author", is("AndroidAuthor")), - hasProperty("title", is("Android")) - ) - ))); + Mockito.when(tutServiceMock.listTutorials()) + .thenReturn(Arrays.asList(new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), + new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor"))); + + mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("index")) + .andExpect(model().attribute("tutorials", hasSize(2))) + .andExpect(model().attribute("tutorials", + hasItem(allOf(hasProperty("tutId", is(1)), hasProperty("author", is("GuavaAuthor")), + hasProperty("title", is("Guava")))))) + .andExpect(model().attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)), + hasProperty("author", is("AndroidAuthor")), hasProperty("title", is("Android")))))); + + mockMvc.perform(get("/")).andExpect(xpath("//table").exists()); + mockMvc.perform(get("/")).andExpect(xpath("//td[@id='tutId_1']").exists()); } } diff --git a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java index e007cf3f94..f9b2cdc76b 100644 --- a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java +++ b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java @@ -1,9 +1,15 @@ package com.baeldung.mvc.velocity.test; -import com.baeldung.mvc.velocity.controller.MainController; -import com.baeldung.mvc.velocity.domain.Tutorial; -import com.baeldung.mvc.velocity.service.TutorialsService; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +import java.util.Arrays; +import java.util.List; + import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -13,18 +19,16 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.Model; -import java.util.Arrays; -import java.util.List; +import com.baeldung.mvc.velocity.controller.MainController; +import com.baeldung.mvc.velocity.domain.Tutorial; +import com.baeldung.mvc.velocity.service.TutorialsService; +import com.baeldung.mvc.velocity.test.config.TestConfig; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) +//@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) +@ContextConfiguration(classes = {TestConfig.class}) public class NavigationControllerTest { private MainController mainController = new MainController(Mockito.mock(TutorialsService.class)); @@ -59,7 +63,7 @@ public class NavigationControllerTest { verifyNoMoreInteractions(mainController.getTutService()); assertEquals("index", view); - assertEquals(tutorials, model.asMap().get("tutorials")); + assertEquals(tutorials, model.asMap().get("tutorials")); } private static List createTutorialList() { diff --git a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/config/TestConfig.java b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/config/TestConfig.java new file mode 100644 index 0000000000..097900327a --- /dev/null +++ b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/config/TestConfig.java @@ -0,0 +1,38 @@ +package com.baeldung.mvc.velocity.test.config; + +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.view.velocity.VelocityConfigurer; +import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver; + +import com.baeldung.mvc.velocity.service.ITutorialsService; + +@Configuration +public class TestConfig { + + + @Bean + public ViewResolver viewResolver() { + final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver(); + bean.setCache(true); + bean.setPrefix("/WEB-INF/views/"); + bean.setLayoutUrl("/WEB-INF/layouts/layout.vm"); + bean.setSuffix(".vm"); + return bean; + } + + @Bean + public VelocityConfigurer velocityConfig() { + VelocityConfigurer velocityConfigurer = new VelocityConfigurer(); + velocityConfigurer.setResourceLoaderPath("/"); + return velocityConfigurer; + } + + @Bean + public ITutorialsService getTutServiceMock() { + return Mockito.mock(ITutorialsService.class); + } + +}