formatting cleanup
This commit is contained in:
parent
dec95f5c25
commit
30552c8d0a
|
@ -9,9 +9,7 @@ public class AttrListener implements ServletContextListener {
|
|||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent servletContextEvent) {
|
||||
servletContextEvent
|
||||
.getServletContext()
|
||||
.setAttribute("servlet-context-attr", "test");
|
||||
servletContextEvent.getServletContext().setAttribute("servlet-context-attr", "test");
|
||||
System.out.println("context init");
|
||||
}
|
||||
|
||||
|
|
|
@ -16,9 +16,7 @@ public class EchoServlet extends HttpServlet {
|
|||
@Override
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response) {
|
||||
try {
|
||||
Path path = File
|
||||
.createTempFile("echo", "tmp")
|
||||
.toPath();
|
||||
Path path = File.createTempFile("echo", "tmp").toPath();
|
||||
Files.copy(request.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
|
||||
Files.copy(path, response.getOutputStream());
|
||||
Files.delete(path);
|
||||
|
|
|
@ -18,9 +18,7 @@ public class HelloFilter implements Filter {
|
|||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
servletResponse
|
||||
.getOutputStream()
|
||||
.print(filterConfig.getInitParameter("msg"));
|
||||
servletResponse.getOutputStream().print(filterConfig.getInitParameter("msg"));
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
}
|
||||
|
||||
|
|
|
@ -8,22 +8,20 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebServlet(urlPatterns = "/hello", initParams = { @WebInitParam(name = "msg", value = "hello")})
|
||||
@WebServlet(urlPatterns = "/hello", initParams = { @WebInitParam(name = "msg", value = "hello") })
|
||||
public class HelloServlet extends HttpServlet {
|
||||
|
||||
private ServletConfig servletConfig;
|
||||
|
||||
@Override
|
||||
public void init(ServletConfig servletConfig){
|
||||
public void init(ServletConfig servletConfig) {
|
||||
this.servletConfig = servletConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) {
|
||||
try {
|
||||
response
|
||||
.getOutputStream()
|
||||
.write(servletConfig.getInitParameter("msg").getBytes());
|
||||
response.getOutputStream().write(servletConfig.getInitParameter("msg").getBytes());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
@ -4,10 +4,8 @@ import org.springframework.boot.SpringApplication;
|
|||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,12 +7,12 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
public class HomeController {
|
||||
|
||||
@RequestMapping("/")
|
||||
public String root(){
|
||||
public String root() {
|
||||
return "Index Page";
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/local")
|
||||
public String local(){
|
||||
public String local() {
|
||||
return "/local";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@ public class WebMvcConfigure extends WebMvcConfigurerAdapter {
|
|||
configurer.enable();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver());
|
||||
|
|
|
@ -10,7 +10,8 @@ import org.springframework.core.env.ConfigurableEnvironment;
|
|||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = { "com.baeldung.*" })
|
||||
@PropertySource("classpath:custom.properties") public class PropertySourcesLoader {
|
||||
@PropertySource("classpath:custom.properties")
|
||||
public class PropertySourcesLoader {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(PropertySourcesLoader.class);
|
||||
|
||||
|
|
|
@ -7,9 +7,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebServlet(name = "AnnotationServlet",
|
||||
description = "Example Servlet Using Annotations",
|
||||
urlPatterns = { "/annotationservlet" })
|
||||
@WebServlet(name = "AnnotationServlet", description = "Example Servlet Using Annotations", urlPatterns = { "/annotationservlet" })
|
||||
public class AnnotationServlet extends HttpServlet {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
|
|
@ -7,12 +7,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages="com.baeldung.utils")
|
||||
@ComponentScan(basePackages = "com.baeldung.utils")
|
||||
public class Application {
|
||||
|
||||
@RolesAllowed("*")
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
@RolesAllowed("*")
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ import javax.servlet.http.HttpServletRequest;
|
|||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.ServletRequestBindingException;
|
||||
import org.springframework.web.bind.ServletRequestUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
@ -13,37 +12,37 @@ import org.springframework.web.util.WebUtils;
|
|||
@Controller
|
||||
public class UtilsController {
|
||||
|
||||
@GetMapping("/utils")
|
||||
public String webUtils(Model model) {
|
||||
return "utils";
|
||||
}
|
||||
|
||||
@PostMapping("/setParam")
|
||||
public String post(HttpServletRequest request, Model model) {
|
||||
String param = ServletRequestUtils.getStringParameter(request, "param", "DEFAULT");
|
||||
|
||||
// Long param = ServletRequestUtils.getLongParameter(request, "param",1L);
|
||||
// boolean param = ServletRequestUtils.getBooleanParameter(request, "param", true);
|
||||
// double param = ServletRequestUtils.getDoubleParameter(request, "param", 1000);
|
||||
// float param = ServletRequestUtils.getFloatParameter(request, "param", (float) 1.00);
|
||||
// int param = ServletRequestUtils.getIntParameter(request, "param", 100);
|
||||
|
||||
// try {
|
||||
// ServletRequestUtils.getRequiredStringParameter(request, "param");
|
||||
// } catch (ServletRequestBindingException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
|
||||
WebUtils.setSessionAttribute(request, "parameter", param);
|
||||
model.addAttribute("parameter", "You set: "+(String) WebUtils.getSessionAttribute(request, "parameter"));
|
||||
return "utils";
|
||||
}
|
||||
|
||||
@GetMapping("/other")
|
||||
public String other(HttpServletRequest request, Model model) {
|
||||
String param = (String) WebUtils.getSessionAttribute(request, "parameter");
|
||||
model.addAttribute("parameter", param);
|
||||
return "other";
|
||||
}
|
||||
|
||||
@GetMapping("/utils")
|
||||
public String webUtils(Model model) {
|
||||
return "utils";
|
||||
}
|
||||
|
||||
@PostMapping("/setParam")
|
||||
public String post(HttpServletRequest request, Model model) {
|
||||
String param = ServletRequestUtils.getStringParameter(request, "param", "DEFAULT");
|
||||
|
||||
// Long param = ServletRequestUtils.getLongParameter(request, "param",1L);
|
||||
// boolean param = ServletRequestUtils.getBooleanParameter(request, "param", true);
|
||||
// double param = ServletRequestUtils.getDoubleParameter(request, "param", 1000);
|
||||
// float param = ServletRequestUtils.getFloatParameter(request, "param", (float) 1.00);
|
||||
// int param = ServletRequestUtils.getIntParameter(request, "param", 100);
|
||||
|
||||
// try {
|
||||
// ServletRequestUtils.getRequiredStringParameter(request, "param");
|
||||
// } catch (ServletRequestBindingException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
|
||||
WebUtils.setSessionAttribute(request, "parameter", param);
|
||||
model.addAttribute("parameter", "You set: " + (String) WebUtils.getSessionAttribute(request, "parameter"));
|
||||
return "utils";
|
||||
}
|
||||
|
||||
@GetMapping("/other")
|
||||
public String other(HttpServletRequest request, Model model) {
|
||||
String param = (String) WebUtils.getSessionAttribute(request, "parameter");
|
||||
model.addAttribute("parameter", param);
|
||||
return "other";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package com.baeldung.webjar;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
public class WebjarsdemoApplication {
|
||||
|
|
|
@ -10,11 +10,11 @@ public class FooService {
|
|||
|
||||
@Autowired
|
||||
private FooRepository fooRepository;
|
||||
|
||||
|
||||
public Foo getFooWithId(Integer id) throws Exception {
|
||||
return fooRepository.findOne(id);
|
||||
}
|
||||
|
||||
|
||||
public Foo getFooWithName(String name) {
|
||||
return fooRepository.findByName(name);
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package org.baeldung.boot.exceptions;
|
||||
|
||||
public class CommonException extends RuntimeException{
|
||||
public class CommonException extends RuntimeException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 3080004140659213332L;
|
||||
|
||||
public CommonException(String message){
|
||||
public CommonException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package org.baeldung.boot.exceptions;
|
||||
|
||||
public class FooNotFoundException extends RuntimeException{
|
||||
public class FooNotFoundException extends RuntimeException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 9042200028456133589L;
|
||||
|
||||
public FooNotFoundException(String message){
|
||||
public FooNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ public class Foo implements Serializable {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
public Foo(Integer id, String name) {
|
||||
super();
|
||||
this.id = id;
|
||||
|
|
|
@ -18,7 +18,7 @@ public class FooController {
|
|||
public Foo getFooWithId(@PathVariable Integer id) throws Exception {
|
||||
return fooService.getFooWithId(id);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/")
|
||||
public Foo getFooWithName(@RequestParam String name) throws Exception {
|
||||
return fooService.getFooWithName(name);
|
||||
|
|
|
@ -22,7 +22,8 @@ import static org.junit.Assert.*;
|
|||
@TestPropertySource(properties = { "security.basic.enabled=false" })
|
||||
public class SpringBootWithServletComponentIntegrationTest {
|
||||
|
||||
@Autowired private ServletContext servletContext;
|
||||
@Autowired
|
||||
private ServletContext servletContext;
|
||||
|
||||
@Test
|
||||
public void givenServletContext_whenAccessAttrs_thenFoundAttrsPutInServletListner() {
|
||||
|
@ -37,12 +38,11 @@ public class SpringBootWithServletComponentIntegrationTest {
|
|||
FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter");
|
||||
|
||||
assertNotNull(filterRegistration);
|
||||
assertTrue(filterRegistration
|
||||
.getServletNameMappings()
|
||||
.contains("echo servlet"));
|
||||
assertTrue(filterRegistration.getServletNameMappings().contains("echo servlet"));
|
||||
}
|
||||
|
||||
@Autowired private TestRestTemplate restTemplate;
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void givenServletFilter_whenGetHello_thenRequestFiltered() {
|
||||
|
@ -58,8 +58,4 @@ public class SpringBootWithServletComponentIntegrationTest {
|
|||
assertEquals("filtering echo", responseEntity.getBody());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -22,9 +22,11 @@ import static org.junit.Assert.*;
|
|||
@TestPropertySource(properties = { "security.basic.enabled=false" })
|
||||
public class SpringBootWithoutServletComponentIntegrationTest {
|
||||
|
||||
@Autowired private ServletContext servletContext;
|
||||
@Autowired
|
||||
private ServletContext servletContext;
|
||||
|
||||
@Autowired private TestRestTemplate restTemplate;
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void givenServletContext_whenAccessAttrs_thenNotFound() {
|
||||
|
@ -46,5 +48,3 @@ public class SpringBootWithoutServletComponentIntegrationTest {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -26,16 +26,12 @@ public class AppLiveTest {
|
|||
|
||||
@Test
|
||||
public void getIndex() throws Exception {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(equalTo("Index Page")));
|
||||
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(equalTo("Index Page")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLocal() throws Exception {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/local").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(equalTo("/local")));
|
||||
mvc.perform(MockMvcRequestBuilders.get("/local").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(equalTo("/local")));
|
||||
}
|
||||
|
||||
}
|
|
@ -10,32 +10,26 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
|
||||
import com.baeldung.utils.controller.UtilsController;
|
||||
|
||||
public class UtilsControllerIntegrationTest {
|
||||
|
||||
@InjectMocks
|
||||
@InjectMocks
|
||||
private UtilsController utilsController;
|
||||
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
this.mockMvc = MockMvcBuilders.standaloneSetup(utilsController)
|
||||
.build();
|
||||
this.mockMvc = MockMvcBuilders.standaloneSetup(utilsController).build();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenParameter_setRequestParam_andSetSessionAttribute() throws Exception {
|
||||
String param = "testparam";
|
||||
this.mockMvc.perform(
|
||||
post("/setParam")
|
||||
.param("param", param)
|
||||
.sessionAttr("parameter", param))
|
||||
.andExpect(status().isOk());
|
||||
String param = "testparam";
|
||||
this.mockMvc.perform(post("/setParam").param("param", param).sessionAttr("parameter", param)).andExpect(status().isOk());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -9,8 +9,6 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.subethamail.wiser.Wiser;
|
||||
import org.subethamail.wiser.WiserMessage;
|
||||
|
||||
|
|
|
@ -23,9 +23,7 @@ import static org.mockito.Matchers.anyInt;
|
|||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(
|
||||
classes = DemoApplication.class,
|
||||
webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class FooComponentIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
package org.baeldung.boot;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -16,28 +17,27 @@ import org.springframework.http.ResponseEntity;
|
|||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes=DemoApplication.class,webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class FooIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate testRestTemplate;
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void givenInquiryingFooWithId_whenIdIsValid_thenHttpStatusOK(){
|
||||
Map<String,String> pathVariables = new HashMap<String,String>();
|
||||
public void givenInquiryingFooWithId_whenIdIsValid_thenHttpStatusOK() {
|
||||
Map<String, String> pathVariables = new HashMap<String, String>();
|
||||
pathVariables.put("id", "1");
|
||||
ResponseEntity<Foo> fooResponse = testRestTemplate.getForEntity("/{id}", Foo.class, pathVariables);
|
||||
Assert.assertNotNull(fooResponse);
|
||||
Assert.assertEquals(HttpStatus.OK,fooResponse.getStatusCode());
|
||||
Assert.assertEquals(HttpStatus.OK, fooResponse.getStatusCode());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenInquiryingFooWithName_whenNameIsValid_thenHttpStatusOK(){
|
||||
Map<String,String> pathVariables = new HashMap<String,String>();
|
||||
public void givenInquiryingFooWithName_whenNameIsValid_thenHttpStatusOK() {
|
||||
Map<String, String> pathVariables = new HashMap<String, String>();
|
||||
pathVariables.put("name", "Foo_Name");
|
||||
ResponseEntity<Foo> fooResponse = testRestTemplate.getForEntity("/?name={name}", Foo.class, pathVariables);
|
||||
Assert.assertNotNull(fooResponse);
|
||||
Assert.assertEquals(HttpStatus.OK,fooResponse.getStatusCode());
|
||||
Assert.assertEquals(HttpStatus.OK, fooResponse.getStatusCode());
|
||||
}
|
||||
}
|
|
@ -27,8 +27,8 @@ public class FooJPAIntegrationTest {
|
|||
this.entityManager.persist(new Foo("Foo_Name_2"));
|
||||
Foo foo = this.repository.findByName("Foo_Name_2");
|
||||
assertNotNull(foo);
|
||||
assertEquals("Foo_Name_2",foo.getName());
|
||||
// Due to having Insert query for Foo with Id 1, so TestEntityManager generates new Id of 2
|
||||
assertEquals(2l,foo.getId().longValue());
|
||||
assertEquals("Foo_Name_2", foo.getName());
|
||||
// Due to having Insert query for Foo with Id 1, so TestEntityManager generates new Id of 2
|
||||
assertEquals(2l, foo.getId().longValue());
|
||||
}
|
||||
}
|
|
@ -16,7 +16,6 @@ public class FooJsonIntegrationTest {
|
|||
|
||||
@Autowired
|
||||
private JacksonTester<Foo> json;
|
||||
|
||||
|
||||
@Test
|
||||
public void testSerialize() throws Exception {
|
||||
|
@ -30,6 +29,6 @@ public class FooJsonIntegrationTest {
|
|||
public void testDeserialize() throws Exception {
|
||||
String content = "{\"id\":4,\"name\":\"Foo_Name_4\"}";
|
||||
assertThat(this.json.parseObject(content).getName()).isEqualTo("Foo_Name_4");
|
||||
assertThat(this.json.parseObject(content).getId()==4);
|
||||
assertThat(this.json.parseObject(content).getId() == 4);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue