Merge branch 'eugenp:master' into master
This commit is contained in:
commit
946ff1183f
@ -10,7 +10,6 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>annotations</artifactId>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -10,7 +10,6 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,8 +12,10 @@ import java.util.Properties;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
// This live test needs a running Docker instance so that a kafka container can be created
|
||||
|
||||
@Testcontainers
|
||||
class KafkaTopicApplicationIntegrationTest {
|
||||
class KafkaTopicApplicationLiveTest {
|
||||
|
||||
@Container
|
||||
private static final KafkaContainer KAFKA_CONTAINER = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.4.3"));
|
@ -20,6 +20,8 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
// This live test needs a Docker instance running so that kafka container can be created
|
||||
|
||||
public class KafkaSerDesLiveTest {
|
||||
private static final String CONSUMER_APP_ID = "consumer_id";
|
||||
private static final String CONSUMER_GROUP_ID = "group_id";
|
||||
|
@ -44,6 +44,8 @@ import static org.apache.kafka.clients.consumer.ConsumerConfig.BOOTSTRAP_SERVERS
|
||||
import static org.apache.kafka.clients.producer.ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG;
|
||||
import static org.apache.kafka.clients.producer.ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG;
|
||||
|
||||
// This live test needs a Docker instance running so that kafka container can be created
|
||||
|
||||
public class KafkaStreamsLiveTest {
|
||||
private final String LEFT_TOPIC = "left-stream-topic";
|
||||
private final String RIGHT_TOPIC = "right-stream-topic";
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -6,7 +6,6 @@ This module contains articles about core Java features that have been introduced
|
||||
|
||||
- [New Features in Java 9](https://www.baeldung.com/new-java-9)
|
||||
- [Java 9 Variable Handles Demystified](http://www.baeldung.com/java-variable-handles)
|
||||
- [Exploring the New HTTP Client in Java 9 and 11](http://www.baeldung.com/java-9-http-client)
|
||||
- [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar)
|
||||
- [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation)
|
||||
- [Introduction to Java 9 StackWalking API](https://www.baeldung.com/java-9-stackwalking-api)
|
||||
|
@ -1,84 +0,0 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.baeldung.httpclient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
import jdk.incubator.http.HttpClient;
|
||||
import jdk.incubator.http.HttpRequest;
|
||||
import jdk.incubator.http.HttpRequest.BodyProcessor;
|
||||
import jdk.incubator.http.HttpResponse;
|
||||
import jdk.incubator.http.HttpResponse.BodyHandler;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author pkaria
|
||||
*/
|
||||
public class HttpClientExample {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
httpGetRequest();
|
||||
httpPostRequest();
|
||||
asynchronousRequest();
|
||||
asynchronousMultipleRequests();
|
||||
}
|
||||
|
||||
public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
|
||||
HttpRequest request = HttpRequest.newBuilder(httpURI).GET()
|
||||
.headers("Accept-Enconding", "gzip, deflate").build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
|
||||
String responseBody = response.body();
|
||||
int responseStatusCode = response.statusCode();
|
||||
System.out.println(responseBody);
|
||||
}
|
||||
|
||||
public static void httpPostRequest() throws URISyntaxException, IOException, InterruptedException {
|
||||
HttpClient client = HttpClient
|
||||
.newBuilder()
|
||||
.build();
|
||||
HttpRequest request = HttpRequest
|
||||
.newBuilder(new URI("http://jsonplaceholder.typicode.com/posts"))
|
||||
.POST(BodyProcessor.fromString("Sample Post Request"))
|
||||
.build();
|
||||
HttpResponse<String> response
|
||||
= client.send(request, HttpResponse.BodyHandler.asString());
|
||||
String responseBody = response.body();
|
||||
System.out.println(responseBody);
|
||||
}
|
||||
|
||||
public static void asynchronousRequest() throws URISyntaxException {
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
|
||||
HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build();
|
||||
CompletableFuture<HttpResponse<String>> futureResponse = client.sendAsync(request,
|
||||
HttpResponse.BodyHandler.asString());
|
||||
}
|
||||
|
||||
public static void asynchronousMultipleRequests() throws URISyntaxException {
|
||||
List<URI> targets = Arrays.asList(new URI("http://jsonplaceholder.typicode.com/posts/1"), new URI("http://jsonplaceholder.typicode.com/posts/2"));
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
List<CompletableFuture<File>> futures = targets
|
||||
.stream()
|
||||
.map(target -> client
|
||||
.sendAsync(
|
||||
HttpRequest.newBuilder(target)
|
||||
.GET()
|
||||
.build(),
|
||||
BodyHandler.asFile(Paths.get("base", target.getPath())))
|
||||
.thenApply(response -> response.body())
|
||||
.thenApply(path -> path.toFile()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
module com.baeldung.httpclient {
|
||||
requires jdk.incubator.httpclient;
|
||||
}
|
@ -1,218 +0,0 @@
|
||||
package com.baeldung.java9.httpclient;
|
||||
|
||||
import jdk.incubator.http.HttpClient;
|
||||
import jdk.incubator.http.HttpRequest;
|
||||
import jdk.incubator.http.HttpResponse;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.collection.IsEmptyCollection.empty;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
public class HttpClientIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturnSampleDataContentWhenConnectViaSystemProxy() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyProcessor.fromString("Sample body"))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newBuilder()
|
||||
.proxy(ProxySelector.getDefault())
|
||||
.build()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.body(), containsString("Sample body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotFollowRedirectWhenSetToDefaultNever() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("http://stackoverflow.com"))
|
||||
.version(HttpClient.Version.HTTP_1_1)
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = HttpClient.newBuilder()
|
||||
.build()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_MOVED_PERM));
|
||||
assertThat(response.body(), containsString(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFollowRedirectWhenSetToAlways() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("http://stackoverflow.com"))
|
||||
.version(HttpClient.Version.HTTP_1_1)
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = HttpClient.newBuilder()
|
||||
.followRedirects(HttpClient.Redirect.ALWAYS)
|
||||
.build()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.finalRequest()
|
||||
.uri()
|
||||
.toString(), equalTo("https://stackoverflow.com/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnOKStatusForAuthenticatedAccess() throws URISyntaxException, IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/basic-auth"))
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = HttpClient.newBuilder()
|
||||
.authenticator(new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication("postman", "password".toCharArray());
|
||||
}
|
||||
})
|
||||
.build()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSendRequestAsync() throws URISyntaxException, InterruptedException, ExecutionException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyProcessor.fromString("Sample body"))
|
||||
.build();
|
||||
CompletableFuture<HttpResponse<String>> response = HttpClient.newBuilder()
|
||||
.build()
|
||||
.sendAsync(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.get()
|
||||
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseJustTwoThreadWhenProcessingSendAsyncRequest() throws URISyntaxException, InterruptedException, ExecutionException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||
|
||||
CompletableFuture<HttpResponse<String>> response1 = HttpClient.newBuilder()
|
||||
.executor(executorService)
|
||||
.build()
|
||||
.sendAsync(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
CompletableFuture<HttpResponse<String>> response2 = HttpClient.newBuilder()
|
||||
.executor(executorService)
|
||||
.build()
|
||||
.sendAsync(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
CompletableFuture<HttpResponse<String>> response3 = HttpClient.newBuilder()
|
||||
.executor(executorService)
|
||||
.build()
|
||||
.sendAsync(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
CompletableFuture.allOf(response1, response2, response3)
|
||||
.join();
|
||||
|
||||
assertThat(response1.get()
|
||||
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response2.get()
|
||||
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response3.get()
|
||||
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotStoreCookieWhenPolicyAcceptNone() throws URISyntaxException, IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpClient httpClient = HttpClient.newBuilder()
|
||||
.cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_NONE))
|
||||
.build();
|
||||
|
||||
httpClient.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(httpClient.cookieManager()
|
||||
.get()
|
||||
.getCookieStore()
|
||||
.getCookies(), empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldStoreCookieWhenPolicyAcceptAll() throws URISyntaxException, IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpClient httpClient = HttpClient.newBuilder()
|
||||
.cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_ALL))
|
||||
.build();
|
||||
|
||||
httpClient.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(httpClient.cookieManager()
|
||||
.get()
|
||||
.getCookieStore()
|
||||
.getCookies(), not(empty()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldProcessMultipleRequestViaStream() throws URISyntaxException, ExecutionException, InterruptedException {
|
||||
List<URI> targets = Arrays.asList(new URI("https://postman-echo.com/get?foo1=bar1"), new URI("https://postman-echo.com/get?foo2=bar2"));
|
||||
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
|
||||
List<CompletableFuture<String>> futures = targets.stream()
|
||||
.map(target -> client.sendAsync(HttpRequest.newBuilder(target)
|
||||
.GET()
|
||||
.build(), HttpResponse.BodyHandler.asString())
|
||||
.thenApply(response -> response.body()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
|
||||
.join();
|
||||
|
||||
if (futures.get(0)
|
||||
.get()
|
||||
.contains("foo1")) {
|
||||
assertThat(futures.get(0)
|
||||
.get(), containsString("bar1"));
|
||||
assertThat(futures.get(1)
|
||||
.get(), containsString("bar2"));
|
||||
} else {
|
||||
assertThat(futures.get(1)
|
||||
.get(), containsString("bar2"));
|
||||
assertThat(futures.get(1)
|
||||
.get(), containsString("bar1"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,171 +0,0 @@
|
||||
package com.baeldung.java9.httpclient;
|
||||
|
||||
import jdk.incubator.http.HttpClient;
|
||||
import jdk.incubator.http.HttpRequest;
|
||||
import jdk.incubator.http.HttpResponse;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Duration;
|
||||
|
||||
import static java.time.temporal.ChronoUnit.SECONDS;
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
public class HttpRequestIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseHttp2WhenWebsiteUsesHttp2() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://stackoverflow.com"))
|
||||
.version(HttpClient.Version.HTTP_2)
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.version(), equalTo(HttpClient.Version.HTTP_2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFallbackToHttp1_1WhenWebsiteDoesNotUseHttp2() throws IOException, InterruptedException, URISyntaxException, NoSuchAlgorithmException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.version(HttpClient.Version.HTTP_2)
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.version(), equalTo(HttpClient.Version.HTTP_1_1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnStatusOKWhenSendGetRequestWithDummyHeaders() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.headers("key1", "value1", "key2", "value2")
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnStatusOKWhenSendGetRequestTimeoutSet() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.timeout(Duration.of(10, SECONDS))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNoContentWhenPostWithNoBody() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.POST(HttpRequest.noBody())
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSampleDataContentWhenPostWithBodyText() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyProcessor.fromString("Sample request body"))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.body(), containsString("Sample request body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSampleDataContentWhenPostWithInputStream() throws IOException, InterruptedException, URISyntaxException {
|
||||
byte[] sampleData = "Sample request body".getBytes();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyProcessor.fromInputStream(() -> new ByteArrayInputStream(sampleData)))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.body(), containsString("Sample request body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSampleDataContentWhenPostWithByteArrayProcessorStream() throws IOException, InterruptedException, URISyntaxException {
|
||||
byte[] sampleData = "Sample request body".getBytes();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyProcessor.fromByteArray(sampleData))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.body(), containsString("Sample request body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSampleDataContentWhenPostWithFileProcessorStream() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyProcessor.fromFile(Paths.get("src/test/resources/sample.txt")))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.body(), containsString("Sample file content"));
|
||||
}
|
||||
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package com.baeldung.java9.httpclient;
|
||||
|
||||
import jdk.incubator.http.HttpClient;
|
||||
import jdk.incubator.http.HttpRequest;
|
||||
import jdk.incubator.http.HttpResponse;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.Matchers.isEmptyString;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
public class HttpResponseIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.body(), not(isEmptyString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldResponseURIDifferentThanRequestUIRWhenRedirect() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("http://stackoverflow.com"))
|
||||
.version(HttpClient.Version.HTTP_1_1)
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = HttpClient.newBuilder()
|
||||
.followRedirects(HttpClient.Redirect.ALWAYS)
|
||||
.build()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(request.uri()
|
||||
.toString(), equalTo("http://stackoverflow.com"));
|
||||
assertThat(response.uri()
|
||||
.toString(), equalTo("https://stackoverflow.com/"));
|
||||
}
|
||||
|
||||
}
|
@ -12,3 +12,4 @@
|
||||
- [Efficient Word Frequency Calculator in Java](https://www.baeldung.com/java-word-frequency)
|
||||
- [Why Missing Annotations Don’t Cause ClassNotFoundException](https://www.baeldung.com/classnotfoundexception-missing-annotation)
|
||||
- [Valid @SuppressWarnings Warning Names](https://www.baeldung.com/java-suppresswarnings-valid-names)
|
||||
- [Get a Field’s Annotations Using Reflection](https://www.baeldung.com/java-get-field-annotations)
|
||||
|
@ -12,9 +12,21 @@
|
||||
<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-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-annotations</finalName>
|
||||
<resources>
|
||||
|
@ -0,0 +1,9 @@
|
||||
package com.baeldung.readannotations;
|
||||
|
||||
public class ClassWithAnnotations {
|
||||
|
||||
@FirstAnnotation
|
||||
@SecondAnnotation
|
||||
@ThirdAnnotation
|
||||
private String classMember;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.baeldung.readannotations;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface FirstAnnotation {
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.baeldung.readannotations;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface SecondAnnotation {
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.baeldung.readannotations;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface ThirdAnnotation {
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.readannotations;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ClassWithAnnotationsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCallingGetDeclaredAnnotations_thenOnlyRuntimeAnnotationsAreAvailable() throws NoSuchFieldException {
|
||||
Field classMemberField = ClassWithAnnotations.class.getDeclaredField("classMember");
|
||||
Annotation[] annotations = classMemberField.getDeclaredAnnotations();
|
||||
assertThat(annotations).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingIsAnnotationPresent_thenOnlyRuntimeAnnotationsAreAvailable() throws NoSuchFieldException {
|
||||
Field classMemberField = ClassWithAnnotations.class.getDeclaredField("classMember");
|
||||
assertThat(classMemberField.isAnnotationPresent(FirstAnnotation.class)).isTrue();
|
||||
assertThat(classMemberField.isAnnotationPresent(SecondAnnotation.class)).isTrue();
|
||||
assertThat(classMemberField.isAnnotationPresent(ThirdAnnotation.class)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingGetDeclaredAnnotationsOrGetAnnotations_thenSameAnnotationsAreReturned() throws NoSuchFieldException {
|
||||
Field classMemberField = ClassWithAnnotations.class.getDeclaredField("classMember");
|
||||
Annotation[] declaredAnnotations = classMemberField.getDeclaredAnnotations();
|
||||
Annotation[] annotations = classMemberField.getAnnotations();
|
||||
assertThat(declaredAnnotations).containsExactly(annotations);
|
||||
}
|
||||
}
|
@ -19,6 +19,11 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
@ -0,0 +1,281 @@
|
||||
package com.baeldung.array.conversions;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
import com.google.common.primitives.Longs;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.Conversion;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
|
||||
class ByteArrayToNumericRepresentation {
|
||||
|
||||
static int convertByteArrayToIntUsingShiftOperator(byte[] bytes) {
|
||||
int value = 0;
|
||||
for (byte b : bytes) {
|
||||
value = (value << 8) + (b & 0xFF);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static byte[] convertIntToByteArrayUsingShiftOperator(int value) {
|
||||
byte[] bytes = new byte[Integer.BYTES];
|
||||
int length = bytes.length;
|
||||
for (int i = 0; i < length; i++) {
|
||||
bytes[length - i - 1] = (byte) (value & 0xFF);
|
||||
value >>= 8;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static long convertByteArrayToLongUsingShiftOperator(byte[] bytes) {
|
||||
long value = 0;
|
||||
for (byte b : bytes) {
|
||||
value <<= 8;
|
||||
value |= (b & 0xFF);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static byte[] convertLongToByteArrayUsingShiftOperator(long value) {
|
||||
byte[] bytes = new byte[Long.BYTES];
|
||||
int length = bytes.length;
|
||||
for (int i = 0; i < length; i++) {
|
||||
bytes[length - i - 1] = (byte) (value & 0xFF);
|
||||
value >>= 8;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static float convertByteArrayToFloatUsingShiftOperator(byte[] bytes) {
|
||||
// convert bytes to int
|
||||
int intValue = 0;
|
||||
for (byte b : bytes) {
|
||||
intValue = (intValue << 8) + (b & 0xFF);
|
||||
}
|
||||
|
||||
// convert int to float
|
||||
return Float.intBitsToFloat(intValue);
|
||||
}
|
||||
|
||||
static byte[] convertFloatToByteArrayUsingShiftOperator(float value) {
|
||||
// convert float to int
|
||||
int intValue = Float.floatToIntBits(value);
|
||||
|
||||
// convert int to bytes
|
||||
byte[] bytes = new byte[Float.BYTES];
|
||||
int length = bytes.length;
|
||||
for (int i = 0; i < length; i++) {
|
||||
bytes[length - i - 1] = (byte) (intValue & 0xFF);
|
||||
intValue >>= 8;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static double convertingByteArrayToDoubleUsingShiftOperator(byte[] bytes) {
|
||||
long longValue = 0;
|
||||
for (byte b : bytes) {
|
||||
longValue = (longValue << 8) + (b & 0xFF);
|
||||
}
|
||||
|
||||
return Double.longBitsToDouble(longValue);
|
||||
}
|
||||
|
||||
static byte[] convertDoubleToByteArrayUsingShiftOperator(double value) {
|
||||
long longValue = Double.doubleToLongBits(value);
|
||||
|
||||
byte[] bytes = new byte[Double.BYTES];
|
||||
int length = bytes.length;
|
||||
for (int i = 0; i < length; i++) {
|
||||
bytes[length - i - 1] = (byte) (longValue & 0xFF);
|
||||
longValue >>= 8;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static int convertByteArrayToIntUsingByteBuffer(byte[] bytes) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
|
||||
buffer.put(bytes);
|
||||
buffer.rewind();
|
||||
return buffer.getInt();
|
||||
}
|
||||
|
||||
static byte[] convertIntToByteArrayUsingByteBuffer(int value) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
|
||||
buffer.putInt(value);
|
||||
buffer.rewind();
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
static long convertByteArrayToLongUsingByteBuffer(byte[] bytes) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
||||
buffer.put(bytes);
|
||||
buffer.rewind();
|
||||
return buffer.getLong();
|
||||
}
|
||||
|
||||
static byte[] convertLongToByteArrayUsingByteBuffer(long value) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
||||
buffer.putLong(value);
|
||||
buffer.rewind();
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
static float convertByteArrayToFloatUsingByteBuffer(byte[] bytes) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES);
|
||||
buffer.put(bytes);
|
||||
buffer.rewind();
|
||||
return buffer.getFloat();
|
||||
}
|
||||
|
||||
static byte[] convertFloatToByteArrayUsingByteBuffer(float value) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES);
|
||||
buffer.putFloat(value);
|
||||
buffer.rewind();
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
static double convertByteArrayToDoubleUsingByteBuffer(byte[] bytes) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
|
||||
buffer.put(bytes);
|
||||
buffer.rewind();
|
||||
return buffer.getDouble();
|
||||
}
|
||||
|
||||
static byte[] convertDoubleToByteArrayUsingByteBuffer(double value) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
|
||||
buffer.putDouble(value);
|
||||
buffer.rewind();
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
static int convertByteArrayToIntUsingBigInteger(byte[] bytes) {
|
||||
return new BigInteger(bytes).intValue();
|
||||
}
|
||||
|
||||
static byte[] convertIntToByteArrayUsingBigInteger(int value) {
|
||||
return BigInteger.valueOf(value).toByteArray();
|
||||
}
|
||||
|
||||
static long convertByteArrayToLongUsingBigInteger(byte[] bytes) {
|
||||
return new BigInteger(bytes).longValue();
|
||||
}
|
||||
|
||||
static byte[] convertLongToByteArrayUsingBigInteger(long value) {
|
||||
return BigInteger.valueOf(value).toByteArray();
|
||||
}
|
||||
|
||||
static float convertByteArrayToFloatUsingBigInteger(byte[] bytes) {
|
||||
int intValue = new BigInteger(bytes).intValue();
|
||||
return Float.intBitsToFloat(intValue);
|
||||
}
|
||||
|
||||
static byte[] convertFloatToByteArrayUsingBigInteger(float value) {
|
||||
int intValue = Float.floatToIntBits(value);
|
||||
return BigInteger.valueOf(intValue).toByteArray();
|
||||
}
|
||||
|
||||
static double convertByteArrayToDoubleUsingBigInteger(byte[] bytes) {
|
||||
long longValue = new BigInteger(bytes).longValue();
|
||||
return Double.longBitsToDouble(longValue);
|
||||
}
|
||||
|
||||
static byte[] convertDoubleToByteArrayUsingBigInteger(double value) {
|
||||
long longValue = Double.doubleToLongBits(value);
|
||||
return BigInteger.valueOf(longValue).toByteArray();
|
||||
}
|
||||
|
||||
static int convertingByteArrayToIntUsingGuava(byte[] bytes) {
|
||||
return Ints.fromByteArray(bytes);
|
||||
}
|
||||
|
||||
static byte[] convertIntToByteArrayUsingGuava(int value) {
|
||||
return Ints.toByteArray(value);
|
||||
}
|
||||
|
||||
static long convertByteArrayToLongUsingGuava(byte[] bytes) {
|
||||
return Longs.fromByteArray(bytes);
|
||||
}
|
||||
|
||||
static byte[] convertLongToByteArrayUsingGuava(long value) {
|
||||
return Longs.toByteArray(value);
|
||||
}
|
||||
|
||||
static float convertByteArrayToFloatUsingGuava(byte[] bytes) {
|
||||
int intValue = Ints.fromByteArray(bytes);
|
||||
return Float.intBitsToFloat(intValue);
|
||||
}
|
||||
|
||||
static byte[] convertFloatToByteArrayUsingGuava(float value) {
|
||||
int intValue = Float.floatToIntBits(value);
|
||||
return Ints.toByteArray(intValue);
|
||||
}
|
||||
|
||||
static double convertByteArrayToDoubleUsingGuava(byte[] bytes) {
|
||||
long longValue = Longs.fromByteArray(bytes);
|
||||
return Double.longBitsToDouble(longValue);
|
||||
}
|
||||
|
||||
static byte[] convertDoubleToByteArrayUsingGuava(double value) {
|
||||
long longValue = Double.doubleToLongBits(value);
|
||||
return Longs.toByteArray(longValue);
|
||||
}
|
||||
|
||||
static int convertByteArrayToIntUsingCommonsLang(byte[] bytes) {
|
||||
byte[] copyBytes = Arrays.copyOf(bytes, bytes.length);
|
||||
ArrayUtils.reverse(copyBytes);
|
||||
return Conversion.byteArrayToInt(copyBytes, 0, 0, 0, copyBytes.length);
|
||||
}
|
||||
|
||||
static byte[] convertIntToByteArrayUsingCommonsLang(int value) {
|
||||
byte[] bytes = new byte[Integer.BYTES];
|
||||
Conversion.intToByteArray(value, 0, bytes, 0, bytes.length);
|
||||
ArrayUtils.reverse(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static long convertByteArrayToLongUsingCommonsLang(byte[] bytes) {
|
||||
byte[] copyBytes = Arrays.copyOf(bytes, bytes.length);
|
||||
ArrayUtils.reverse(copyBytes);
|
||||
return Conversion.byteArrayToLong(copyBytes, 0, 0, 0, copyBytes.length);
|
||||
}
|
||||
|
||||
static byte[] convertLongToByteArrayUsingCommonsLang(long value) {
|
||||
byte[] bytes = new byte[Long.BYTES];
|
||||
Conversion.longToByteArray(value, 0, bytes, 0, bytes.length);
|
||||
ArrayUtils.reverse(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static float convertByteArrayToFloatUsingCommonsLang(byte[] bytes) {
|
||||
byte[] copyBytes = Arrays.copyOf(bytes, bytes.length);
|
||||
ArrayUtils.reverse(copyBytes);
|
||||
int intValue = Conversion.byteArrayToInt(copyBytes, 0, 0, 0, copyBytes.length);
|
||||
return Float.intBitsToFloat(intValue);
|
||||
}
|
||||
|
||||
static byte[] convertFloatToByteArrayUsingCommonsLang(float value) {
|
||||
int intValue = Float.floatToIntBits(value);
|
||||
byte[] bytes = new byte[Float.BYTES];
|
||||
Conversion.intToByteArray(intValue, 0, bytes, 0, bytes.length);
|
||||
ArrayUtils.reverse(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static double convertByteArrayToDoubleUsingCommonsLang(byte[] bytes) {
|
||||
byte[] copyBytes = Arrays.copyOf(bytes, bytes.length);
|
||||
ArrayUtils.reverse(copyBytes);
|
||||
long longValue = Conversion.byteArrayToLong(copyBytes, 0, 0, 0, copyBytes.length);
|
||||
return Double.longBitsToDouble(longValue);
|
||||
}
|
||||
|
||||
static byte[] convertDoubleToByteArrayUsingCommonsLang(double value) {
|
||||
long longValue = Double.doubleToLongBits(value);
|
||||
byte[] bytes = new byte[Long.BYTES];
|
||||
Conversion.longToByteArray(longValue, 0, bytes, 0, bytes.length);
|
||||
ArrayUtils.reverse(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,316 @@
|
||||
package com.baeldung.array.conversions;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.baeldung.array.conversions.ByteArrayToNumericRepresentation.*;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ByteArrayToNumericRepresentationUnitTest {
|
||||
private static final byte[] INT_BYTE_ARRAY = new byte[]{
|
||||
(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE
|
||||
};
|
||||
private static final int INT_VALUE = 0xCAFEBABE;
|
||||
|
||||
|
||||
private static final byte[] FLOAT_BYTE_ARRAY = new byte[]{
|
||||
(byte) 0x40, (byte) 0x48, (byte) 0xF5, (byte) 0xC3
|
||||
};
|
||||
private static final float FLOAT_VALUE = 3.14F;
|
||||
|
||||
|
||||
private static final byte[] LONG_BYTE_ARRAY = new byte[]{
|
||||
(byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67,
|
||||
(byte) 0x89, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF
|
||||
};
|
||||
private static final long LONG_VALUE = 0x0123456789ABCDEFL;
|
||||
|
||||
|
||||
private static final byte[] DOUBLE_BYTE_ARRAY = new byte[]{
|
||||
(byte) 0x3F, (byte) 0xE3, (byte) 0xC6, (byte) 0xA7,
|
||||
(byte) 0xEF, (byte) 0x9D, (byte) 0xB2, (byte) 0x2D
|
||||
};
|
||||
private static final double DOUBLE_VALUE = 0.618D;
|
||||
|
||||
|
||||
@Test
|
||||
public void givenShiftOperator_whenConvertingByteArrayToInt_thenSuccess() {
|
||||
int value = convertByteArrayToIntUsingShiftOperator(INT_BYTE_ARRAY);
|
||||
|
||||
assertEquals(INT_VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenShiftOperator_whenConvertingIntToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertIntToByteArrayUsingShiftOperator(INT_VALUE);
|
||||
|
||||
assertArrayEquals(INT_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenShiftOperator_whenConvertingByteArrayToLong_thenSuccess() {
|
||||
long value = convertByteArrayToLongUsingShiftOperator(LONG_BYTE_ARRAY);
|
||||
|
||||
assertEquals(LONG_VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenShiftOperator_whenConvertingLongToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertLongToByteArrayUsingShiftOperator(LONG_VALUE);
|
||||
|
||||
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenShiftOperator_whenConvertingByteArrayToFloat_thenSuccess() {
|
||||
float value = convertByteArrayToFloatUsingShiftOperator(FLOAT_BYTE_ARRAY);
|
||||
|
||||
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenShiftOperator_whenConvertingFloatToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertFloatToByteArrayUsingShiftOperator(FLOAT_VALUE);
|
||||
|
||||
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenShiftOperator_whenConvertingByteArrayToDouble_thenSuccess() {
|
||||
double value = convertingByteArrayToDoubleUsingShiftOperator(DOUBLE_BYTE_ARRAY);
|
||||
|
||||
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenShiftOperator_whenConvertingDoubleToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertDoubleToByteArrayUsingShiftOperator(DOUBLE_VALUE);
|
||||
|
||||
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenByteBuffer_whenConvertingByteArrayToInt_thenSuccess() {
|
||||
int value = convertByteArrayToIntUsingByteBuffer(INT_BYTE_ARRAY);
|
||||
|
||||
assertEquals(INT_VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenByteBuffer_whenConvertingIntToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertIntToByteArrayUsingByteBuffer(INT_VALUE);
|
||||
|
||||
assertArrayEquals(INT_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenByteBuffer_whenConvertingByteArrayToLong_thenSuccess() {
|
||||
long value = convertByteArrayToLongUsingByteBuffer(LONG_BYTE_ARRAY);
|
||||
|
||||
assertEquals(LONG_VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenByteBuffer_whenConvertingLongToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertLongToByteArrayUsingByteBuffer(LONG_VALUE);
|
||||
|
||||
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenByteBuffer_whenConvertingByteArrayToFloat_thenSuccess() {
|
||||
float value = convertByteArrayToFloatUsingByteBuffer(FLOAT_BYTE_ARRAY);
|
||||
|
||||
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenByteBuffer_whenConvertingFloatToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertFloatToByteArrayUsingByteBuffer(FLOAT_VALUE);
|
||||
|
||||
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenByteBuffer_whenConvertingByteArrayToDouble_thenSuccess() {
|
||||
double value = convertByteArrayToDoubleUsingByteBuffer(DOUBLE_BYTE_ARRAY);
|
||||
|
||||
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenByteBuffer_whenConvertingDoubleToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertDoubleToByteArrayUsingByteBuffer(DOUBLE_VALUE);
|
||||
|
||||
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigInteger_whenConvertingByteArrayToInt_thenSuccess() {
|
||||
int value = convertByteArrayToIntUsingBigInteger(INT_BYTE_ARRAY);
|
||||
|
||||
assertEquals(INT_VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigInteger_whenConvertingIntToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertIntToByteArrayUsingBigInteger(INT_VALUE);
|
||||
|
||||
assertArrayEquals(INT_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigInteger_whenConvertingByteArrayToLong_thenSuccess() {
|
||||
long value = convertByteArrayToLongUsingBigInteger(LONG_BYTE_ARRAY);
|
||||
|
||||
assertEquals(LONG_VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigInteger_whenConvertingLongToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertLongToByteArrayUsingBigInteger(LONG_VALUE);
|
||||
|
||||
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigInteger_whenConvertingByteArrayToFloat_thenSuccess() {
|
||||
float value = convertByteArrayToFloatUsingBigInteger(FLOAT_BYTE_ARRAY);
|
||||
|
||||
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigInteger_whenConvertingFloatToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertFloatToByteArrayUsingBigInteger(FLOAT_VALUE);
|
||||
|
||||
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigInteger_whenConvertingByteArrayToDouble_thenSuccess() {
|
||||
double value = convertByteArrayToDoubleUsingBigInteger(DOUBLE_BYTE_ARRAY);
|
||||
|
||||
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBigInteger_whenConvertingDoubleToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertDoubleToByteArrayUsingBigInteger(DOUBLE_VALUE);
|
||||
|
||||
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuava_whenConvertingByteArrayToInt_thenSuccess() {
|
||||
int value = convertingByteArrayToIntUsingGuava(INT_BYTE_ARRAY);
|
||||
|
||||
assertEquals(INT_VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuava_whenConvertingIntToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertIntToByteArrayUsingGuava(INT_VALUE);
|
||||
|
||||
assertArrayEquals(INT_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuava_whenConvertingByteArrayToLong_thenSuccess() {
|
||||
long value = convertByteArrayToLongUsingGuava(LONG_BYTE_ARRAY);
|
||||
|
||||
assertEquals(LONG_VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuava_whenConvertingLongToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertLongToByteArrayUsingGuava(LONG_VALUE);
|
||||
|
||||
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuava_whenConvertingByteArrayToFloat_thenSuccess() {
|
||||
float value = convertByteArrayToFloatUsingGuava(FLOAT_BYTE_ARRAY);
|
||||
|
||||
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuava_whenConvertingFloatToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertFloatToByteArrayUsingGuava(FLOAT_VALUE);
|
||||
|
||||
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuava_whenConvertingByteArrayToDouble_thenSuccess() {
|
||||
double value = convertByteArrayToDoubleUsingGuava(DOUBLE_BYTE_ARRAY);
|
||||
|
||||
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuava_whenConvertingDoubleToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertDoubleToByteArrayUsingGuava(DOUBLE_VALUE);
|
||||
|
||||
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsLang_whenConvertingByteArrayToInt_thenSuccess() {
|
||||
int value = convertByteArrayToIntUsingCommonsLang(INT_BYTE_ARRAY);
|
||||
|
||||
assertEquals(INT_VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsLang_whenConvertingIntToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertIntToByteArrayUsingCommonsLang(INT_VALUE);
|
||||
|
||||
assertArrayEquals(INT_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsLang_whenConvertingByteArrayToLong_thenSuccess() {
|
||||
long value = convertByteArrayToLongUsingCommonsLang(LONG_BYTE_ARRAY);
|
||||
|
||||
assertEquals(LONG_VALUE, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsLang_whenConvertingLongToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertLongToByteArrayUsingCommonsLang(LONG_VALUE);
|
||||
|
||||
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsLang_whenConvertingByteArrayToFloat_thenSuccess() {
|
||||
float value = convertByteArrayToFloatUsingCommonsLang(FLOAT_BYTE_ARRAY);
|
||||
|
||||
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsLang_whenConvertingFloatToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertFloatToByteArrayUsingCommonsLang(FLOAT_VALUE);
|
||||
|
||||
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsLang_whenConvertingByteArrayToDouble_thenSuccess() {
|
||||
double value = convertByteArrayToDoubleUsingCommonsLang(DOUBLE_BYTE_ARRAY);
|
||||
|
||||
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsLang_whenConvertingDoubleToByteArray_thenSuccess() {
|
||||
byte[] bytes = convertDoubleToByteArrayUsingCommonsLang(DOUBLE_VALUE);
|
||||
|
||||
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
|
||||
}
|
||||
|
||||
}
|
@ -11,7 +11,6 @@
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -11,7 +11,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -0,0 +1,74 @@
|
||||
package com.baeldung.map.hashmap.entryremoval;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class RemoveEntryApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
HashMap<String, String> foodItemTypeMap = new HashMap<>();
|
||||
foodItemTypeMap.put("Apple", "Fruit");
|
||||
foodItemTypeMap.put("Grape", "Fruit");
|
||||
foodItemTypeMap.put("Mango", "Fruit");
|
||||
foodItemTypeMap.put("Carrot", "Vegetable");
|
||||
foodItemTypeMap.put("Potato", "Vegetable");
|
||||
foodItemTypeMap.put("Spinach", "Vegetable");
|
||||
// Current Map Status: {Potato=Vegetable, Apple=Fruit, Carrot=Vegetable, Grape=Fruit, Mango=Fruit, Spinach=Vegetable}
|
||||
|
||||
foodItemTypeMap.remove("Apple");
|
||||
// Current Map Status: {Potato=Vegetable, Carrot=Vegetable, Grape=Fruit, Mango=Fruit, Spinach=Vegetable}
|
||||
|
||||
foodItemTypeMap.remove("Grape", "Vegetable");
|
||||
// Current Map Status: {Potato=Vegetable, Carrot=Vegetable, Grape=Fruit, Mango=Fruit, Spinach=Vegetable}
|
||||
|
||||
try {
|
||||
for (Entry<String, String> item : foodItemTypeMap.entrySet()) {
|
||||
if (item.getKey()
|
||||
.equals("Potato")) {
|
||||
foodItemTypeMap.remove(item.getKey());
|
||||
}
|
||||
}
|
||||
} catch (ConcurrentModificationException e) {
|
||||
System.out.println("Exception occured while updating map: " + e.toString());
|
||||
}
|
||||
|
||||
foodItemTypeMap.entrySet()
|
||||
.removeIf(entry -> entry.getKey()
|
||||
.equals("Grape"));
|
||||
// Current Map Status: {Carrot=Vegetable, Mango=Fruit, Spinach=Vegetable}
|
||||
|
||||
Iterator<Entry<String, String>> iterator = foodItemTypeMap.entrySet()
|
||||
.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
if (iterator.next()
|
||||
.getKey()
|
||||
.equals("Carrot"))
|
||||
iterator.remove();
|
||||
}
|
||||
// Current Map Status: {Mango=Fruit, Spinach=Vegetable}
|
||||
|
||||
// Use ConcurrentHashMap
|
||||
ConcurrentHashMap<String, String> foodItemTypeConcMap = new ConcurrentHashMap<>();
|
||||
foodItemTypeConcMap.put("Apple", "Fruit");
|
||||
foodItemTypeConcMap.put("Grape", "Fruit");
|
||||
foodItemTypeConcMap.put("Mango", "Fruit");
|
||||
foodItemTypeConcMap.put("Carrot", "Vegetable");
|
||||
foodItemTypeConcMap.put("Potato", "Vegetable");
|
||||
foodItemTypeConcMap.put("Spinach", "Vegetable");
|
||||
|
||||
for (Entry<String, String> item : foodItemTypeConcMap.entrySet()) {
|
||||
if (item.getKey() != null && item.getKey()
|
||||
.equals("Potato")) {
|
||||
foodItemTypeConcMap.remove(item.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
// foodItemTypeConcMap : {Apple=Fruit, Carrot=Vegetable, Grape=Fruit, Mango=Fruit, Spinach=Vegetable}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -3,4 +3,4 @@
|
||||
This module contains articles about Map data structures in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Using a Custom Class as a Key in a Java HashMap](https://www.baeldung.com/custom-key-hashmap)
|
||||
- [Using a Custom Class as a Key in a Java HashMap](https://www.baeldung.com/java-custom-class-map-key)
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -11,7 +11,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -13,7 +13,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -13,7 +13,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
</project>
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.baeldung.jndi;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
@ -57,4 +58,10 @@ class JndiUnitTest {
|
||||
assertNotNull(ds.getConnection());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void tearDown() throws Exception {
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import javax.naming.InitialContext;
|
||||
import javax.naming.NameNotFoundException;
|
||||
import javax.naming.NoInitialContextException;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -17,13 +16,16 @@ import org.springframework.mock.jndi.SimpleNamingContextBuilder;
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class JndiExceptionsUnitTest {
|
||||
|
||||
InitialContext ctx;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void givenNoContext_whenLookupObject_thenThrowNoInitialContext() {
|
||||
assertThrows(NoInitialContextException.class, () -> {
|
||||
JndiTemplate jndiTemplate = new JndiTemplate();
|
||||
InitialContext ctx = (InitialContext) jndiTemplate.getContext();
|
||||
ctx = (InitialContext) jndiTemplate.getContext();
|
||||
ctx.lookup("java:comp/env/jdbc/datasource");
|
||||
ctx.close();
|
||||
}).printStackTrace();
|
||||
}
|
||||
|
||||
@ -35,8 +37,9 @@ public class JndiExceptionsUnitTest {
|
||||
builder.activate();
|
||||
|
||||
JndiTemplate jndiTemplate = new JndiTemplate();
|
||||
InitialContext ctx = (InitialContext) jndiTemplate.getContext();
|
||||
ctx = (InitialContext) jndiTemplate.getContext();
|
||||
ctx.lookup("badJndiName");
|
||||
ctx.close();
|
||||
}).printStackTrace();
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
This module contains articles about core features in the Java language
|
||||
|
||||
- [Class.isInstance vs Class.isAssignableFrom](https://www.baeldung.com/java-isinstance-isassignablefrom)
|
||||
- [Class.isInstance vs Class.isAssignableFrom and instanceof](https://www.baeldung.com/java-isinstance-isassignablefrom)
|
||||
- [Converting a Java String Into a Boolean](https://www.baeldung.com/java-string-to-boolean)
|
||||
- [When are Static Variables Initialized in Java?](https://www.baeldung.com/java-static-variables-initialization)
|
||||
- [Checking if a Class Exists in Java](https://www.baeldung.com/java-check-class-exists)
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -11,7 +11,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -11,7 +11,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
|
@ -11,7 +11,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -11,7 +11,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
</project>
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
</project>
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -11,7 +11,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user