Merge remote-tracking branch 'upstream/master'

This commit is contained in:
macroscopic64
2019-09-26 23:29:49 +05:30
185 changed files with 1347 additions and 401 deletions
@@ -0,0 +1,17 @@
package com.baeldung.category;
class BaeldungCategory {
public static String capitalize(String self) {
String capitalizedStr = self;
if (self.size() > 0) {
capitalizedStr = self.substring(0, 1).toUpperCase() + self.substring(1);
}
return capitalizedStr
}
public static double toThePower(Number self, Number exponent) {
return Math.pow(self, exponent);
}
}
@@ -0,0 +1,17 @@
package com.baeldung.category;
import groovy.lang.Category
@Category(Number)
class NumberCategory {
public Number cube() {
return this*this*this
}
public int divideWithRoundUp(BigDecimal divisor, boolean isRoundUp) {
def mathRound = isRoundUp ? BigDecimal.ROUND_UP : BigDecimal.ROUND_DOWN
return (int)new BigDecimal(this).divide(divisor, 0, mathRound)
}
}
@@ -0,0 +1,101 @@
package com.baeldung.category
import groovy.time.*
import java.text.SimpleDateFormat
import groovy.xml.*
import groovy.xml.dom.*
import com.baeldung.category.BaeldungCategory
import com.baeldung.category.NumberCategory
class CategoryUnitTest extends GroovyTestCase {
void test_whenUsingTimeCategory_thenOperationOnDate() {
def jan_1_2019 = new Date("01/01/2019")
use (TimeCategory) {
assert jan_1_2019 + 10.seconds == new Date("01/01/2019 00:00:10")
assert jan_1_2019 + 20.minutes == new Date("01/01/2019 00:20:00")
assert jan_1_2019 + 2.hours == new Date("01/01/2019 02:00:00")
assert jan_1_2019 - 1.day == new Date("12/31/2018")
assert jan_1_2019 + 2.weeks == new Date("01/15/2019")
assert jan_1_2019 - 2.months == new Date("11/01/2018")
assert jan_1_2019 + 3.years == new Date("01/01/2022")
}
}
void test_whenUsingTimeCategory_thenOperationOnNumber() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy")
use (TimeCategory) {
assert sdf.format(5.days.from.now) == sdf.format(new Date() + 5.days)
sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss")
assert sdf.format(10.minutes.from.now) == sdf.format(new Date() + 10.minutes)
assert sdf.format(2.hours.ago) == sdf.format(new Date() - 2.hours)
}
}
void test_whenUsingDOMCategory_thenOperationOnXML() {
def baeldungArticlesText = """
<articles>
<article core-java="true">
<title>An Intro to the Java Debug Interface (JDI)</title>
<desc>A quick and practical overview of Java Debug Interface.</desc>
</article>
<article core-java="false">
<title>A Quick Guide to Working with Web Services in Groovy</title>
<desc>Learn how to work with Web Services in Groovy.</desc>
</article>
</articles>
"""
def baeldungArticlesDom = DOMBuilder.newInstance().parseText(baeldungArticlesText)
def root = baeldungArticlesDom.documentElement
use (DOMCategory) {
assert root.article.size() == 2
def articles = root.article
assert articles[0].title.text() == "An Intro to the Java Debug Interface (JDI)"
assert articles[1].desc.text() == "Learn how to work with Web Services in Groovy."
def articleNode3 = root.appendNode(new QName("article"), ["core-java": "false"])
articleNode3.appendNode("title", "Metaprogramming in Groovy")
articleNode3.appendNode("desc", "Explore the concept of runtime and compile-time metaprogramming in Groovy")
assert root.article.size() == 3
assert root.article[2].title.text() == "Metaprogramming in Groovy"
}
}
void test_whenUsingBaeldungCategory_thenCapitalizeString() {
use (BaeldungCategory) {
assert "norman".capitalize() == "Norman"
}
}
void test_whenUsingBaeldungCategory_thenOperationsOnNumber() {
use (BaeldungCategory) {
assert 50.toThePower(2) == 2500
assert 2.4.toThePower(4) == 33.1776
}
}
void test_whenUsingNumberCategory_thenOperationsOnNumber() {
use (NumberCategory) {
assert 3.cube() == 27
assert 25.divideWithRoundUp(6, true) == 5
assert 120.23.divideWithRoundUp(6.1, true) == 20
assert 150.9.divideWithRoundUp(12.1, false) == 12
}
}
}
@@ -0,0 +1,10 @@
=========
## Core Java Concurrency Basic 2 Examples
### Relevant Articles:
- [How to Delay Code Execution in Java](https://www.baeldung.com/java-delay-code-execution)
- [wait and notify() Methods in Java](https://www.baeldung.com/java-wait-notify)
- [Difference Between Wait and Sleep in Java](https://www.baeldung.com/java-wait-and-sleep)
- [Guide to the Synchronized Keyword in Java](https://www.baeldung.com/java-synchronized)
- [Life Cycle of a Thread in Java](https://www.baeldung.com/java-thread-lifecycle)
@@ -0,0 +1,33 @@
<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>core-java-concurrency-basic-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-concurrency-basic-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
</dependencies>
<build>
<finalName>core-java-concurrency-basic-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
</properties>
</project>
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -1,15 +1,15 @@
package com.baeldung.concurrent.waitandnotify;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class NetworkIntegrationTest {
@@ -3,18 +3,13 @@
## Core Java Concurrency Basic Examples
### Relevant Articles:
- [Guide To CompletableFuture](http://www.baeldung.com/java-completablefuture)
- [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial)
- [Guide to java.util.concurrent.Future](http://www.baeldung.com/java-future)
- [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep)
- [Guide to the Synchronized Keyword in Java](http://www.baeldung.com/java-synchronized)
- [Overview of the java.util.concurrent](http://www.baeldung.com/java-util-concurrent)
- [Implementing a Runnable vs Extending a Thread](http://www.baeldung.com/java-runnable-vs-extending-thread)
- [How to Kill a Java Thread](http://www.baeldung.com/java-thread-stop)
- [ExecutorService – Waiting for Threads to Finish](http://www.baeldung.com/java-executor-wait-for-threads)
- [wait and notify() Methods in Java](http://www.baeldung.com/java-wait-notify)
- [Life Cycle of a Thread in Java](http://www.baeldung.com/java-thread-lifecycle)
- [Runnable vs. Callable in Java](http://www.baeldung.com/java-runnable-callable)
- [Guide To CompletableFuture](https://www.baeldung.com/java-completablefuture)
- [A Guide to the Java ExecutorService](https://www.baeldung.com/java-executor-service-tutorial)
- [Guide to java.util.concurrent.Future](https://www.baeldung.com/java-future)
- [Overview of the java.util.concurrent](https://www.baeldung.com/java-util-concurrent)
- [Implementing a Runnable vs Extending a Thread](https://www.baeldung.com/java-runnable-vs-extending-thread)
- [How to Kill a Java Thread](https://www.baeldung.com/java-thread-stop)
- [ExecutorService – Waiting for Threads to Finish](https://www.baeldung.com/java-executor-wait-for-threads)
- [Runnable vs. Callable in Java](https://www.baeldung.com/java-runnable-callable)
- [What is Thread-Safety and How to Achieve it?](https://www.baeldung.com/java-thread-safety)
- [How to Start a Thread in Java](https://www.baeldung.com/java-start-thread)
- [How to Delay Code Execution in Java](https://www.baeldung.com/java-delay-code-execution)
@@ -1,14 +1,16 @@
package com.baeldung.concurrent.threadsafety.tests;
package com.baeldung.concurrent.threadsafety;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import com.baeldung.concurrent.threadsafety.callables.CounterCallable;
import com.baeldung.concurrent.threadsafety.services.Counter;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CounterTest {
import static org.assertj.core.api.Assertions.assertThat;
public class CounterUnitTest {
@Test
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
@@ -1,14 +1,16 @@
package com.baeldung.concurrent.threadsafety.tests;
package com.baeldung.concurrent.threadsafety;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import com.baeldung.concurrent.threadsafety.callables.ExtrinsicLockCounterCallable;
import com.baeldung.concurrent.threadsafety.services.ExtrinsicLockCounter;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ExtrinsicLockCounterTest {
import static org.assertj.core.api.Assertions.assertThat;
public class ExtrinsicLockCounterUnitTest {
@Test
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
@@ -1,13 +1,16 @@
package com.baeldung.concurrent.threadsafety.tests;
package com.baeldung.concurrent.threadsafety;
import com.baeldung.concurrent.threadsafety.mathutils.MathUtils;
import org.junit.Test;
import java.math.BigInteger;
import static org.assertj.core.api.Assertions.assertThat;
public class MathUtilsTest {
public class MathUtilsUnitTest {
@Test
public void whenCalledFactorialMethod_thenCorrect() {
assertThat(MathUtils.factorial(2)).isEqualTo(2);
assertThat(MathUtils.factorial(2)).isEqualTo(new BigInteger("2"));
}
}
@@ -1,14 +1,16 @@
package com.baeldung.concurrent.threadsafety.tests;
package com.baeldung.concurrent.threadsafety;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import com.baeldung.concurrent.threadsafety.callables.MessageServiceCallable;
import com.baeldung.concurrent.threadsafety.services.MessageService;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class MessageServiceTest {
import static org.assertj.core.api.Assertions.assertThat;
public class MessageServiceUnitTest {
@Test
public void whenCalledgetMessage_thenCorrect() throws Exception {

Some files were not shown because too many files have changed in this diff Show More