Merge branch 'eugenp:master' into master

This commit is contained in:
neha298 2023-04-16 23:39:57 +05:30 committed by GitHub
commit 9882fe90ea
709 changed files with 2640 additions and 33523 deletions

View File

@ -0,0 +1,40 @@
package com.baeldung.httpclient;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpStatus;
import org.junit.jupiter.api.Test;
class HttpClientCancelRequestLiveTest {
private static final String SAMPLE_URL = "http://www.github.com";
@Test
void whenRequestIsCanceled_thenCorrect() throws IOException {
HttpGet request = new HttpGet(SAMPLE_URL);
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
httpClient.execute(request, response -> {
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getCode());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
System.out.println("----------------------------------------");
// Do not feel like reading the response body
// Call abort on the request object
request.abort();
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
return response;
});
}
}
}

View File

@ -1,8 +1,8 @@
package com.baeldung.httpclient.base; package com.baeldung.httpclient.base;
import com.baeldung.httpclient.ResponseUtil; import com.baeldung.httpclient.ResponseUtil;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders; import org.apache.http.HttpHeaders;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
@ -10,7 +10,6 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager; import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -73,30 +72,4 @@ public class HttpClientLiveTest {
assertThat(headers, not(emptyArray())); assertThat(headers, not(emptyArray()));
} }
// tests - cancel request
@Test
public final void whenRequestIsCanceled_thenCorrect() throws IOException {
instance = HttpClients.custom().build();
final HttpGet request = new HttpGet(SAMPLE_URL);
response = instance.execute(request);
try {
final HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
System.out.println("----------------------------------------");
// Do not feel like reading the response body
// Call abort on the request object
request.abort();
} finally {
response.close();
}
}
} }

View File

@ -4,6 +4,7 @@ This module contains articles about Apache HttpClient 4.5
### Relevant Articles ### Relevant Articles
- [Apache HttpClient Cancel Request](https://www.baeldung.com/httpclient-cancel-request)
- [Apache HttpClient with SSL](https://www.baeldung.com/httpclient-ssl) - [Apache HttpClient with SSL](https://www.baeldung.com/httpclient-ssl)
- [Apache HttpClient Timeout](https://www.baeldung.com/httpclient-timeout) - [Apache HttpClient Timeout](https://www.baeldung.com/httpclient-timeout)
- [Custom HTTP Header with the Apache HttpClient](https://www.baeldung.com/httpclient-custom-http-header) - [Custom HTTP Header with the Apache HttpClient](https://www.baeldung.com/httpclient-custom-http-header)

View File

@ -0,0 +1,57 @@
package com.baeldung.httpclient;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HttpClientCancelRequestV4LiveTest {
private static final String SAMPLE_URL = "http://www.github.com";
private CloseableHttpClient instance;
private CloseableHttpResponse response;
@Before
public final void before() {
instance = HttpClientBuilder.create().build();
}
@After
public final void after() throws IllegalStateException, IOException {
ResponseUtil.closeResponse(response);
}
@Test
public final void whenRequestIsCanceled_thenCorrect() throws IOException {
instance = HttpClients.custom().build();
final HttpGet request = new HttpGet(SAMPLE_URL);
response = instance.execute(request);
try {
final HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
System.out.println("----------------------------------------");
// Do not feel like reading the response body
// Call abort on the request object
request.abort();
} finally {
response.close();
}
}
}

View File

@ -24,7 +24,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class ApacheHttpClientRetryIntegrationTest { public class ApacheHttpClientRetryLiveTest {
private Integer requestCounter; private Integer requestCounter;
private CloseableHttpClient httpClient; private CloseableHttpClient httpClient;

View File

@ -10,7 +10,7 @@
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>aws-lambda</artifactId> <artifactId>aws-lambda-modules</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
</parent> </parent>

View File

@ -3,8 +3,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> 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> <modelVersion>4.0.0</modelVersion>
<artifactId>aws-lambda</artifactId> <artifactId>aws-lambda-modules</artifactId>
<name>aws-lambda</name> <name>aws-lambda-modules</name>
<packaging>pom</packaging> <packaging>pom</packaging>
<parent> <parent>
@ -14,9 +14,9 @@
</parent> </parent>
<modules> <modules>
<module>lambda</module> <module>lambda-function</module>
<module>shipping-tracker/ShippingFunction</module> <module>shipping-tracker-lambda/ShippingFunction</module>
<module>todo-reminder/ToDoFunction</module> <module>todo-reminder-lambda/ToDoFunction</module>
</modules> </modules>
</project> </project>

View File

@ -9,7 +9,7 @@
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>aws-lambda</artifactId> <artifactId>aws-lambda-modules</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath> <relativePath>../../</relativePath>
</parent> </parent>

View File

@ -8,6 +8,13 @@
<name>ToDoFunction</name> <name>ToDoFunction</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>aws-lambda-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.amazonaws</groupId> <groupId>com.amazonaws</groupId>

View File

@ -21,7 +21,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class AppTest { public class AppUnitTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mockContext; private Context mockContext;

View File

@ -7,7 +7,7 @@ import uk.org.webcompere.systemstubs.rules.SystemOutRule;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
public class ToDoReaderServiceTest { public class ToDoReaderServiceUnitTest {
@Rule @Rule
public SystemOutRule systemOutRule = new SystemOutRule(); public SystemOutRule systemOutRule = new SystemOutRule();

View File

@ -15,7 +15,7 @@
<modules> <modules>
<module>aws-app-sync</module> <module>aws-app-sync</module>
<module>aws-lambda</module> <module>aws-lambda-modules</module>
<module>aws-miscellaneous</module> <module>aws-miscellaneous</module>
<module>aws-reactive</module> <module>aws-reactive</module>
<module>aws-s3</module> <module>aws-s3</module>

View File

@ -3,9 +3,9 @@
xmlns="http://maven.apache.org/POM/4.0.0" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>checker-plugin</artifactId> <artifactId>checker-framework</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<name>checker-plugin</name> <name>checker-framework</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<parent> <parent>

View File

@ -0,0 +1,20 @@
package com.baeldung.parsingDates;
import java.util.Arrays;
import java.util.List;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
public class SimpleDateTimeFormat {
public static LocalDate parseDate(String date) {
List<String> patternList = Arrays.asList("MM/dd/yyyy", "dd.MM.yyyy", "yyyy-MM-dd");
for (String pattern : patternList) {
try {
return DateTimeFormat.forPattern(pattern).parseLocalDate(date);
} catch (IllegalArgumentException e) {
}
}
return null;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.parsingDates;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
public class SimpleDateTimeFormater {
public static LocalDate parseDate(String date) {
DateTimeFormatterBuilder dateTimeFormatterBuilder = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ofPattern("[MM/dd/yyyy]" + "[dd-MM-yyyy]" + "[yyyy-MM-dd]"));
DateTimeFormatter dateTimeFormatter = dateTimeFormatterBuilder.toFormatter();
return LocalDate.parse(date, dateTimeFormatter);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.parsingDates;
import java.text.ParseException;
import java.util.Date;
import org.apache.commons.lang3.time.DateUtils;
public class SimpleDateUtils {
public static Date parseDate(String date) {
try {
return DateUtils.parseDateStrictly(date,
new String[]{"yyyy/MM/dd", "dd/MM/yyyy", "yyyy-MM-dd"});
} catch (ParseException ex) {
return null;
}
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.parsingDates;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class SimpleParseDate {
public Date parseDate(String dateString, List<String> formatStrings) {
for (String formatString : formatStrings) {
try {
return new SimpleDateFormat(formatString).parse(dateString);
} catch (ParseException e) {
}
}
return null;
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.parsingDates;
import com.baeldung.parsingDates.SimpleDateTimeFormat;
import com.baeldung.parsingDates.SimpleDateTimeFormater;
import com.baeldung.parsingDates.SimpleDateUtils;
import com.baeldung.parsingDates.SimpleParseDate;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import org.junit.*;
import static org.junit.Assert.*;
import org.joda.time.LocalDate;
public class SimpleParseDateUnitTest {
@Test
public void whenInvalidInput_thenGettingUnexpectedResult() {
SimpleParseDate simpleParseDate = new SimpleParseDate();
String date = "2022-40-40";
assertEquals("Sat May 10 00:00:00 UTC 2025", simpleParseDate.parseDate(date, Arrays.asList("MM/dd/yyyy", "dd.MM.yyyy", "yyyy-MM-dd")).toString());
}
@Test
public void whenInvalidDate_thenAssertThrows() {
SimpleDateTimeFormater simpleDateTimeFormater = new SimpleDateTimeFormater();
assertEquals(java.time.LocalDate.parse("2022-12-04"), simpleDateTimeFormater.parseDate("2022-12-04"));
assertThrows(DateTimeParseException.class, () -> simpleDateTimeFormater.parseDate("2022-13-04"));
}
@Test
public void whenDateIsCorrect_thenParseCorrect() {
SimpleDateUtils simpleDateUtils = new SimpleDateUtils();
assertNull(simpleDateUtils.parseDate("53/10/2014"));
assertEquals("Wed Sep 10 00:00:00 UTC 2014", simpleDateUtils.parseDate("10/09/2014").toString());
}
@Test
public void whenDateIsCorrect_thenResultCorrect() {
SimpleDateTimeFormat simpleDateTimeFormat = new SimpleDateTimeFormat();
assertNull(simpleDateTimeFormat.parseDate("53/10/2014"));
assertEquals(LocalDate.parse("2014-10-10"), simpleDateTimeFormat.parseDate("2014-10-10"));
}
}

View File

@ -0,0 +1,2 @@
#!/usr/bin/env bash
javac -d mods/com.baeldung.library.core $(find library-core/src/main -name "*.java")

View File

@ -0,0 +1,10 @@
#!/usr/bin/env bash
javac --class-path outDir/library-core/:\
libs/junit-jupiter-engine-5.9.2.jar:\
libs/junit-platform-engine-1.9.2.jar:\
libs/apiguardian-api-1.1.2.jar:\
libs/junit-jupiter-params-5.9.2.jar:\
libs/junit-jupiter-api-5.9.2.jar:\
libs/opentest4j-1.2.0.jar:\
libs/junit-platform-commons-1.9.2.jar \
-d outDir/library-test library-core/src/test/java/com/baeldung/library/core/LibraryUnitTest.java

View File

@ -0,0 +1,13 @@
#!/usr/bin/env bash
javac --class-path libs/junit-jupiter-engine-5.9.2.jar:\
libs/junit-platform-engine-1.9.2.jar:\
libs/apiguardian-api-1.1.2.jar:\
libs/junit-jupiter-params-5.9.2.jar:\
libs/junit-jupiter-api-5.9.2.jar:\
libs/opentest4j-1.2.0.jar:\
libs/junit-platform-commons-1.9.2.jar \
-d outDir/library-core \
library-core/src/main/java/com/baeldung/library/core/Book.java \
library-core/src/main/java/com/baeldung/library/core/Library.java \
library-core/src/main/java/com/baeldung/library/core/Main.java \
library-core/src/test/java/com/baeldung/library/core/LibraryUnitTest.java

View File

@ -0,0 +1,12 @@
#!/usr/bin/env bash
javac --class-path libs/junit-jupiter-engine-5.9.2.jar:\
libs/junit-platform-engine-1.9.2.jar:\
libs/apiguardian-api-1.1.2.jar:\
libs/junit-jupiter-params-5.9.2.jar:\
libs/junit-jupiter-api-5.9.2.jar:\
libs/opentest4j-1.2.0.jar:\
libs/junit-platform-commons-1.9.2.jar \
-d outDir/library-core \
library-core/src/main/java/com/baeldung/library/core/Book.java \
library-core/src/main/java/com/baeldung/library/core/Library.java \
library-core/src/main/java/com/baeldung/library/core/Main.java

View File

@ -0,0 +1,2 @@
#!/usr/bin/env bash
javac --module-path mods:libs -d mods/com.baeldung.library.test $(find library-test/src/test -name "*.java")

View File

@ -0,0 +1,10 @@
#!/usr/bin/env bash
wget -P libs/ https://repo1.maven.org/maven2/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar /
wget -P libs/ https://repo1.maven.org/maven2/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar /
wget -P libs/ https://repo1.maven.org/maven2/org/junit/platform/junit-platform-commons/1.9.2/junit-platform-commons-1.9.2.jar /
wget -P libs/ https://repo1.maven.org/maven2/org/junit/platform/junit-platform-engine/1.9.2/junit-platform-engine-1.9.2.jar /
wget -P libs/ https://repo1.maven.org/maven2/org/junit/platform/junit-platform-reporting/1.9.2/junit-platform-reporting-1.9.2.jar /
wget -P libs/ https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console/1.9.2/junit-platform-console-1.9.2.jar /
wget -P libs/ https://repo1.maven.org/maven2/org/junit/platform/junit-platform-launcher/1.9.2/junit-platform-launcher-1.9.2.jar /
wget -P libs/ https://repo1.maven.org/maven2/org/junit/jupiter/junit-jupiter-engine/5.9.2/junit-jupiter-engine-5.9.2.jar /
wget -P libs/ https://repo1.maven.org/maven2/org/junit/jupiter/junit-jupiter-params/5.9.2/junit-jupiter-params-5.9.2.jar

View File

@ -0,0 +1,55 @@
<?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>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>core-java-9-jigsaw</artifactId>
<version>0.2-SNAPSHOT</version>
</parent>
<artifactId>library-core</artifactId>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>9</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,52 @@
package com.baeldung.library.core;
import java.util.Objects;
public class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
@Override
public String toString() {
return "Book [title=" + title + ", author=" + author + "]";
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Book book = (Book) o;
if (!Objects.equals(title, book.title)) {
return false;
}
return Objects.equals(author, book.author);
}
@Override
public int hashCode() {
int result = title != null ? title.hashCode() : 0;
result = 31 * result + (author != null ? author.hashCode() : 0);
return result;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.library.core;
import java.util.ArrayList;
import java.util.List;
public class Library {
private List<Book> books = new ArrayList<>();
public void addBook(Book book) {
books.add(book);
}
public List<Book> getBooks() {
return books;
}
void removeBook(Book book) {
books.remove(book);
}
protected void removeBookByAuthor(String author) {
books.removeIf(book -> book.getAuthor().equals(author));
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.library.core;
public class Main {
public static void main(String[] args) {
Library library = new Library();
library.addBook(new Book("The Lord of the Rings", "J.R.R. Tolkien"));
library.addBook(new Book("The Hobbit", "J.R.R. Tolkien"));
library.addBook(new Book("The Silmarillion", "J.R.R. Tolkien"));
library.addBook(new Book("The Chronicles of Narnia", "C.S. Lewis"));
library.addBook(new Book("The Lion, the Witch and the Wardrobe", "C.S. Lewis"));
System.out.println("Welcome to our library!");
System.out.println("We have the following books:");
library.getBooks().forEach(System.out::println);
}
}

View File

@ -0,0 +1,3 @@
module com.baeldung.library.core {
exports com.baeldung.library.core;
}

View File

@ -0,0 +1,47 @@
package com.baeldung.library.core;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class LibraryUnitTest {
@Test
void givenEmptyLibrary_whenAddABook_thenLibraryHasOneBook() {
Library library = new Library();
Book theLordOfTheRings = new Book("The Lord of the Rings", "J.R.R. Tolkien");
library.addBook(theLordOfTheRings);
int expected = 1;
int actual = library.getBooks().size();
assertEquals(expected, actual);
}
@Test
void givenTheLibraryWithABook_whenRemoveABook_thenLibraryIsEmpty() {
Library library = new Library();
Book theLordOfTheRings = new Book("The Lord of the Rings", "J.R.R. Tolkien");
library.addBook(theLordOfTheRings);
library.removeBook(theLordOfTheRings);
int expected = 0;
int actual = library.getBooks().size();
assertEquals(expected, actual);
}
@Test
void givenTheLibraryWithSeveralBook_whenRemoveABookByAuthor_thenLibraryHasNoBooksByTheAuthor() {
Library library = new Library();
Book theLordOfTheRings = new Book("The Lord of the Rings", "J.R.R. Tolkien");
Book theHobbit = new Book("The Hobbit", "J.R.R. Tolkien");
Book theSilmarillion = new Book("The Silmarillion", "J.R.R. Tolkien");
Book theHungerGames = new Book("The Hunger Games", "Suzanne Collins");
library.addBook(theLordOfTheRings);
library.addBook(theHobbit);
library.addBook(theSilmarillion);
library.addBook(theHungerGames);
library.removeBookByAuthor("J.R.R. Tolkien");
int expected = 1;
int actual = library.getBooks().size();
assertEquals(expected, actual);
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.library.test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.baeldung.library.core.Book;
import com.baeldung.library.core.Library;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
class LibraryUnitTest {
@Test
void givenEmptyLibrary_whenAddABook_thenLibraryHasOneBook() {
Library library = new Library();
Book theLordOfTheRings = new Book("The Lord of the Rings", "J.R.R. Tolkien");
library.addBook(theLordOfTheRings);
int expected = 1;
int actual = library.getBooks().size();
assertEquals(expected, actual);
}
@Test
void givenTheLibraryWithABook_whenRemoveABook_thenLibraryIsEmpty()
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Library library = new Library();
Book theLordOfTheRings = new Book("The Lord of the Rings", "J.R.R. Tolkien");
library.addBook(theLordOfTheRings);
Method removeBook = Library.class.getDeclaredMethod("removeBook", Book.class);
removeBook.setAccessible(true);
removeBook.invoke(library, theLordOfTheRings);
int expected = 0;
int actual = library.getBooks().size();
assertEquals(expected, actual);
}
@Test
void givenTheLibraryWithSeveralBook_whenRemoveABookByAuthor_thenLibraryHasNoBooksByTheAuthor() {
TestLibrary library = new TestLibrary();
Book theLordOfTheRings = new Book("The Lord of the Rings", "J.R.R. Tolkien");
Book theHobbit = new Book("The Hobbit", "J.R.R. Tolkien");
Book theSilmarillion = new Book("The Silmarillion", "J.R.R. Tolkien");
Book theHungerGames = new Book("The Hunger Games", "Suzanne Collins");
library.addBook(theLordOfTheRings);
library.addBook(theHobbit);
library.addBook(theSilmarillion);
library.addBook(theHungerGames);
library.removeBookByAuthor("J.R.R. Tolkien");
int expected = 1;
int actual = library.getBooks().size();
assertEquals(expected, actual);
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.library.test;
import com.baeldung.library.core.Library;
public class TestLibrary extends Library {
@Override
public void removeBookByAuthor(final String author) {
super.removeBookByAuthor(author);
}
}

View File

@ -0,0 +1,5 @@
module com.baeldung.library.test {
requires com.baeldung.library.core;
requires org.junit.jupiter.api;
opens com.baeldung.library.test to org.junit.platform.commons;
}

View File

@ -5,6 +5,10 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>core-java-9-jigsaw</artifactId> <artifactId>core-java-9-jigsaw</artifactId>
<name>core-java-9-jigsaw</name> <name>core-java-9-jigsaw</name>
<packaging>pom</packaging>
<modules>
<module>library-core</module>
</modules>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>

View File

@ -0,0 +1,7 @@
#!/usr/bin/env bash
java --module-path mods:libs \
--add-modules com.baeldung.library.core \
--add-opens com.baeldung.library.core/com.baeldung.library.core=org.junit.platform.commons \
--add-reads com.baeldung.library.core=org.junit.jupiter.api \
--patch-module com.baeldung.library.core=outDir/library-test \
--module org.junit.platform.console --select-class com.baeldung.library.core.LibraryUnitTest

View File

@ -0,0 +1,2 @@
#!/usr/bin/env bash
java --module-path mods --module com.baeldung.library.core/com.baeldung.library.core.Main

View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
java --module-path libs \
org.junit.platform.console.ConsoleLauncher \
--classpath ./outDir/library-core \
--select-class com.baeldung.library.core.LibraryUnitTest

View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
java --module-path mods:libs \
--add-modules com.baeldung.library.test \
--add-opens com.baeldung.library.core/com.baeldung.library.core=com.baeldung.library.test \
org.junit.platform.console.ConsoleLauncher --select-class com.baeldung.library.test.LibraryUnitTest

View File

@ -0,0 +1,6 @@
## Core Java Compiler
### Relevant Articles:
- [Compiling Java *.class Files with javac](http://www.baeldung.com/javac)
- [Illegal Character Compilation Error](https://www.baeldung.com/java-illegal-character-error)

View File

@ -0,0 +1,29 @@
<?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-compiler</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-compiler</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.google.gdata</groupId>
<artifactId>core</artifactId>
<version>${gdata.version}</version>
</dependency>
</dependencies>
<properties>
<gdata.version>1.47.1</gdata.version>
</properties>
</project>

View File

@ -4,7 +4,6 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 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> <modelVersion>4.0.0</modelVersion>
<artifactId>core-java-jpms</artifactId> <artifactId>core-java-jpms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>core-java-jpms</name> <name>core-java-jpms</name>
<packaging>pom</packaging> <packaging>pom</packaging>
@ -15,8 +14,8 @@
</parent> </parent>
<modules> <modules>
<module>decoupling-pattern1</module> <module>service-provider-factory-pattern</module>
<module>decoupling-pattern2</module> <module>service-loader-api-pattern</module>
</modules> </modules>
<build> <build>

View File

@ -8,8 +8,8 @@
<version>1.0</version> <version>1.0</version>
<parent> <parent>
<groupId>com.baeldung.decoupling-pattern2</groupId> <groupId>com.baeldung.service-loader-api-pattern</groupId>
<artifactId>decoupling-pattern2</artifactId> <artifactId>service-loader-api-pattern</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>

View File

@ -3,8 +3,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> 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> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.decoupling-pattern2</groupId> <groupId>com.baeldung.service-loader-api-pattern</groupId>
<artifactId>decoupling-pattern2</artifactId> <artifactId>service-loader-api-pattern</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>

View File

@ -8,8 +8,8 @@
<version>1.0</version> <version>1.0</version>
<parent> <parent>
<groupId>com.baeldung.decoupling-pattern2</groupId> <groupId>com.baeldung.service-loader-api-pattern</groupId>
<artifactId>decoupling-pattern2</artifactId> <artifactId>service-loader-api-pattern</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>

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