Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
4541f7f58a
|
@ -14,4 +14,8 @@ public class Operations {
|
||||||
return a && b;
|
return a && b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected int max(int a, int b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.LongAdder;
|
import java.util.concurrent.atomic.LongAdder;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
import static com.jayway.awaitility.Awaitility.await;
|
||||||
import static junit.framework.TestCase.assertEquals;
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
|
||||||
public class LongAdderTest {
|
public class LongAdderTest {
|
||||||
|
@ -60,6 +61,7 @@ public class LongAdderTest {
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
|
|
||||||
assertEquals(counter.sumThenReset(), numberOfIncrements * numberOfThreads);
|
assertEquals(counter.sumThenReset(), numberOfIncrements * numberOfThreads);
|
||||||
assertEquals(counter.sum(), 0);
|
|
||||||
|
await().until(() -> assertEquals(counter.sum(), 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.baeldung.dateapi;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class JavaDurationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test2() {
|
||||||
|
Instant start = Instant.parse("2017-10-03T10:15:30.00Z");
|
||||||
|
Instant end = Instant.parse("2017-10-03T10:16:30.00Z");
|
||||||
|
|
||||||
|
Duration duration = Duration.between(start, end);
|
||||||
|
|
||||||
|
assertFalse(duration.isNegative());
|
||||||
|
|
||||||
|
assertEquals(60, duration.getSeconds());
|
||||||
|
assertEquals(1, duration.toMinutes());
|
||||||
|
|
||||||
|
Duration fromDays = Duration.ofDays(1);
|
||||||
|
assertEquals(86400, fromDays.getSeconds());
|
||||||
|
|
||||||
|
Duration fromMinutes = Duration.ofMinutes(60);
|
||||||
|
assertEquals(1, fromMinutes.toHours());
|
||||||
|
|
||||||
|
assertEquals(120, duration.plusSeconds(60).getSeconds());
|
||||||
|
assertEquals(30, duration.minusSeconds(30).getSeconds());
|
||||||
|
|
||||||
|
assertEquals(120, duration.plus(60, ChronoUnit.SECONDS).getSeconds());
|
||||||
|
assertEquals(30, duration.minus(30, ChronoUnit.SECONDS).getSeconds());
|
||||||
|
|
||||||
|
Duration fromChar1 = Duration.parse("P1DT1H10M10.5S");
|
||||||
|
Duration fromChar2 = Duration.parse("PT10M");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.dateapi;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Period;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
|
||||||
|
public class JavaPeriodTest {
|
||||||
|
|
||||||
|
private static final Logger LOG = Logger.getLogger(JavaPeriodTest.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTestPeriod_thenOk() {
|
||||||
|
|
||||||
|
LocalDate startDate = LocalDate.of(2015, 2, 15);
|
||||||
|
LocalDate endDate = LocalDate.of(2017, 1, 21);
|
||||||
|
|
||||||
|
Period period = Period.between(startDate, endDate);
|
||||||
|
|
||||||
|
LOG.info(String.format("Years:%d months:%d days:%d", period.getYears(), period.getMonths(), period.getDays()));
|
||||||
|
|
||||||
|
assertFalse(period.isNegative());
|
||||||
|
assertEquals(56, period.plusDays(50).getDays());
|
||||||
|
assertEquals(9, period.minusMonths(2).getMonths());
|
||||||
|
|
||||||
|
Period fromUnits = Period.of(3, 10, 10);
|
||||||
|
Period fromDays = Period.ofDays(50);
|
||||||
|
Period fromMonths = Period.ofMonths(5);
|
||||||
|
Period fromYears = Period.ofYears(10);
|
||||||
|
Period fromWeeks = Period.ofWeeks(40);
|
||||||
|
|
||||||
|
assertEquals(280, fromWeeks.getDays());
|
||||||
|
|
||||||
|
Period fromCharYears = Period.parse("P2Y");
|
||||||
|
assertEquals(2, fromCharYears.getYears());
|
||||||
|
Period fromCharUnits = Period.parse("P2Y3M5D");
|
||||||
|
assertEquals(5, fromCharUnits.getDays());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -15,22 +15,22 @@ public class OperationsUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalAccessException.class)
|
@Test(expected = IllegalAccessException.class)
|
||||||
public void givenObject_whenInvokePrivatedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
|
public void givenObject_whenInvokePrivateMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
||||||
Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
|
Method andPrivateMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
|
||||||
|
|
||||||
Operations operationsInstance = new Operations();
|
Operations operationsInstance = new Operations();
|
||||||
Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false);
|
Boolean result = (Boolean) andPrivateMethod.invoke(operationsInstance, true, false);
|
||||||
|
|
||||||
assertFalse(result);
|
assertFalse(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenObject_whenInvokePrivateMethod_thenCorrect() throws Exception {
|
public void givenObject_whenInvokePrivateMethod_thenCorrect() throws Exception {
|
||||||
Method andInstanceMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
|
Method andPrivatedMethod = Operations.class.getDeclaredMethod("and", boolean.class, boolean.class);
|
||||||
andInstanceMethod.setAccessible(true);
|
andPrivatedMethod.setAccessible(true);
|
||||||
|
|
||||||
Operations operationsInstance = new Operations();
|
Operations operationsInstance = new Operations();
|
||||||
Boolean result = (Boolean)andInstanceMethod.invoke(operationsInstance, true, false);
|
Boolean result = (Boolean) andPrivatedMethod.invoke(operationsInstance, true, false);
|
||||||
|
|
||||||
assertFalse(result);
|
assertFalse(result);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.baeldung.java.reflection.operations;
|
||||||
|
|
||||||
|
import com.baeldung.java.reflection.*;
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
public class MoreOperationsUnitTest {
|
||||||
|
|
||||||
|
public MoreOperationsUnitTest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalAccessException.class)
|
||||||
|
public void givenObject_whenInvokeProtectedMethod_thenFail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
||||||
|
Method maxProtectedMethod = Operations.class.getDeclaredMethod("max", int.class, int.class);
|
||||||
|
|
||||||
|
Operations operationsInstance = new Operations();
|
||||||
|
Integer result = (Integer) maxProtectedMethod.invoke(operationsInstance, 2, 4);
|
||||||
|
System.out.println("result = " + result);
|
||||||
|
assertThat(result, equalTo(4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenObject_whenInvokeProtectedMethod_thenCorrect() throws Exception {
|
||||||
|
Method maxProtectedMethod = Operations.class.getDeclaredMethod("max", int.class, int.class);
|
||||||
|
maxProtectedMethod.setAccessible(true);
|
||||||
|
|
||||||
|
Operations operationsInstance = new Operations();
|
||||||
|
Integer result = (Integer) maxProtectedMethod.invoke(operationsInstance, 2, 4);
|
||||||
|
|
||||||
|
assertThat(result, equalTo(4));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
3
pom.xml
3
pom.xml
|
@ -229,8 +229,7 @@
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<configuration>
|
<configuration>
|
||||||
<forkCount>3</forkCount>
|
|
||||||
<reuseForks>true</reuseForks>
|
|
||||||
<excludes>
|
<excludes>
|
||||||
<exclude>**/*IntegrationTest.java</exclude>
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package com.baeldung.functional;
|
package com.baeldung.functional;
|
||||||
|
|
||||||
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers;
|
import static org.springframework.web.reactive.function.BodyExtractors.toDataBuffers;
|
||||||
|
@ -21,21 +23,22 @@ public class FormHandler {
|
||||||
.filter(formData -> "baeldung".equals(formData.get("user")))
|
.filter(formData -> "baeldung".equals(formData.get("user")))
|
||||||
.filter(formData -> "you_know_what_to_do".equals(formData.get("token")))
|
.filter(formData -> "you_know_what_to_do".equals(formData.get("token")))
|
||||||
.flatMap(formData -> ok().body(Mono.just("welcome back!"), String.class))
|
.flatMap(formData -> ok().body(Mono.just("welcome back!"), String.class))
|
||||||
.or(ServerResponse.badRequest().build());
|
.switchIfEmpty(ServerResponse.badRequest().build());
|
||||||
}
|
}
|
||||||
|
|
||||||
Mono<ServerResponse> handleUpload(ServerRequest request) {
|
Mono<ServerResponse> handleUpload(ServerRequest request) {
|
||||||
return request
|
return request
|
||||||
.body(toDataBuffers())
|
.body(toDataBuffers())
|
||||||
.collectList()
|
.collectList()
|
||||||
.flatMap(dataBuffers -> {
|
.flatMap(dataBuffers -> ok()
|
||||||
|
.body(fromObject(extractData(dataBuffers).toString())));
|
||||||
|
}
|
||||||
|
|
||||||
|
private AtomicLong extractData(List<DataBuffer> dataBuffers) {
|
||||||
AtomicLong atomicLong = new AtomicLong(0);
|
AtomicLong atomicLong = new AtomicLong(0);
|
||||||
dataBuffers.forEach(d -> atomicLong.addAndGet(d
|
dataBuffers.forEach(d -> atomicLong.addAndGet(d
|
||||||
.asByteBuffer()
|
.asByteBuffer()
|
||||||
.array().length));
|
.array().length));
|
||||||
|
return atomicLong;
|
||||||
return ok()
|
|
||||||
.body(fromObject(atomicLong.toString()));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,18 @@ import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.boot.web.server.WebServer;
|
import org.springframework.boot.web.server.WebServer;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
|
||||||
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
|
import org.springframework.web.reactive.function.BodyInserters;
|
||||||
|
|
||||||
|
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
||||||
|
import static org.springframework.web.reactive.function.BodyInserters.fromResource;
|
||||||
|
|
||||||
public class FunctionalWebApplicationIntegrationTest {
|
public class FunctionalWebApplicationIntegrationTest {
|
||||||
|
|
||||||
private static WebTestClient client;
|
private static WebTestClient client;
|
||||||
|
@ -49,7 +59,7 @@ public class FunctionalWebApplicationIntegrationTest {
|
||||||
.isEqualTo("helloworld");
|
.isEqualTo("helloworld");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @Test
|
@Test
|
||||||
public void givenLoginForm_whenPostValidToken_thenSuccess() throws Exception {
|
public void givenLoginForm_whenPostValidToken_thenSuccess() throws Exception {
|
||||||
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(1);
|
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(1);
|
||||||
formData.add("user", "baeldung");
|
formData.add("user", "baeldung");
|
||||||
|
@ -59,15 +69,15 @@ public class FunctionalWebApplicationIntegrationTest {
|
||||||
.post()
|
.post()
|
||||||
.uri("/login")
|
.uri("/login")
|
||||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||||
.exchange(BodyInserters.fromFormData(formData))
|
.body(BodyInserters.fromFormData(formData))
|
||||||
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isOk()
|
.isOk()
|
||||||
.expectBody(String.class)
|
.expectBody(String.class)
|
||||||
.value()
|
|
||||||
.isEqualTo("welcome back!");
|
.isEqualTo("welcome back!");
|
||||||
}*/
|
}
|
||||||
|
|
||||||
/* @Test
|
@Test
|
||||||
public void givenLoginForm_whenRequestWithInvalidToken_thenFail() throws Exception {
|
public void givenLoginForm_whenRequestWithInvalidToken_thenFail() throws Exception {
|
||||||
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(2);
|
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(2);
|
||||||
formData.add("user", "baeldung");
|
formData.add("user", "baeldung");
|
||||||
|
@ -77,27 +87,28 @@ public class FunctionalWebApplicationIntegrationTest {
|
||||||
.post()
|
.post()
|
||||||
.uri("/login")
|
.uri("/login")
|
||||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||||
.exchange(BodyInserters.fromFormData(formData))
|
.body(BodyInserters.fromFormData(formData))
|
||||||
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isBadRequest();
|
.isBadRequest();
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
/* @Test
|
@Test
|
||||||
public void givenUploadForm_whenRequestWithMultipartData_thenSuccess() throws Exception {
|
public void givenUploadForm_whenRequestWithMultipartData_thenSuccess() throws Exception {
|
||||||
Resource resource = new ClassPathResource("/baeldung-weekly.png");
|
Resource resource = new ClassPathResource("/baeldung-weekly.png");
|
||||||
client
|
client
|
||||||
.post()
|
.post()
|
||||||
.uri("/upload")
|
.uri("/upload")
|
||||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||||
.exchange(fromResource(resource))
|
.body(fromResource(resource))
|
||||||
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isOk()
|
.isOk()
|
||||||
.expectBody(String.class)
|
.expectBody(String.class)
|
||||||
.value()
|
|
||||||
.isEqualTo(String.valueOf(resource.contentLength()));
|
.isEqualTo(String.valueOf(resource.contentLength()));
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
/* @Test
|
@Test
|
||||||
public void givenActors_whenAddActor_thenAdded() throws Exception {
|
public void givenActors_whenAddActor_thenAdded() throws Exception {
|
||||||
client
|
client
|
||||||
.get()
|
.get()
|
||||||
|
@ -105,14 +116,14 @@ public class FunctionalWebApplicationIntegrationTest {
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isOk()
|
.isOk()
|
||||||
.expectBody(Actor.class)
|
.expectBodyList(Actor.class)
|
||||||
.list()
|
|
||||||
.hasSize(2);
|
.hasSize(2);
|
||||||
|
|
||||||
client
|
client
|
||||||
.post()
|
.post()
|
||||||
.uri("/actor")
|
.uri("/actor")
|
||||||
.exchange(fromObject(new Actor("Clint", "Eastwood")))
|
.body(fromObject(new Actor("Clint", "Eastwood")))
|
||||||
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isOk();
|
.isOk();
|
||||||
|
|
||||||
|
@ -122,10 +133,9 @@ public class FunctionalWebApplicationIntegrationTest {
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isOk()
|
.isOk()
|
||||||
.expectBody(Actor.class)
|
.expectBodyList(Actor.class)
|
||||||
.list()
|
|
||||||
.hasSize(3);
|
.hasSize(3);
|
||||||
}*/
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenResources_whenAccess_thenGot() throws Exception {
|
public void givenResources_whenAccess_thenGot() throws Exception {
|
||||||
|
|
|
@ -126,6 +126,18 @@
|
||||||
<version>6.0.6</version>
|
<version>6.0.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-websocket</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-messaging</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.togglz</groupId>
|
<groupId>org.togglz</groupId>
|
||||||
<artifactId>togglz-spring-boot-starter</artifactId>
|
<artifactId>togglz-spring-boot-starter</artifactId>
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package org.baeldung.websocket.client;
|
||||||
|
|
||||||
|
public class Message {
|
||||||
|
|
||||||
|
private String from;
|
||||||
|
private String text;
|
||||||
|
|
||||||
|
public String getText() {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFrom() {
|
||||||
|
return from;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFrom(String from) {
|
||||||
|
this.from = from;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setText(String text) {
|
||||||
|
this.text = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
package org.baeldung.websocket.client;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.springframework.messaging.simp.stomp.StompCommand;
|
||||||
|
import org.springframework.messaging.simp.stomp.StompHeaders;
|
||||||
|
import org.springframework.messaging.simp.stomp.StompSession;
|
||||||
|
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is an implementation for <code>StompSessionHandlerAdapter</code>.
|
||||||
|
* Once a connection is established, We subscribe to /topic/messages and
|
||||||
|
* send a sample message to server.
|
||||||
|
*
|
||||||
|
* @author Kalyan
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MyStompSessionHandler extends StompSessionHandlerAdapter {
|
||||||
|
|
||||||
|
private Logger logger = Logger.getLogger(MyStompSessionHandler.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
|
||||||
|
logger.info("New session established : "+session.getSessionId());
|
||||||
|
session.subscribe("/topic/messages", this);
|
||||||
|
logger.info("Subscribed to /topic/messages");
|
||||||
|
session.send("/app/chat", getSampleMessage());
|
||||||
|
logger.info("Message sent to websocket server");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
|
||||||
|
logger.error("Got an exception", exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type getPayloadType(StompHeaders headers) {
|
||||||
|
return Message.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleFrame(StompHeaders headers, Object payload) {
|
||||||
|
Message msg = (Message)payload;
|
||||||
|
logger.info("Received : "+ msg.getText()+ " from : "+msg.getFrom());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A sample message instance.
|
||||||
|
* @return instance of <code>Message</code>
|
||||||
|
*/
|
||||||
|
private Message getSampleMessage(){
|
||||||
|
Message msg = new Message();
|
||||||
|
msg.setFrom("Nicky");
|
||||||
|
msg.setText("Howdy!!");
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package org.baeldung.websocket.client;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
|
||||||
|
import org.springframework.messaging.simp.stomp.StompSessionHandler;
|
||||||
|
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
|
||||||
|
import org.springframework.web.socket.messaging.WebSocketStompClient;
|
||||||
|
import org.springframework.web.socket.sockjs.client.SockJsClient;
|
||||||
|
import org.springframework.web.socket.sockjs.client.Transport;
|
||||||
|
import org.springframework.web.socket.sockjs.client.WebSocketTransport;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stand alone WebSocketStompClient.
|
||||||
|
* @author Kalyan
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class StompClient {
|
||||||
|
|
||||||
|
private static String URL = "ws://localhost:8080/spring-mvc-java/chat";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Transport webSocketTransport = new WebSocketTransport(new StandardWebSocketClient());
|
||||||
|
List<Transport> transports = Collections.singletonList(webSocketTransport);
|
||||||
|
SockJsClient sockJsClient = new SockJsClient(transports);
|
||||||
|
|
||||||
|
WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
|
||||||
|
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
|
||||||
|
|
||||||
|
StompSessionHandler sessionHandler = new MyStompSessionHandler();
|
||||||
|
stompClient.connect(URL, sessionHandler);
|
||||||
|
|
||||||
|
new Scanner(System.in).nextLine(); // Don't close immediately.
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.baeldung.websocket.client;
|
||||||
|
|
||||||
|
import org.baeldung.websocket.client.MyStompSessionHandler;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.springframework.messaging.simp.stomp.StompHeaders;
|
||||||
|
import org.springframework.messaging.simp.stomp.StompSession;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for MyStompSessionHandler
|
||||||
|
* @author Kalyan
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MyStompSessionHandlerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAfterConnectedSuccessfull() {
|
||||||
|
StompSession mockSession = Mockito.mock(StompSession.class);
|
||||||
|
StompHeaders mockHeader = Mockito.mock(StompHeaders.class);
|
||||||
|
MyStompSessionHandler sessionHandler = new MyStompSessionHandler();
|
||||||
|
sessionHandler.afterConnected(mockSession, mockHeader);
|
||||||
|
Mockito.verify(mockSession).subscribe("/topic/messages", sessionHandler);
|
||||||
|
Mockito.verify(mockSession).send(Mockito.anyString(), Mockito.anyObject());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -31,8 +31,7 @@ public class CustomisedReports implements IReporter {
|
||||||
initReportTemplate();
|
initReportTemplate();
|
||||||
for (ISuite suite : suites) {
|
for (ISuite suite : suites) {
|
||||||
Map<String, ISuiteResult> suiteResults = suite.getResults();
|
Map<String, ISuiteResult> suiteResults = suite.getResults();
|
||||||
for (String testName : suiteResults.keySet()) {
|
suiteResults.forEach((testName, suiteResult) -> {
|
||||||
ISuiteResult suiteResult = suiteResults.get(testName);
|
|
||||||
ITestContext testContext = suiteResult.getTestContext();
|
ITestContext testContext = suiteResult.getTestContext();
|
||||||
|
|
||||||
IResultMap failedResult = testContext.getFailedTests();
|
IResultMap failedResult = testContext.getFailedTests();
|
||||||
|
@ -52,8 +51,7 @@ public class CustomisedReports implements IReporter {
|
||||||
for (ITestResult testResult : testsSkipped) {
|
for (ITestResult testResult : testsSkipped) {
|
||||||
reportWriter.println(String.format(resultRow, "warning", suite.getName(), testName, testResult.getName(), "SKIPPED", "NA"));
|
reportWriter.println(String.format(resultRow, "warning", suite.getName(), testName, testResult.getName(), "SKIPPED", "NA"));
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
finishReportTemplate();
|
finishReportTemplate();
|
||||||
reportWriter.flush();
|
reportWriter.flush();
|
||||||
|
|
Loading…
Reference in New Issue