Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
59cc02f07f
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.string;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class Palindrome {
|
||||
|
||||
public boolean isPalindrome(String text) {
|
||||
text = text.replaceAll("\\s+", "")
|
||||
.toLowerCase();
|
||||
int length = text.length();
|
||||
int forward = 0;
|
||||
int backward = length - 1;
|
||||
boolean palindrome = true;
|
||||
while (backward > forward) {
|
||||
char forwardChar = text.charAt(forward++);
|
||||
char backwardChar = text.charAt(backward--);
|
||||
if (forwardChar != backwardChar)
|
||||
return false;
|
||||
}
|
||||
return palindrome;
|
||||
}
|
||||
|
||||
public boolean isPalindromeReverseTheString(String text) {
|
||||
String reverse = "";
|
||||
text = text.replaceAll("\\s+", "").toLowerCase();
|
||||
char[] plain = text.toCharArray();
|
||||
for (int i = plain.length - 1; i >= 0; i--)
|
||||
reverse += plain[i];
|
||||
return reverse.equals(text);
|
||||
}
|
||||
|
||||
public boolean isPalindromeUsingStringBuilder(String text) {
|
||||
StringBuilder plain = new StringBuilder(text);
|
||||
StringBuilder reverse = plain.reverse();
|
||||
return reverse.equals(plain);
|
||||
}
|
||||
|
||||
public boolean isPalindromeUsingStringBuffer(String text) {
|
||||
StringBuffer plain = new StringBuffer(text);
|
||||
StringBuffer reverse = plain.reverse();
|
||||
return reverse.equals(plain);
|
||||
}
|
||||
|
||||
public boolean isPalindromeRecursive(String text, int forward, int backward) {
|
||||
if (forward == backward)
|
||||
return true;
|
||||
if ((text.charAt(forward)) != (text.charAt(backward)))
|
||||
return false;
|
||||
if (forward < backward + 1) {
|
||||
return isPalindromeRecursive(text, forward + 1, backward - 1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isPalindromeUsingIntStream(String text) {
|
||||
String temp = text.replaceAll("\\s+", "").toLowerCase();
|
||||
return IntStream.range(0, temp.length() / 2)
|
||||
.noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
package com.baeldung.string;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PalindromeTest {
|
||||
|
||||
private String[] words = {
|
||||
"Anna",
|
||||
"Civic",
|
||||
"Kayak",
|
||||
"Level",
|
||||
"Madam",
|
||||
};
|
||||
|
||||
private String[] sentences = {
|
||||
"Sore was I ere I saw Eros",
|
||||
"Euston saw I was not Sue",
|
||||
"Too hot to hoot",
|
||||
"No mists or frost Simon",
|
||||
"Stella won no wallets"
|
||||
};
|
||||
|
||||
private Palindrome palindrome = new Palindrome();
|
||||
|
||||
@Test
|
||||
public void whenWord_shouldBePalindrome() {
|
||||
for(String word:words)
|
||||
assertTrue(palindrome.isPalindrome(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSentence_shouldBePalindrome() {
|
||||
for(String sentence:sentences)
|
||||
assertTrue(palindrome.isPalindrome(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseWord_shouldBePalindrome() {
|
||||
for(String word:words)
|
||||
assertTrue(palindrome.isPalindromeReverseTheString(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseSentence_shouldBePalindrome() {
|
||||
for(String sentence:sentences)
|
||||
assertTrue(palindrome.isPalindromeReverseTheString(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBuilderWord_shouldBePalindrome() {
|
||||
for(String word:words)
|
||||
assertTrue(palindrome.isPalindromeUsingStringBuilder(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBuilderSentence_shouldBePalindrome() {
|
||||
for(String sentence:sentences)
|
||||
assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBufferWord_shouldBePalindrome() {
|
||||
for(String word:words)
|
||||
assertTrue(palindrome.isPalindromeUsingStringBuffer(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBufferSentence_shouldBePalindrome() {
|
||||
for(String sentence:sentences)
|
||||
|
||||
assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindromeRecursive_wordShouldBePalindrome() {
|
||||
for(String word:words) {
|
||||
word = word.replaceAll("\\s+", "").toLowerCase();
|
||||
int backward = word.length()-1;
|
||||
|
||||
assertTrue(palindrome.isPalindromeRecursive(word,0,backward));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindromeRecursive_sentenceShouldBePalindrome() {
|
||||
for(String sentence:sentences) {
|
||||
sentence = sentence.replaceAll("\\s+", "").toLowerCase();
|
||||
int backward = sentence.length()-1;
|
||||
|
||||
assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindromeStreams_wordShouldBePalindrome() {
|
||||
for(String word:words) {
|
||||
assertTrue(palindrome.isPalindromeUsingIntStream(word));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindromeStreams_sentenceShouldBePalindrome() {
|
||||
for(String sentence:sentences) {
|
||||
assertTrue(palindrome.isPalindromeUsingIntStream(sentence));
|
||||
}
|
||||
}
|
||||
}
|
2
pom.xml
2
pom.xml
|
@ -49,7 +49,9 @@
|
|||
<module>core-java</module>
|
||||
<module>core-java-io</module>
|
||||
<module>core-java-8</module>
|
||||
<!--
|
||||
<module>core-java-concurrency</module>
|
||||
-->
|
||||
<module>couchbase</module>
|
||||
|
||||
<module>deltaspike</module>
|
||||
|
|
|
@ -26,11 +26,17 @@
|
|||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-test</artifactId>
|
||||
<version>${reactor-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<reactor-core.version>3.0.5.RELEASE</reactor-core.version>
|
||||
<reactor-core.version>3.1.3.RELEASE</reactor-core.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -0,0 +1,182 @@
|
|||
package com.baeldung.reactor.core;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
public class CombiningPublishersTest {
|
||||
|
||||
private static Integer min = 1;
|
||||
private static Integer max = 5;
|
||||
|
||||
private static Flux<Integer> evenNumbers = Flux.range(min, max).filter(x -> x % 2 == 0);
|
||||
private static Flux<Integer> oddNumbers = Flux.range(min, max).filter(x -> x % 2 > 0);
|
||||
|
||||
|
||||
@Test
|
||||
public void testMerge() {
|
||||
Flux<Integer> fluxOfIntegers = Flux.merge(
|
||||
evenNumbers,
|
||||
oddNumbers);
|
||||
|
||||
StepVerifier.create(fluxOfIntegers)
|
||||
.expectNext(2)
|
||||
.expectNext(4)
|
||||
.expectNext(1)
|
||||
.expectNext(3)
|
||||
.expectNext(5)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeWithDelayedElements() {
|
||||
Flux<Integer> fluxOfIntegers = Flux.merge(
|
||||
evenNumbers.delayElements(Duration.ofMillis(500L)),
|
||||
oddNumbers.delayElements(Duration.ofMillis(300L)));
|
||||
|
||||
StepVerifier.create(fluxOfIntegers)
|
||||
.expectNext(1)
|
||||
.expectNext(2)
|
||||
.expectNext(3)
|
||||
.expectNext(5)
|
||||
.expectNext(4)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcat() {
|
||||
Flux<Integer> fluxOfIntegers = Flux.concat(
|
||||
evenNumbers.delayElements(Duration.ofMillis(500L)),
|
||||
oddNumbers.delayElements(Duration.ofMillis(300L)));
|
||||
|
||||
StepVerifier.create(fluxOfIntegers)
|
||||
.expectNext(2)
|
||||
.expectNext(4)
|
||||
.expectNext(1)
|
||||
.expectNext(3)
|
||||
.expectNext(5)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcatWith() {
|
||||
Flux<Integer> fluxOfIntegers = evenNumbers
|
||||
.concatWith(oddNumbers);
|
||||
|
||||
StepVerifier.create(fluxOfIntegers)
|
||||
.expectNext(2)
|
||||
.expectNext(4)
|
||||
.expectNext(1)
|
||||
.expectNext(3)
|
||||
.expectNext(5)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCombineLatest() {
|
||||
Flux<Integer> fluxOfIntegers = Flux.combineLatest(
|
||||
evenNumbers,
|
||||
oddNumbers,
|
||||
(a, b) -> a + b);
|
||||
|
||||
StepVerifier.create(fluxOfIntegers)
|
||||
.expectNext(5)
|
||||
.expectNext(7)
|
||||
.expectNext(9)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCombineLatest1() {
|
||||
StepVerifier.create(Flux.combineLatest(obj -> (int) obj[1], evenNumbers, oddNumbers))
|
||||
.expectNext(1)
|
||||
.expectNext(3)
|
||||
.expectNext(5)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeSequential() {
|
||||
Flux<Integer> fluxOfIntegers = Flux.mergeSequential(
|
||||
evenNumbers,
|
||||
oddNumbers);
|
||||
|
||||
StepVerifier.create(fluxOfIntegers)
|
||||
.expectNext(2)
|
||||
.expectNext(4)
|
||||
.expectNext(1)
|
||||
.expectNext(3)
|
||||
.expectNext(5)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMergeDelayError() {
|
||||
Flux<Integer> fluxOfIntegers = Flux.mergeDelayError(1,
|
||||
evenNumbers.delayElements(Duration.ofMillis(500L)),
|
||||
oddNumbers.delayElements(Duration.ofMillis(300L)));
|
||||
|
||||
StepVerifier.create(fluxOfIntegers)
|
||||
.expectNext(1)
|
||||
.expectNext(2)
|
||||
.expectNext(3)
|
||||
.expectNext(5)
|
||||
.expectNext(4)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeWith() {
|
||||
Flux<Integer> fluxOfIntegers = evenNumbers.mergeWith(oddNumbers);
|
||||
|
||||
StepVerifier.create(fluxOfIntegers)
|
||||
.expectNext(2)
|
||||
.expectNext(4)
|
||||
.expectNext(1)
|
||||
.expectNext(3)
|
||||
.expectNext(5)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZip() {
|
||||
Flux<Integer> fluxOfIntegers = Flux.zip(
|
||||
evenNumbers,
|
||||
oddNumbers,
|
||||
(a, b) -> a + b);
|
||||
|
||||
StepVerifier.create(fluxOfIntegers)
|
||||
.expectNext(3)
|
||||
.expectNext(7)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZipWith() {
|
||||
Flux<Integer> fluxOfIntegers = evenNumbers
|
||||
.zipWith(oddNumbers,
|
||||
(a, b) -> a * b);
|
||||
|
||||
StepVerifier.create(fluxOfIntegers)
|
||||
.expectNext(2)
|
||||
.expectNext(12)
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -3,11 +3,11 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>oauth2client</artifactId>
|
||||
<artifactId>auth-client</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>oauth2client</name>
|
||||
<name>auth-client</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
|
@ -4,11 +4,11 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>personresource</artifactId>
|
||||
<artifactId>auth-resource</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>personresource</name>
|
||||
<name>auth-resource</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
|
@ -4,7 +4,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>authserver</artifactId>
|
||||
<artifactId>auth-server</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
|
|
@ -1,8 +1,11 @@
|
|||
package com.baeldung.si;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.hamcrest.core.IsInstanceOf;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
|
@ -21,6 +24,9 @@ import com.baeldung.si.security.SecurityConfig;
|
|||
@ContextConfiguration(classes = { SecurityConfig.class, SecuredDirectChannel.class, MessageConsumer.class })
|
||||
public class TestSpringIntegrationSecurity {
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
@Autowired
|
||||
SubscribableChannel startDirectChannel;
|
||||
|
||||
|
@ -34,34 +40,28 @@ public class TestSpringIntegrationSecurity {
|
|||
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
||||
}
|
||||
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
@Test
|
||||
@WithMockUser(username = "jane", roles = { "LOGGER" })
|
||||
public void givenRoleLogger_whenSendMessageToDirectChannel_thenAccessDenied() throws Throwable {
|
||||
try {
|
||||
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
||||
} catch (Exception e) {
|
||||
throw e.getCause();
|
||||
}
|
||||
public void givenRoleLogger_whenSendMessageToDirectChannel_thenAccessDenied() {
|
||||
expectedException.expectCause(IsInstanceOf.<Throwable> instanceOf(AccessDeniedException.class));
|
||||
|
||||
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
||||
}
|
||||
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
@Test
|
||||
@WithMockUser(username = "jane")
|
||||
public void givenJane_whenSendMessageToDirectChannel_thenAccessDenied() throws Throwable {
|
||||
try {
|
||||
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
||||
} catch (Exception e) {
|
||||
throw e.getCause();
|
||||
}
|
||||
public void givenJane_whenSendMessageToDirectChannel_thenAccessDenied() {
|
||||
expectedException.expectCause(IsInstanceOf.<Throwable> instanceOf(AccessDeniedException.class));
|
||||
|
||||
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
||||
}
|
||||
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
@Test
|
||||
@WithMockUser(roles = { "VIEWER" })
|
||||
public void givenRoleViewer_whenSendToDirectChannel_thenAccessDenied() throws Throwable {
|
||||
try {
|
||||
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
||||
} catch (Exception e) {
|
||||
throw e.getCause();
|
||||
}
|
||||
public void givenRoleViewer_whenSendToDirectChannel_thenAccessDenied() {
|
||||
expectedException.expectCause(IsInstanceOf.<Throwable> instanceOf(AccessDeniedException.class));
|
||||
|
||||
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -78,4 +78,4 @@ public class TestSpringIntegrationSecurity {
|
|||
assertEquals(DIRECT_CHANNEL_MESSAGE, messageConsumer.getMessageContent());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue