merged PR locally

This commit is contained in:
Seun Matt 2017-06-16 11:26:52 +01:00
commit b8fe12f709
35 changed files with 300 additions and 54 deletions

View File

@ -113,4 +113,21 @@
- [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep)
- [LongAdder and LongAccumulator in Java](http://www.baeldung.com/java-longadder-and-longaccumulator)
- [Using Java MappedByteBuffer](http://www.baeldung.com/java-mapped-byte-buffer)
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)
- [How to Copy an Array in Java](http://www.baeldung.com/java-array-copy)
- [Introduction to JDBC](http://www.baeldung.com/java-jdbc)
- [Guide to CopyOnWriteArrayList](http://www.baeldung.com/java-copy-on-write-arraylist)
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
- [Converting a Stack Trace to a String in Java](http://www.baeldung.com/java-stacktrace-to-string)
- [Guide to the Java Phaser](http://www.baeldung.com/java-phaser)
- [Count Occurrences of a Char in a String](http://www.baeldung.com/java-count-chars)
- [Java Double Brace Initialization](http://www.baeldung.com/java-double-brace-initialization)
- [The StackOverflowError in Java](http://www.baeldung.com/java-stack-overflow-error)
- [Split a String in Java](http://www.baeldung.com/java-split-string)
- [Introduction to Java Serialization](http://www.baeldung.com/java-serialization)
- [How to Remove the Last Character of a String?](http://www.baeldung.com/java-remove-last-character-of-string)
- [Guide to Synchronized Keyword in Java](http://www.baeldung.com/java-synchronized)
- [ClassNotFoundException vs NoClassDefFoundError](http://www.baeldung.com/java-classnotfoundexception-and-noclassdeffounderror)
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
- [How to Get the Last Element of a Stream in Java?](http://www.baeldung.com/java-stream-last-element)
- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char)

View File

@ -0,0 +1,51 @@
package com.baeldung.maths;
import java.math.BigDecimal;
public class FloatingPointArithmetic {
public static void main(String[] args) {
double a = 13.22;
double b = 4.88;
double c = 21.45;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
double sum_ab = a + b;
System.out.println("a + b = " + sum_ab);
double abc = a + b + c;
System.out.println("a + b + c = " + abc);
double ab_c = sum_ab + c;
System.out.println("ab + c = " + ab_c);
double sum_ac = a + c;
System.out.println("a + c = " + sum_ac);
double acb = a + c + b;
System.out.println("a + c + b = " + acb);
double ac_b = sum_ac + b;
System.out.println("ac + b = " + ac_b);
double ab = 18.1;
double ac = 34.67;
double sum_ab_c = ab + c;
double sum_ac_b = ac + b;
System.out.println("ab + c = " + sum_ab_c);
System.out.println("ac + b = " + sum_ac_b);
BigDecimal d = new BigDecimal(String.valueOf(a));
BigDecimal e = new BigDecimal(String.valueOf(b));
BigDecimal f = new BigDecimal(String.valueOf(c));
BigDecimal def = d.add(e).add(f);
BigDecimal dfe = d.add(f).add(e);
System.out.println("d + e + f = " + def);
System.out.println("d + f + e = " + dfe);
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.java.currentmethod;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* The class presents various ways of finding the name of currently executed method.
*/
public class CurrentlyExecutedMethodFinderTest {
@Test
public void givenCurrentThread_whenGetStackTrace_thenFindMethod() {
final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
assertEquals("givenCurrentThread_whenGetStackTrace_thenFindMethod", stackTrace[1].getMethodName());
}
@Test
public void givenException_whenGetStackTrace_thenFindMethod() {
String methodName = new Exception().getStackTrace()[0].getMethodName();
assertEquals("givenException_whenGetStackTrace_thenFindMethod", methodName);
}
@Test
public void givenThrowable_whenGetStacktrace_thenFindMethod() {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
assertEquals("givenThrowable_whenGetStacktrace_thenFindMethod", stackTrace[0].getMethodName());
}
@Test
public void givenObject_whenGetEnclosingMethod_thenFindMethod() {
String methodName = new Object() {}.getClass().getEnclosingMethod().getName();
assertEquals("givenObject_whenGetEnclosingMethod_thenFindMethod", methodName);
}
@Test
public void givenLocal_whenGetEnclosingMethod_thenFindMethod() {
class Local {};
String methodName = Local.class.getEnclosingMethod().getName();
assertEquals("givenLocal_whenGetEnclosingMethod_thenFindMethod", methodName);
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.maths;
import java.math.BigDecimal;
import org.junit.Assert;
import org.junit.Test;
public class FloatingPointArithmeticTest {
@Test
public void givenDecimalNumbers_whenAddedTogether_thenGetExpectedResult() {
double a = 13.22;
double b = 4.88;
double c = 21.45;
double result = 39.55;
double abc = a + b + c;
double acb = a + c + b;
Assert.assertEquals(result, abc, 0);
Assert.assertNotEquals(result, acb, 0);
double ab = 18.1;
double ac = 34.67;
double ab_c = ab + c;
double ac_b = ac + b;
Assert.assertEquals(result, ab_c, 0);
Assert.assertNotEquals(result, ac_b, 0);
BigDecimal d = new BigDecimal(String.valueOf(a));
BigDecimal e = new BigDecimal(String.valueOf(b));
BigDecimal f = new BigDecimal(String.valueOf(c));
BigDecimal sum = new BigDecimal("39.55");
BigDecimal def = d.add(e).add(f);
BigDecimal dfe = d.add(f).add(e);
Assert.assertEquals(0, def.compareTo(sum));
Assert.assertEquals(0, dfe.compareTo(sum));
Assert.assertNotEquals(0, sum.compareTo(new BigDecimal(String.valueOf(acb))));
}
}

View File

@ -2,41 +2,58 @@ package com.baeldung.string;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
public class StringToCharStreamUnitTest {
private String testString = "Tests";
@Test
public void givenTestString_whenChars_thenReturnIntStream() {
assertThat(StringToCharStream.getIntStreamFromChars("test"), instanceOf(IntStream.class));
assertThat(testString.chars(), instanceOf(IntStream.class));
}
@Test
public void givenTestString_whenCodePoints_thenReturnIntStream() {
assertThat(StringToCharStream.getIntStreamFromCodePoints("test"), instanceOf(IntStream.class));
assertThat(testString.codePoints(), instanceOf(IntStream.class));
}
@Test
public void givenTestString_whenCodePoints_thenShowOccurences() throws Exception {
Map<Character, Integer> map = testString.codePoints()
.mapToObj(c -> (char) c)
.filter(Character::isLetter)
.collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
System.out.println(map);
}
@Test
public void givenIntStream_whenMapToObj_thenReturnCharacterStream() {
Stream<Character> characterStream
= StringToCharStream.mapIntStreamToCharStream(StringToCharStream.getIntStreamFromChars("test"));
Stream<Character> characterStream1
= StringToCharStream.mapIntStreamToCharStream(StringToCharStream.getIntStreamFromCodePoints("test"));
Stream<Character> characterStream = testString.chars()
.mapToObj(c -> (char) c);
Stream<Character> characterStream1 = testString.codePoints()
.mapToObj(c -> (char) c);
assertNotNull("IntStream returned by chars() did not map to Stream<Character>", characterStream);
assertNotNull("IntStream returned by codePoints() did not map to Stream<Character>", characterStream1);
}
@Test
public void givenIntStream_whenMapToObj_thenReturnStringStream() {
Stream<String> stringStream
= StringToCharStream.mapIntStreamToStringStream(StringToCharStream.getIntStreamFromCodePoints("test"));
assertNotNull(stringStream);
List<String> strings = testString.codePoints()
.mapToObj(c -> String.valueOf((char) c))
.collect(Collectors.toList());
assertEquals(strings.size(), 5);
}
}

View File

@ -25,3 +25,6 @@
- [Guide to Guava RangeMap](http://www.baeldung.com/guava-rangemap)
- [Guide to Guava Table](http://www.baeldung.com/guava-table)
- [Guide to Guavas Reflection Utilities](http://www.baeldung.com/guava-reflection)
- [Guide to Guava ClassToInstanceMap](http://www.baeldung.com/guava-class-to-instance-map)
- [Guide to Guava MinMaxPriorityQueue and EvictingQueue](http://www.baeldung.com/guava-minmax-priority-queue-and-evicting-queue)
- [Guide to Mathematical Utilities in Guava](http://www.baeldung.com/guava-math)

View File

@ -4,3 +4,4 @@
- [Converters, Listeners and Validators in Java EE 7](http://www.baeldung.com/java-ee7-converter-listener-validator)
- [Introduction to JAX-WS](http://www.baeldung.com/jax-ws)
- [A Guide to Java EE Web-Related Annotations](http://www.baeldung.com/javaee-web-annotations)
- [Introduction to Testing with Arquillian](http://www.baeldung.com/arquillian)

View File

@ -1,3 +1,6 @@
### Relevant Articles:
- [The Basics of JUnit 5 A Preview](http://www.baeldung.com/junit-5-preview)
- [A Guide to JUnit 5](http://www.baeldung.com/junit-5)
- [Guide to Dynamic Tests in Junit 5](http://www.baeldung.com/junit5-dynamic-tests)
- [A Guide to @RepeatedTest in Junit 5](http://www.baeldung.com/junit-5-repeated-test)
- [Guide to Dynamic Tests in Junit 5](http://www.baeldung.com/junit5-dynamic-tests)

View File

@ -4,5 +4,5 @@
- [A guide to the “when{}” block in Kotlin](http://www.baeldung.com/kotlin-when)
- [Comprehensive Guide to Null Safety in Kotlin](http://www.baeldung.com/kotlin-null-safety)
- [Kotlin Java Interoperability](http://www.baeldung.com/kotlin-java-interoperability)
- [Difference Between “==” and “===” in Kotlin]()
- [Difference Between “==” and “===” in Kotlin](http://www.baeldung.com/kotlin-equality-operators)
- [Generics in Kotlin](http://www.baeldung.com/kotlin-generics)

View File

@ -12,6 +12,11 @@
- [Introduction to Apache Commons Math](http://www.baeldung.com/apache-commons-math)
- [Intro to JaVer](http://www.baeldung.com/serenity-bdd)
- [Introduction to Netty](http://www.baeldung.com/netty)
- [Guide to Java Data Objects](http://www.baeldung.com/jdo)
- [Software Transactional Memory in Java Using Multiverse](http://www.baeldung.com/java-multiverse-stm)
- [Introduction to HikariCP](http://www.baeldung.com/hikaricp)
- [Serenity BDD with Spring and JBehave](http://www.baeldung.com/serenity-spring-jbehave)
The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own.

View File

@ -180,6 +180,12 @@
<artifactId>serenity-core</artifactId>
<version>${serenity.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-java-integration</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
@ -323,6 +329,16 @@
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj-pdf</artifactId>
<version>1.5.0-alpha.11</version>
</dependency>
<!-- OpenNLP -->
<dependency>
<groupId>org.apache.opennlp</groupId>

View File

@ -0,0 +1,33 @@
package com.baeldung.asciidoctor;
import static org.asciidoctor.Asciidoctor.Factory.create;
import static org.asciidoctor.OptionsBuilder.options;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.asciidoctor.Asciidoctor;
public class AsciidoctorDemo {
private final Asciidoctor asciidoctor;
public AsciidoctorDemo() {
asciidoctor = create();
}
public void generatePDFFromString(final String input) {
final Map<String, Object> options = options().inPlace(true)
.backend("pdf")
.asMap();
final String outfile = asciidoctor.convertFile(new File("sample.adoc"), options);
}
public String generateHTMLFromString(final String input) {
final String output = asciidoctor.convert("Hello _Baeldung_!", new HashMap<String, Object>());
return output;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.asciidoctor;
import org.junit.Assert;
import org.junit.Test;
public class AsciidoctorDemoTest {
@Test
public void givenString_whenConverting_thenResultingHTMLCode() {
final AsciidoctorDemo asciidoctorDemo = new AsciidoctorDemo();
Assert.assertEquals(asciidoctorDemo.generateHTMLFromString("Hello _Baeldung_!"), "<div class=\"paragraph\">\n<p>Hello <em>Baeldung</em>!</p>\n</div>");
}
}

2
mustache/README.md Normal file
View File

@ -0,0 +1,2 @@
### Relevant Articles:
[Introduction to Mustache](http://www.baeldung.com/mustache)

View File

@ -25,7 +25,6 @@
</properties>
<modules>
<module>spring-vertx</module>
<module>aws</module>
<module>akka-streams</module>
<module>algorithms</module>
@ -208,6 +207,7 @@
<module>spring-userservice</module>
<module>spring-zuul</module>
<module>spring-reactor</module>
<module>spring-vertx</module>
<module>testing</module>
<module>testng</module>

Binary file not shown.

View File

@ -66,6 +66,6 @@
<properties>
<testng.version>6.10</testng.version>
<selenium-java.version>3.0.1</selenium-java.version>
<selenium-java.version>3.4.0</selenium-java.version>
</properties>
</project>

View File

@ -1,32 +1,31 @@
package main.java.com.baeldung.selenium;
import main.java.com.baeldung.selenium.config.SeleniumConfig;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class SeleniumExample {
private WebDriver webDriver;
private SeleniumConfig config;
private String url = "http://www.baeldung.com/";
public SeleniumExample() {
System.setProperty("webdriver.firefox.marionette", "C:\\selenium\\geckodriver.exe");
webDriver = new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
webDriver.get(url);
config = new SeleniumConfig();
config.getDriver().get(url);
}
public void closeWindow() {
webDriver.close();
this.config.getDriver().close();
}
public String getTitle() {
return webDriver.getTitle();
return this.config.getDriver().getTitle();
}
public void getAboutBaeldungPage() {
@ -36,7 +35,7 @@ public class SeleniumExample {
}
private void closeOverlay() {
List<WebElement> webElementList = webDriver.findElements(By.tagName("a"));
List<WebElement> webElementList = this.config.getDriver().findElements(By.tagName("a"));
if (webElementList != null) {
webElementList.stream()
.filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title")))
@ -47,14 +46,18 @@ public class SeleniumExample {
}
private void clickAboutLink() {
webDriver.findElement(By.partialLinkText("About")).click();
this.config.getDriver().findElement(By.partialLinkText("About")).click();
}
private void clickAboutUsLink() {
webDriver.findElement(By.partialLinkText("About Baeldung.")).click();
Actions builder = new Actions(config.getDriver());
WebElement element = this.config.getDriver().findElement(By.partialLinkText("About Baeldung."));
builder.moveToElement(element).build().perform();
}
public boolean isAuthorInformationAvailable() {
return webDriver.findElement(By.xpath("//*[contains(text(), 'an engineer with a passion for teaching and building stuff on the web')]")).isDisplayed();
return this.config.getDriver()
.findElement(By.cssSelector("article > .row > div"))
.isDisplayed();
}
}

View File

@ -4,9 +4,11 @@ import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -15,30 +17,13 @@ public class SeleniumConfig {
private WebDriver driver;
public SeleniumConfig() {
Capabilities capabilities = DesiredCapabilities.chrome();
driver = new ChromeDriver(capabilities);
Capabilities capabilities = DesiredCapabilities.firefox();
driver = new FirefoxDriver(capabilities);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
static {
String osName = getOsName("os.name").toLowerCase();
final Matcher matcher = Pattern.compile("(mac|nux|win)").matcher(osName);
if (matcher.find()) {
switch (matcher.group()) {
case "mac":
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.mac"));
break;
case "nux":
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.linux"));
break;
case "win":
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.exe"));
break;
}
}
}
private static String getOsName(String prop) {
return (System.getProperty(prop));
System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac"));
}
static private String findFile(String filename) {
@ -51,7 +36,7 @@ public class SeleniumConfig {
}
public void close() {
driver.quit();
driver.close();
}
public void navigateTo(String url) {

View File

@ -10,7 +10,7 @@ import static org.testng.Assert.*;
public class SeleniumWithJUnitLiveTest {
private static SeleniumExample seleniumExample;
private String expectedTitle = "About Baeldung | Baeldung";
private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials";
@BeforeClass
public static void setUp() {

View File

@ -10,7 +10,7 @@ import static org.testng.Assert.*;
public class SeleniumWithTestNGLiveTest {
private SeleniumExample seleniumExample;
private String expectedTitle = "About Baeldung | Baeldung";
private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials";
@BeforeSuite
public void setUp() {

View File

@ -23,3 +23,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Create a Custom Auto-Configuration with Spring Boot](http://www.baeldung.com/spring-boot-custom-auto-configuration)
- [Testing in Spring Boot](http://www.baeldung.com/spring-boot-testing)
- [Guide to @ConfigurationProperties in Spring Boot](http://www.baeldung.com/configuration-properties-in-spring-boot)
- [Spring Boot and Togglz Aspect](http://www.baeldung.com/spring-togglz)

View File

@ -2,6 +2,7 @@
- [Spring Cloud Bootstrapping](http://www.baeldung.com/spring-cloud-bootstrapping)
- [Spring Cloud Securing Services](http://www.baeldung.com/spring-cloud-securing-services)
- [Spring Cloud Tracing Services with Zipkin](http://www.baeldung.com/tracing-services-with-zipkin)
- [Spring Cloud Adding Angular 4](http://www.baeldung.com/spring-cloud-angular)
- To run the project:

View File

@ -6,3 +6,4 @@
### Relevant Articles:
-
- [Guide to Spring Web Flow](http://www.baeldung.com/spring-web-flow)

View File

@ -4,6 +4,7 @@
- [Intro to Spring Remoting with HTTP Invokers](http://www.baeldung.com/spring-remoting-http-invoker)
- [Spring Remoting with Hessian and Burlap](http://www.baeldung.com/spring-remoting-hessian-burlap)
- [Spring Remoting with AMQP](http://www.baeldung.com/spring-remoting-amqp)
- [Spring Remoting with JMS](http://www.baeldung.com/spring-remoting-jms)
### Overview
This Maven project contains the Java source code for various modules used in the Spring Remoting series of articles.

2
spring-roo/README.md Normal file
View File

@ -0,0 +1,2 @@
### Relevant Articles:
[Quick Guide to Spring Roo](http://www.baeldung.com/spring-roo)

View File

@ -8,7 +8,7 @@
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>vertx-spring</name>
<name>spring-vertx</name>
<description>A demo project with vertx spring integration</description>
<parent>
@ -41,6 +41,7 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>

View File

@ -11,7 +11,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.baeldung.vertxspring.verticles.ServerVerticle;
import com.baeldung.vertxspring.verticles.ServiceVerticle;
import com.baeldung.vertxspring.verticles.ArticleRecipientVerticle;
import io.vertx.core.Vertx;
@ -26,7 +26,7 @@ public class VertxSpringApplication {
private ServerVerticle serverVerticle;
@Autowired
private ServiceVerticle serviceVerticle;
private ArticleRecipientVerticle serviceVerticle;
public static void main(String[] args) {
SpringApplication.run(VertxSpringApplication.class, args);

View File

@ -13,7 +13,7 @@ import io.vertx.core.eventbus.Message;
import io.vertx.core.json.Json;
@Component
public class ServiceVerticle extends AbstractVerticle {
public class ArticleRecipientVerticle extends AbstractVerticle {
public static final String GET_ALL_ARTICLES = "get.articles.all";

View File

@ -11,7 +11,7 @@ public class ServerVerticle extends AbstractVerticle {
private void getAllArticlesHandler(RoutingContext routingContext) {
vertx.eventBus()
.<String>send(ServiceVerticle.GET_ALL_ARTICLES, "", result -> {
.<String>send(ArticleRecipientVerticle.GET_ALL_ARTICLES, "", result -> {
if (result.succeeded()) {
routingContext.response()
.putHeader("content-type", "application/json")

View File

@ -11,3 +11,4 @@
- [Cucumber and Scenario Outline](http://www.baeldung.com/cucumber-scenario-outline)
- [Testing with Google Truth](http://www.baeldung.com/google-truth)
- [Testing with JGoTesting](http://www.baeldung.com/jgotesting)
- [Introduction to JUnitParams](http://www.baeldung.com/junit-params)

View File

@ -1,3 +1,4 @@
### Relevant articles
- [Introduction to TestNG](http://www.baeldung.com/testng)
- [Custom Reporting with TestNG](http://www.baeldung.com/testng-custom-reporting)