Merge pull request #67 from eugenp/master

update
This commit is contained in:
Maiklins 2020-07-31 21:32:48 +02:00 committed by GitHub
commit 71db2e9a0d
39 changed files with 290 additions and 8 deletions

View File

@ -0,0 +1,39 @@
package com.baeldung.arraycompare;
import java.util.Objects;
public class Plane {
private final String name;
private final String model;
public Plane(String name, String model) {
this.name = name;
this.model = model;
}
public String getName() {
return name;
}
public String getModel() {
return model;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Plane plane = (Plane) o;
return Objects.equals(name, plane.name) && Objects.equals(model, plane.model);
}
@Override
public int hashCode() {
return Objects.hash(name, model);
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.arraycompare;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
public class DeepEqualsCompareUnitTest {
@Test
public void givenSameContents_whenDeepEquals_thenTrue() {
final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
new Plane[] { new Plane("Plane 2", "B738") } };
final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
new Plane[] { new Plane("Plane 2", "B738") } };
assertThat(Arrays.deepEquals(planes1, planes2)).isTrue();
}
@Test
public void givenSameContentsWithDifferentOrder_whenDeepEquals_thenFalse() {
final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
new Plane[] { new Plane("Plane 2", "B738") } };
final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 2", "B738") },
new Plane[] { new Plane("Plane 1", "A320") } };
assertThat(Arrays.deepEquals(planes1, planes2)).isFalse();
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.arraycompare;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
public class EqualsCompareUnitTest {
@Test
public void givenSameContents_whenEquals_thenTrue() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
assertThat(Arrays.equals(planes1, planes2)).isTrue();
}
@Test
public void givenSameContentsDifferentOrder_whenEquals_thenFalse() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final String[] planes2 = new String[] { "B738", "A320", "A321", "A319", "B77W", "B737", "A333", "A332" };
assertThat(Arrays.equals(planes1, planes2)).isFalse();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.arraycompare;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class LengthsCompareUnitTest {
@Test
public void givenSameContent_whenSizeCompare_thenTrue() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final Integer[] quantities = new Integer[] { 10, 12, 34, 45, 12, 43, 5, 2 };
assertThat(planes1).hasSize(8);
assertThat(quantities).hasSize(8);
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.arraycompare;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Comparator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
public class OrderCompareUnitTest {
@Test
public void givenSameContentDifferentOrder_whenSortedAndDeepEquals_thenTrue() {
final Plane[][] planes1 = new Plane[][] {
new Plane[] { new Plane("Plane 1", "A320"), new Plane("Plane 2", "B738") } };
final Plane[][] planes2 = new Plane[][] {
new Plane[] { new Plane("Plane 2", "B738"), new Plane("Plane 1", "A320") } };
Comparator<Plane> planeComparator = (o1, o2) -> {
if (o1.getName()
.equals(o2.getName())) {
return o2.getModel()
.compareTo(o1.getModel());
}
return o2.getName()
.compareTo(o1.getName());
};
Arrays.sort(planes1[0], planeComparator);
Arrays.sort(planes2[0], planeComparator);
assertThat(Arrays.deepEquals(planes1, planes2)).isTrue();
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.arraycompare;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ReferenceCompareUnitTest {
@Test
public void givenSameReferences_whenSame_thenTrue() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final String[] planes2 = planes1;
assertThat(planes1).isSameAs(planes2);
planes2[0] = "747";
assertThat(planes1).isSameAs(planes2);
assertThat(planes2[0]).isEqualTo("747");
assertThat(planes1[0]).isEqualTo("747");
}
@Test
public void givenSameContentDifferentReferences_whenSame_thenFalse() {
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
assertThat(planes1).isNotSameAs(planes2);
}
}

View File

@ -0,0 +1,51 @@
import javax.imageio.ImageIO;
import java.awt.Component;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class ScreenshotUnitTest {
@Test
public void givenMainScreen_whenTakeScreenshot_thenSaveToFile() throws Exception {
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
File imageFile = File.createTempFile("single-screen", "bmp");
ImageIO.write(capture, "bmp", imageFile);
assertTrue(imageFile.exists());
}
@Test
public void givenMultipleScreens_whenTakeScreenshot_thenSaveToFile() throws Exception {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screens = ge.getScreenDevices();
Rectangle allScreenBounds = new Rectangle();
for (GraphicsDevice screen : screens) {
Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
allScreenBounds.width += screenBounds.width;
allScreenBounds.height = Math.max(allScreenBounds.height, screenBounds.height);
}
BufferedImage capture = new Robot().createScreenCapture(allScreenBounds);
File imageFile = File.createTempFile("all-screens", "bmp");
ImageIO.write(capture, "bmp", imageFile);
assertTrue(imageFile.exists());
}
@Test
public void givenComponent_whenTakeScreenshot_thenSaveToFile(Component component) throws Exception {
Rectangle componentRect = component.getBounds();
BufferedImage bufferedImage = new BufferedImage(componentRect.width, componentRect.height, BufferedImage.TYPE_INT_ARGB);
component.paint(bufferedImage.getGraphics());
File imageFile = File.createTempFile("component-screenshot", "bmp");
ImageIO.write(bufferedImage, "bmp", imageFile);
assertTrue(imageFile.exists());
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.jsoup;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class PreservingLineBreaksUnitTest {
@Test
public void whenBackSlashNNewLineCharacter_thenPreserveLineBreak() {
String strHTML = "<html><body>Hello\nworld</body></html>";
Document.OutputSettings outputSettings = new Document.OutputSettings();
outputSettings.prettyPrint(false);
String strWithNewLines = Jsoup.clean(strHTML, "", Whitelist.none(), outputSettings);
assertEquals("Hello\nworld", strWithNewLines);
}
@Test
public void whenHTMLNewLineCharacters_thenPreserveLineBreak() {
String strHTML = "<html><body>" +
"Hello" +
"<br>" +
"World" +
"<p>Paragraph</p>" +
"</body></html>";
Document jsoupDoc = Jsoup.parse(strHTML);
Document.OutputSettings outputSettings = new Document.OutputSettings();
outputSettings.prettyPrint(false);
jsoupDoc.outputSettings(outputSettings);
jsoupDoc.select("br").before("\\n");
jsoupDoc.select("p").before("\\n");
String str = jsoupDoc.html().replaceAll("\\\\n", "\n");
String strWithNewLines =
Jsoup.clean(str, "", Whitelist.none(), outputSettings);
assertEquals("Hello\nWorld\nParagraph", strWithNewLines);
}
}

View File

@ -8,5 +8,5 @@ This module contains articles about reactive Spring 5
- [Testing Reactive Streams Using StepVerifier and TestPublisher](https://www.baeldung.com/reactive-streams-step-verifier-test-publisher) - [Testing Reactive Streams Using StepVerifier and TestPublisher](https://www.baeldung.com/reactive-streams-step-verifier-test-publisher)
- [Debugging Reactive Streams in Spring 5](https://www.baeldung.com/spring-debugging-reactive-streams) - [Debugging Reactive Streams in Spring 5](https://www.baeldung.com/spring-debugging-reactive-streams)
- [Static Content in Spring WebFlux](https://www.baeldung.com/spring-webflux-static-content) - [Static Content in Spring WebFlux](https://www.baeldung.com/spring-webflux-static-content)
- [Spring WebClient Filters](https://www.baeldung.com/spring-webclient-filters) - [Server-Sent Events in Spring](https://www.baeldung.com/spring-server-sent-events)
- More articles: [[<-- prev]](/spring-5-reactive) - More articles: [[<-- prev]](/spring-5-reactive)

View File

@ -53,6 +53,11 @@
<artifactId>reactor-test</artifactId> <artifactId>reactor-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -8,7 +8,9 @@ import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
@SpringBootTest import com.baeldung.reactive.serversentevents.server.ServerSSEApplication;
@SpringBootTest(classes = ServerSSEApplication.class)
@WithMockUser @WithMockUser
public class ServiceSentEventLiveTest { public class ServiceSentEventLiveTest {

View File

@ -10,3 +10,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Simultaneous Spring WebClient Calls](https://www.baeldung.com/spring-webclient-simultaneous-calls) - [Simultaneous Spring WebClient Calls](https://www.baeldung.com/spring-webclient-simultaneous-calls)
- [Logging Spring WebClient Calls](https://www.baeldung.com/spring-log-webclient-calls) - [Logging Spring WebClient Calls](https://www.baeldung.com/spring-log-webclient-calls)
- [Mocking a WebClient in Spring](https://www.baeldung.com/spring-mocking-webclient) - [Mocking a WebClient in Spring](https://www.baeldung.com/spring-mocking-webclient)
- [Spring WebClient Filters](https://www.baeldung.com/spring-webclient-filters)

View File

@ -33,6 +33,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId> <artifactId>spring-boot-starter-webflux</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.projectreactor</groupId> <groupId>org.projectreactor</groupId>
<artifactId>reactor-spring</artifactId> <artifactId>reactor-spring</artifactId>

View File

@ -12,3 +12,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Guide to Spring 5 WebFlux](https://www.baeldung.com/spring-webflux) - [Guide to Spring 5 WebFlux](https://www.baeldung.com/spring-webflux)
- [Introduction to the Functional Web Framework in Spring 5](https://www.baeldung.com/spring-5-functional-web) - [Introduction to the Functional Web Framework in Spring 5](https://www.baeldung.com/spring-5-functional-web)
- [Guide to the AuthenticationManagerResolver in Spring Security](https://www.baeldung.com/spring-security-authenticationmanagerresolver) - [Guide to the AuthenticationManagerResolver in Spring Security](https://www.baeldung.com/spring-security-authenticationmanagerresolver)
- [Spring Webflux and CORS](https://www.baeldung.com/spring-webflux-cors)

View File

@ -6,7 +6,9 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec; import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) import com.baeldung.reactive.cors.annotated.CorsOnAnnotatedElementsApplication;
@SpringBootTest(classes = CorsOnAnnotatedElementsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CorsOnAnnotatedElementsLiveTest { public class CorsOnAnnotatedElementsLiveTest {
private static final String BASE_URL = "http://localhost:8081"; private static final String BASE_URL = "http://localhost:8081";

View File

@ -6,7 +6,9 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec; import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) import com.baeldung.reactive.cors.global.CorsGlobalConfigApplication;
@SpringBootTest(classes = CorsGlobalConfigApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CorsOnGlobalConfigLiveTest { public class CorsOnGlobalConfigLiveTest {
private static final String BASE_URL = "http://localhost:8082"; private static final String BASE_URL = "http://localhost:8082";

View File

@ -6,7 +6,9 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec; import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) import com.baeldung.reactive.cors.webfilter.CorsWebFilterApplication;
@SpringBootTest(classes = CorsWebFilterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CorsOnWebFilterLiveTest { public class CorsOnWebFilterLiveTest {
private static final String BASE_URL = "http://localhost:8083"; private static final String BASE_URL = "http://localhost:8083";

View File

@ -7,14 +7,11 @@ The "REST With Spring" Classes: https://bit.ly/restwithspring
### Relevant Articles ### Relevant Articles
- [Introduction to the Functional Web Framework in Spring 5](https://www.baeldung.com/spring-5-functional-web)
- [Spring 5 WebClient](https://www.baeldung.com/spring-5-webclient) - [Spring 5 WebClient](https://www.baeldung.com/spring-5-webclient)
- [Exploring the Spring 5 WebFlux URL Matching](https://www.baeldung.com/spring-5-mvc-url-matching) - [Exploring the Spring 5 WebFlux URL Matching](https://www.baeldung.com/spring-5-mvc-url-matching)
- [Reactive WebSockets with Spring 5](https://www.baeldung.com/spring-5-reactive-websockets) - [Reactive WebSockets with Spring 5](https://www.baeldung.com/spring-5-reactive-websockets)
- [Spring Webflux Filters](https://www.baeldung.com/spring-webflux-filters) - [Spring Webflux Filters](https://www.baeldung.com/spring-webflux-filters)
- [How to Set a Header on a Response with Spring 5](https://www.baeldung.com/spring-response-header) - [How to Set a Header on a Response with Spring 5](https://www.baeldung.com/spring-response-header)
- [Spring Webflux and CORS](https://www.baeldung.com/spring-webflux-cors)
- [Handling Errors in Spring WebFlux](https://www.baeldung.com/spring-webflux-errors) - [Handling Errors in Spring WebFlux](https://www.baeldung.com/spring-webflux-errors)
- [Server-Sent Events in Spring](https://www.baeldung.com/spring-server-sent-events)
- [A Guide to Spring Session Reactive Support: WebSession](https://www.baeldung.com/spring-session-reactive) - [A Guide to Spring Session Reactive Support: WebSession](https://www.baeldung.com/spring-session-reactive)
- More articles: [[next -->]](/spring-5-reactive-2) - More articles: [[next -->]](/spring-5-reactive-2)