Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-633
Conflicts: pom.xml
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<artifactId>apache-velocity</artifactId>
|
||||
|
||||
<packaging>war</packaging>
|
||||
<name>apache-velocity</name>
|
||||
|
||||
<properties>
|
||||
<jdk.version>1.8</jdk.version>
|
||||
<jstl.version>1.2</jstl.version>
|
||||
<junit.version>4.11</junit.version>
|
||||
<logback.version>1.0.13</logback.version>
|
||||
<jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version>
|
||||
<maven-compiler-plugin.version>3.6.0</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>
|
||||
<org.apache.httpcomponents.version>4.5.2</org.apache.httpcomponents.version>
|
||||
<velocity-version>1.7</velocity-version>
|
||||
<velocity-tools-version>2.0</velocity-tools-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity</artifactId>
|
||||
<version>${velocity-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity-tools</artifactId>
|
||||
<version>${velocity-tools-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${jcl-over-slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${org.apache.httpcomponents.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>apache-velocity</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${jdk.version}</source>
|
||||
<target>${jdk.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
<systemPropertyVariables>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.apache.velocity.model;
|
||||
|
||||
public class Product {
|
||||
|
||||
private String name;
|
||||
private double price;
|
||||
|
||||
public Product(String name, double price) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Product{" + "name='" + name + '\'' + ", price=" + price + '}';
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.apache.velocity.service;
|
||||
|
||||
import com.baeldung.apache.velocity.model.Product;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ProductService {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(ProductService.class);
|
||||
|
||||
public List<Product> getProducts() {
|
||||
logger.debug("Product service returning list of products");
|
||||
|
||||
return Arrays.asList(new Product("Laptop", 31000.00), new Product("Mobile", 16000.00),
|
||||
new Product("Tablet", 15000.00), new Product("Camera", 23000.00));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.apache.velocity.servlet;
|
||||
|
||||
import com.baeldung.apache.velocity.model.Product;
|
||||
import com.baeldung.apache.velocity.service.ProductService;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.context.Context;
|
||||
import org.apache.velocity.tools.view.VelocityLayoutServlet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
public class LayoutServlet extends VelocityLayoutServlet {
|
||||
ProductService service = new ProductService();
|
||||
|
||||
@Override
|
||||
public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context context) {
|
||||
|
||||
Logger logger= LoggerFactory.getLogger(LayoutServlet.class);
|
||||
|
||||
List<Product> products = service.getProducts();
|
||||
|
||||
context.put("products", products);
|
||||
|
||||
Template template = null;
|
||||
|
||||
try {
|
||||
template = getTemplate("templates/layoutdemo.vm");
|
||||
|
||||
response.setHeader("Template Returned", "Success");
|
||||
} catch (Exception e) {
|
||||
logger.error("Error while reading the template ",e);
|
||||
}
|
||||
|
||||
return template;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.apache.velocity.servlet;
|
||||
|
||||
import com.baeldung.apache.velocity.model.Product;
|
||||
import com.baeldung.apache.velocity.service.ProductService;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.context.Context;
|
||||
import org.apache.velocity.tools.view.VelocityViewServlet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
public class ProductServlet extends VelocityViewServlet {
|
||||
|
||||
ProductService service = new ProductService();
|
||||
|
||||
@Override
|
||||
public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context context) {
|
||||
|
||||
Logger logger= LoggerFactory.getLogger(ProductServlet.class);
|
||||
|
||||
List<Product> products = service.getProducts();
|
||||
|
||||
context.put("products", products);
|
||||
|
||||
Template template = null;
|
||||
|
||||
try {
|
||||
template = getTemplate("templates/index.vm");
|
||||
response.setHeader("Template Returned", "Success");
|
||||
} catch (Exception e) {
|
||||
logger.error("Error while reading the template ", e);
|
||||
}
|
||||
|
||||
return template;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||
|
||||
<Pattern>
|
||||
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
|
||||
</Pattern>
|
||||
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<logger name="com.baeldung.apache.velocity.service" level="debug"
|
||||
additivity="false">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</logger>
|
||||
|
||||
<root level="error">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
@@ -0,0 +1,4 @@
|
||||
resource.loader=webapp
|
||||
webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader
|
||||
webapp.resource.loader.path = .
|
||||
webapp.resource.loader.cache = true
|
||||
@@ -0,0 +1,49 @@
|
||||
<!DOCTYPE web-app PUBLIC
|
||||
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
|
||||
"http://java.sun.com/dtd/web-app_2_3.dtd" >
|
||||
|
||||
<web-app>
|
||||
<display-name>apache-velocity</display-name>
|
||||
<servlet>
|
||||
<servlet-name>ProductServlet</servlet-name>
|
||||
<servlet-class>com.baeldung.apache.velocity.servlet.ProductServlet</servlet-class>
|
||||
</servlet>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>LayoutServlet</servlet-name>
|
||||
<servlet-class>com.baeldung.apache.velocity.servlet.LayoutServlet</servlet-class>
|
||||
</servlet>
|
||||
<servlet>
|
||||
<servlet-name>velocityLayout</servlet-name>
|
||||
<servlet-class>org.apache.velocity.tools.view.VelocityLayoutServlet</servlet-class>
|
||||
|
||||
<init-param>
|
||||
<param-name>org.apache.velocity.properties</param-name>
|
||||
<param-value>/WEB-INF/velocity.properties</param-value>
|
||||
</init-param>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>ProductServlet</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>LayoutServlet</servlet-name>
|
||||
<url-pattern>/layout</url-pattern>
|
||||
</servlet-mapping>
|
||||
<servlet-mapping>
|
||||
<servlet-name>velocityLayout</servlet-name>
|
||||
<url-pattern>*.vm</url-pattern>
|
||||
</servlet-mapping>
|
||||
<!-- session timeout -->
|
||||
<session-config>
|
||||
<session-timeout>
|
||||
30
|
||||
</session-timeout>
|
||||
</session-config>
|
||||
|
||||
<!-- welcome file -->
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.html</welcome-file>
|
||||
</welcome-file-list>
|
||||
</web-app>
|
||||
@@ -0,0 +1,4 @@
|
||||
<div
|
||||
style="background: #63B175; text-align: center; padding: 5px; margin-top: 10px;">
|
||||
@Copyright baeldung.com
|
||||
</div>
|
||||
@@ -0,0 +1,5 @@
|
||||
<div style="background: #63B175; height: 80px; padding: 5px;">
|
||||
<div style="float: left">
|
||||
<h1> Layout Demo Page</h1>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,22 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Velocity</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
#parse("/fragments/header.vm")
|
||||
</div>
|
||||
|
||||
|
||||
<div>
|
||||
|
||||
<!-- View index.vm is inserted here -->
|
||||
$screen_content
|
||||
|
||||
</div>
|
||||
|
||||
<div>
|
||||
#parse("/fragments/footer.vm")
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,63 @@
|
||||
<HTML>
|
||||
<HEAD>
|
||||
<TITLE>Online Electronic Store</TITLE>
|
||||
<style>
|
||||
body {background-color: powderblue;}
|
||||
h1 {color: blue;}
|
||||
p {color: red;}
|
||||
table.gridtable {
|
||||
font-family: verdana,arial,sans-serif;
|
||||
font-size:11px;
|
||||
color:#333333;
|
||||
border-width: 1px;
|
||||
border-color: #666666;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
table.gridtable th {
|
||||
border-width: 1px;
|
||||
padding: 8px;
|
||||
border-style: solid;
|
||||
border-color: #666666;
|
||||
background-color: #dedede;
|
||||
}
|
||||
table.gridtable td {
|
||||
border-width: 1px;
|
||||
padding: 8px;
|
||||
border-style: solid;
|
||||
border-color: #666666;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
</HEAD>
|
||||
<BODY>
|
||||
<CENTER>
|
||||
<h1>Today's Offers</h1>
|
||||
<BR/>
|
||||
<BR/>
|
||||
<h2>$products.size() Products on Sale!</h2>
|
||||
<BR/>
|
||||
We are proud to offer these fine products
|
||||
at these amazing prices.
|
||||
<BR/>
|
||||
<BR/>
|
||||
#set( $count = 1 )
|
||||
<TABLE class="gridtable">
|
||||
<TR>
|
||||
<TH>Serial #</TH><TH>Product Name</TH><TH>Price</TH>
|
||||
</TR>
|
||||
#foreach( $product in $products )
|
||||
<TR>
|
||||
<TD>$count)</TD>
|
||||
<TD>$product.getName()</TD>
|
||||
<TD>$product.getPrice()</TD>
|
||||
</TR>
|
||||
#set( $count = $count + 1 )
|
||||
#end
|
||||
</TABLE>
|
||||
<BR/>
|
||||
</CENTER>
|
||||
|
||||
</BODY>
|
||||
</HTML>
|
||||
@@ -0,0 +1,27 @@
|
||||
#set( $layout = "layout.vm" )
|
||||
<CENTER>
|
||||
<h1>Today's Offers</h1>
|
||||
<BR/>
|
||||
<BR/>
|
||||
<h2>$products.size() Products on Sale!</h2>
|
||||
<BR/>
|
||||
We are proud to offer these fine products
|
||||
at these amazing prices.
|
||||
<BR/>
|
||||
<BR/>
|
||||
#set( $count = 1 )
|
||||
<TABLE class="gridtable">
|
||||
<TR>
|
||||
<TH>Serial #</TH><TH>Product Name</TH><TH>Price</TH>
|
||||
</TR>
|
||||
#foreach( $product in $products )
|
||||
<TR>
|
||||
<TD>$count)</TD>
|
||||
<TD>$product.getName()</TD>
|
||||
<TD>$product.getPrice()</TD>
|
||||
</TR>
|
||||
#set( $count = $count + 1 )
|
||||
#end
|
||||
</TABLE>
|
||||
<BR/>
|
||||
</CENTER>
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.apache.velocity.servlet;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
||||
public class LayoutServletLiveTest {
|
||||
|
||||
@Test
|
||||
public void whenRequestUsingHttpClient_thenCorrectResponse() throws Exception {
|
||||
|
||||
HttpClient client = new DefaultHttpClient();
|
||||
HttpGet method= new HttpGet("http://localhost:8080/layout");
|
||||
|
||||
HttpResponse httpResponse = client.execute(method);
|
||||
|
||||
assertEquals("Success", httpResponse.getHeaders("Template Returned")[0].getValue());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.apache.velocity.servlet;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ProductServletLiveTest {
|
||||
|
||||
@Test
|
||||
public void whenRequestUsingHttpClient_thenCorrectResponse() throws Exception {
|
||||
|
||||
HttpClient client = new DefaultHttpClient();
|
||||
HttpGet method= new HttpGet("http://localhost:8080/");
|
||||
|
||||
HttpResponse httpResponse = client.execute(method);
|
||||
|
||||
assertEquals("Success", httpResponse.getHeaders("Template Returned")[0].getValue());
|
||||
|
||||
}
|
||||
}
|
||||
+1
-39
@@ -9,45 +9,7 @@
|
||||
<name>core-java</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j</groupId>
|
||||
<artifactId>neo4j</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j.driver</groupId>
|
||||
<artifactId>neo4j-java-driver</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j</groupId>
|
||||
<artifactId>neo4j-jdbc-driver</artifactId>
|
||||
<version>3.0.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j</groupId>
|
||||
<artifactId>neo4j-ogm-core</artifactId>
|
||||
<version>2.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j</groupId>
|
||||
<artifactId>neo4j-ogm-embedded-driver</artifactId>
|
||||
<version>2.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.inject</groupId>
|
||||
<artifactId>guice</artifactId>
|
||||
<version>4.1.0</version>
|
||||
<classifier>no_aop</classifier>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.collections</groupId>
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.java_8_features;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
private Integer age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.java8;
|
||||
|
||||
import com.baeldung.java_8_features.Person;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class Java8MaxMinTest {
|
||||
|
||||
@Test
|
||||
public void whenListIsOfIntegerThenMaxCanBeDoneUsingIntegerComparator() {
|
||||
//given
|
||||
final List<Integer> listOfIntegers = Arrays.asList(1, 2, 3, 4, 56, 7, 89, 10);
|
||||
final Integer expectedResult = 89;
|
||||
|
||||
//then
|
||||
final Integer max = listOfIntegers
|
||||
.stream()
|
||||
.mapToInt(v -> v)
|
||||
.max().orElseThrow(NoSuchElementException::new);
|
||||
|
||||
assertEquals("Should be 89", expectedResult, max);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListIsOfPersonObjectThenMinCanBeDoneUsingCustomComparatorThroughLambda() {
|
||||
//given
|
||||
final Person alex = new Person("Alex", 23);
|
||||
final Person john = new Person("John", 40);
|
||||
final Person peter = new Person("Peter", 32);
|
||||
final List<Person> people = Arrays.asList(alex, john, peter);
|
||||
|
||||
//then
|
||||
final Person minByAge = people
|
||||
.stream()
|
||||
.min(Comparator.comparing(Person::getAge))
|
||||
.orElseThrow(NoSuchElementException::new);
|
||||
|
||||
assertEquals("Should be Alex", alex, minByAge);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import com.baeldung.string.JoinerSplitter;
|
||||
public class JoinerSplitterTest {
|
||||
|
||||
@Test
|
||||
public void givenArray_transformedToStream_convertToString() {
|
||||
public void provided_array_convert_to_stream_and_convert_to_string() {
|
||||
|
||||
String[] programming_languages = {"java", "python", "nodejs", "ruby"};
|
||||
|
||||
@@ -24,6 +24,7 @@ public class JoinerSplitterTest {
|
||||
|
||||
@Test
|
||||
public void givenArray_transformedToStream_convertToPrefixPostfixString() {
|
||||
|
||||
String[] programming_languages = {"java", "python",
|
||||
"nodejs", "ruby"};
|
||||
String expectation = "[java,python,nodejs,ruby]";
|
||||
@@ -34,6 +35,7 @@ public class JoinerSplitterTest {
|
||||
|
||||
@Test
|
||||
public void givenString_transformedToStream_convertToList() {
|
||||
|
||||
String programming_languages = "java,python,nodejs,ruby";
|
||||
|
||||
List<String> expectation = new ArrayList<String>();
|
||||
@@ -49,6 +51,7 @@ public class JoinerSplitterTest {
|
||||
|
||||
@Test
|
||||
public void givenString_transformedToStream_convertToListOfChar() {
|
||||
|
||||
String programming_languages = "java,python,nodejs,ruby";
|
||||
|
||||
List<Character> expectation = new ArrayList<Character>();
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
|
||||
class EventBusWrapper {
|
||||
|
||||
private static EventBus eventBus = new EventBus();
|
||||
|
||||
static void register(Object object) {
|
||||
eventBus.register(object);
|
||||
}
|
||||
|
||||
static void unregister(Object object) {
|
||||
eventBus.unregister(object);
|
||||
}
|
||||
|
||||
static void post(Object object) {
|
||||
eventBus.post(object);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import com.google.common.eventbus.DeadEvent;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -21,6 +22,12 @@ public class EventListener {
|
||||
eventsHandled++;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void handleDeadEvent(DeadEvent deadEvent) {
|
||||
LOG.info("unhandled event [" + deadEvent.getEvent() + "]");
|
||||
eventsHandled++;
|
||||
}
|
||||
|
||||
public int getEventsHandled() {
|
||||
return eventsHandled;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -9,25 +10,27 @@ import static org.junit.Assert.*;
|
||||
public class GuavaEventBusTest {
|
||||
|
||||
private EventListener listener;
|
||||
private EventBus eventBus;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
eventBus = new EventBus();
|
||||
listener = new EventListener();
|
||||
EventBusWrapper.register(listener);
|
||||
|
||||
eventBus.register(listener);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
EventBusWrapper.unregister(listener);
|
||||
eventBus.unregister(listener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringEvent_whenEventHandled_thenSuccess() {
|
||||
listener.resetEventsHandled();
|
||||
|
||||
EventBusWrapper.post("String Event");
|
||||
eventBus.post("String Event");
|
||||
assertEquals(1, listener.getEventsHandled());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -35,8 +38,18 @@ public class GuavaEventBusTest {
|
||||
listener.resetEventsHandled();
|
||||
|
||||
CustomEvent customEvent = new CustomEvent("Custom Event");
|
||||
EventBusWrapper.post(customEvent);
|
||||
eventBus.post(customEvent);
|
||||
|
||||
assertEquals(1, listener.getEventsHandled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnSubscribedEvent_whenEventHandledByDeadEvent_thenSuccess() throws InterruptedException {
|
||||
listener.resetEventsHandled();
|
||||
|
||||
eventBus.post(12345);
|
||||
|
||||
assertEquals(1, listener.getEventsHandled());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
-2
@@ -1,4 +1,5 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
@@ -113,6 +114,13 @@
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.tomakehurst</groupId>
|
||||
<artifactId>wiremock</artifactId>
|
||||
<version>${wiremock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
@@ -145,7 +153,7 @@
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
@@ -202,6 +210,7 @@
|
||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito.version>1.10.19</mockito.version>
|
||||
<wiremock.version>2.5.1</wiremock.version>
|
||||
|
||||
<httpcore.version>4.4.5</httpcore.version>
|
||||
<httpclient.version>4.5.2</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
|
||||
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
package org.baeldung.httpclient.advancedconfig;
|
||||
|
||||
|
||||
import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
||||
import org.apache.http.HttpHeaders;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.AuthCache;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.auth.BasicScheme;
|
||||
import org.apache.http.impl.client.BasicAuthCache;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class HttpClientAdvancedConfiguration {
|
||||
|
||||
@Rule
|
||||
public WireMockRule serviceMock = new WireMockRule(8089);
|
||||
|
||||
@Rule
|
||||
public WireMockRule proxyMock = new WireMockRule(8090);
|
||||
|
||||
@Test
|
||||
public void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException {
|
||||
//given
|
||||
String userAgent = "BaeldungAgent/1.0";
|
||||
serviceMock.stubFor(get(urlEqualTo("/detail"))
|
||||
.withHeader("User-Agent", equalTo(userAgent))
|
||||
.willReturn(aResponse()
|
||||
.withStatus(200)));
|
||||
|
||||
HttpClient httpClient = HttpClients.createDefault();
|
||||
HttpGet httpGet = new HttpGet("http://localhost:8089/detail");
|
||||
httpGet.setHeader(HttpHeaders.USER_AGENT, userAgent);
|
||||
|
||||
//when
|
||||
HttpResponse response = httpClient.execute(httpGet);
|
||||
|
||||
//then
|
||||
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException {
|
||||
//given
|
||||
String xmlBody = "<xml><id>1</id></xml>";
|
||||
serviceMock.stubFor(post(urlEqualTo("/person"))
|
||||
.withHeader("Content-Type", equalTo("application/xml"))
|
||||
.withRequestBody(equalTo(xmlBody))
|
||||
.willReturn(aResponse()
|
||||
.withStatus(200)));
|
||||
|
||||
HttpClient httpClient = HttpClients.createDefault();
|
||||
HttpPost httpPost = new HttpPost("http://localhost:8089/person");
|
||||
httpPost.setHeader("Content-Type", "application/xml");
|
||||
StringEntity xmlEntity = new StringEntity(xmlBody);
|
||||
httpPost.setEntity(xmlEntity);
|
||||
|
||||
//when
|
||||
HttpResponse response = httpClient.execute(httpPost);
|
||||
|
||||
//then
|
||||
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaProxy_shouldReturn200() throws IOException {
|
||||
//given
|
||||
proxyMock.stubFor(get(urlMatching(".*"))
|
||||
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
|
||||
|
||||
serviceMock.stubFor(get(urlEqualTo("/private"))
|
||||
.willReturn(aResponse().withStatus(200)));
|
||||
|
||||
|
||||
HttpHost proxy = new HttpHost("localhost", 8090);
|
||||
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
|
||||
HttpClient httpclient = HttpClients.custom()
|
||||
.setRoutePlanner(routePlanner)
|
||||
.build();
|
||||
|
||||
//when
|
||||
final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
|
||||
HttpResponse response = httpclient.execute(httpGet);
|
||||
|
||||
//then
|
||||
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
||||
proxyMock.verify(getRequestedFor(urlEqualTo("/private")));
|
||||
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
|
||||
//given
|
||||
proxyMock.stubFor(get(urlMatching("/private"))
|
||||
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
|
||||
serviceMock.stubFor(get(urlEqualTo("/private"))
|
||||
.willReturn(aResponse().withStatus(200)));
|
||||
|
||||
|
||||
HttpHost proxy = new HttpHost("localhost", 8090);
|
||||
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
|
||||
|
||||
// Client credentials
|
||||
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||
credentialsProvider.setCredentials(new AuthScope(proxy),
|
||||
new UsernamePasswordCredentials("username_admin", "secret_password"));
|
||||
|
||||
|
||||
// Create AuthCache instance
|
||||
AuthCache authCache = new BasicAuthCache();
|
||||
|
||||
// Generate BASIC scheme object and add it to the local auth cache
|
||||
BasicScheme basicAuth = new BasicScheme();
|
||||
authCache.put(proxy, basicAuth);
|
||||
HttpClientContext context = HttpClientContext.create();
|
||||
context.setCredentialsProvider(credentialsProvider);
|
||||
context.setAuthCache(authCache);
|
||||
|
||||
|
||||
HttpClient httpclient = HttpClients.custom()
|
||||
.setRoutePlanner(routePlanner)
|
||||
.setDefaultCredentialsProvider(credentialsProvider)
|
||||
.build();
|
||||
|
||||
|
||||
//when
|
||||
final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
|
||||
HttpResponse response = httpclient.execute(httpGet, context);
|
||||
|
||||
//then
|
||||
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
||||
proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
|
||||
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
FROM openjdk:8-jre-alpine
|
||||
ADD target/mesos-marathon-0.0.1-SNAPSHOT.jar app.jar
|
||||
EXPOSE 8082
|
||||
ENTRYPOINT ["java","-jar","/app.jar"]
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
docker login -u mogronalol -p $DOCKER_PASSWORD
|
||||
docker build -t baeldung/mesos-marathon-demo:$BUILD_NUMBER .
|
||||
docker push baeldung/mesos-marathon-demo:$BUILD_NUMBER
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"id": "mesos-marathon-demo",
|
||||
"container": {
|
||||
"type": "DOCKER",
|
||||
"docker": {
|
||||
"image": "",
|
||||
"network": "BRIDGE",
|
||||
"portMappings": [
|
||||
{ "containerPort": 8082, "hostPort": 0 }
|
||||
]
|
||||
},
|
||||
"volumes": []
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>mesos-marathon</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.1.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>1.5.1.RELEASE</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.mogronalol;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.mogronalol;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController(value = "/")
|
||||
public class HelloController {
|
||||
|
||||
@GetMapping
|
||||
@ResponseBody
|
||||
public String getMapping() {
|
||||
return "Hello world";
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user