Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -85,5 +85,6 @@ transaction.log
|
||||
*-shell.log
|
||||
|
||||
apache-cxf/cxf-aegis/baeldung.xml
|
||||
testing-modules/report-*.json
|
||||
|
||||
libraries-2/*.db
|
||||
@@ -11,3 +11,4 @@ This module contains articles about searching algorithms.
|
||||
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](https://www.baeldung.com/java-monte-carlo-tree-search)
|
||||
- [Range Search Algorithm in Java](https://www.baeldung.com/java-range-search)
|
||||
- [Fast Pattern Matching of Strings Using Suffix Tree](https://www.baeldung.com/java-pattern-matching-suffix-tree)
|
||||
- [Topological Sort of Directed Acyclic Graph](https://www.baeldung.com/cs/dag-topological-sort)
|
||||
|
||||
@@ -7,4 +7,3 @@ This module contains articles about Apache CXF
|
||||
- [Apache CXF Support for RESTful Web Services](https://www.baeldung.com/apache-cxf-rest-api)
|
||||
- [A Guide to Apache CXF with Spring](https://www.baeldung.com/apache-cxf-with-spring)
|
||||
- [Introduction to Apache CXF](https://www.baeldung.com/introduction-to-apache-cxf)
|
||||
- [Server-Sent Events (SSE) In JAX-RS](https://www.baeldung.com/java-ee-jax-rs-sse)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Server-Sent Events (SSE) In JAX-RS](https://www.baeldung.com/java-ee-jax-rs-sse)
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.poi.excel.setformula;
|
||||
|
||||
import org.apache.poi.xssf.usermodel.XSSFCell;
|
||||
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ExcelFormula {
|
||||
public double setFormula(String fileLocation, XSSFWorkbook wb, String formula) throws IOException {
|
||||
XSSFSheet sheet = wb.getSheetAt(0);
|
||||
int lastCellNum = sheet.getRow(0).getLastCellNum();
|
||||
XSSFCell formulaCell = sheet.getRow(0).createCell(lastCellNum);
|
||||
formulaCell.setCellFormula(formula);
|
||||
XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
|
||||
formulaEvaluator.evaluateFormulaCell(formulaCell);
|
||||
FileOutputStream fileOut = new FileOutputStream(new File(fileLocation));
|
||||
wb.write(fileOut);
|
||||
wb.close();
|
||||
fileOut.close();
|
||||
return formulaCell.getNumericCellValue();
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.poi.excel.setformula;
|
||||
|
||||
import org.apache.poi.ss.util.CellReference;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
class ExcelFormulaUnitTest {
|
||||
private static String FILE_NAME = "com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx";
|
||||
private String fileLocation;
|
||||
private ExcelFormula excelFormula;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() throws URISyntaxException {
|
||||
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString();
|
||||
excelFormula = new ExcelFormula();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenExcelData_whenSetFormula_thenSuccess() throws IOException {
|
||||
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
|
||||
XSSFWorkbook wb = new XSSFWorkbook(inputStream);
|
||||
XSSFSheet sheet = wb.getSheetAt(0);
|
||||
double resultColumnA = 0;
|
||||
double resultColumnB = 0;
|
||||
for (int row = 0; row <= sheet.getLastRowNum(); row++) {
|
||||
resultColumnA += sheet.getRow(row).getCell(0).getNumericCellValue();
|
||||
resultColumnB += sheet.getRow(row).getCell(1).getNumericCellValue();
|
||||
}
|
||||
String colNameA = CellReference.convertNumToColString(0);
|
||||
String colNameB = CellReference.convertNumToColString(1);
|
||||
String startCellA = colNameA + 1;
|
||||
String stopCellA = colNameA + (sheet.getLastRowNum() + 1);
|
||||
String sumFormulaForColumnA = String.format("SUM(%s:%s)", startCellA, stopCellA);
|
||||
String startCellB = colNameB + 1;
|
||||
String stopCellB = colNameB + (sheet.getLastRowNum() + 1);
|
||||
String sumFormulaForColumnB = String.format("SUM(%s:%s)", startCellB, stopCellB);
|
||||
|
||||
double resultValue = excelFormula.setFormula(fileLocation, wb, sumFormulaForColumnA + "-" + sumFormulaForColumnB);
|
||||
|
||||
Assert.assertEquals(resultColumnA - resultColumnB, resultValue, 0d);
|
||||
}
|
||||
}
|
||||
@@ -14,3 +14,4 @@ This module contains articles about core Java features that have been introduced
|
||||
- [Java 9 Reactive Streams](https://www.baeldung.com/java-9-reactive-streams)
|
||||
- [Multi-Release JAR Files with Maven](https://www.baeldung.com/maven-multi-release-jars)
|
||||
- [The Difference between RxJava API and the Java 9 Flow API](https://www.baeldung.com/rxjava-vs-java-flow-api)
|
||||
- [How to Get a Name of a Method Being Executed?](https://www.baeldung.com/java-name-of-executing-method)
|
||||
|
||||
+39
@@ -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);
|
||||
}
|
||||
}
|
||||
+30
@@ -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();
|
||||
}
|
||||
}
|
||||
+26
@@ -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();
|
||||
}
|
||||
}
|
||||
+18
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+33
@@ -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();
|
||||
}
|
||||
}
|
||||
+30
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,12 @@
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package com.baeldung.list.difference;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class FindDifferencesBetweenListsUnitTest {
|
||||
|
||||
private static final List<String> listOne = Arrays.asList("Jack", "Tom", "Sam", "John", "James", "Jack");
|
||||
private static final List<String> listTwo = Arrays.asList("Jack", "Daniel", "Sam", "Alan", "James", "George");
|
||||
|
||||
@Test
|
||||
public void givenLists_whenUsingPlainJavaImpl_thenDifferencesAreFound() {
|
||||
List<String> differences = new ArrayList<>(listOne);
|
||||
differences.removeAll(listTwo);
|
||||
assertEquals(2, differences.size());
|
||||
assertThat(differences).containsExactly("Tom", "John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenReverseLists_whenUsingPlainJavaImpl_thenDifferencesAreFound() {
|
||||
List<String> differences = new ArrayList<>(listTwo);
|
||||
differences.removeAll(listOne);
|
||||
assertEquals(3, differences.size());
|
||||
assertThat(differences).containsExactly("Daniel", "Alan", "George");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLists_whenUsingJavaStreams_thenDifferencesAreFound() {
|
||||
List<String> differences = listOne.stream()
|
||||
.filter(element -> !listTwo.contains(element))
|
||||
.collect(Collectors.toList());
|
||||
assertEquals(2, differences.size());
|
||||
assertThat(differences).containsExactly("Tom", "John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenReverseLists_whenUsingJavaStreams_thenDifferencesAreFound() {
|
||||
List<String> differences = listTwo.stream()
|
||||
.filter(element -> !listOne.contains(element))
|
||||
.collect(Collectors.toList());
|
||||
assertEquals(3, differences.size());
|
||||
assertThat(differences).containsExactly("Daniel", "Alan", "George");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLists_whenUsingGoogleGuava_thenDifferencesAreFound() {
|
||||
List<String> differences = new ArrayList<>(Sets.difference(Sets.newHashSet(listOne), Sets.newHashSet(listTwo)));
|
||||
assertEquals(2, differences.size());
|
||||
assertThat(differences).containsExactlyInAnyOrder("Tom", "John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenReverseLists_whenUsingGoogleGuava_thenDifferencesAreFound() {
|
||||
List<String> differences = new ArrayList<>(Sets.difference(Sets.newHashSet(listTwo), Sets.newHashSet(listOne)));
|
||||
assertEquals(3, differences.size());
|
||||
assertThat(differences).containsExactlyInAnyOrder("Daniel", "Alan", "George");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLists_whenUsingApacheCommons_thenDifferencesAreFound() {
|
||||
List<String> differences = new ArrayList<>((CollectionUtils.removeAll(listOne, listTwo)));
|
||||
assertEquals(2, differences.size());
|
||||
assertThat(differences).containsExactly("Tom", "John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenReverseLists_whenUsingApacheCommons_thenDifferencesAreFound() {
|
||||
List<String> differences = new ArrayList<>((CollectionUtils.removeAll(listTwo, listOne)));
|
||||
assertEquals(3, differences.size());
|
||||
assertThat(differences).containsExactly("Daniel", "Alan", "George");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLists_whenUsingPlainJavaImpl_thenDifferencesWithDuplicatesAreFound() {
|
||||
List<String> differences = new ArrayList<>(listOne);
|
||||
listTwo.forEach(differences::remove);
|
||||
assertThat(differences).containsExactly("Tom", "John", "Jack");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLists_whenUsingApacheCommons_thenDifferencesWithDuplicatesAreFound() {
|
||||
List<String> differences = new ArrayList<>(CollectionUtils.subtract(listOne, listTwo));
|
||||
assertEquals(3, differences.size());
|
||||
assertThat(differences).containsExactly("Tom", "John", "Jack");
|
||||
}
|
||||
|
||||
}
|
||||
+9
-1
@@ -15,7 +15,15 @@ import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class PrimeNumbersUnitManualTest {
|
||||
/**
|
||||
* This test expects the file target/test-classes/META-INF/BenchmarkList to be present.
|
||||
*
|
||||
* Before running the test ensure that the file is present.
|
||||
* If not, please run mvn install on the module.
|
||||
*
|
||||
*/
|
||||
|
||||
public class PrimeNumbersManualTest {
|
||||
|
||||
private static Logger logger = Logger.getAnonymousLogger();
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.threadlocal;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.RejectedExecutionHandler;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ThreadLocalAwareThreadPool extends ThreadPoolExecutor {
|
||||
|
||||
public ThreadLocalAwareThreadPool(int corePoolSize,
|
||||
int maximumPoolSize,
|
||||
long keepAliveTime,
|
||||
TimeUnit unit,
|
||||
BlockingQueue<Runnable> workQueue,
|
||||
ThreadFactory threadFactory,
|
||||
RejectedExecutionHandler handler) {
|
||||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterExecute(Runnable r, Throwable t) {
|
||||
// Call remove on each ThreadLocal
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
test-link*
|
||||
0.*
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.copydirectory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
public class ApacheCommons {
|
||||
|
||||
public static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) throws IOException {
|
||||
File sourceDirectory = new File(sourceDirectoryLocation);
|
||||
File destinationDirectory = new File(destinationDirectoryLocation);
|
||||
FileUtils.copyDirectory(sourceDirectory, destinationDirectory);
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.copydirectory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class CoreOld {
|
||||
|
||||
public static void copyDirectoryJavaUnder7(File source, File destination) throws IOException {
|
||||
if (source.isDirectory()) {
|
||||
copyDirectory(source, destination);
|
||||
} else {
|
||||
copyFile(source, destination);
|
||||
}
|
||||
}
|
||||
|
||||
private static void copyDirectory(File sourceDirectory, File destinationDirectory) throws IOException {
|
||||
if (!destinationDirectory.exists()) {
|
||||
destinationDirectory.mkdir();
|
||||
}
|
||||
for (String f : sourceDirectory.list()) {
|
||||
copyDirectoryJavaUnder7(new File(sourceDirectory, f), new File(destinationDirectory, f));
|
||||
}
|
||||
}
|
||||
|
||||
private static void copyFile(File sourceFile, File destinationFile) throws IOException {
|
||||
try (InputStream in = new FileInputStream(sourceFile); OutputStream out = new FileOutputStream(destinationFile)) {
|
||||
byte[] buf = new byte[1024];
|
||||
int length;
|
||||
while ((length = in.read(buf)) > 0) {
|
||||
out.write(buf, 0, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.copydirectory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class JavaNio {
|
||||
|
||||
public static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) throws IOException {
|
||||
Files.walk(Paths.get(sourceDirectoryLocation))
|
||||
.forEach(source -> {
|
||||
Path destination = Paths.get(destinationDirectoryLocation, source.toString()
|
||||
.substring(sourceDirectoryLocation.length()));
|
||||
try {
|
||||
Files.copy(source, destination);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.copydirectory;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ApacheCommonsUnitTest {
|
||||
|
||||
private final String sourceDirectoryLocation = "src/test/resources/sourceDirectory3";
|
||||
private final String subDirectoryName = "/childDirectory";
|
||||
private final String fileName = "/file.txt";
|
||||
private final String destinationDirectoryLocation = "src/test/resources/destinationDirectory3";
|
||||
|
||||
@BeforeEach
|
||||
public void createDirectoryWithSubdirectoryAndFile() throws IOException {
|
||||
Files.createDirectories(Paths.get(sourceDirectoryLocation));
|
||||
Files.createDirectories(Paths.get(sourceDirectoryLocation + subDirectoryName));
|
||||
Files.createFile(Paths.get(sourceDirectoryLocation + subDirectoryName + fileName));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSourceDirectoryExists_thenDirectoryIsFullyCopied() throws IOException {
|
||||
ApacheCommons.copyDirectory(sourceDirectoryLocation, destinationDirectoryLocation);
|
||||
|
||||
assertTrue(new File(destinationDirectoryLocation).exists());
|
||||
assertTrue(new File(destinationDirectoryLocation + subDirectoryName).exists());
|
||||
assertTrue(new File(destinationDirectoryLocation + subDirectoryName + fileName).exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSourceDirectoryDoesNotExist_thenExceptionIsThrown() {
|
||||
assertThrows(Exception.class, () -> ApacheCommons.copyDirectory("nonExistingDirectory", destinationDirectoryLocation));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void cleanUp() throws IOException {
|
||||
Files.walk(Paths.get(sourceDirectoryLocation))
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
if (new File(destinationDirectoryLocation).exists()) {
|
||||
Files.walk(Paths.get(destinationDirectoryLocation))
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.copydirectory;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CoreOldUnitTest {
|
||||
|
||||
private final String sourceDirectoryLocation = "src/test/resources/sourceDirectory1";
|
||||
private final String subDirectoryName = "/childDirectory";
|
||||
private final String fileName = "/file.txt";
|
||||
private final String destinationDirectoryLocation = "src/test/resources/destinationDirectory1";
|
||||
|
||||
@BeforeEach
|
||||
public void createDirectoryWithSubdirectoryAndFile() throws IOException {
|
||||
Files.createDirectories(Paths.get(sourceDirectoryLocation));
|
||||
Files.createDirectories(Paths.get(sourceDirectoryLocation + subDirectoryName));
|
||||
Files.createFile(Paths.get(sourceDirectoryLocation + subDirectoryName + fileName));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSourceDirectoryExists_thenDirectoryIsFullyCopied() throws IOException {
|
||||
File sourceDirectory = new File(sourceDirectoryLocation);
|
||||
File destinationDirectory = new File(destinationDirectoryLocation);
|
||||
CoreOld.copyDirectoryJavaUnder7(sourceDirectory, destinationDirectory);
|
||||
|
||||
assertTrue(new File(destinationDirectoryLocation).exists());
|
||||
assertTrue(new File(destinationDirectoryLocation + subDirectoryName).exists());
|
||||
assertTrue(new File(destinationDirectoryLocation + subDirectoryName + fileName).exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSourceDirectoryDoesNotExist_thenExceptionIsThrown() throws IOException {
|
||||
File sourceDirectory = new File("nonExistingDirectory");
|
||||
File destinationDirectory = new File(destinationDirectoryLocation);
|
||||
assertThrows(IOException.class, () -> CoreOld.copyDirectoryJavaUnder7(sourceDirectory, destinationDirectory));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void cleanUp() throws IOException {
|
||||
Files.walk(Paths.get(sourceDirectoryLocation))
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
if (new File(destinationDirectoryLocation).exists()) {
|
||||
Files.walk(Paths.get(destinationDirectoryLocation))
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.copydirectory;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class JavaNioUnitTest {
|
||||
|
||||
private final String sourceDirectoryLocation = "src/test/resources/sourceDirectory2";
|
||||
private final String subDirectoryName = "/childDirectory";
|
||||
private final String fileName = "/file.txt";
|
||||
private final String destinationDirectoryLocation = "src/test/resources/destinationDirectory2";
|
||||
|
||||
@BeforeEach
|
||||
public void createDirectoryWithSubdirectoryAndFile() throws IOException {
|
||||
Files.createDirectories(Paths.get(sourceDirectoryLocation));
|
||||
Files.createDirectories(Paths.get(sourceDirectoryLocation + subDirectoryName));
|
||||
Files.createFile(Paths.get(sourceDirectoryLocation + subDirectoryName + fileName));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSourceDirectoryExists_thenDirectoryIsFullyCopied() throws IOException {
|
||||
JavaNio.copyDirectory(sourceDirectoryLocation, destinationDirectoryLocation);
|
||||
|
||||
assertTrue(new File(destinationDirectoryLocation).exists());
|
||||
assertTrue(new File(destinationDirectoryLocation + subDirectoryName).exists());
|
||||
assertTrue(new File(destinationDirectoryLocation + subDirectoryName + fileName).exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSourceDirectoryDoesNotExist_thenExceptionIsThrown() {
|
||||
assertThrows(IOException.class, () -> JavaNio.copyDirectory("nonExistingDirectory", destinationDirectoryLocation));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void cleanUp() throws IOException {
|
||||
Files.walk(Paths.get(sourceDirectoryLocation))
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
if (new File(destinationDirectoryLocation).exists()) {
|
||||
Files.walk(Paths.get(destinationDirectoryLocation))
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package com.baeldung.existence;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ExistenceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenFile_whenDoesNotExist_thenFilesReturnsFalse() {
|
||||
Path path = Paths.get("does-not-exist.txt");
|
||||
|
||||
assertFalse(Files.exists(path));
|
||||
assertTrue(Files.notExists(path));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFile_whenExists_thenFilesShouldReturnTrue() throws IOException {
|
||||
Path tempFile = Files.createTempFile("baeldung", "exist-nio");
|
||||
assertTrue(Files.exists(tempFile));
|
||||
assertFalse(Files.notExists(tempFile));
|
||||
|
||||
Path tempDirectory = Files.createTempDirectory("baeldung-exists");
|
||||
assertTrue(Files.exists(tempDirectory));
|
||||
assertFalse(Files.notExists(tempDirectory));
|
||||
|
||||
assertTrue(Files.isDirectory(tempDirectory));
|
||||
assertFalse(Files.isDirectory(tempFile));
|
||||
assertTrue(Files.isRegularFile(tempFile));
|
||||
|
||||
assertTrue(Files.isReadable(tempFile));
|
||||
|
||||
Files.deleteIfExists(tempFile);
|
||||
Files.deleteIfExists(tempDirectory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSymbolicLink_whenTargetDoesNotExists_thenFollowOrNotBasedOnTheOptions() throws IOException {
|
||||
Path target = Files.createTempFile("baeldung", "target");
|
||||
Path symbol = Paths.get("test-link-" + ThreadLocalRandom.current().nextInt());
|
||||
|
||||
Path symbolicLink = Files.createSymbolicLink(symbol, target);
|
||||
assertTrue(Files.exists(symbolicLink));
|
||||
assertTrue(Files.isSymbolicLink(symbolicLink));
|
||||
assertFalse(Files.isSymbolicLink(target));
|
||||
|
||||
Files.deleteIfExists(target);
|
||||
assertFalse(Files.exists(symbolicLink));
|
||||
assertTrue(Files.exists(symbolicLink, LinkOption.NOFOLLOW_LINKS));
|
||||
|
||||
Files.deleteIfExists(symbolicLink);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFile_whenDoesNotExist_thenFileReturnsFalse() {
|
||||
assertFalse(new File("invalid").exists());
|
||||
assertFalse(new File("invalid").isFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFile_whenExist_thenShouldReturnTrue() throws IOException {
|
||||
Path tempFilePath = Files.createTempFile("baeldung", "exist-io");
|
||||
Path tempDirectoryPath = Files.createTempDirectory("baeldung-exists-io");
|
||||
|
||||
File tempFile = new File(tempFilePath.toString());
|
||||
File tempDirectory = new File(tempDirectoryPath.toString());
|
||||
|
||||
assertTrue(tempFile.exists());
|
||||
assertTrue(tempDirectory.exists());
|
||||
|
||||
assertTrue(tempFile.isFile());
|
||||
assertFalse(tempDirectory.isFile());
|
||||
|
||||
assertTrue(tempDirectory.isDirectory());
|
||||
assertFalse(tempFile.isDirectory());
|
||||
|
||||
assertTrue(tempFile.canRead());
|
||||
|
||||
Files.deleteIfExists(tempFilePath);
|
||||
Files.deleteIfExists(tempDirectoryPath);
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package com.baeldung.openoptions;
|
||||
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class OpenOptionsUnitTest {
|
||||
|
||||
private static final String HOME = System.getProperty("user.home");
|
||||
private static final String DUMMY_FILE_NAME = "sample.txt";
|
||||
private static final String EXISTING_FILE_NAME = "existingFile.txt";
|
||||
|
||||
private static final String DUMMY_TEXT = "This is a sample text.";
|
||||
private static final String ANOTHER_DUMMY_TEXT = "This is a another text.";
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeAll() throws IOException {
|
||||
Path path = Paths.get(HOME, DUMMY_FILE_NAME);
|
||||
|
||||
try (OutputStream out = Files.newOutputStream(path)) {
|
||||
out.write(DUMMY_TEXT.getBytes());
|
||||
}
|
||||
|
||||
Files.createFile(Paths.get(HOME, EXISTING_FILE_NAME));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterAll() throws IOException {
|
||||
Files.delete(Paths.get(HOME, "newfile.txt"));
|
||||
Files.delete(Paths.get(HOME, "sparse.txt"));
|
||||
Files.delete(Paths.get(HOME, DUMMY_FILE_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingPath_whenCreateNewFile_thenCorrect() throws IOException {
|
||||
Path path = Paths.get(HOME, "newfile.txt");
|
||||
assertFalse(Files.exists(path));
|
||||
|
||||
Files.write(path, DUMMY_TEXT.getBytes(), StandardOpenOption.CREATE);
|
||||
assertTrue(Files.exists(path));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingPath_whenReadExistingFile_thenCorrect() throws IOException {
|
||||
Path path = Paths.get(HOME, DUMMY_FILE_NAME);
|
||||
|
||||
try (InputStream in = Files.newInputStream(path); BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
assertThat(line, CoreMatchers.containsString(DUMMY_TEXT));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingPath_whenWriteToExistingFile_thenCorrect() throws IOException {
|
||||
Path path = Paths.get(HOME, DUMMY_FILE_NAME);
|
||||
|
||||
try (OutputStream out = Files.newOutputStream(path, StandardOpenOption.APPEND, StandardOpenOption.WRITE)) {
|
||||
out.write(ANOTHER_DUMMY_TEXT.getBytes());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingPath_whenCreateSparseFile_thenCorrect() throws IOException {
|
||||
Path path = Paths.get(HOME, "sparse.txt");
|
||||
Files.write(path, DUMMY_TEXT.getBytes(), StandardOpenOption.CREATE_NEW, StandardOpenOption.SPARSE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingPath_whenDeleteOnClose_thenCorrect() throws IOException {
|
||||
Path path = Paths.get(HOME, EXISTING_FILE_NAME);
|
||||
assertTrue(Files.exists(path)); // file was already created and exists
|
||||
|
||||
try (OutputStream out = Files.newOutputStream(path, StandardOpenOption.APPEND, StandardOpenOption.WRITE, StandardOpenOption.DELETE_ON_CLOSE)) {
|
||||
out.write(ANOTHER_DUMMY_TEXT.getBytes());
|
||||
}
|
||||
|
||||
assertFalse(Files.exists(path)); // file is deleted
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingPath_whenWriteAndSync_thenCorrect() throws IOException {
|
||||
Path path = Paths.get(HOME, DUMMY_FILE_NAME);
|
||||
Files.write(path, ANOTHER_DUMMY_TEXT.getBytes(), StandardOpenOption.APPEND, StandardOpenOption.WRITE, StandardOpenOption.SYNC);
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,12 @@
|
||||
<artifactId>core-java-lang-oop-types</artifactId>
|
||||
<name>core-java-lang-oop-types</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.primitivetype;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PrimitiveTypeUtil {
|
||||
|
||||
private static final Map<Class<?>, Class<?>> WRAPPER_TYPE_MAP;
|
||||
static {
|
||||
WRAPPER_TYPE_MAP = new HashMap<Class<?>, Class<?>>(16);
|
||||
WRAPPER_TYPE_MAP.put(Integer.class, int.class);
|
||||
WRAPPER_TYPE_MAP.put(Byte.class, byte.class);
|
||||
WRAPPER_TYPE_MAP.put(Character.class, char.class);
|
||||
WRAPPER_TYPE_MAP.put(Boolean.class, boolean.class);
|
||||
WRAPPER_TYPE_MAP.put(Double.class, double.class);
|
||||
WRAPPER_TYPE_MAP.put(Float.class, float.class);
|
||||
WRAPPER_TYPE_MAP.put(Long.class, long.class);
|
||||
WRAPPER_TYPE_MAP.put(Short.class, short.class);
|
||||
WRAPPER_TYPE_MAP.put(Void.class, void.class);
|
||||
}
|
||||
|
||||
public static boolean isPrimitiveType(Object source) {
|
||||
return WRAPPER_TYPE_MAP.containsKey(source.getClass());
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.primitivetype;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.apache.commons.lang3.ClassUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.primitives.Primitives;
|
||||
|
||||
public class PrimitiveTypeUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAClass_whenCheckWithPrimitiveTypeUtil_thenShouldValidate() {
|
||||
assertTrue(PrimitiveTypeUtil.isPrimitiveType(false));
|
||||
assertTrue(PrimitiveTypeUtil.isPrimitiveType(1L));
|
||||
assertFalse(PrimitiveTypeUtil.isPrimitiveType(StringUtils.EMPTY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAClass_whenCheckWithCommonsLang_thenShouldValidate() {
|
||||
assertTrue(ClassUtils.isPrimitiveOrWrapper(Boolean.FALSE.getClass()));
|
||||
assertTrue(ClassUtils.isPrimitiveOrWrapper(boolean.class));
|
||||
assertFalse(ClassUtils.isPrimitiveOrWrapper(StringUtils.EMPTY.getClass()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAClass_whenCheckWithGuava_thenShouldValidate() {
|
||||
assertTrue(Primitives.isWrapperType(Boolean.FALSE.getClass()));
|
||||
assertFalse(Primitives.isWrapperType(StringUtils.EMPTY.getClass()));
|
||||
assertFalse(Primitives.isWrapperType(Boolean.TYPE.getClass()));
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user