Merge remote-tracking branch 'origin/master'

This commit is contained in:
pivovarit 2016-12-10 14:43:45 +01:00
commit 5679aa4e5c
12 changed files with 98 additions and 94 deletions

View File

@ -38,3 +38,4 @@
- [Guide to the Fork/Join Framework in Java](http://www.baeldung.com/java-fork-join) - [Guide to the Fork/Join Framework in Java](http://www.baeldung.com/java-fork-join)
- [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java) - [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java)
- [How to Convert String to different data types in Java](http://www.baeldung.com/java-string-conversions) - [How to Convert String to different data types in Java](http://www.baeldung.com/java-string-conversions)
- [Introduction to Java Generics](http://www.baeldung.com/java-generics)

View File

@ -14,11 +14,14 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining;
import static org.hamcrest.CoreMatchers.*;
public class EncoderDecoderUnitTest { public class EncoderDecoderUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(EncoderDecoderUnitTest.class); private static final Logger LOGGER = LoggerFactory.getLogger(EncoderDecoderUnitTest.class);
private static final String testUrl = "http://www.baeldung.com/path+1?key1=value+1&key2=value%40%21%242&key3=value%253#dummy+Fragment"; private static final String testUrl = "http://www.baeldung.com?key1=value+1&key2=value%40%21%242&key3=value%253";
private static final String testUrlWithPath = "http://www.baeldung.com/path+1?key1=value+1&key2=value%40%21%242&key3=value%253";
private String encodeValue(String value) { private String encodeValue(String value) {
String encoded = null; String encoded = null;
@ -42,13 +45,11 @@ public class EncoderDecoderUnitTest {
@Test @Test
public void givenURL_whenAnalyze_thenCorrect() throws Exception { public void givenURL_whenAnalyze_thenCorrect() throws Exception {
URI uri = new URI(testUrl); URL url = new URL(testUrl);
Assert.assertThat(uri.getScheme(), CoreMatchers.is("http")); Assert.assertThat(url.getProtocol(), is("http"));
Assert.assertThat(uri.getHost(), CoreMatchers.is("www.baeldung.com")); Assert.assertThat(url.getHost(), is("www.baeldung.com"));
Assert.assertThat(uri.getRawPath(), CoreMatchers.is("/path+1")); Assert.assertThat(url.getQuery(), is("key1=value+1&key2=value%40%21%242&key3=value%253"));
Assert.assertThat(uri.getRawQuery(), CoreMatchers.is("key1=value+1&key2=value%40%21%242&key3=value%253"));
Assert.assertThat(uri.getRawFragment(), CoreMatchers.is("dummy+Fragment"));
} }
@Test @Test
@ -58,35 +59,29 @@ public class EncoderDecoderUnitTest {
requestParams.put("key2", "value@!$2"); requestParams.put("key2", "value@!$2");
requestParams.put("key3", "value%3"); requestParams.put("key3", "value%3");
String path = "path+1"; String encodedURL = requestParams.keySet().stream()
String fragment = "dummy Fragment"; .map(key -> key + "=" + encodeValue(requestParams.get(key)))
.collect(joining("&", "http://www.baeldung.com?", ""));
String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com/" + getPath(path) + "?", "#" + encodeValue(fragment))); Assert.assertThat(testUrl, is(encodedURL));
Assert.assertThat(testUrl, CoreMatchers.is(encodedURL));
} }
@Test @Test
public void givenRequestParam_whenUTF8Scheme_thenDecodeRequestParams() throws Exception { public void givenRequestParam_whenUTF8Scheme_thenDecodeRequestParams() throws Exception {
URI uri = new URI(testUrl); URL url = new URL(testUrl);
String scheme = uri.getScheme(); String query = url.getQuery();
String host = uri.getHost();
String query = uri.getRawQuery();
String path = uri.getRawPath();
String fragment = uri.getRawFragment();
String decodedQuery = Arrays.stream(query.split("&")).map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1])).collect(joining("&")); String decodedQuery = Arrays.stream(query.split("&")).map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1])).collect(joining("&"));
String decodedPath = Arrays.stream(path.split("/")).map(param -> getPath(param)).collect(joining("/"));
Assert.assertEquals("http://www.baeldung.com/path+1?key1=value 1&key2=value@!$2&key3=value%3#dummy Fragment", scheme + "://" + host + decodedPath + "?" + decodedQuery + "#" + decode(fragment)); Assert.assertEquals("http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery);
} }
private String getPath(String path) { private String encodePath(String path) {
try { try {
path = new URI(null, null, path, null).getPath(); path = new URI(null, null, path, null).getPath();
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); LOGGER.error("Error encoding parameter {}", e.getMessage(), e);
} }
return path; return path;
} }
@ -98,4 +93,22 @@ public class EncoderDecoderUnitTest {
Assert.assertEquals("/Path 1/Path+2", uri.getPath()); Assert.assertEquals("/Path 1/Path+2", uri.getPath());
Assert.assertEquals("/Path%201/Path+2", uri.getRawPath()); Assert.assertEquals("/Path%201/Path+2", uri.getRawPath());
} }
@Test
public void givenPathAndRequestParam_whenUTF8Scheme_thenEncode() throws Exception {
Map<String, String> requestParams = new HashMap<>();
requestParams.put("key1", "value 1");
requestParams.put("key2", "value@!$2");
requestParams.put("key3", "value%3");
String path = "path+1";
String encodedURL = requestParams.keySet().stream()
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
.collect(joining("&", "http://www.baeldung.com/" + encodePath(path) + "?", ""));
Assert.assertThat(testUrlWithPath, is(encodedURL));
}
} }

View File

@ -9,9 +9,11 @@
<packaging>war</packaging> <packaging>war</packaging>
<properties> <properties>
<resteasy.version>3.0.14.Final</resteasy.version> <resteasy.version>3.0.19.Final</resteasy.version>
<junit.version>4.12</junit.version>
<commons-io.version>2.5</commons-io.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version> <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<cargo-maven2-plugin.version>1.6.0</cargo-maven2-plugin.version> <cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
</properties> </properties>
<build> <build>
@ -93,13 +95,13 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.4</version> <version>${junit.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>2.4</version> <version>${commons-io.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -9,7 +9,7 @@
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version> <version>${maven-compiler-plugin.version}</version>
<configuration> <configuration>
<source>1.8</source> <source>1.8</source>
<target>1.8</target> <target>1.8</target>
@ -18,7 +18,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version> <version>${maven-surefire-plugin.version}</version>
<configuration> <configuration>
<testFailureIgnore>true</testFailureIgnore> <testFailureIgnore>true</testFailureIgnore>
<includes> <includes>
@ -38,7 +38,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version> <version>${maven-surefire-plugin.version}</version>
<configuration> <configuration>
<includes> <includes>
<include>**/*LiveTest.java</include> <include>**/*LiveTest.java</include>
@ -55,17 +55,26 @@
<dependency> <dependency>
<groupId>org.seleniumhq.selenium</groupId> <groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId> <artifactId>selenium-java</artifactId>
<version>3.0.1</version> <version>${selenium-java.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version> <version>${junit.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.testng</groupId> <groupId>org.testng</groupId>
<artifactId>testng</artifactId> <artifactId>testng</artifactId>
<version>6.9.13.6</version> <version>${testng.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
<properties>
<junit.version>4.12</junit.version>
<testng.version>6.10</testng.version>
<selenium-java.version>3.0.1</selenium-java.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
</properties>
</project> </project>

View File

@ -115,11 +115,11 @@
</profiles> </profiles>
<properties> <properties>
<spring.version>4.3.2.RELEASE</spring.version> <spring.version>4.3.4.RELEASE</spring.version>
<akka.version>2.4.8</akka.version> <akka.version>2.4.14</akka.version>
<junit.version>4.12</junit.version> <junit.version>4.12</junit.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version> <maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version> <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
</properties> </properties>

View File

@ -11,7 +11,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version> <version>1.4.2.RELEASE</version>
</parent> </parent>
<dependencies> <dependencies>
@ -134,7 +134,7 @@
<dependency> <dependency>
<groupId>org.assertj</groupId> <groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId> <artifactId>assertj-core</artifactId>
<version>3.5.1</version> <version>${assertj.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
@ -158,13 +158,13 @@
<dependency> <dependency>
<groupId>org.easymock</groupId> <groupId>org.easymock</groupId>
<artifactId>easymock</artifactId> <artifactId>easymock</artifactId>
<version>3.4</version> <version>${easymock.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.ehcache</groupId> <groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId> <artifactId>ehcache</artifactId>
<version>3.1.3</version> <version>${ehcache.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
@ -273,42 +273,17 @@
<properties> <properties>
<!-- Spring --> <!-- Spring -->
<org.springframework.version>4.3.1.RELEASE</org.springframework.version> <org.springframework.version>4.3.4.RELEASE</org.springframework.version>
<org.springframework.security.version>4.0.4.RELEASE</org.springframework.security.version> <org.springframework.security.version>4.2.0.RELEASE</org.springframework.security.version>
<javassist.version>3.20.0-GA</javassist.version>
<jstl.version>1.2</jstl.version>
<!-- persistence --> <!-- persistence -->
<hibernate.version>4.3.11.Final</hibernate.version> <hibernate.version>5.2.5.Final</hibernate.version>
<mysql-connector-java.version>5.1.38</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.13</org.slf4j.version>
<logback.version>1.1.3</logback.version>
<!-- various -->
<hibernate-validator.version>5.2.2.Final</hibernate-validator.version>
<!-- util --> <!-- util -->
<guava.version>19.0</guava.version> <guava.version>19.0</guava.version>
<commons-lang3.version>3.4</commons-lang3.version> <ehcache.version>3.1.3</ehcache.version>
<easymock.version>3.4</easymock.version>
<!-- testing --> <assertj.version>3.6.1</assertj.version>
<org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.12</junit.version>
<mockito.version>1.10.19</mockito.version>
<httpcore.version>4.4.1</httpcore.version>
<httpclient.version>4.5</httpclient.version>
<rest-assured.version>2.9.0</rest-assured.version>
<!-- maven plugins -->
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.6</maven-war-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
</properties> </properties>

View File

@ -10,9 +10,9 @@
<properties> <properties>
<env.camel.version>2.16.1</env.camel.version> <env.camel.version>2.16.1</env.camel.version>
<env.spring.version>4.2.4.RELEASE</env.spring.version> <env.spring.version>4.3.4.RELEASE</env.spring.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version> <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<java.version>1.7</java.version> <java.version>1.8</java.version>
<junit.version>4.12</junit.version> <junit.version>4.12</junit.version>
</properties> </properties>

View File

@ -12,8 +12,9 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>4.2.5.RELEASE</org.springframework.version> <org.springframework.version>4.3.4.RELEASE</org.springframework.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version> <junit.version>4.12</junit.version>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version> <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
</properties> </properties>
@ -21,7 +22,7 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.11</version> <version>${junit.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>

View File

@ -11,9 +11,9 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.0.RELEASE</spring.version> <spring.version>4.3.4.RELEASE</spring.version>
<spring.batch.version>3.0.5.RELEASE</spring.batch.version> <spring.batch.version>3.0.7.RELEASE</spring.batch.version>
<sqlite.version>3.8.11.2</sqlite.version> <sqlite.version>3.15.1</sqlite.version>
</properties> </properties>
<dependencies> <dependencies>

View File

@ -73,19 +73,19 @@
<dependency> <dependency>
<groupId>org.subethamail</groupId> <groupId>org.subethamail</groupId>
<artifactId>subethasmtp</artifactId> <artifactId>subethasmtp</artifactId>
<version>3.1.7</version> <version>${subethasmtp.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.webjars</groupId> <groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId> <artifactId>bootstrap</artifactId>
<version>3.3.7-1</version> <version>${bootstrap.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.webjars</groupId> <groupId>org.webjars</groupId>
<artifactId>jquery</artifactId> <artifactId>jquery</artifactId>
<version>3.1.1</version> <version>${jquery.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
@ -122,7 +122,7 @@
<plugin> <plugin>
<groupId>pl.project13.maven</groupId> <groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId> <artifactId>git-commit-id-plugin</artifactId>
<version>2.2.1</version> <version>${git-commit-id-plugin.version}</version>
</plugin> </plugin>
<plugin> <plugin>
@ -182,6 +182,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<spring.version>4.3.4.RELEASE</spring.version> <spring.version>4.3.4.RELEASE</spring.version>
<git-commit-id-plugin.version>2.2.1</git-commit-id-plugin.version>
<jquery.version>3.1.1</jquery.version>
<bootstrap.version>3.3.7-1</bootstrap.version>
<subethasmtp.version>3.1.7</subethasmtp.version>
</properties> </properties>
</project> </project>

View File

@ -1,10 +1,7 @@
package com.baeldung.htmlunit; package com.baeldung.htmlunit;
import java.io.IOException;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -14,7 +11,6 @@ import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder; import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlPage;
@ -33,8 +29,7 @@ public class HtmlUnitAndSpringTest {
@Before @Before
public void setup() { public void setup() {
webClient = MockMvcWebClientBuilder webClient = MockMvcWebClientBuilder.webAppContextSetup(wac).build();
.webAppContextSetup(wac).build();
} }
@Test @Test
@ -49,12 +44,10 @@ public class HtmlUnitAndSpringTest {
messageText.setValueAttribute(text); messageText.setValueAttribute(text);
HtmlForm form = page.getForms().get(0); HtmlForm form = page.getForms().get(0);
HtmlSubmitInput submit = form.getOneHtmlElementByAttribute( HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");
"input", "type", "submit");
HtmlPage newPage = submit.click(); HtmlPage newPage = submit.click();
String receivedText = newPage.getHtmlElementById("received") String receivedText = newPage.getHtmlElementById("received").getTextContent();
.getTextContent();
Assert.assertEquals(receivedText, text); Assert.assertEquals(receivedText, text);
} }

6
spring-session/README.md Normal file
View File

@ -0,0 +1,6 @@
=========
## Spring Session Examples
### Relevant Articles:
- [Introduction to Spring Session](http://www.baeldung.com/spring-session)