fix conflicts
This commit is contained in:
@@ -41,3 +41,4 @@ SpringDataInjectionDemo/.mvn/wrapper/maven-wrapper.properties
|
||||
spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties
|
||||
|
||||
spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
|
||||
/vertx-and-rxjava/.vertx/
|
||||
|
||||
Executable
+194
@@ -0,0 +1,194 @@
|
||||
package com.baeldung.algorithms.string.search;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Random;
|
||||
|
||||
public class StringSearchAlgorithms {
|
||||
public static long getBiggerPrime(int m) {
|
||||
BigInteger prime = BigInteger.probablePrime(getNumberOfBits(m) + 1, new Random());
|
||||
return prime.longValue();
|
||||
}
|
||||
|
||||
public static long getLowerPrime(long number) {
|
||||
BigInteger prime = BigInteger.probablePrime(getNumberOfBits(number) - 1, new Random());
|
||||
return prime.longValue();
|
||||
}
|
||||
|
||||
private static int getNumberOfBits(final int number) {
|
||||
return Integer.SIZE - Integer.numberOfLeadingZeros(number);
|
||||
}
|
||||
|
||||
private static int getNumberOfBits(final long number) {
|
||||
return Long.SIZE - Long.numberOfLeadingZeros(number);
|
||||
}
|
||||
|
||||
public static int simpleTextSearch(char[] pattern, char[] text) {
|
||||
int patternSize = pattern.length;
|
||||
int textSize = text.length;
|
||||
|
||||
int i = 0;
|
||||
|
||||
while ((i + patternSize) <= textSize) {
|
||||
int j = 0;
|
||||
while (text[i + j] == pattern[j]) {
|
||||
j += 1;
|
||||
if (j >= patternSize)
|
||||
return i;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int RabinKarpMethod(char[] pattern, char[] text) {
|
||||
int patternSize = pattern.length; // m
|
||||
int textSize = text.length; // n
|
||||
|
||||
long prime = getBiggerPrime(patternSize);
|
||||
|
||||
long r = 1;
|
||||
for (int i = 0; i < patternSize - 1; i++) {
|
||||
r *= 2;
|
||||
r = r % prime;
|
||||
}
|
||||
|
||||
long[] t = new long[textSize];
|
||||
t[0] = 0;
|
||||
|
||||
long pfinger = 0;
|
||||
|
||||
for (int j = 0; j < patternSize; j++) {
|
||||
t[0] = (2 * t[0] + text[j]) % prime;
|
||||
pfinger = (2 * pfinger + pattern[j]) % prime;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
boolean passed = false;
|
||||
|
||||
int diff = textSize - patternSize;
|
||||
for (i = 0; i <= diff; i++) {
|
||||
if (t[i] == pfinger) {
|
||||
passed = true;
|
||||
for (int k = 0; k < patternSize; k++) {
|
||||
if (text[i + k] != pattern[k]) {
|
||||
passed = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (passed) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
if (i < diff) {
|
||||
long value = 2 * (t[i] - r * text[i]) + text[i + patternSize];
|
||||
t[i + 1] = ((value % prime) + prime) % prime;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
public static int KnuthMorrisPrattSearch(char[] pattern, char[] text) {
|
||||
int patternSize = pattern.length; // m
|
||||
int textSize = text.length; // n
|
||||
|
||||
int i = 0, j = 0;
|
||||
|
||||
int[] shift = KnuthMorrisPrattShift(pattern);
|
||||
|
||||
while ((i + patternSize) <= textSize) {
|
||||
while (text[i + j] == pattern[j]) {
|
||||
j += 1;
|
||||
if (j >= patternSize)
|
||||
return i;
|
||||
}
|
||||
|
||||
if (j > 0) {
|
||||
i += shift[j - 1];
|
||||
j = Math.max(j - shift[j - 1], 0);
|
||||
} else {
|
||||
i++;
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int[] KnuthMorrisPrattShift(char[] pattern) {
|
||||
int patternSize = pattern.length;
|
||||
|
||||
int[] shift = new int[patternSize];
|
||||
shift[0] = 1;
|
||||
|
||||
int i = 1, j = 0;
|
||||
|
||||
while ((i + j) < patternSize) {
|
||||
if (pattern[i + j] == pattern[j]) {
|
||||
shift[i + j] = i;
|
||||
j++;
|
||||
} else {
|
||||
if (j == 0)
|
||||
shift[i] = i + 1;
|
||||
|
||||
if (j > 0) {
|
||||
i = i + shift[j - 1];
|
||||
j = Math.max(j - shift[j - 1], 0);
|
||||
} else {
|
||||
i = i + 1;
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return shift;
|
||||
}
|
||||
|
||||
public static int BoyerMooreHorspoolSimpleSearch(char[] pattern, char[] text) {
|
||||
int patternSize = pattern.length;
|
||||
int textSize = text.length;
|
||||
|
||||
int i = 0, j = 0;
|
||||
|
||||
while ((i + patternSize) <= textSize) {
|
||||
j = patternSize - 1;
|
||||
while (text[i + j] == pattern[j]) {
|
||||
j--;
|
||||
if (j < 0)
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int BoyerMooreHorspoolSearch(char[] pattern, char[] text) {
|
||||
|
||||
int shift[] = new int[256];
|
||||
|
||||
for (int k = 0; k < 256; k++) {
|
||||
shift[k] = pattern.length;
|
||||
}
|
||||
|
||||
for (int k = 0; k < pattern.length - 1; k++) {
|
||||
shift[pattern[k]] = pattern.length - 1 - k;
|
||||
}
|
||||
|
||||
int i = 0, j = 0;
|
||||
|
||||
while ((i + pattern.length) <= text.length) {
|
||||
j = pattern.length - 1;
|
||||
|
||||
while (text[i + j] == pattern[j]) {
|
||||
j -= 1;
|
||||
if (j < 0)
|
||||
return i;
|
||||
}
|
||||
|
||||
i = i + shift[text[i + pattern.length - 1]];
|
||||
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package algorithms;
|
||||
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.string.search.StringSearchAlgorithms;
|
||||
|
||||
public class StringSearchAlgorithmsTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testStringSearchAlgorithms(){
|
||||
String text = "This is some nice text.";
|
||||
String pattern = "some";
|
||||
|
||||
int realPosition = text.indexOf(pattern);
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.simpleTextSearch(pattern.toCharArray(), text.toCharArray()));
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.RabinKarpMethod(pattern.toCharArray(), text.toCharArray()));
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.KnuthMorrisPrattSearch(pattern.toCharArray(), text.toCharArray()));
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.BoyerMooreHorspoolSimpleSearch(pattern.toCharArray(), text.toCharArray()));
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.BoyerMooreHorspoolSearch(pattern.toCharArray(), text.toCharArray()));
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.concurrent.callable;
|
||||
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class FactorialTask implements Callable<Integer> {
|
||||
int number;
|
||||
|
||||
public FactorialTask(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public Integer call() throws InvalidParamaterException {
|
||||
int fact=1;
|
||||
if(number < 0)
|
||||
throw new InvalidParamaterException("Number must be positive");
|
||||
|
||||
for(int count=number;count>1;count--){
|
||||
fact=fact * count;
|
||||
}
|
||||
|
||||
return fact;
|
||||
}
|
||||
|
||||
private class InvalidParamaterException extends Exception {
|
||||
public InvalidParamaterException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.concurrent.callable;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class FactorialTaskManualTest {
|
||||
|
||||
private ExecutorService executorService;
|
||||
|
||||
@Before
|
||||
public void setup(){
|
||||
executorService = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTaskSubmitted_ThenFutureResultObtained() throws ExecutionException, InterruptedException {
|
||||
FactorialTask task = new FactorialTask(5);
|
||||
Future<Integer> future= executorService.submit(task);
|
||||
assertEquals(120,future.get().intValue());
|
||||
}
|
||||
|
||||
@Test(expected = ExecutionException.class)
|
||||
public void whenException_ThenCallableThrowsIt() throws ExecutionException, InterruptedException {
|
||||
FactorialTask task = new FactorialTask(-5);
|
||||
Future<Integer> future= executorService.submit(task);
|
||||
Integer result=future.get().intValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenException_ThenCallableDoesntThrowsItIfGetIsNotCalled(){
|
||||
FactorialTask task = new FactorialTask(-5);
|
||||
Future<Integer> future= executorService.submit(task);
|
||||
assertEquals(false,future.isDone());
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup(){
|
||||
executorService.shutdown();
|
||||
}
|
||||
}
|
||||
@@ -162,6 +162,12 @@
|
||||
<version>${avaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
@@ -186,6 +192,16 @@
|
||||
<artifactId>fscontext</artifactId>
|
||||
<version>${fscontext.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.codepoetics</groupId>
|
||||
<artifactId>protonpack</artifactId>
|
||||
<version>${protonpack.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>one.util</groupId>
|
||||
<artifactId>streamex</artifactId>
|
||||
<version>${streamex.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -408,12 +424,16 @@
|
||||
<grep4j.version>1.8.7</grep4j.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
<fscontext.version>4.6-b01</fscontext.version>
|
||||
<protonpack.version>1.13</protonpack.version>
|
||||
<streamex.version>0.6.5</streamex.version>
|
||||
|
||||
<!-- testing -->
|
||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito.version>2.8.9</mockito.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
<spring-core.version>4.3.10.RELEASE</spring-core.version>
|
||||
|
||||
|
||||
<!-- maven plugins -->
|
||||
|
||||
+11
-4
@@ -5,9 +5,16 @@ import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||
public class AdapterPatternDriver {
|
||||
|
||||
public static void main(String args[]) {
|
||||
LuxuryCarsSpeedAdapter luxuryCars = new LuxuryCarsSpeedAdapterImpl();
|
||||
LOG.info("Bugatti Veyron Super Sport's top speed is " + luxuryCars.bugattiVeyronInKMPH() + " Kmph.");
|
||||
LOG.info("McLaren F1 top speed is " + luxuryCars.mcLarenInKMPH() + " Kmph.");
|
||||
LOG.info("Aston Martin One-77 top speed is " + luxuryCars.astonMartinInKMPH() + " Kmph.");
|
||||
LuxuryCars bugattiVeyron = new BugattiVeyron();
|
||||
LuxuryCarsAdapter bugattiVeyronAdapter = new LuxuryCarsAdapterImpl(bugattiVeyron);
|
||||
LOG.info("Bugatti Veyron Super Sport's top speed is " + bugattiVeyronAdapter.speedInKMPH() + " Kmph.");
|
||||
|
||||
LuxuryCars mcLaren = new McLaren();
|
||||
LuxuryCarsAdapter mcLarenAdapter = new LuxuryCarsAdapterImpl(mcLaren);
|
||||
LOG.info("McLaren F1 top speed is " + mcLarenAdapter.speedInKMPH() + " Kmph.");
|
||||
|
||||
LuxuryCars astonMartin = new AstonMartin();
|
||||
LuxuryCarsAdapter astonMartinAdapter = new LuxuryCarsAdapterImpl(astonMartin);
|
||||
LOG.info("McLaren F1 top speed is " + astonMartinAdapter.speedInKMPH() + " Kmph.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class AstonMartin implements LuxuryCars {
|
||||
@Override
|
||||
public double speedInMPH() {
|
||||
return 220;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class BugattiVeyron implements LuxuryCars {
|
||||
@Override
|
||||
public double speedInMPH() {
|
||||
return 268;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public interface LuxuryCars {
|
||||
public double speedInMPH();
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public interface LuxuryCarsAdapter {
|
||||
public double speedInKMPH();
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class LuxuryCarsAdapterImpl implements LuxuryCarsAdapter {
|
||||
private LuxuryCars luxuryCars;
|
||||
|
||||
public LuxuryCarsAdapterImpl(LuxuryCars luxuryCars) {
|
||||
this.luxuryCars = luxuryCars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double speedInKMPH() {
|
||||
double mph = luxuryCars.speedInMPH();
|
||||
return convertMPHtoKMPH(mph);
|
||||
}
|
||||
|
||||
private double convertMPHtoKMPH(double mph) {
|
||||
return mph * 1.60934;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class LuxuryCarsSpeed {
|
||||
public double bugattiVeyronInMPH() {
|
||||
return 268;
|
||||
}
|
||||
|
||||
public double mcLarenInMPH() {
|
||||
return 241;
|
||||
}
|
||||
|
||||
public double astonMartinInMPH() {
|
||||
return 220;
|
||||
}
|
||||
}
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public interface LuxuryCarsSpeedAdapter {
|
||||
public double bugattiVeyronInKMPH();
|
||||
|
||||
public double mcLarenInKMPH();
|
||||
|
||||
public double astonMartinInKMPH();
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class LuxuryCarsSpeedAdapterImpl extends LuxuryCarsSpeed implements LuxuryCarsSpeedAdapter {
|
||||
|
||||
@Override
|
||||
public double bugattiVeyronInKMPH() {
|
||||
double mph = super.bugattiVeyronInMPH();
|
||||
return convertMPHtoKMPH(mph);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double mcLarenInKMPH() {
|
||||
double mph = super.mcLarenInMPH();
|
||||
return convertMPHtoKMPH(mph);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double astonMartinInKMPH() {
|
||||
double mph = super.astonMartinInMPH();
|
||||
return convertMPHtoKMPH(mph);
|
||||
}
|
||||
|
||||
private double convertMPHtoKMPH(double mph) {
|
||||
return mph * 1.60934;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class McLaren implements LuxuryCars {
|
||||
@Override
|
||||
public double speedInMPH() {
|
||||
return 241;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import com.codepoetics.protonpack.Indexed;
|
||||
import com.codepoetics.protonpack.StreamUtils;
|
||||
|
||||
public class StreamIndices {
|
||||
|
||||
public static List<String> getEvenIndexedStrings(String[] names) {
|
||||
List<String> evenIndexedNames = IntStream.range(0, names.length)
|
||||
.filter(i -> i % 2 == 0).mapToObj(i -> names[i])
|
||||
.collect(Collectors.toList());
|
||||
return evenIndexedNames;
|
||||
}
|
||||
|
||||
public static List<Indexed<String>> getEvenIndexedStrings(List<String> names) {
|
||||
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
|
||||
.filter(i -> i.getIndex() % 2 == 0).collect(Collectors.toList());
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<Indexed<String>> getOddIndexedStrings(List<String> names) {
|
||||
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream())
|
||||
.filter(i -> i.getIndex() % 2 == 1).collect(Collectors.toList());
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<String> getOddIndexedStrings(String[] names) {
|
||||
List<String> oddIndexedNames = IntStream.range(0, names.length)
|
||||
.filter(i -> i % 2 == 1).mapToObj(i -> names[i])
|
||||
.collect(Collectors.toList());
|
||||
return oddIndexedNames;
|
||||
}
|
||||
}
|
||||
+8
-8
@@ -1,20 +1,20 @@
|
||||
package com.baeldung.designpatterns;
|
||||
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.designpatterns.adapter.LuxuryCarsSpeedAdapter;
|
||||
import com.baeldung.designpatterns.adapter.LuxuryCarsSpeedAdapterImpl;
|
||||
import com.baeldung.designpatterns.adapter.AstonMartin;
|
||||
import com.baeldung.designpatterns.adapter.BugattiVeyron;
|
||||
import com.baeldung.designpatterns.adapter.LuxuryCarsAdapterImpl;
|
||||
import com.baeldung.designpatterns.adapter.McLaren;
|
||||
|
||||
public class AdapterPatternIntegrationTest {
|
||||
@Test
|
||||
public void givenLuxuryCarsAdapter_WhenConvertingMPHToKMPH_thenSuccessfullyConverted() {
|
||||
LuxuryCarsSpeedAdapter luxuryCars = new LuxuryCarsSpeedAdapterImpl();
|
||||
assertEquals(luxuryCars.bugattiVeyronInKMPH(), 431.30312, 0.00001);
|
||||
assertEquals(luxuryCars.mcLarenInKMPH(), 387.85094, 0.00001);
|
||||
assertEquals(luxuryCars.astonMartinInKMPH(), 354.0548, 0.00001);
|
||||
assertEquals(new LuxuryCarsAdapterImpl(new BugattiVeyron()).speedInKMPH(), 431.30312, 0.00001);
|
||||
assertEquals(new LuxuryCarsAdapterImpl(new McLaren()).speedInKMPH(), 387.85094, 0.00001);
|
||||
assertEquals(new LuxuryCarsAdapterImpl(new AstonMartin()).speedInKMPH(), 354.0548, 0.00001);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.codepoetics.protonpack.Indexed;
|
||||
|
||||
public class StreamIndicesTest {
|
||||
|
||||
@Test
|
||||
public void givenArray_whenGetIndexedStrings_thenReturnListOfEvenIndexedStrings() {
|
||||
String[] names = { "Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim" };
|
||||
List<String> expectedResult = Arrays.asList("Afrim", "Besim", "Durim");
|
||||
List<String> actualResult = StreamIndices.getEvenIndexedStrings(names);
|
||||
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArray_whenGetIndexedStrings_thenReturnListOfOddStrings() {
|
||||
String[] names = { "Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim" };
|
||||
List<String> expectedResult = Arrays.asList("Bashkim", "Lulzim", "Shpetim");
|
||||
List<String> actualResult = StreamIndices.getOddIndexedStrings(names);
|
||||
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenGetIndexedStrings_thenReturnListOfEvenIndexedStrings() {
|
||||
List<String> names = Arrays.asList("Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim");
|
||||
List<Indexed<String>> expectedResult = Arrays.asList(Indexed.index(0, "Afrim"), Indexed.index(2, "Besim"), Indexed.index(4, "Durim"));
|
||||
List<Indexed<String>> actualResult = StreamIndices.getEvenIndexedStrings(names);
|
||||
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenGetIndexedStrings_thenReturnListOfOddIndexedStrings() {
|
||||
List<String> names = Arrays.asList("Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim");
|
||||
List<Indexed<String>> expectedResult = Arrays.asList(Indexed.index(1, "Bashkim"), Indexed.index(3, "Lulzim"), Indexed.index(5, "Shpetim"));
|
||||
List<Indexed<String>> actualResult = StreamIndices.getOddIndexedStrings(names);
|
||||
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package org.baeldung.java.io;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.FileSystemUtils;
|
||||
|
||||
public class JavaDirectoryDeleteUnitTest {
|
||||
private static Path TEMP_DIRECTORY;
|
||||
private static final String DIRECTORY_NAME = "toBeDeleted";
|
||||
|
||||
public static final List<String> ALL_LINES = Arrays.asList(new String[] { "This is line 1", "This is line 2", "This is line 3", "This is line 4", "This is line 5", "This is line 6" });
|
||||
|
||||
@BeforeClass
|
||||
public static void initializeTempDirectory() throws IOException {
|
||||
TEMP_DIRECTORY = Files.createTempDirectory("tmpForJUnit");
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanTempDirectory() throws IOException {
|
||||
FileUtils.deleteDirectory(TEMP_DIRECTORY.toFile());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setupDirectory() throws IOException {
|
||||
Path tempPathForEachTest = Files.createDirectory(TEMP_DIRECTORY.resolve(DIRECTORY_NAME));
|
||||
|
||||
// Create a directory structure
|
||||
Files.write(tempPathForEachTest.resolve("file1.txt"), ALL_LINES.subList(0, 2));
|
||||
Files.write(tempPathForEachTest.resolve("file2.txt"), ALL_LINES.subList(2, 4));
|
||||
|
||||
Files.createDirectories(tempPathForEachTest.resolve("Empty"));
|
||||
|
||||
Path aSubDir = Files.createDirectories(tempPathForEachTest.resolve("notEmpty"));
|
||||
Files.write(aSubDir.resolve("file3.txt"), ALL_LINES.subList(3, 5));
|
||||
Files.write(aSubDir.resolve("file4.txt"), ALL_LINES.subList(0, 3));
|
||||
|
||||
aSubDir = Files.createDirectories(aSubDir.resolve("anotherSubDirectory"));
|
||||
Files.write(aSubDir.resolve("file5.txt"), ALL_LINES.subList(4, 5));
|
||||
Files.write(aSubDir.resolve("file6.txt"), ALL_LINES.subList(0, 2));
|
||||
}
|
||||
|
||||
@After
|
||||
public void checkAndCleanupIfRequired() throws IOException {
|
||||
Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
|
||||
if (Files.exists(pathToBeDeleted)) {
|
||||
FileUtils.deleteDirectory(pathToBeDeleted.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
boolean deleteDirectory(File directoryToBeDeleted) {
|
||||
File[] allContents = directoryToBeDeleted.listFiles();
|
||||
|
||||
for (File file : allContents) {
|
||||
if (file.isDirectory()) {
|
||||
deleteDirectory(file);
|
||||
} else {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
return directoryToBeDeleted.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectory_whenDeletedWithRecursion_thenIsGone() throws IOException {
|
||||
Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
|
||||
|
||||
boolean result = deleteDirectory(pathToBeDeleted.toFile());
|
||||
|
||||
assertTrue("Could not delete directory", result);
|
||||
assertFalse("Directory still exists", Files.exists(pathToBeDeleted));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectory_whenDeletedWithCommonsIOFileUtils_thenIsGone() throws IOException {
|
||||
Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
|
||||
|
||||
FileUtils.deleteDirectory(pathToBeDeleted.toFile());
|
||||
|
||||
assertFalse("Directory still exists", Files.exists(pathToBeDeleted));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectory_whenDeletedWithSpringFileSystemUtils_thenIsGone() throws IOException {
|
||||
Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
|
||||
|
||||
boolean result = FileSystemUtils.deleteRecursively(pathToBeDeleted.toFile());
|
||||
|
||||
assertTrue("Could not delete directory", result);
|
||||
assertFalse("Directory still exists", Files.exists(pathToBeDeleted));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectory_whenDeletedWithFilesWalk_thenIsGone() throws IOException {
|
||||
Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
|
||||
|
||||
Files.walk(pathToBeDeleted)
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
|
||||
assertFalse("Directory still exists", Files.exists(pathToBeDeleted));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectory_whenDeletedWithNIO2WalkFileTree_thenIsGone() throws IOException {
|
||||
Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
|
||||
|
||||
Files.walkFileTree(pathToBeDeleted, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
||||
Files.delete(dir);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
Files.delete(file);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
|
||||
assertFalse("Directory still exists", Files.exists(pathToBeDeleted));
|
||||
}
|
||||
}
|
||||
+1
-3
@@ -1,7 +1,5 @@
|
||||
package com.baeldung.ejbclient.application;
|
||||
package com.baeldung.ejb.wildfly;
|
||||
|
||||
import com.baeldung.ejbmodule.TextProcessorBean;
|
||||
import com.baeldung.ejbmodule.TextProcessorRemote;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.ejbclient.application;
|
||||
package com.baeldung.ejb.wildfly;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.ejbmodule;
|
||||
package com.baeldung.ejb.wildfly;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.ejbmodule;
|
||||
package com.baeldung.ejb.wildfly;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
endpoint.name=client-endpoint
|
||||
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
|
||||
remote.connections=default
|
||||
remote.connection.default.host=127.0.0.1
|
||||
remote.connection.default.port=8080
|
||||
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
|
||||
remote.connection.default.username=myusername
|
||||
remote.connection.default.password=mypassword
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.ejbmodule;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TextProcessorBeanTest {
|
||||
@Test
|
||||
public void givenInputString_whenComparedToStringParsedByBean_thenSuccessful() {
|
||||
TextProcessorBean textProcessor = new TextProcessorBean();
|
||||
assertEquals("TEST", textProcessor.processText("test"));
|
||||
}
|
||||
}
|
||||
@@ -81,6 +81,5 @@
|
||||
<module>ejb-remote</module>
|
||||
<module>ejb-client</module>
|
||||
<module>ejb-session-beans</module>
|
||||
<module>ejb-session-beans-client</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"query": "mutation($name: String! $email: String! $age: String! ){ createUser ( name: $name email: $email age: $age) { id name email age } }",
|
||||
"parameters": {
|
||||
"name": "John",
|
||||
"email": "[email protected]",
|
||||
"age": 34
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"query": "mutation($name: String! ){ deleteUser ( id: $id) { id name email age} }",
|
||||
"parameters": {
|
||||
"id": 2
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"query": "{ listUsers{ id name email age}}",
|
||||
"parameters": {}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user