@@ -4,6 +4,4 @@ This module contains articles about Apache CXF
|
||||
|
||||
## Relevant Articles:
|
||||
|
||||
- [Apache CXF Support for RESTful Web Services](https://www.baeldung.com/apache-cxf-rest-api)
|
||||
- [A Guide to Apache CXF with Spring](https://www.baeldung.com/apache-cxf-with-spring)
|
||||
- [Introduction to Apache CXF](https://www.baeldung.com/introduction-to-apache-cxf)
|
||||
- [Introduction to Apache CXF Aegis Data Binding](https://www.baeldung.com/aegis-data-binding-in-apache-cxf)
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
### Relevant Articles:
|
||||
- [Introduction to Apache CXF](http://www.baeldung.com/introduction-to-apache-cxf)
|
||||
- [Introduction to Apache CXF](https://www.baeldung.com/introduction-to-apache-cxf)
|
||||
|
||||
+2
-2
@@ -19,7 +19,7 @@
|
||||
<dependency>
|
||||
<groupId>org.axonframework</groupId>
|
||||
<artifactId>axon-bom</artifactId>
|
||||
<version>${axon.version}</version>
|
||||
<version>${axon-bom.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
@@ -57,7 +57,7 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<axon.version>4.5.0</axon.version>
|
||||
<axon-bom.version>4.5.13</axon-bom.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -10,10 +10,9 @@
|
||||
<description>Demo project for CAS</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung.cas</groupId>
|
||||
<artifactId>cas</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
-1
@@ -88,7 +88,6 @@ public class CasSecuredApplication {
|
||||
@Bean
|
||||
public SingleSignOutFilter singleSignOutFilter() {
|
||||
SingleSignOutFilter singleSignOutFilter = new SingleSignOutFilter();
|
||||
singleSignOutFilter.setCasServerUrlPrefix("https://localhost:8443");
|
||||
singleSignOutFilter.setLogoutCallbackPath("/exit/cas");
|
||||
singleSignOutFilter.setIgnoreInitConfiguration(true);
|
||||
return singleSignOutFilter;
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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.cas</groupId>
|
||||
<artifactId>cas</artifactId>
|
||||
<name>cas</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>cas-secured-app</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
@@ -13,3 +13,4 @@ This module contains articles about Java 11 core features
|
||||
- [Call Methods at Runtime Using Java Reflection](https://www.baeldung.com/java-method-reflection)
|
||||
- [Java HttpClient Basic Authentication](https://www.baeldung.com/java-httpclient-basic-auth)
|
||||
- [Java HttpClient With SSL](https://www.baeldung.com/java-httpclient-ssl)
|
||||
- [Adding Parameters to Java HttpClient Requests](https://www.baeldung.com/java-httpclient-request-parameters)
|
||||
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.httpclient.parameters;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class HttpClientParametersLiveTest {
|
||||
|
||||
private static HttpClient client;
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
client = HttpClient.newHttpClient();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueryParams_whenGetRequest_thenResponseOk() throws IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.version(HttpClient.Version.HTTP_2)
|
||||
.uri(URI.create("https://postman-echo.com/get?param1=value1¶m2=value2"))
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
assertEquals(response.statusCode(), 200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueryParams_whenGetRequestWithDefaultConfiguration_thenResponseOk() throws IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create("https://postman-echo.com/get?param1=value1¶m2=value2"))
|
||||
.build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
assertEquals(response.statusCode(), 200);
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -21,7 +21,7 @@ public class HttpClientSSLBypassUnitTest {
|
||||
.build();
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create("https://www.testingmcafeesites.com/"))
|
||||
.uri(URI.create("https://wrong.host.badssl.com/"))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.bealdung.java9.finalizers;
|
||||
|
||||
import java.lang.ref.Cleaner;
|
||||
|
||||
class CleaningDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final Cleaner cleaner = Cleaner.create();
|
||||
try (Order order = new Order(cleaner)) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
order.register(new Product(i), i);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error: " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.bealdung.java9.finalizers;
|
||||
|
||||
import java.lang.ref.Cleaner;
|
||||
|
||||
class Order implements AutoCloseable {
|
||||
|
||||
private final Cleaner cleaner;
|
||||
private Cleaner.Cleanable cleanable;
|
||||
|
||||
public Order(Cleaner cleaner) {
|
||||
this.cleaner = cleaner;
|
||||
}
|
||||
|
||||
public void register(Product product, int id) {
|
||||
this.cleanable = cleaner.register(product, new CleaningAction(id));
|
||||
}
|
||||
|
||||
public void close() {
|
||||
cleanable.clean();
|
||||
System.out.println("Cleanable closed");
|
||||
}
|
||||
|
||||
static class CleaningAction implements Runnable {
|
||||
|
||||
private final int id;
|
||||
|
||||
public CleaningAction(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.printf("Object with id %s is garbage collected. %n", id);
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.bealdung.java9.finalizers;
|
||||
|
||||
class Product {
|
||||
private final int id;
|
||||
|
||||
public Product(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.bealdung.java9.finalizers;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
|
||||
class Resource implements AutoCloseable {
|
||||
|
||||
final BufferedReader reader;
|
||||
|
||||
public Resource(String filename) throws FileNotFoundException {
|
||||
reader = new BufferedReader(new FileReader(filename));
|
||||
}
|
||||
|
||||
public long getLineNumber() {
|
||||
return reader.lines()
|
||||
.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
reader.close();
|
||||
System.out.println("BufferedReader resource closed");
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.bealdung.java9.finalizers;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class FinalizeUnitTest {
|
||||
|
||||
@Test
|
||||
void givenFilename_whenGetLineNumber_thenReturnCorrectNumber() throws IOException {
|
||||
final File tmpFile = File.createTempFile("test", ".tmp");
|
||||
final BufferedWriter writer = new BufferedWriter(new FileWriter(tmpFile));
|
||||
writer.write("Baeldung");
|
||||
writer.close();
|
||||
|
||||
long lineNumber = 0;
|
||||
try (Resource resource = new Resource(tmpFile.getAbsolutePath())) {
|
||||
lineNumber = resource.getLineNumber();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error " + e);
|
||||
}
|
||||
|
||||
assertEquals(1, lineNumber);
|
||||
}
|
||||
}
|
||||
+6
-1
@@ -32,7 +32,7 @@ public class EmptyMapInitializer {
|
||||
return emptyMap;
|
||||
}
|
||||
|
||||
public Map createGenericEmptyMapUsingMapsObject() {
|
||||
public Map createGenericEmptyMapUsingGuavaMapsObject() {
|
||||
Map genericEmptyMap = Maps.<String, Integer>newHashMap();
|
||||
return genericEmptyMap;
|
||||
}
|
||||
@@ -43,6 +43,11 @@ public class EmptyMapInitializer {
|
||||
return emptyMapUsingGuava;
|
||||
}
|
||||
|
||||
public static Map<String, String> createImmutableMapUsingGuava() {
|
||||
Map<String, String> emptyImmutableMapUsingGuava = ImmutableMap.of();
|
||||
return emptyImmutableMapUsingGuava;
|
||||
}
|
||||
|
||||
public SortedMap<String, String> createEmptySortedMap() {
|
||||
SortedMap<String, String> sortedMap = Collections.emptySortedMap();
|
||||
return sortedMap;
|
||||
|
||||
+5
@@ -28,4 +28,9 @@ public class EmptyMapInitializerUnitTest {
|
||||
assertFalse(emptyMapUsingGuava.isEmpty());
|
||||
}
|
||||
|
||||
@Test(expected=UnsupportedOperationException.class)
|
||||
public void givenImmutableEmptyMapUsingGuava_whenAddingEntries_throwsException() {
|
||||
Map<String, String> map = EmptyMapInitializer.createImmutableMapUsingGuava();
|
||||
map.put("key", "value");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,5 +13,4 @@ This module contains articles about the Java List collection
|
||||
- [Finding the Differences Between Two Lists in Java](https://www.baeldung.com/java-lists-difference)
|
||||
- [List vs. ArrayList in Java](https://www.baeldung.com/java-list-vs-arraylist)
|
||||
- [How to Store HashMap<String, ArrayList> Inside a List](https://www.baeldung.com/java-hashmap-inside-list)
|
||||
- [Working With a List of Lists in Java](https://www.baeldung.com/java-list-of-lists)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-collections-list-2)
|
||||
|
||||
@@ -30,11 +30,29 @@
|
||||
<artifactId>hirondelle-date4j</artifactId>
|
||||
<version>${hirondelle-date4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.time4j</groupId>
|
||||
<artifactId>time4j-base</artifactId>
|
||||
<version>${time4j-base.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.time4j</groupId>
|
||||
<artifactId>time4j-sqlxml</artifactId>
|
||||
<version>${time4j-sqlxml.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.ocpsoft.prettytime</groupId>
|
||||
<artifactId>prettytime</artifactId>
|
||||
<version>${prettytime.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
||||
<prettytime.version>3.2.7.Final</prettytime.version>
|
||||
<time4j-base.version>5.9</time4j-base.version>
|
||||
<time4j-sqlxml.version>5.8</time4j-sqlxml.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
package com.baeldung.timeago.version7;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.Period;
|
||||
import org.joda.time.format.PeriodFormat;
|
||||
import org.joda.time.format.PeriodFormatter;
|
||||
import org.joda.time.format.PeriodFormatterBuilder;
|
||||
|
||||
public class TimeAgoCalculator {
|
||||
|
||||
public static String calculateTimeAgoByTimeGranularity(Date pastTime, TimeGranularity granularity) {
|
||||
Date currentTime = new Date();
|
||||
long timeDifferenceInMillis = currentTime.getTime() - pastTime.getTime();
|
||||
return timeDifferenceInMillis / granularity.toMillis() + " " + granularity.name()
|
||||
.toLowerCase() + " ago";
|
||||
}
|
||||
|
||||
public static String calculateHumanFriendlyTimeAgo(Date pastTime) {
|
||||
Date currentTime = new Date();
|
||||
long timeDifferenceInMillis = currentTime.getTime() - pastTime.getTime();
|
||||
if (timeDifferenceInMillis / TimeGranularity.DECADES.toMillis() > 0)
|
||||
return "several decades ago";
|
||||
else if (timeDifferenceInMillis / TimeGranularity.YEARS.toMillis() > 0)
|
||||
return "several years ago";
|
||||
else if (timeDifferenceInMillis / TimeGranularity.MONTHS.toMillis() > 0)
|
||||
return "several months ago";
|
||||
else if (timeDifferenceInMillis / TimeGranularity.WEEKS.toMillis() > 0)
|
||||
return "several weeks ago";
|
||||
else if (timeDifferenceInMillis / TimeGranularity.DAYS.toMillis() > 0)
|
||||
return "several days ago";
|
||||
else if (timeDifferenceInMillis / TimeGranularity.HOURS.toMillis() > 0)
|
||||
return "several hours ago";
|
||||
else if (timeDifferenceInMillis / TimeGranularity.MINUTES.toMillis() > 0)
|
||||
return "several minutes ago";
|
||||
else
|
||||
return "moments ago";
|
||||
}
|
||||
|
||||
public static String calculateExactTimeAgoWithJodaTime(Date pastTime) {
|
||||
Period period = new Period(new DateTime(pastTime.getTime()), new DateTime());
|
||||
PeriodFormatter formatter = new PeriodFormatterBuilder().appendYears()
|
||||
.appendSuffix(" year ", " years ")
|
||||
.appendSeparator("and ")
|
||||
.appendMonths()
|
||||
.appendSuffix(" month ", " months ")
|
||||
.appendSeparator("and ")
|
||||
.appendWeeks()
|
||||
.appendSuffix(" week ", " weeks ")
|
||||
.appendSeparator("and ")
|
||||
.appendDays()
|
||||
.appendSuffix(" day ", " days ")
|
||||
.appendSeparator("and ")
|
||||
.appendHours()
|
||||
.appendSuffix(" hour ", " hours ")
|
||||
.appendSeparator("and ")
|
||||
.appendMinutes()
|
||||
.appendSuffix(" minute ", " minutes ")
|
||||
.appendSeparator("and ")
|
||||
.appendSeconds()
|
||||
.appendSuffix(" second", " seconds")
|
||||
.toFormatter();
|
||||
return formatter.print(period);
|
||||
}
|
||||
|
||||
public static String calculateHumanFriendlyTimeAgoWithJodaTime(Date pastTime) {
|
||||
Period period = new Period(new DateTime(pastTime.getTime()), new DateTime());
|
||||
if (period.getYears() != 0)
|
||||
return "several years ago";
|
||||
else if (period.getMonths() != 0)
|
||||
return "several months ago";
|
||||
else if (period.getWeeks() != 0)
|
||||
return "several weeks ago";
|
||||
else if (period.getDays() != 0)
|
||||
return "several days ago";
|
||||
else if (period.getHours() != 0)
|
||||
return "several hours ago";
|
||||
else if (period.getMinutes() != 0)
|
||||
return "several minutes ago";
|
||||
else
|
||||
return "moments ago";
|
||||
}
|
||||
|
||||
public static String calculateZonedTimeAgoWithJodaTime(Date pastTime, TimeZone zone) {
|
||||
DateTimeZone dateTimeZone = DateTimeZone.forID(zone.getID());
|
||||
Period period = new Period(new DateTime(pastTime.getTime(), dateTimeZone), new DateTime(dateTimeZone));
|
||||
return PeriodFormat.getDefault()
|
||||
.print(period);
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.timeago.version7;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public enum TimeGranularity {
|
||||
SECONDS {
|
||||
public long toMillis() {
|
||||
return TimeUnit.SECONDS.toMillis(1);
|
||||
}
|
||||
}, MINUTES {
|
||||
public long toMillis() {
|
||||
return TimeUnit.MINUTES.toMillis(1);
|
||||
}
|
||||
}, HOURS {
|
||||
public long toMillis() {
|
||||
return TimeUnit.HOURS.toMillis(1);
|
||||
}
|
||||
}, DAYS {
|
||||
public long toMillis() {
|
||||
return TimeUnit.DAYS.toMillis(1);
|
||||
}
|
||||
}, WEEKS {
|
||||
public long toMillis() {
|
||||
return TimeUnit.DAYS.toMillis(7);
|
||||
}
|
||||
}, MONTHS {
|
||||
public long toMillis() {
|
||||
return TimeUnit.DAYS.toMillis(30);
|
||||
}
|
||||
}, YEARS {
|
||||
public long toMillis() {
|
||||
return TimeUnit.DAYS.toMillis(365);
|
||||
}
|
||||
}, DECADES {
|
||||
public long toMillis() {
|
||||
return TimeUnit.DAYS.toMillis(365 * 10);
|
||||
}
|
||||
};
|
||||
|
||||
public abstract long toMillis();
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.timeago.version8;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Period;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.ocpsoft.prettytime.PrettyTime;
|
||||
|
||||
public class TimeAgoCalculator {
|
||||
|
||||
public static String calculateTimeAgoWithPeriodAndDuration(LocalDateTime pastTime, ZoneId zone) {
|
||||
Period period = Period.between(pastTime.toLocalDate(), LocalDate.now(zone));
|
||||
Duration duration = Duration.between(pastTime, LocalDateTime.now(zone));
|
||||
if (period.getYears() != 0)
|
||||
return "several years ago";
|
||||
else if (period.getMonths() != 0)
|
||||
return "several months ago";
|
||||
else if (period.getDays() != 0)
|
||||
return "several days ago";
|
||||
else if (duration.toHours() != 0)
|
||||
return "several hours ago";
|
||||
else if (duration.toMinutes() != 0)
|
||||
return "several minutes ago";
|
||||
else if (duration.getSeconds() != 0)
|
||||
return "several seconds ago";
|
||||
else
|
||||
return "moments ago";
|
||||
}
|
||||
|
||||
public static String calculateTimeAgoWithPrettyTime(Date pastTime) {
|
||||
PrettyTime prettyTime = new PrettyTime();
|
||||
return prettyTime.format(pastTime);
|
||||
}
|
||||
|
||||
public static String calculateTimeAgoWithTime4J(Date pastTime, ZoneId zone, Locale locale) {
|
||||
return net.time4j.PrettyTime.of(locale)
|
||||
.printRelative(pastTime.toInstant(), zone);
|
||||
}
|
||||
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.timeago.version7;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TimeAgoCalculatorUnitTest {
|
||||
|
||||
// fixing tests in BAEL-5647
|
||||
//@Test
|
||||
public void timeAgoByTimeGranularityTest() {
|
||||
long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
|
||||
Assert.assertEquals("5 seconds ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * 1000)), TimeGranularity.SECONDS));
|
||||
Assert.assertEquals("5 minutes ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * 60 * 1000)), TimeGranularity.MINUTES));
|
||||
Assert.assertEquals("5 hours ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000)), TimeGranularity.HOURS));
|
||||
Assert.assertEquals("5 days ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS)), TimeGranularity.DAYS));
|
||||
Assert.assertEquals("5 months ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 30)), TimeGranularity.MONTHS));
|
||||
Assert.assertEquals("5 weeks ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 7)), TimeGranularity.WEEKS));
|
||||
Assert.assertEquals("5 years ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365)), TimeGranularity.YEARS));
|
||||
Assert.assertEquals("5 decades ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365 * 10)), TimeGranularity.DECADES));
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void humanFriendlyTimeAgoTest() {
|
||||
long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
|
||||
Assert.assertEquals("moments ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * 1000))));
|
||||
Assert.assertEquals("several minutes ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * 60 * 1000))));
|
||||
Assert.assertEquals("several hours ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000))));
|
||||
Assert.assertEquals("several days ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS))));
|
||||
Assert.assertEquals("several months ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 30))));
|
||||
Assert.assertEquals("several weeks ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (3 * DAY_IN_MILLIS * 7))));
|
||||
Assert.assertEquals("several years ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365))));
|
||||
Assert.assertEquals("several decades ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365 * 10))));
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void calculateExactTimeAgoWithJodaTimeTest() {
|
||||
Assert.assertEquals("5 hours and 15 minutes and 3 seconds", TimeAgoCalculator.calculateExactTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000 + 15 * 60 * 1000 + 3 * 1000))));
|
||||
Assert.assertEquals("5 hours and 1 minute and 1 second", TimeAgoCalculator.calculateExactTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1 * 1000))));
|
||||
Assert.assertEquals("2 days and 1 minute and 1 second", TimeAgoCalculator.calculateExactTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (2 * 24 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1 * 1000))));
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void calculateHumanFriendlyTimeAgoWithJodaTimeTest() {
|
||||
long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
|
||||
Assert.assertEquals("moments ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 1000))));
|
||||
Assert.assertEquals("several minutes ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 60 * 1000))));
|
||||
Assert.assertEquals("several hours ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000))));
|
||||
Assert.assertEquals("several days ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS))));
|
||||
Assert.assertEquals("several months ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 30))));
|
||||
Assert.assertEquals("several weeks ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (3 * DAY_IN_MILLIS * 7))));
|
||||
Assert.assertEquals("several years ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365))));
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.timeago.version8;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TimeAgoCalculatorUnitTest {
|
||||
|
||||
// fixing test in BAEL-5647
|
||||
//@Test
|
||||
public void calculateTimeAgoWithPeriodAndDurationTest() {
|
||||
long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
|
||||
Assert.assertEquals("moments ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several seconds ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * 1000)), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several minutes ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * 60 * 1000)), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several hours ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * 60 * 60 * 1000)), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several days ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * DAY_IN_MILLIS)), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several months ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 30)), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several years ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365)), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
}
|
||||
}
|
||||
@@ -14,3 +14,5 @@ This module contains articles about core java exceptions
|
||||
- [How to Find an Exception’s Root Cause in Java](https://www.baeldung.com/java-exception-root-cause)
|
||||
- [Java IOException “Too many open files”](https://www.baeldung.com/java-too-many-open-files)
|
||||
- [When Does Java Throw the ExceptionInInitializerError?](https://www.baeldung.com/java-exceptionininitializererror)
|
||||
- More articles: [[<-- prev]](../core-java-exceptions) [[next -->]](../core-java-exceptions-3)
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
### Relevant Articles:
|
||||
## Core Java Exceptions
|
||||
|
||||
This module contains articles about core java exceptions
|
||||
|
||||
### Relevant Articles:
|
||||
- [NoSuchMethodError in Java](https://www.baeldung.com/java-nosuchmethod-error)
|
||||
- [IllegalArgumentException or NullPointerException for a Null Parameter?](https://www.baeldung.com/java-illegalargumentexception-or-nullpointerexception)
|
||||
- [IllegalMonitorStateException in Java](https://www.baeldung.com/java-illegalmonitorstateexception)
|
||||
@@ -10,3 +13,5 @@
|
||||
- [NoSuchFieldError in Java](https://www.baeldung.com/java-nosuchfielderror)
|
||||
- [IllegalAccessError in Java](https://www.baeldung.com/java-illegalaccesserror)
|
||||
- [Working with (Unknown Source) Stack Traces in Java](https://www.baeldung.com/java-unknown-source-stack-trace)
|
||||
- More articles: [[<-- prev]](../core-java-exceptions-2) [[next -->]](../core-java-exceptions-4)
|
||||
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
### Relevant Articles:
|
||||
## Core Java Exceptions
|
||||
|
||||
This module contains articles about core java exceptions
|
||||
|
||||
### Relevant articles:
|
||||
- [Java ArrayIndexOutOfBoundsException](https://www.baeldung.com/java-arrayindexoutofboundsexception)
|
||||
- [Java Missing Return Statement](https://www.baeldung.com/java-missing-return-statement)
|
||||
- [Convert long to int Type in Java](https://www.baeldung.com/java-convert-long-to-int)
|
||||
- [“Sneaky Throws” in Java](https://www.baeldung.com/java-sneaky-throws)
|
||||
- [[<-- Prev]](../core-java-exceptions-3)
|
||||
@@ -22,6 +22,12 @@
|
||||
<version>${h2.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.exception.sneakythrows;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SneakyThrowsExamples {
|
||||
|
||||
public static <E extends Throwable> void sneakyThrow(Throwable e) throws E {
|
||||
throw (E) e;
|
||||
}
|
||||
|
||||
public static void throwSneakyIOException() {
|
||||
sneakyThrow(new IOException("sneaky"));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static void throwSneakyIOExceptionUsingLombok() {
|
||||
throw new IOException("lombok sneaky");
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.exception.sneakythrows;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static com.baeldung.exception.sneakythrows.SneakyThrowsExamples.throwSneakyIOException;
|
||||
import static com.baeldung.exception.sneakythrows.SneakyThrowsExamples.throwSneakyIOExceptionUsingLombok;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
public class SneakyThrowsExamplesUnitTest {
|
||||
|
||||
@Test
|
||||
public void throwSneakyIOException_IOExceptionShouldBeThrown() {
|
||||
assertThatThrownBy(() -> throwSneakyIOException())
|
||||
.isInstanceOf(IOException.class)
|
||||
.hasMessage("sneaky")
|
||||
.hasStackTraceContaining("SneakyThrowsExamples.throwSneakyIOException");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void throwSneakyIOExceptionUsingLombok_IOExceptionShouldBeThrown() {
|
||||
assertThatThrownBy(() -> throwSneakyIOExceptionUsingLombok())
|
||||
.isInstanceOf(IOException.class)
|
||||
.hasMessage("lombok sneaky")
|
||||
.hasStackTraceContaining("SneakyThrowsExamples.throwSneakyIOExceptionUsingLombok");
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,9 @@ This module contains articles about core java exceptions
|
||||
- [Exception Handling in Java](https://www.baeldung.com/java-exceptions)
|
||||
- [Differences Between Final, Finally and Finalize in Java](https://www.baeldung.com/java-final-finally-finalize)
|
||||
- [Difference Between Throw and Throws in Java](https://www.baeldung.com/java-throw-throws)
|
||||
- [“Sneaky Throws” in Java](https://www.baeldung.com/java-sneaky-throws)
|
||||
- [The StackOverflowError in Java](https://www.baeldung.com/java-stack-overflow-error)
|
||||
- [Checked and Unchecked Exceptions in Java](https://www.baeldung.com/java-checked-unchecked-exceptions)
|
||||
- [Common Java Exceptions](https://www.baeldung.com/java-common-exceptions)
|
||||
- [Will an Error Be Caught by Catch Block in Java?](https://www.baeldung.com/java-error-catch)
|
||||
- [[Next -->]](/core-java-modules/core-java-exceptions-2)
|
||||
- [[Next -->]](../core-java-exceptions-2)
|
||||
-
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user