Skip to content

Commit 8a3c918

Browse files
author
Grzegorz Piwowarek
committed
Merge branch 'master' of git://github.com/jgarciaheredero/tutorials into jgarciaheredero-master
# Conflicts: # spring-mvc-java/pom.xml
2 parents ee3db42 + 08c31ec commit 8a3c918

6 files changed

Lines changed: 211 additions & 2 deletions

File tree

‎spring-mvc-java/pom.xml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@
250250
<!-- logging -->
251251
<org.slf4j.version>1.7.21</org.slf4j.version>
252252
<logback.version>1.1.5</logback.version>
253-
253+
254254
<!-- various -->
255255
<hibernate-validator.version>5.2.2.Final</hibernate-validator.version>
256256
<!-- util -->
@@ -263,6 +263,7 @@
263263
<httpcore.version>4.4.1</httpcore.version>
264264
<httpclient.version>4.5</httpclient.version>
265265
<rest-assured.version>2.9.0</rest-assured.version>
266+
<net.sourceforge.htmlunit>2.23</net.sourceforge.htmlunit>
266267
<!-- maven plugins -->
267268
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
268269
<maven-war-plugin.version>2.6</maven-war-plugin.version>
@@ -274,4 +275,5 @@
274275
<aspectj.version>1.8.7</aspectj.version>
275276
</properties>
276277

277-
</project>
278+
</project>
279+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.web.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
9+
@Controller
10+
@RequestMapping("/message")
11+
public class MessageController {
12+
13+
@RequestMapping(value = "/showForm", method = RequestMethod.GET)
14+
public String showForm() {
15+
return "message";
16+
}
17+
18+
@RequestMapping(value = "/processForm", method = RequestMethod.POST)
19+
public String processForm(@RequestParam("message") final String message, final Model model) {
20+
model.addAttribute("message", message);
21+
return "message";
22+
}
23+
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.htmlunit;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import com.gargoylesoftware.htmlunit.WebClient;
7+
import com.gargoylesoftware.htmlunit.html.HtmlPage;
8+
9+
public class HtmlUnitAndJUnitTest {
10+
11+
@Test
12+
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
13+
try (final WebClient webClient = new WebClient()) {
14+
15+
webClient.getOptions().setThrowExceptionOnScriptError(false);
16+
17+
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
18+
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
19+
}
20+
}
21+
22+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.baeldung.htmlunit;
2+
3+
import java.io.IOException;
4+
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Ignore;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.test.context.ContextConfiguration;
12+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13+
import org.springframework.test.context.web.WebAppConfiguration;
14+
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
15+
import org.springframework.web.context.WebApplicationContext;
16+
17+
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
18+
import com.gargoylesoftware.htmlunit.WebClient;
19+
import com.gargoylesoftware.htmlunit.html.HtmlForm;
20+
import com.gargoylesoftware.htmlunit.html.HtmlPage;
21+
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
22+
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
23+
24+
@RunWith(SpringJUnit4ClassRunner.class)
25+
@WebAppConfiguration
26+
@ContextConfiguration(classes = { TestConfig.class })
27+
public class HtmlUnitAndSpringTest {
28+
29+
@Autowired
30+
private WebApplicationContext wac;
31+
32+
private WebClient webClient;
33+
34+
@Before
35+
public void setup() {
36+
webClient = MockMvcWebClientBuilder.webAppContextSetup(wac).build();
37+
}
38+
39+
@Test
40+
@Ignore
41+
public void givenAMessage_whenSent_thenItShows() {
42+
final String text = "Hello world!";
43+
HtmlPage page;
44+
45+
try {
46+
47+
page = webClient.getPage("http://localhost/message/showForm");
48+
System.out.println(page.asXml());
49+
50+
final HtmlTextInput messageText = page.getHtmlElementById("message");
51+
messageText.setValueAttribute(text);
52+
53+
final HtmlForm form = page.getForms().get(0);
54+
final HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");
55+
final HtmlPage newPage = submit.click();
56+
57+
final String receivedText = newPage.getHtmlElementById("received").getTextContent();
58+
59+
Assert.assertEquals(receivedText, text);
60+
System.out.println(newPage.asXml());
61+
62+
} catch (FailingHttpStatusCodeException | IOException e) {
63+
e.printStackTrace();
64+
}
65+
66+
}
67+
68+
@Test
69+
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
70+
try (final WebClient client = new WebClient()) {
71+
72+
webClient.getOptions().setThrowExceptionOnScriptError(false);
73+
74+
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
75+
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
76+
}
77+
}
78+
79+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.htmlunit;
2+
3+
import java.util.List;
4+
5+
import com.gargoylesoftware.htmlunit.WebClient;
6+
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
7+
import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
8+
import com.gargoylesoftware.htmlunit.html.HtmlHeading2;
9+
import com.gargoylesoftware.htmlunit.html.HtmlPage;
10+
11+
public class HtmlUnitWebScraping {
12+
13+
public static void main(final String[] args) throws Exception {
14+
try (final WebClient webClient = new WebClient()) {
15+
16+
webClient.getOptions().setCssEnabled(false);
17+
webClient.getOptions().setJavaScriptEnabled(false);
18+
19+
final HtmlPage page = webClient.getPage("http://www.baeldung.com/full_archive");
20+
final HtmlAnchor latestPostLink = (HtmlAnchor) page.getByXPath("(//ul[@class='car-monthlisting']/li)[1]/a").get(0);
21+
22+
System.out.println("Entering: " + latestPostLink.getHrefAttribute());
23+
24+
final HtmlPage postPage = latestPostLink.click();
25+
26+
final HtmlHeading1 heading1 = (HtmlHeading1) postPage.getByXPath("//h1").get(0);
27+
System.out.println("Title: " + heading1.getTextContent());
28+
29+
final List<HtmlHeading2> headings2 = (List<HtmlHeading2>) postPage.getByXPath("//h2");
30+
31+
final StringBuilder sb = new StringBuilder(heading1.getTextContent());
32+
for (final HtmlHeading2 h2 : headings2) {
33+
sb.append("\n").append(h2.getTextContent());
34+
}
35+
36+
System.out.println(sb.toString());
37+
}
38+
}
39+
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.htmlunit;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.ViewResolver;
7+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
9+
import org.thymeleaf.spring4.SpringTemplateEngine;
10+
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
11+
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
12+
13+
@Configuration
14+
@EnableWebMvc
15+
@ComponentScan(basePackages = { "com.baeldung.web.controller" })
16+
public class TestConfig extends WebMvcConfigurerAdapter {
17+
18+
@Bean
19+
public ViewResolver thymeleafViewResolver() {
20+
final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
21+
viewResolver.setTemplateEngine(templateEngine());
22+
viewResolver.setOrder(1);
23+
return viewResolver;
24+
}
25+
26+
@Bean
27+
public ServletContextTemplateResolver templateResolver() {
28+
final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
29+
templateResolver.setPrefix("/WEB-INF/templates/");
30+
templateResolver.setSuffix(".html");
31+
templateResolver.setTemplateMode("HTML5");
32+
return templateResolver;
33+
}
34+
35+
@Bean
36+
public SpringTemplateEngine templateEngine() {
37+
final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
38+
templateEngine.setTemplateResolver(templateResolver());
39+
return templateEngine;
40+
}
41+
42+
}

0 commit comments

Comments
 (0)