commit
b7a41880e6
|
@ -85,3 +85,5 @@ transaction.log
|
|||
*-shell.log
|
||||
|
||||
apache-cxf/cxf-aegis/baeldung.xml
|
||||
|
||||
libraries-2/*.db
|
|
@ -1,16 +1,21 @@
|
|||
package com.baeldung.jgrapht;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.jgrapht.ext.JGraphXAdapter;
|
||||
import org.jgrapht.graph.DefaultDirectedGraph;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mxgraph.layout.mxCircleLayout;
|
||||
import com.mxgraph.layout.mxIGraphLayout;
|
||||
import com.mxgraph.util.mxCellRenderer;
|
||||
|
@ -20,7 +25,7 @@ public class GraphImageGenerationUnitTest {
|
|||
|
||||
@Before
|
||||
public void createGraph() throws IOException {
|
||||
File imgFile = new File("src/test/resources/graph.png");
|
||||
File imgFile = new File("src/test/resources/graph1.png");
|
||||
imgFile.createNewFile();
|
||||
g = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
|
||||
String x1 = "x1";
|
||||
|
@ -34,12 +39,18 @@ public class GraphImageGenerationUnitTest {
|
|||
g.addEdge(x3, x1);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
File imgFile = new File("src/test/resources/graph1.png");
|
||||
imgFile.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAdaptedGraph_whenWriteBufferedImage_ThenFileShouldExist() throws IOException {
|
||||
JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<String, DefaultEdge>(g);
|
||||
mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
|
||||
layout.execute(graphAdapter.getDefaultParent());
|
||||
File imgFile = new File("src/test/resources/graph.png");
|
||||
File imgFile = new File("src/test/resources/graph1.png");
|
||||
BufferedImage image = mxCellRenderer.createBufferedImage(graphAdapter, null, 2, Color.WHITE, true, null);
|
||||
ImageIO.write(image, "PNG", imgFile);
|
||||
assertTrue(imgFile.exists());
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<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>
|
||||
<artifactId>apache-miscellaneous-1</artifactId>
|
||||
<artifactId>apache-libraries</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>apache-libraries</name>
|
||||
|
||||
|
|
|
@ -13,10 +13,6 @@
|
|||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>cf-uaa-oauth2-client</artifactId>
|
||||
<name>uaa-client-webapp</name>
|
||||
<name>cf-uaa-oauth2-client</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-8-datetime-2</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<name>core-java-8-datetime</name>
|
||||
<name>core-java-8-datetime-2</name>
|
||||
<packaging>jar</packaging>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.java9.currentmethod;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CurrentExecutingMethodUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenJava9_whenWalkingTheStack_thenFindMethod() {
|
||||
StackWalker walker = StackWalker.getInstance();
|
||||
Optional<String> methodName = walker.walk(frames -> frames
|
||||
.findFirst()
|
||||
.map(StackWalker.StackFrame::getMethodName)
|
||||
);
|
||||
|
||||
assertTrue(methodName.isPresent());
|
||||
assertEquals("givenJava9_whenWalkingTheStack_thenFindMethod", methodName.get());
|
||||
}
|
||||
}
|
|
@ -13,32 +13,6 @@
|
|||
<name>core-java-arrays-operations-basic</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${shade.plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<finalName>benchmarks</finalName>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.openjdk.jmh.Main</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
|
@ -66,6 +40,32 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${shade.plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<finalName>benchmarks</finalName>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.openjdk.jmh.Main</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<shade.plugin.version>3.2.0</shade.plugin.version>
|
||||
|
||||
|
|
|
@ -14,32 +14,6 @@
|
|||
<name>core-java-arrays-sorting</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${shade.plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<finalName>benchmarks</finalName>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.openjdk.jmh.Main</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<!-- Utilities -->
|
||||
<dependency>
|
||||
|
@ -74,6 +48,32 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${shade.plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<finalName>benchmarks</finalName>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.openjdk.jmh.Main</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<shade.plugin.version>3.2.0</shade.plugin.version>
|
||||
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.concurrent.localvariables;
|
||||
|
||||
public class LocalAndLambda {
|
||||
public static void main(String... args) {
|
||||
String text = "";
|
||||
// Un-commenting the next line will break compilation, because text is no longer effectively final
|
||||
// text = "675";
|
||||
new Thread(() -> System.out.println(text)).start();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.concurrent.localvariables;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class LocalVariables implements Runnable {
|
||||
private int field;
|
||||
|
||||
public static void main(String... args) {
|
||||
LocalVariables target = new LocalVariables();
|
||||
new Thread(target).start();
|
||||
new Thread(target).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
field = new SecureRandom().nextInt();
|
||||
int local = new SecureRandom().nextInt();
|
||||
System.out.println(field + " - " + local);
|
||||
}
|
||||
}
|
|
@ -33,7 +33,6 @@
|
|||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<commons.lang3.version>3.10</commons.lang3.version>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.exceptionininitializererror;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
public class ExceptionInInitializerErrorUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenStaticVar_whenThrows_thenWrapsItInAnExceptionInInitializerError() {
|
||||
assertThatThrownBy(StaticVar::new)
|
||||
.isInstanceOf(ExceptionInInitializerError.class)
|
||||
.hasCauseInstanceOf(RuntimeException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStaticBlock_whenThrows_thenWrapsItInAnExceptionInInitializerError() {
|
||||
assertThatThrownBy(StaticBlock::new)
|
||||
.isInstanceOf(ExceptionInInitializerError.class)
|
||||
.hasCauseInstanceOf(ArithmeticException.class);
|
||||
}
|
||||
|
||||
private static class CheckedConvention {
|
||||
|
||||
private static Constructor<?> constructor;
|
||||
|
||||
static {
|
||||
try {
|
||||
constructor = CheckedConvention.class.getDeclaredConstructor();
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new ExceptionInInitializerError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class StaticVar {
|
||||
|
||||
private static int state = initializeState();
|
||||
|
||||
private static int initializeState() {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
private static class StaticBlock {
|
||||
|
||||
private static int state;
|
||||
|
||||
static {
|
||||
state = 42 / 0;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.objectsize;
|
||||
|
||||
public class Course {
|
||||
|
||||
private String name;
|
||||
|
||||
public Course(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.objectsize;
|
||||
|
||||
public class InstrumentedSize {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String ds = "Data Structures";
|
||||
Course course = new Course(ds);
|
||||
|
||||
System.out.println(ObjectSizeCalculator.sizeOf(course));
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
Premain-Class: com.baeldung.objectsize.ObjectSizeCalculator
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.objectsize;
|
||||
|
||||
import java.lang.instrument.Instrumentation;
|
||||
|
||||
public class ObjectSizeCalculator {
|
||||
|
||||
private static Instrumentation instrumentation;
|
||||
|
||||
public static void premain(String args, Instrumentation inst) {
|
||||
instrumentation = inst;
|
||||
}
|
||||
|
||||
public static long sizeOf(Object o) {
|
||||
return instrumentation.getObjectSize(o);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.objectsize;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.openjdk.jol.info.ClassLayout;
|
||||
import org.openjdk.jol.info.GraphLayout;
|
||||
import org.openjdk.jol.vm.VM;
|
||||
|
||||
public class ObjectSizeUnitTest {
|
||||
|
||||
@Test
|
||||
public void printingTheVMDetails() {
|
||||
System.out.println(VM.current().details());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void printingTheProfClassLayout() {
|
||||
System.out.println(ClassLayout.parseClass(Professor.class).toPrintable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void printingTheCourseClassLayout() {
|
||||
System.out.println(ClassLayout.parseClass(Course.class).toPrintable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void printingACourseInstanceLayout() {
|
||||
String ds = "Data Structures";
|
||||
Course course = new Course(ds);
|
||||
|
||||
System.out.println("The shallow size is :" + VM.current().sizeOf(course));
|
||||
|
||||
System.out.println(ClassLayout.parseInstance(course).toPrintable());
|
||||
System.out.println(ClassLayout.parseInstance(ds).toPrintable());
|
||||
System.out.println(ClassLayout.parseInstance(ds.toCharArray()).toPrintable());
|
||||
|
||||
System.out.println(GraphLayout.parseInstance(course).totalSize());
|
||||
System.out.println(GraphLayout.parseInstance(course).toFootprint());
|
||||
System.out.println(GraphLayout.parseInstance(course).toPrintable());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.objectsize;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
public class Professor {
|
||||
|
||||
private String name;
|
||||
private boolean tenured;
|
||||
private List<Course> courses;
|
||||
private int level;
|
||||
private LocalDate birthDay;
|
||||
private double lastEvaluation;
|
||||
|
||||
public Professor(String name, boolean tenured, List<Course> courses,
|
||||
int level, LocalDate birthDay, double lastEvaluation) {
|
||||
this.name = name;
|
||||
this.tenured = tenured;
|
||||
this.courses = courses;
|
||||
this.level = level;
|
||||
this.birthDay = birthDay;
|
||||
this.lastEvaluation = lastEvaluation;
|
||||
}
|
||||
}
|
|
@ -13,4 +13,4 @@ This module contains articles about core features in the Java language
|
|||
- [Comparing Long Values in Java](https://www.baeldung.com/java-compare-long-values)
|
||||
- [Comparing Objects in Java](https://www.baeldung.com/java-comparing-objects)
|
||||
- [Casting int to Enum in Java](https://www.baeldung.com/java-cast-int-to-enum)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang)
|
||||
[[ <-- Prev]](/core-java-modules/core-java-lang)[[Next --> ]](/core-java-modules/core-java-lang-3)
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
## Core Java Lang (Part 3)
|
||||
|
||||
This module contains articles about core features in the Java language
|
||||
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang-2)
|
|
@ -0,0 +1,42 @@
|
|||
<?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>
|
||||
<artifactId>core-java-lang-3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-lang-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-lang-3</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.12.2</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.stringtoboolean;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringToBooleanUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenStringTrue_whenUsingParseBoolean_thenTrue() {
|
||||
assertThat(Boolean.parseBoolean("true")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringTrue_whenUsingValueOf_thenTrue() {
|
||||
assertThat(Boolean.valueOf("true")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringTrue_whenUsingGetBoolean_thenFalse() {
|
||||
assertThat(Boolean.getBoolean("true")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSystemProperty_whenUsingGetBoolean_thenTrue() {
|
||||
System.setProperty("CODING_IS_FUN", "true");
|
||||
|
||||
assertThat(Boolean.getBoolean("CODING_IS_FUN")).isTrue();
|
||||
}
|
||||
}
|
|
@ -13,4 +13,5 @@ This module contains articles about core features in the Java language
|
|||
- [Retrieving a Class Name in Java](https://www.baeldung.com/java-class-name)
|
||||
- [The Java continue and break Keywords](https://www.baeldung.com/java-continue-and-break)
|
||||
- [Infinite Loops in Java](https://www.baeldung.com/infinite-loops-java)
|
||||
- [[More --> ]](/core-java-modules/core-java-lang-2)
|
||||
|
||||
[[Next --> ]](/core-java-modules/core-java-lang-2)
|
||||
|
|
|
@ -0,0 +1,110 @@
|
|||
package com.baeldung.regex.countmatches;
|
||||
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit Test intended to count number of matches of a RegEx using Java 8 and 9.
|
||||
*
|
||||
* Java 9 is needed to run the commented out tests.
|
||||
*/
|
||||
public class CountMatchesUnitTest {
|
||||
|
||||
private static final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile("([a-z0-9_.-]+)@([a-z0-9_.-]+[a-z])");
|
||||
private static final String TEXT_CONTAINING_EMAIL_ADDRESSES = "You can contact me through: writer@baeldung.com, editor@baeldung.com and team@bealdung.com";
|
||||
private static final String TEXT_CONTAINING_FIVE_EMAIL_ADDRESSES = "Valid emails are: me@gmail.com, you@baeldung.com, contact@hotmail.com, press@anysite.com and support@bealdung.com";
|
||||
private static final String TEXT_CONTAINING_OVERLAP_EMAIL_ADDRESSES = "Try to contact us at team@baeldung.comeditor@baeldung.com, support@baeldung.com.";
|
||||
|
||||
@Test
|
||||
public void givenContainingEmailString_whenJava8Match_thenCountMacthesFound() {
|
||||
Matcher countEmailMatcher = EMAIL_ADDRESS_PATTERN.matcher(TEXT_CONTAINING_EMAIL_ADDRESSES);
|
||||
|
||||
int count = 0;
|
||||
while (countEmailMatcher.find()) {
|
||||
count++;
|
||||
}
|
||||
|
||||
assertEquals(3, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenContainingFiveEmailsString_whenJava8Match_thenCountMacthesFound() {
|
||||
Matcher countFiveEmailsMatcher = EMAIL_ADDRESS_PATTERN.matcher(TEXT_CONTAINING_FIVE_EMAIL_ADDRESSES);
|
||||
|
||||
int count = 0;
|
||||
while (countFiveEmailsMatcher.find()) {
|
||||
count++;
|
||||
}
|
||||
|
||||
assertEquals(5, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringWithoutEmails_whenJava8Match_thenCountMacthesNotFound() {
|
||||
Matcher noEmailMatcher = EMAIL_ADDRESS_PATTERN.matcher("Simple text without emails.");
|
||||
|
||||
int count = 0;
|
||||
while (noEmailMatcher.find()) {
|
||||
count++;
|
||||
}
|
||||
|
||||
assertEquals(0, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringWithOverlappingEmails_whenJava8Match_thenCountWrongMatches() {
|
||||
Matcher countOverlappingEmailsMatcher = EMAIL_ADDRESS_PATTERN.matcher(TEXT_CONTAINING_OVERLAP_EMAIL_ADDRESSES);
|
||||
|
||||
int count = 0;
|
||||
while (countOverlappingEmailsMatcher.find()) {
|
||||
count++;
|
||||
}
|
||||
|
||||
assertNotEquals(3, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenContainingEmailString_whenStartingInJava9Match_thenCountMacthesFound() {
|
||||
// uncomment to try with Java 9
|
||||
// Matcher countEmailMatcher = EMAIL_ADDRESS_PATTERN.matcher(TEXT_CONTAINING_EMAIL_ADDRESSES);
|
||||
|
||||
// long count = countEmailMatcher.results().count();
|
||||
|
||||
// assertEquals(3, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenContainingFiveEmailsString_whenStartingInJava9Match_thenCountMacthesFound() {
|
||||
// uncomment to try with Java 9
|
||||
// Matcher countFiveEmailsMatcher = EMAIL_ADDRESS_PATTERN.matcher(TEXT_CONTAINING_FIVE_EMAIL_ADDRESSES);
|
||||
|
||||
// long count = countFiveEmailsMatcher.results().count();
|
||||
|
||||
// assertEquals(5, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringWithoutEmails_whenJava9Match_thenCountMacthesNotFound() {
|
||||
// uncomment to try with Java 9
|
||||
// Matcher noEmailMatcher = EMAIL_ADDRESS_PATTERN.matcher("Simple text without emails.");
|
||||
|
||||
// long count = noEmailMatcher.results().count();
|
||||
|
||||
// assertEquals(0, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringWithOverlappingEmails_whenJava9Match_thenCountWrongMatches() {
|
||||
// uncomment to try with Java 9
|
||||
// Matcher countOverlappingEmailsMatcher = EMAIL_ADDRESS_PATTERN.matcher(TEXT_CONTAINING_OVERLAP_EMAIL_ADDRESSES);
|
||||
|
||||
// long count = countOverlappingEmailsMatcher.results().count();
|
||||
|
||||
// assertNotEquals(3, count);
|
||||
}
|
||||
}
|
|
@ -9,7 +9,6 @@ This module contains articles about the Stream API in Java.
|
|||
- [Iterable to Stream in Java](https://www.baeldung.com/java-iterable-to-stream)
|
||||
- [How to Iterate Over a Stream With Indices](https://www.baeldung.com/java-stream-indices)
|
||||
- [Stream Ordering in Java](https://www.baeldung.com/java-stream-ordering)
|
||||
- [Introduction to Protonpack](https://www.baeldung.com/java-protonpack)
|
||||
- [Java Stream Filter with Lambda Expression](https://www.baeldung.com/java-stream-filter-lambda)
|
||||
- [Counting Matches on a Stream Filter](https://www.baeldung.com/java-stream-filter-count)
|
||||
- [Summing Numbers with Java Streams](https://www.baeldung.com/java-stream-sum)
|
||||
|
|
|
@ -10,7 +10,6 @@ This module contains articles about string operations.
|
|||
- [Java String equalsIgnoreCase()](https://www.baeldung.com/java-string-equalsignorecase)
|
||||
- [Case-Insensitive String Matching in Java](https://www.baeldung.com/java-case-insensitive-string-matching)
|
||||
- [L-Trim and R-Trim Alternatives in Java](https://www.baeldung.com/java-trim-alternatives)
|
||||
- [Java Convert PDF to Base64](https://www.baeldung.com/java-convert-pdf-to-base64)
|
||||
- [Encode a String to UTF-8 in Java](https://www.baeldung.com/java-string-encode-utf-8)
|
||||
- [Guide to Character Encoding](https://www.baeldung.com/java-char-encoding)
|
||||
- [Convert Hex to ASCII in Java](https://www.baeldung.com/java-convert-hex-to-ascii) #remove additional readme file
|
||||
|
|
|
@ -85,6 +85,7 @@
|
|||
<module>core-java-lambdas</module>
|
||||
<module>core-java-lang</module>
|
||||
<module>core-java-lang-2</module>
|
||||
<module>core-java-lang-3</module>
|
||||
<module>core-java-lang-math</module>
|
||||
<module>core-java-lang-math-2</module>
|
||||
<module>core-java-lang-oop-constructors</module>
|
||||
|
@ -134,18 +135,6 @@
|
|||
<module>pre-jpms</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
@ -163,6 +152,18 @@
|
|||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
|
|
20
ddd/pom.xml
20
ddd/pom.xml
|
@ -17,16 +17,6 @@
|
|||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
@ -105,6 +95,16 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
## Guava
|
||||
|
||||
This module contains articles a Google Guava
|
||||
|
||||
### Relevant Articles:
|
||||
- [Introduction to Guava Throwables](https://www.baeldung.com/guava-throwables)
|
||||
- [Guava CharMatcher](https://www.baeldung.com/guava-string-charmatcher)
|
|
@ -0,0 +1,48 @@
|
|||
<?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>
|
||||
<artifactId>guava-2</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>guava-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>guava</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,113 @@
|
|||
package com.baeldung.guava.charmatcher;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetEncoder;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
public class GuavaCharMatcherUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenRemoveSpecialCharacters_thenRemoved() {
|
||||
final String input = "H*el.lo,}12";
|
||||
final CharMatcher matcher = CharMatcher.javaLetterOrDigit();
|
||||
final String result = matcher.retainFrom(input);
|
||||
|
||||
assertEquals("Hello12", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemoveNonASCIIChars_thenRemoved() {
|
||||
final String input = "あhello₤";
|
||||
|
||||
String result = CharMatcher.ascii().retainFrom(input);
|
||||
assertEquals("hello", result);
|
||||
|
||||
result = CharMatcher.inRange('0', 'z').retainFrom(input);
|
||||
assertEquals("hello", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidateString_thenValid() {
|
||||
final String input = "hello";
|
||||
|
||||
boolean result = CharMatcher.javaLowerCase().matchesAllOf(input);
|
||||
assertTrue(result);
|
||||
|
||||
result = CharMatcher.is('e').matchesAnyOf(input);
|
||||
assertTrue(result);
|
||||
|
||||
result = CharMatcher.javaDigit().matchesNoneOf(input);
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTrimString_thenTrimmed() {
|
||||
final String input = "---hello,,,";
|
||||
|
||||
String result = CharMatcher.is('-').trimLeadingFrom(input);
|
||||
assertEquals("hello,,,", result);
|
||||
|
||||
result = CharMatcher.is(',').trimTrailingFrom(input);
|
||||
assertEquals("---hello", result);
|
||||
|
||||
result = CharMatcher.anyOf("-,").trimFrom(input);
|
||||
assertEquals("hello", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollapseFromString_thenCollapsed() {
|
||||
final String input = " hel lo ";
|
||||
|
||||
String result = CharMatcher.is(' ').collapseFrom(input, '-');
|
||||
assertEquals("-hel-lo-", result);
|
||||
|
||||
result = CharMatcher.is(' ').trimAndCollapseFrom(input, '-');
|
||||
assertEquals("hel-lo", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplaceFromString_thenReplaced() {
|
||||
final String input = "apple-banana.";
|
||||
|
||||
String result = CharMatcher.anyOf("-.").replaceFrom(input, '!');
|
||||
assertEquals("apple!banana!", result);
|
||||
|
||||
result = CharMatcher.is('-').replaceFrom(input, " and ");
|
||||
assertEquals("apple and banana.", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCountCharInString_thenCorrect() {
|
||||
final String input = "a, c, z, 1, 2";
|
||||
|
||||
int result = CharMatcher.is(',').countIn(input);
|
||||
assertEquals(4, result);
|
||||
|
||||
result = CharMatcher.inRange('a', 'h').countIn(input);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemoveCharsNotInCharset_thenRemoved() {
|
||||
final Charset charset = Charset.forName("cp437");
|
||||
final CharsetEncoder encoder = charset.newEncoder();
|
||||
|
||||
final Predicate<Character> inRange = new Predicate<Character>() {
|
||||
@Override
|
||||
public boolean apply(final Character c) {
|
||||
return encoder.canEncode(c);
|
||||
}
|
||||
};
|
||||
|
||||
final String result = CharMatcher.forPredicate(inRange).retainFrom("helloは");
|
||||
assertEquals("hello", result);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.guava;
|
||||
package com.baeldung.guava.throwables;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import org.junit.Test;
|
|
@ -0,0 +1,13 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -0,0 +1 @@
|
|||
John Jane Adam Tom
|
|
@ -0,0 +1 @@
|
|||
Hello world
|
|
@ -0,0 +1 @@
|
|||
Test
|
|
@ -0,0 +1,4 @@
|
|||
John
|
||||
Jane
|
||||
Adam
|
||||
Tom
|
|
@ -0,0 +1 @@
|
|||
Hello world
|
Binary file not shown.
|
@ -14,6 +14,21 @@
|
|||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>guava-collections-map</finalName>
|
||||
|
||||
|
@ -33,21 +48,6 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
</properties>
|
||||
|
|
|
@ -13,18 +13,6 @@
|
|||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>guava-collections-set</finalName>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
|
@ -47,6 +35,18 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>guava-collections-set</finalName>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
|
|
|
@ -14,4 +14,3 @@ This module contains articles about Google Guava collections
|
|||
- [Guava – Lists](https://www.baeldung.com/guava-lists)
|
||||
- [Guide to Guava MinMaxPriorityQueue and EvictingQueue](https://www.baeldung.com/guava-minmax-priority-queue-and-evicting-queue)
|
||||
- [Guide to Guava Table](https://www.baeldung.com/guava-table)
|
||||
- [Guava CharMatcher](https://www.baeldung.com/guava-string-charmatcher)
|
||||
|
|
|
@ -15,25 +15,6 @@
|
|||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>guava-collections</finalName>
|
||||
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
|
@ -76,6 +57,25 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>guava-collections</finalName>
|
||||
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
|
|
|
@ -4,20 +4,19 @@ import static org.hamcrest.Matchers.contains;
|
|||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetEncoder;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
public class GuavaStringUnitTest {
|
||||
|
||||
|
@ -113,102 +112,4 @@ public class GuavaStringUnitTest {
|
|||
assertEquals(4, result.size());
|
||||
assertThat(result, contains("a", "b", "c", "d,e"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemoveSpecialCharacters_thenRemoved() {
|
||||
final String input = "H*el.lo,}12";
|
||||
final CharMatcher matcher = CharMatcher.javaLetterOrDigit();
|
||||
final String result = matcher.retainFrom(input);
|
||||
|
||||
assertEquals("Hello12", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemoveNonASCIIChars_thenRemoved() {
|
||||
final String input = "あhello₤";
|
||||
|
||||
String result = CharMatcher.ascii().retainFrom(input);
|
||||
assertEquals("hello", result);
|
||||
|
||||
result = CharMatcher.inRange('0', 'z').retainFrom(input);
|
||||
assertEquals("hello", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidateString_thenValid() {
|
||||
final String input = "hello";
|
||||
|
||||
boolean result = CharMatcher.javaLowerCase().matchesAllOf(input);
|
||||
assertTrue(result);
|
||||
|
||||
result = CharMatcher.is('e').matchesAnyOf(input);
|
||||
assertTrue(result);
|
||||
|
||||
result = CharMatcher.javaDigit().matchesNoneOf(input);
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTrimString_thenTrimmed() {
|
||||
final String input = "---hello,,,";
|
||||
|
||||
String result = CharMatcher.is('-').trimLeadingFrom(input);
|
||||
assertEquals("hello,,,", result);
|
||||
|
||||
result = CharMatcher.is(',').trimTrailingFrom(input);
|
||||
assertEquals("---hello", result);
|
||||
|
||||
result = CharMatcher.anyOf("-,").trimFrom(input);
|
||||
assertEquals("hello", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollapseFromString_thenCollapsed() {
|
||||
final String input = " hel lo ";
|
||||
|
||||
String result = CharMatcher.is(' ').collapseFrom(input, '-');
|
||||
assertEquals("-hel-lo-", result);
|
||||
|
||||
result = CharMatcher.is(' ').trimAndCollapseFrom(input, '-');
|
||||
assertEquals("hel-lo", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplaceFromString_thenReplaced() {
|
||||
final String input = "apple-banana.";
|
||||
|
||||
String result = CharMatcher.anyOf("-.").replaceFrom(input, '!');
|
||||
assertEquals("apple!banana!", result);
|
||||
|
||||
result = CharMatcher.is('-').replaceFrom(input, " and ");
|
||||
assertEquals("apple and banana.", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCountCharInString_thenCorrect() {
|
||||
final String input = "a, c, z, 1, 2";
|
||||
|
||||
int result = CharMatcher.is(',').countIn(input);
|
||||
assertEquals(4, result);
|
||||
|
||||
result = CharMatcher.inRange('a', 'h').countIn(input);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemoveCharsNotInCharset_thenRemoved() {
|
||||
final Charset charset = Charset.forName("cp437");
|
||||
final CharsetEncoder encoder = charset.newEncoder();
|
||||
|
||||
final Predicate<Character> inRange = new Predicate<Character>() {
|
||||
@Override
|
||||
public boolean apply(final Character c) {
|
||||
return encoder.canEncode(c);
|
||||
}
|
||||
};
|
||||
|
||||
final String result = CharMatcher.forPredicate(inRange).retainFrom("helloは");
|
||||
assertEquals("hello", result);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,6 +16,21 @@
|
|||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>guava-io</finalName>
|
||||
|
||||
|
@ -35,18 +50,4 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -22,16 +22,6 @@
|
|||
<module>guava-21</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
|
@ -46,4 +36,15 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -12,5 +12,4 @@ This module contains articles a Google Guava
|
|||
- [Guide to Mathematical Utilities in Guava](https://www.baeldung.com/guava-math)
|
||||
- [Bloom Filter in Java using Guava](https://www.baeldung.com/guava-bloom-filter)
|
||||
- [Quick Guide to the Guava RateLimiter](https://www.baeldung.com/guava-rate-limiter)
|
||||
- [Introduction to Guava Throwables](https://www.baeldung.com/guava-throwables)
|
||||
- [Guava Cache](https://www.baeldung.com/guava-cache)
|
||||
|
|
|
@ -15,25 +15,6 @@
|
|||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>guava</finalName>
|
||||
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
|
@ -62,6 +43,25 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>guava</finalName>
|
||||
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
|
|
|
@ -60,6 +60,27 @@
|
|||
<artifactId>tesseract-platform</artifactId>
|
||||
<version>${tesseract-platform.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.imgscalr</groupId>
|
||||
<artifactId>imgscalr-lib</artifactId>
|
||||
<version>${imgscalr-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.coobird</groupId>
|
||||
<artifactId>thumbnailator</artifactId>
|
||||
<version>${thumbnailator-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.downgoon</groupId>
|
||||
<artifactId>marvin</artifactId>
|
||||
<version>${marvin-version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.downgoon</groupId>
|
||||
<artifactId>MarvinPlugins</artifactId>
|
||||
<version>${marvin-version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
@ -69,6 +90,9 @@
|
|||
<tess4j.version>4.5.1</tess4j.version>
|
||||
<tesseract-platform.version>4.1.0-1.5.2</tesseract-platform.version>
|
||||
<opencv.version>3.4.2-0</opencv.version>
|
||||
<imgscalr-version>4.2</imgscalr-version>
|
||||
<thumbnailator-version>0.4.11</thumbnailator-version>
|
||||
<marvin-version>1.5.5</marvin-version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.image.resize.core;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class Graphics2DExample {
|
||||
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
||||
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D graphics2D = resizedImage.createGraphics();
|
||||
graphics2D.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
|
||||
graphics2D.dispose();
|
||||
return resizedImage;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-graphics2d.jpg"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.image.resize.core;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class ImageScaledInstanceExample {
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
||||
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT);
|
||||
BufferedImage bufferedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
||||
bufferedImage.getGraphics()
|
||||
.drawImage(resultingImage, 0, 0, null);
|
||||
return bufferedImage;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-scaledinstance.jpg"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.image.resize.imgscalr;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.imgscalr.Scalr;
|
||||
|
||||
public class ImgscalrExample {
|
||||
public static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) {
|
||||
return Scalr.resize(originalImage, targetWidth);
|
||||
}
|
||||
|
||||
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
||||
return Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, targetWidth, targetHeight, Scalr.OP_ANTIALIAS);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-imgscalr.jpg"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.image.resize.marvin;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.marvinproject.image.transform.scale.Scale;
|
||||
|
||||
import marvin.image.MarvinImage;
|
||||
|
||||
public class MarvinExample {
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
||||
MarvinImage image = new MarvinImage(originalImage);
|
||||
Scale scale = new Scale();
|
||||
scale.load();
|
||||
scale.setAttribute("newWidth", targetWidth);
|
||||
scale.setAttribute("newHeight", targetHeight);
|
||||
scale.process(image.clone(), image, null, null, false);
|
||||
return image.getBufferedImageNoAlpha();
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws IOException {
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-marvin.jpg"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.image.resize.thumbnailator;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import net.coobird.thumbnailator.Thumbnails;
|
||||
|
||||
public class ThumbnailatorExample {
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
Thumbnails.of(originalImage)
|
||||
.size(targetWidth, targetHeight)
|
||||
.outputFormat("JPEG")
|
||||
.outputQuality(0.90)
|
||||
.toOutputStream(outputStream);
|
||||
byte[] data = outputStream.toByteArray();
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
|
||||
return ImageIO.read(inputStream);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-thumbnailator.jpg"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package com.baeldung.image.resize.core;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class Graphics2DExampleUnitTest {
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertEquals(outputImage.getWidth(), targetWidth);
|
||||
assertEquals(outputImage.getHeight(), targetHeight);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||
int targetWidth = 0;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 0;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageDoesNotExist_thenErrorIsNotThrownAndImageIsGenerated() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage outputImage = Graphics2DExample.resizeImage(null, targetWidth, targetHeight);
|
||||
|
||||
assertNotNull(outputImage);
|
||||
assertEquals(outputImage.getWidth(), targetWidth);
|
||||
assertEquals(outputImage.getHeight(), targetHeight);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package com.baeldung.image.resize.core;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ImageScaledInstanceExampleUnitTest {
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertEquals(outputImage.getWidth(), targetWidth);
|
||||
assertEquals(outputImage.getHeight(), targetHeight);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||
int targetWidth = 0;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 0;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(null, targetWidth, targetHeight);
|
||||
|
||||
assertNull(outputImage);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package com.baeldung.image.resize.imgscalr;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ImgscalrExampleUnitTest {
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertEquals(outputImage.getWidth(), targetWidth);
|
||||
assertEquals(outputImage.getHeight(), targetHeight);
|
||||
}
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenTargetWidthIsZero_thenImageIsCreated() throws IOException {
|
||||
int targetWidth = 0;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenTargetHeightIsZero_thenImageIsCreated() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 0;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage outputImage = ImgscalrExample.resizeImage(null, targetWidth, targetHeight);
|
||||
|
||||
assertNull(outputImage);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.image.resize.marvin;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MarvinExampleUnitTest {
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||
BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertEquals(outputImage.getWidth(), targetWidth);
|
||||
assertEquals(outputImage.getHeight(), targetHeight);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||
int targetWidth = 0;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 0;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage outputImage = MarvinExample.resizeImage(null, targetWidth, targetHeight);
|
||||
|
||||
assertNull(outputImage);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.image.resize.thumbnailator;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ThumbnailatorExampleUnitTest {
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||
BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertEquals(outputImage.getWidth(), targetWidth);
|
||||
assertEquals(outputImage.getHeight(), targetHeight);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||
int targetWidth = 0;
|
||||
int targetHeight = 200;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 0;
|
||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
BufferedImage outputImage = ThumbnailatorExample.resizeImage(null, targetWidth, targetHeight);
|
||||
|
||||
assertNull(outputImage);
|
||||
}
|
||||
}
|
|
@ -7,48 +7,59 @@ import java.io.IOException;
|
|||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.google.common.io.Files;
|
||||
|
||||
|
||||
public class CsvUnitTest {
|
||||
|
||||
private File csvFromJson;
|
||||
private File jsonFromCsv;
|
||||
private File formattedCsvFromJson;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
csvFromJson = new File("src/main/resources/csv/csvFromJson.csv");
|
||||
jsonFromCsv = new File("src/main/resources/csv/jsonFromCsv.json");
|
||||
formattedCsvFromJson = new File("src/main/resources/csv/formattedCsvFromJson.csv");
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
csvFromJson.deleteOnExit();
|
||||
jsonFromCsv.deleteOnExit();
|
||||
formattedCsvFromJson.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonInput_thenWriteCsv() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.JsonToCsv(new File("src/main/resources/csv/orderLines.json"),
|
||||
new File("src/main/resources/csv/csvFromJson.csv"));
|
||||
|
||||
assertEquals(readFile("src/main/resources/csv/csvFromJson.csv"),
|
||||
readFile("src/test/resources/csv/expectedCsvFromJson.csv"));
|
||||
JsonCsvConverter.JsonToCsv(new File("src/main/resources/csv/orderLines.json"), csvFromJson);
|
||||
|
||||
assertEquals(readFile(csvFromJson.getAbsolutePath()), readFile("src/test/resources/csv/expectedCsvFromJson.csv"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenCsvInput_thenWritesJson() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.csvToJson(new File("src/main/resources/csv/orderLines.csv"),
|
||||
new File("src/main/resources/csv/jsonFromCsv.json"));
|
||||
|
||||
assertEquals(readFile("src/main/resources/csv/jsonFromCsv.json"),
|
||||
readFile("src/test/resources/csv/expectedJsonFromCsv.json"));
|
||||
|
||||
JsonCsvConverter.csvToJson(new File("src/main/resources/csv/orderLines.csv"), jsonFromCsv);
|
||||
|
||||
assertEquals(readFile(jsonFromCsv.getAbsolutePath()), readFile("src/test/resources/csv/expectedJsonFromCsv.json"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenJsonInput_thenWriteFormattedCsvOutput() throws JsonParseException, JsonMappingException, IOException {
|
||||
JsonCsvConverter.JsonToFormattedCsv(new File("src/main/resources/csv/orderLines.json"),
|
||||
new File("src/main/resources/csv/formattedCsvFromJson.csv"));
|
||||
JsonCsvConverter.JsonToFormattedCsv(new File("src/main/resources/csv/orderLines.json"), formattedCsvFromJson);
|
||||
|
||||
assertEquals(readFile(formattedCsvFromJson.getAbsolutePath()), readFile("src/test/resources/csv/expectedFormattedCsvFromJson.csv"));
|
||||
|
||||
assertEquals(readFile("src/main/resources/csv/formattedCsvFromJson.csv"),
|
||||
readFile("src/test/resources/csv/expectedFormattedCsvFromJson.csv"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
private List<String> readFile(String filename) throws IOException {
|
||||
return Files.readLines(new File(filename), Charset.forName("utf-8"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
;
|
||||
|
||||
};
|
|
@ -12,6 +12,7 @@ import java.time.format.DateTimeFormatter;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -25,12 +26,19 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
|
|||
|
||||
public class YamlUnitTest {
|
||||
private ObjectMapper mapper;
|
||||
private File orderOutput;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER));
|
||||
mapper.findAndRegisterModules();
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
orderOutput = new File("src/test/resources/yaml/orderOutput.yaml");
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
orderOutput.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -53,9 +61,9 @@ public class YamlUnitTest {
|
|||
LocalDate.parse("2019-04-18", DateTimeFormatter.ISO_DATE),
|
||||
"Customer, Jane",
|
||||
lines);
|
||||
mapper.writeValue(new File("src/test/resources/yaml/orderOutput.yaml"), order);
|
||||
mapper.writeValue(orderOutput, order);
|
||||
|
||||
File outputYaml = new File("src/test/resources/yaml/orderOutput.yaml");
|
||||
File outputYaml = new File(orderOutput.getAbsolutePath());
|
||||
assertTrue(outputYaml.exists());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,16 +23,6 @@
|
|||
<module>jackson-exceptions</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
|
@ -60,6 +50,16 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
</properties>
|
||||
|
|
|
@ -13,25 +13,6 @@
|
|||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>jackson-simple</finalName>
|
||||
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<!--jackson for xml -->
|
||||
<dependency>
|
||||
|
@ -61,6 +42,25 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>jackson-simple</finalName>
|
||||
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
|
|
|
@ -274,10 +274,6 @@
|
|||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<properties>
|
||||
<serverProfile>standalone-full.xml</serverProfile>
|
||||
<serverRoot>${project.build.directory}/wildfly-${wildfly.version}</serverRoot>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.undertow</groupId>
|
||||
|
@ -347,15 +343,6 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<environmentVariables>
|
||||
<JBOSS_HOME>${project.build.directory}/wildfly-${wildfly.version}</JBOSS_HOME>
|
||||
</environmentVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
<arquillian xmlns="http://jboss.org/schema/arquillian" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
|
||||
|
||||
<container qualifier="wildfly-managed" default="true">
|
||||
<configuration>
|
||||
<property name="jbossHome">target/wildfly-8.2.1.Final</property>
|
||||
<property name="serverConfig">standalone.xml</property>
|
||||
<property name="outputToConsole">true</property>
|
||||
<property name="managementPort">9990</property>
|
||||
<property name="javaVmArguments">-Djboss.http.port=8639</property>
|
||||
</configuration>
|
||||
</container>
|
||||
|
||||
<container qualifier="wildfly-remote">
|
||||
<configuration>
|
||||
<property name="managementAddress">127.0.0.1</property>
|
||||
<property name="managementPort">9990</property>
|
||||
<property name="username">admin</property>
|
||||
<property name="password">pass</property>
|
||||
<property name="allowConnectingToRunningServer">true</property>
|
||||
</configuration>
|
||||
</container>
|
||||
|
||||
</arquillian>
|
|
@ -14,4 +14,5 @@ Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-m
|
|||
- [Introduction to Functional Java](https://www.baeldung.com/java-functional-library)
|
||||
- [A Guide to the Reflections Library](https://www.baeldung.com/reflections-library)
|
||||
- [Exactly Once Processing in Kafka](https://www.baeldung.com/kafka-exactly-once)
|
||||
- [Introduction to Protonpack](https://www.baeldung.com/java-protonpack)
|
||||
- More articles [[<-- prev]](/libraries-5)
|
||||
|
|
|
@ -10,8 +10,6 @@ import org.junit.Test;
|
|||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
|
@ -184,6 +184,24 @@
|
|||
</repository>
|
||||
</repositories>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<!-- Excludes FastR classes from compilations since they require GraalVM -->
|
||||
<excludes>
|
||||
<exclude>com/baeldung/r/FastRMean.java</exclude>
|
||||
</excludes>
|
||||
<testExcludes>
|
||||
<exclude>com/baeldung/r/FastRMeanUnitTest.java</exclude>
|
||||
</testExcludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<flink.version>1.5.0</flink.version>
|
||||
<hll.version>1.6.0</hll.version>
|
||||
|
@ -205,21 +223,4 @@
|
|||
<kafka.version>2.5.0</kafka.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<!-- Excludes FastR classes from compilations since they require GraalVM -->
|
||||
<excludes>
|
||||
<exclude>com/baeldung/r/FastRMean.java</exclude>
|
||||
</excludes>
|
||||
<testExcludes>
|
||||
<exclude>com/baeldung/r/FastRMeanUnitTest.java</exclude>
|
||||
</testExcludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -2,15 +2,26 @@ package com.baeldung.univocity;
|
|||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.univocity.model.Product;
|
||||
|
||||
public class ParsingServiceUnitTest {
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
File csvFile = new File("src/test/resources/outputProductList.csv");
|
||||
csvFile.deleteOnExit();
|
||||
|
||||
File textFile = new File("src/test/resources/outputProductList.txt");
|
||||
textFile.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCsvFile_thenParsedResultsShouldBeReturned() {
|
||||
ParsingService parsingService = new ParsingService();
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package com.baeldung.serenity.spring;
|
||||
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Scope("prototype")
|
||||
public class AdderService {
|
||||
|
||||
private int num;
|
||||
|
|
|
@ -1,19 +1,21 @@
|
|||
package com.baeldung.mdc;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.baeldung.mdc.log4j.Log4JRunnable;
|
||||
import com.baeldung.mdc.log4j2.Log4J2Runnable;
|
||||
import com.baeldung.mdc.pool.MdcAwareThreadPoolExecutor;
|
||||
import com.baeldung.mdc.slf4j.Slf4jRunnable;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||
|
||||
public class TransferDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
ExecutorService executor = Executors.newFixedThreadPool(3);
|
||||
ExecutorService executor = new MdcAwareThreadPoolExecutor(3, 3, 0, MINUTES,
|
||||
new LinkedBlockingQueue<>(), Thread::new, new AbortPolicy());
|
||||
|
||||
TransactionFactory transactionFactory = new TransactionFactory();
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.mdc.pool;
|
||||
|
||||
import org.apache.logging.log4j.ThreadContext;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.RejectedExecutionHandler;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class MdcAwareThreadPoolExecutor extends ThreadPoolExecutor {
|
||||
|
||||
public MdcAwareThreadPoolExecutor(int corePoolSize,
|
||||
int maximumPoolSize,
|
||||
long keepAliveTime,
|
||||
TimeUnit unit,
|
||||
BlockingQueue<Runnable> workQueue,
|
||||
ThreadFactory threadFactory,
|
||||
RejectedExecutionHandler handler) {
|
||||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterExecute(Runnable r, Throwable t) {
|
||||
System.out.println("Cleaning the MDC context");
|
||||
MDC.clear();
|
||||
org.apache.log4j.MDC.clear();
|
||||
ThreadContext.clearAll();
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ public class Slf4jRunnable implements Runnable {
|
|||
|
||||
new Slf4TransferService().transfer(tx.getAmount());
|
||||
|
||||
MDC.clear();
|
||||
// MDC.clear(); We don't need this with MdcAwareThreadPoolExecutor
|
||||
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>machine-learning</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>Supervised Learning</name>
|
||||
<name>machine-learning</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [How to Create a Maven Plugin](https://www.baeldung.com/maven-plugin)
|
|
@ -1,18 +0,0 @@
|
|||
## Apache Maven
|
||||
|
||||
This module contains articles about core Apache Maven. Articles about other Maven plugins (such as the Maven WAR Plugin)
|
||||
have their own dedicated modules.
|
||||
|
||||
### Relevant Articles
|
||||
|
||||
- [Guide to the Core Maven Plugins](https://www.baeldung.com/core-maven-plugins)
|
||||
- [Maven Resources Plugin](https://www.baeldung.com/maven-resources-plugin)
|
||||
- [Quick Guide to the Maven Surefire Plugin](https://www.baeldung.com/maven-surefire-plugin)
|
||||
- [The Maven Failsafe Plugin](https://www.baeldung.com/maven-failsafe-plugin)
|
||||
- [The Maven Verifier Plugin](https://www.baeldung.com/maven-verifier-plugin)
|
||||
- [The Maven Clean Plugin](https://www.baeldung.com/maven-clean-plugin)
|
||||
- [Build a Jar with Maven and Ignore the Test Results](https://www.baeldung.com/maven-ignore-test-results)
|
||||
- [Maven Project with Multiple Source Directories](https://www.baeldung.com/maven-project-multiple-src-directories)
|
||||
- [Integration Testing with Maven](https://www.baeldung.com/maven-integration-test)
|
||||
- [Apache Maven Standard Directory Layout](https://www.baeldung.com/maven-directory-structure)
|
||||
- [Multi-Module Project with Maven](https://www.baeldung.com/maven-multi-module)
|
|
@ -1,3 +0,0 @@
|
|||
### Relevant Articles
|
||||
|
||||
- [Maven Enforcer Plugin](https://www.baeldung.com/maven-enforcer-plugin)
|
|
@ -0,0 +1,54 @@
|
|||
<?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">
|
||||
<parent>
|
||||
<artifactId>maven-all</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>version-collision</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>project-a</module>
|
||||
<module>project-b</module>
|
||||
<module>project-collision</module>
|
||||
</modules>
|
||||
|
||||
<!--comment section below to provoke version collision in project-collision module-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>29.0-jre</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<!-- uncomment section below to ban the use of transitive dependencies -->
|
||||
<!-- <build>-->
|
||||
<!-- <plugins>-->
|
||||
<!-- <plugin>-->
|
||||
<!-- <groupId>org.apache.maven.plugins</groupId>-->
|
||||
<!-- <artifactId>maven-enforcer-plugin</artifactId>-->
|
||||
<!-- <version>3.0.0-M3</version>-->
|
||||
<!-- <executions>-->
|
||||
<!-- <execution>-->
|
||||
<!-- <id>enforce-banned-dependencies</id>-->
|
||||
<!-- <goals>-->
|
||||
<!-- <goal>enforce</goal>-->
|
||||
<!-- </goals>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <rules>-->
|
||||
<!-- <banTransitiveDependencies/>-->
|
||||
<!-- </rules>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- </execution>-->
|
||||
<!-- </executions>-->
|
||||
<!-- </plugin>-->
|
||||
<!-- </plugins>-->
|
||||
<!-- </build>-->
|
||||
</project>
|
|
@ -0,0 +1,21 @@
|
|||
<?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">
|
||||
<parent>
|
||||
<artifactId>version-collision</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>project-a</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>22.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,21 @@
|
|||
<?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">
|
||||
<parent>
|
||||
<artifactId>version-collision</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>project-b</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>29.0-jre</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,34 @@
|
|||
<?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">
|
||||
<parent>
|
||||
<artifactId>version-collision</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>project-collision</artifactId>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>project-a</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<!-- uncomment to exclude guava transitive artifact from module -->
|
||||
<!-- <exclusions>-->
|
||||
<!-- <exclusion>-->
|
||||
<!-- <groupId>com.google.guava</groupId>-->
|
||||
<!-- <artifactId>guava</artifactId>-->
|
||||
<!-- </exclusion>-->
|
||||
<!-- </exclusions>-->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>project-b</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.version.collision;
|
||||
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class VersionCollisionUnitTest {
|
||||
@Test
|
||||
public void whenVersionCollisionDoesNotExist_thenShouldCompile() {
|
||||
assertThat(Futures.immediateVoidFuture(), notNullValue());
|
||||
}
|
||||
}
|
|
@ -5,4 +5,5 @@ This module contains articles about Apache Maven. Please refer to its submodules
|
|||
### Relevant Articles
|
||||
|
||||
- [Apache Maven Tutorial](https://www.baeldung.com/maven)
|
||||
- [Find Unused Maven Dependencies](https://www.baeldung.com/maven-unused-dependencies)
|
||||
- [Apache Maven Standard Directory Layout](https://www.baeldung.com/maven-directory-structure)
|
||||
- [Multi-Module Project with Maven](https://www.baeldung.com/maven-multi-module)
|
|
@ -0,0 +1,7 @@
|
|||
## Apache Maven
|
||||
|
||||
This module contains articles about creating a custom plugin in Maven.
|
||||
|
||||
### Relevant Articles
|
||||
|
||||
- [How to Create a Maven Plugin](https://www.baeldung.com/maven-plugin)
|
|
@ -0,0 +1,21 @@
|
|||
<?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>
|
||||
<artifactId>maven-custom-plugin</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>maven-custom-plugin</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>maven-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>counter-maven-plugin</module>
|
||||
<module>usage-example</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
|
@ -5,7 +5,7 @@
|
|||
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>example</artifactId>
|
||||
<artifactId>usage-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
## Apache Maven
|
||||
|
||||
This module contains articles about Integration Testing with Maven and related plugins.
|
||||
|
||||
### Relevant Articles
|
||||
|
||||
- [Integration Testing with Maven](https://www.baeldung.com/maven-integration-test)
|
||||
- [Build a Jar with Maven and Ignore the Test Results](https://www.baeldung.com/maven-ignore-test-results)
|
||||
- [Quick Guide to the Maven Surefire Plugin](https://www.baeldung.com/maven-surefire-plugin)
|
||||
- [The Maven Failsafe Plugin](https://www.baeldung.com/maven-failsafe-plugin)
|
|
@ -2,10 +2,9 @@
|
|||
<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>
|
||||
<artifactId>maven</artifactId>
|
||||
<artifactId>maven-integration-test</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>maven</name>
|
||||
<packaging>pom</packaging>
|
||||
<name>maven-integration-test</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -14,11 +13,6 @@
|
|||
<relativePath>../..</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>custom-rule</module>
|
||||
<module>maven-enforcer</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.containers</groupId>
|
||||
|
@ -126,20 +120,6 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-verifier-plugin</artifactId>
|
||||
<version>${maven.verifier.version}</version>
|
||||
<configuration>
|
||||
<verificationFile>input-resources/verifications.xml</verificationFile>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>verify</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>${maven.clean.version}</version>
|
||||
|
@ -156,17 +136,6 @@
|
|||
<artifactId>build-helper-maven-plugin</artifactId>
|
||||
<version>${maven.build.helper.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>add-source</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>src/main/another-src</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>add-integration-test-source</id>
|
||||
<phase>generate-test-sources</phase>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue